import streamlit as st import os import requests # --- Page Config --- st.set_page_config(page_title="EngGloss - Engineering Term Explainer", page_icon="πŸ“˜") # --- App Title --- st.title("πŸ“˜ EngGloss - Engineering Term Explainer") st.markdown("Enter any engineering term, and I'll break it down for you in a beginner-friendly way!") # --- Input --- term = st.text_input("πŸ” Enter an engineering term:") # --- Model Selector --- model = st.selectbox("🧠 Choose a model to use:", [ "llama3-70b-8192", "llama3-8b-8192" ], index=0) # --- API Call Function --- def get_explanation_from_groq(term, model): api_key = os.getenv("GROQ_API_KEY") # Set this in Hugging Face or your local .env if not api_key: return "❌ API key not found. Please set the GROQ_API_KEY environment variable." prompt = f""" You are EngGloss, an expert AI tutor who explains engineering terms in a clear and beginner-friendly way. Given a single engineering term, respond with a structured and concise explanation using the format below. Keep the tone educational, simple, and free of technical jargon whenever possible. Make the output useful for students who are new to engineering. Use this exact structure and emojis: πŸ” Term: {term} πŸ“˜ Simple Explanation: Give a brief, beginner-level explanation of the term. Avoid jargon and keep it easy to understand. 🧠 Real-World Analogy: Describe a relatable analogy from everyday life that helps explain the concept. πŸ“ Typical Formula: If there’s a well-known formula, write it out and explain what each variable means. If no standard formula exists, state: β€œThere is no standard formula for this term.” πŸ—οΈ Engineering Applications: List 2–3 specific real-world applications where this term is used in engineering practice. For example, describe actual scenarios, devices, systems, or problems that involve this concept. """ headers = { "Authorization": f"Bearer {api_key}", "Content-Type": "application/json" } payload = { "model": model, "messages": [ {"role": "user", "content": prompt} ] } response = requests.post("https://api.groq.com/openai/v1/chat/completions", headers=headers, json=payload) if response.status_code == 200: result = response.json() return result["choices"][0]["message"]["content"] else: return f"❌ Error: {response.status_code} - {response.text}" # --- Generate Output --- if st.button("Explain Term"): if term.strip(): with st.spinner("Thinking..."): explanation = get_explanation_from_groq(term, model) st.markdown(explanation) else: st.warning("Please enter a term to explain.")