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")