import os import streamlit as st import requests # Set your Groq API key (replace with your actual key or use an environment variable) GROQ_API_KEY = os.getenv("GROQ_API_KEY", "YOUR_GROQ_API_KEY") # Groq API endpoint (compatible with OpenAI format) GROQ_API_URL = "https://api.groq.com/openai/v1/chat/completions" # Function to generate explanation def generate_explanation(engineering_term: str) -> str: prompt = ( "You are a friendly and knowledgeable engineering professor. Explain the engineering term provided below in a clear and accessible manner. " "Your response should be structured into four sections:\n\n" "1. **Definition:** Provide a concise definition.\n" "2. **Background/Context:** Explain the history or context behind the term.\n" "3. **Application/Significance:** Describe how this concept is used in **a real-world engineering task or product** (e.g., a machine, device, or construction process).\n" "4. **Example:** Give a clear, everyday analogy or practical situation a user can relate to (e.g., how it works in a car, smartphone, or bridge).\n\n" f"Term: {engineering_term}" ) headers = { "Content-Type": "application/json", "Authorization": f"Bearer {GROQ_API_KEY}", } payload = { "model": "llama3-8b-8192", # Replace with your actual Groq-supported model "messages": [ {"role": "system", "content": "You are a helpful and friendly engineering professor."}, {"role": "user", "content": prompt} ], "temperature": 0.7, "max_tokens": 500 } try: response = requests.post(GROQ_API_URL, headers=headers, json=payload) response.raise_for_status() result = response.json() return result['choices'][0]['message']['content'] except Exception as e: return f"❌ An error occurred while fetching explanation:\n\n{e}" # Streamlit UI st.set_page_config(page_title="Engineering Term Explainer", page_icon="🔍") st.title("🔧 Engineering Term Explainer") st.write("Enter an engineering term to get a structured, friendly explanation:") term = st.text_input("Engineering Term") if term: with st.spinner("Generating explanation..."): explanation = generate_explanation(term) st.markdown(explanation)