CoolFace
Apppublic

Musaddiq1234/Engineering_term_explanation

sourceHugging Facemitupdated 1y agoView on Hugging Face
0likes
aap.py65 linesDownload Raw Back to root
1import streamlit as st2import requests3import os4 5st.set_page_config(page_title="EngGloss - Engineering Term Explainer")6 7st.title("EngGloss - AI Tutor for Engineering Terms")8st.markdown("Enter an engineering term below to get a beginner-friendly explanation:")9 10term = st.text_input("Engineering Term")11 12# Get API key from Hugging Face secrets or environment13GROQ_API_KEY = os.environ.get("GROQ_API_KEY")14if not GROQ_API_KEY:15    st.error("GROQ_API_KEY is not set. Please add it in Hugging Face Secrets.")16    st.stop()17 18HEADERS = {19    "Authorization": f"Bearer {GROQ_API_KEY}",20    "Content-Type": "application/json"21}22 23API_URL = "https://api.groq.com/openai/v1/chat/completions"24 25PROMPT_TEMPLATE = """26You are EngGloss, an expert AI tutor for engineering students. Your job is to explain complex engineering terms in a structured and beginner-friendly way. Avoid jargon, keep explanations short, and ensure clarity.27 28Given a technical engineering term, respond with the following 5 sections:29 30๐Ÿ” Term: {term}31 32๐Ÿ“˜ Simple Explanation: Provide a concise and beginner-friendly explanation. Avoid technical language and keep it under 4 sentences.33 34๐Ÿง  Real-World Analogy: Use a relatable analogy from everyday life to help students understand the concept better.35 36๐Ÿ“ Typical Formula: Include a commonly used formula (if one exists). Briefly explain each variable. If no standard formula exists, state "No standard formula."37 38๐Ÿ—๏ธ Engineering Applications: List 2 to 3 specific branches of engineering where this term is commonly applied.39 40Your tone should be friendly, clear, and focused on helping new students understand the concept with confidence.41"""42 43def explain_term(term):44    payload = {45        "model": "mixtral-8x7b-32768",46        "messages": [47            {"role": "user", "content": PROMPT_TEMPLATE.format(term=term)}48        ],49        "temperature": 0.350    }51 52    try:53        response = requests.post(API_URL, headers=HEADERS, json=payload)54        if response.status_code == 200:55            return response.json()["choices"][0]["message"]["content"]56        else:57            return f"Error {response.status_code}: {response.text}"58    except Exception as e:59        return f"Request failed: {e}"60 61if st.button("Explain Term") and term:62    with st.spinner("Asking EngGloss..."):63        result = explain_term(term)64    st.markdown("### Result")65    st.markdown(result)