Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import os
|
| 3 |
+
from groq import Groq
|
| 4 |
+
|
| 5 |
+
# --- Page Config ---
|
| 6 |
+
st.set_page_config(
|
| 7 |
+
page_title="EngGloss | AI Engineering Tutor",
|
| 8 |
+
page_icon="⚙️",
|
| 9 |
+
layout="centered"
|
| 10 |
+
)
|
| 11 |
+
|
| 12 |
+
# --- Header ---
|
| 13 |
+
st.title("⚙️ EngGloss")
|
| 14 |
+
st.caption("Enter an engineering term below to get a beginner-friendly explanation.")
|
| 15 |
+
|
| 16 |
+
# --- API Setup ---
|
| 17 |
+
# Tries to get the API key from Streamlit Secrets (Best for Hugging Face)
|
| 18 |
+
# If not found, tries to get it from environment variables (Best for Local)
|
| 19 |
+
api_key = st.secrets.get("GROQ_API_KEY") or os.getenv("GROQ_API_KEY")
|
| 20 |
+
|
| 21 |
+
if not api_key:
|
| 22 |
+
st.error("⚠️ Groq API Key not found!")
|
| 23 |
+
st.info("If you are running this locally, create a .env file. If on Hugging Face, add GROQ_API_KEY to your Space Secrets.")
|
| 24 |
+
st.stop()
|
| 25 |
+
|
| 26 |
+
client = Groq(api_key=api_key)
|
| 27 |
+
|
| 28 |
+
# --- The Prompt Template ---
|
| 29 |
+
system_prompt = """
|
| 30 |
+
You are EngGloss, an expert AI engineering tutor. Your goal is to explain technical engineering terms to students and beginners in a way that is structured, strictly factual, yet easy to understand.
|
| 31 |
+
|
| 32 |
+
When given a term, you must output the response in the following Markdown format:
|
| 33 |
+
|
| 34 |
+
### 📖 Term: <The Term>
|
| 35 |
+
|
| 36 |
+
**💡 Simple Explanation:**
|
| 37 |
+
<A concise, jargon-free definition (2-3 sentences max). Imagine explaining this to a first-year student.>
|
| 38 |
+
|
| 39 |
+
**🍎 Real-World Analogy:**
|
| 40 |
+
<An analogy using everyday objects (like water, cars, cooking, or sports) to explain the concept.>
|
| 41 |
+
|
| 42 |
+
**➗ Typical Formula:**
|
| 43 |
+
<Display the core formula using LaTeX formatting (e.g., $F=ma$). If no formula exists, write "No standard formula." Brief bullet points explaining the variables.>
|
| 44 |
+
|
| 45 |
+
**⚙️ Engineering Applications:**
|
| 46 |
+
<List 2-3 specific engineering fields where this is used (e.g., Civil Engineering, Avionics).>
|
| 47 |
+
|
| 48 |
+
Constraint: If the user input is NOT an engineering or scientific term, politely refuse and ask for a valid technical term.
|
| 49 |
+
"""
|
| 50 |
+
|
| 51 |
+
# --- UI & Logic ---
|
| 52 |
+
term = st.text_input("Enter a term (e.g., Thermodynamics, Torque, Reynolds Number):")
|
| 53 |
+
|
| 54 |
+
if st.button("Explain Term", type="primary"):
|
| 55 |
+
if not term:
|
| 56 |
+
st.warning("Please enter a term first.")
|
| 57 |
+
else:
|
| 58 |
+
try:
|
| 59 |
+
with st.spinner(f"EngGloss is analyzing '{term}'..."):
|
| 60 |
+
chat_completion = client.chat.completions.create(
|
| 61 |
+
messages=[
|
| 62 |
+
{
|
| 63 |
+
"role": "system",
|
| 64 |
+
"content": system_prompt,
|
| 65 |
+
},
|
| 66 |
+
{
|
| 67 |
+
"role": "user",
|
| 68 |
+
"content": term,
|
| 69 |
+
}
|
| 70 |
+
],
|
| 71 |
+
model="llama3-8b-8192", # Fast and efficient model
|
| 72 |
+
temperature=0.5, # Low temp for factual accuracy
|
| 73 |
+
max_tokens=500,
|
| 74 |
+
)
|
| 75 |
+
|
| 76 |
+
response = chat_completion.choices[0].message.content
|
| 77 |
+
|
| 78 |
+
# Create a nice container for the result
|
| 79 |
+
st.markdown("---")
|
| 80 |
+
st.markdown(response)
|
| 81 |
+
|
| 82 |
+
except Exception as e:
|
| 83 |
+
st.error(f"An error occurred: {e}")
|
| 84 |
+
|
| 85 |
+
# --- Footer ---
|
| 86 |
+
st.markdown("---")
|
| 87 |
+
st.markdown("Build with [Groq](https://groq.com) & [Streamlit](https://streamlit.io)")
|