Spaces:
Sleeping
Sleeping
import streamlit as st | |
from groq import Groq | |
import os | |
import schemdraw | |
import schemdraw.elements as elm | |
import matplotlib.pyplot as plt # Ensure matplotlib is imported | |
# Set your Groq API Key | |
client = Groq(api_key=os.getenv("groq_api_key")) | |
# Streamlit UI | |
st.title("Electronics RAG App") | |
st.sidebar.title("Options") | |
st.sidebar.write("Choose an option below to get started:") | |
# Sidebar Options | |
option = st.sidebar.selectbox("Choose a feature:", ["Ask Electronics Question", "Generate Circuit Diagram"]) | |
if option == "Ask Electronics Question": | |
st.header("Ask Electronics Question") | |
user_question = st.text_input("Enter your question:") | |
if st.button("Submit Question"): | |
if user_question: | |
try: | |
# Query Groq API | |
chat_completion = client.chat.completions.create( | |
messages=[{"role": "user", "content": user_question}], | |
model="llama3-8b-8192", | |
stream=False, | |
) | |
response = chat_completion.choices[0].message.content | |
st.success("Answer:") | |
st.write(response) | |
except Exception as e: | |
st.error(f"An error occurred: {e}") | |
else: | |
st.warning("Please enter a question!") | |
elif option == "Generate Circuit Diagram": | |
st.header("Generate Circuit Diagram") | |
circuit_description = st.text_area("Describe your circuit (e.g., 'A resistor, capacitor, and voltage source in series'):") | |
if st.button("Generate Diagram"): | |
if circuit_description: | |
# Generate a mock circuit diagram | |
try: | |
with schemdraw.Drawing() as d: | |
d += elm.SourceV().label("V1").up() | |
d += elm.Resistor().label("R1").right() | |
d += elm.Capacitor().label("C1").down() | |
d += elm.Line().left() | |
d += elm.Line().up() | |
# To display the diagram correctly in Streamlit, use st.pyplot | |
st.pyplot(d.fig) | |
st.success("Circuit Diagram Generated!") | |
except Exception as e: | |
st.error(f"An error occurred: {e}") | |
else: | |
st.warning("Please describe your circuit!") | |
st.sidebar.info("Developed for creating and exploring electronics concepts interactively.") | |