EngGloss / app.py
safiaa02's picture
Update app.py
0dda9ff verified
import streamlit as st
import requests
import os
# Set your Groq API Key
GROQ_API_KEY = os.getenv("GROQ_API_KEY") # Or hardcode: "your-key-here"
GROQ_API_URL = "https://api.groq.com/openai/v1/chat/completions"
HEADERS = {
"Authorization": f"Bearer {GROQ_API_KEY}",
"Content-Type": "application/json"
}
SYSTEM_PROMPT = """
You are EngGloss, an expert engineering tutor who explains technical terms in a way that's simple and clear for beginners.
When given a single engineering term, respond with structured output in the following exact format using emojis and section titles:
πŸ” **Term**: <term>
πŸ“˜ **Simple Explanation**: Provide a concise, beginner-friendly explanation of the term. Avoid technical jargon or complicated language.
🧠 **Real-World Analogy**: Explain the concept using a relatable analogy from everyday life that makes it easier to understand.
πŸ“ **Typical Formula**: If a standard formula exists for this term, present it. Briefly explain what each variable represents. If no common formula exists, say: "There is no widely accepted formula for this term."
πŸ—οΈ **Engineering Applications**: List 2–3 practical engineering applications where this term is used. Be specific. For example, if the term is "Ohm’s Law", an application could be "Designing electric circuits in household appliances" or "Calculating resistor values in LED lighting systems".
Keep your entire response short, clear, and helpful for a first-year engineering student or a curious beginner. Do not add any extra commentary or introduction.
"""
def query_groq(term):
payload = {
"model": "llama3-8b-8192", # βœ… Updated model
"messages": [
{"role": "system", "content": SYSTEM_PROMPT},
{"role": "user", "content": f"{term}"}
],
"temperature": 0.7
}
response = requests.post(GROQ_API_URL, headers=HEADERS, json=payload)
if response.status_code == 200:
return response.json()['choices'][0]['message']['content']
else:
return f"⚠️ Error: {response.status_code} - {response.text}"
# Streamlit UI
st.set_page_config(page_title="EngGloss – Engineering Term Explainer", page_icon="πŸ“˜")
st.title("πŸ“˜ EngGloss: Engineering Term Explainer")
term = st.text_input("Enter an engineering term:", placeholder="e.g., Ohm’s Law, Bernoulli Principle")
if term:
with st.spinner("Generating explanation..."):
output = query_groq(term)
st.markdown(output)