Spaces:
Build error
Build error
File size: 3,743 Bytes
f68e9fb 614ab33 f68e9fb 8884e45 f68e9fb c682652 f68e9fb c682652 f68e9fb ccd821c f68e9fb ccd821c 5a2d969 ccd821c f68e9fb be848bf 614ab33 be848bf f68e9fb c682652 f68e9fb 614ab33 5a2d969 f68e9fb ccd821c 5a2d969 f68e9fb 5a2d969 f68e9fb 5a2d969 f68e9fb 5a2d969 614ab33 5a2d969 f68e9fb | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 | import streamlit as st
import requests
import os
import re
GROQ_API_KEY = os.getenv("EngGlass_GroqAPI")
GROQ_API_URL = "https://api.groq.com/openai/v1/chat/completions"
if not GROQ_API_KEY:
st.error("❌ API key not found. Please set 'EngGlass_GroqAPI' in Hugging Face repository secrets.")
st.stop()
def generate_prompt(term):
return f"""
You are engGlass, a friendly, knowledgeable AI tutor who specializes in explaining engineering terms in simple, structured, and accurate language. Your goal is to help beginners and non-specialists understand technical concepts clearly. Use real-world analogies where appropriate. Avoid jargon unless absolutely necessary—and if you must use a technical term, define it in plain English.
Explain the engineering term: {term}
Respond in the following structure:
1. **Definition:** One-sentence definition in plain English.
2. **Brief Explanation:** 2–3 short paragraphs elaborating the concept.
3. **Formula(s) (if any):** Include key formulas (use LaTeX format, like this: $$E = mc^2$$) and explain them simply.
4. **Real-World Analogy:** A simple analogy that helps understand the concept.
5. **Real-World Application:** A practical example of where this concept is applied.
"""
def get_explanation_from_groq(prompt):
headers = {
"Authorization": f"Bearer {GROQ_API_KEY}",
"Content-Type": "application/json"
}
payload = {
"model": "llama-3.3-70b-versatile",
"messages": [
{"role": "system", "content": "You are engGlass, a helpful AI tutor."},
{"role": "user", "content": prompt}
],
"temperature": 0.5,
"max_tokens": 1024
}
response = requests.post(GROQ_API_URL, headers=headers, json=payload)
if response.status_code != 200:
st.error(f"❌ Groq API returned {response.status_code}: {response.text}")
st.stop()
return response.json()["choices"][0]["message"]["content"]
def extract_latex(text):
return re.findall(r"\$\$(.+?)\$\$", text, re.DOTALL)
def render_explanation(term, explanation):
latex_formulas = extract_latex(explanation)
text_without_latex = re.sub(r"\$\$(.+?)\$\$", "", explanation, flags=re.DOTALL)
# Layout: Two columns
col1, col2 = st.columns([2, 1])
with col1:
st.markdown(text_without_latex.strip())
if latex_formulas:
st.markdown("### 🧮 Formula(s):")
for formula in latex_formulas:
st.latex(formula.strip())
with col2:
st.markdown("### 🖼️ Illustration")
# Image placeholder — you can plug in your own API or static images
st.image("https://placehold.co/300x200?text=Diagram+of+" + term.replace(" ", "+"))
st.download_button(
label="📥 Download Explanation",
data=explanation,
file_name=f"{term.strip().replace(' ', '_')}_explanation.txt",
mime="text/plain"
)
# Streamlit UI
st.set_page_config(page_title="engGlass - Engineering Terms Explained", layout="centered")
st.title("📘 engGlass")
st.markdown("### 🔍 Learn Engineering Visually\nUnderstand technical terms with clarity, context, and illustrations.")
user_input = st.text_input("Enter an engineering term:", placeholder="e.g., Fluid Dynamics")
if st.button("Explain") and user_input.strip():
with st.spinner("Generating explanation and illustration..."):
try:
prompt = generate_prompt(user_input.strip())
output = get_explanation_from_groq(prompt)
render_explanation(user_input.strip(), output)
except Exception as e:
st.error(f"❌ Error: {str(e)}")
st.markdown("---")
st.caption("Built with ❤️ using Groq + Streamlit")
|