Engr-arehmankhan786's picture
Update app.py
26c542e verified
import gradio as gr
import requests
import os
# Set your Groq API key (store it as an environment variable or replace directly here)
GROQ_API_KEY = os.getenv("GROQ_API_KEY")
def explain_term(term):
prompt = f"""
You are EngGloss, an expert AI tutor who explains engineering terms in a clear, structured, and beginner-friendly way.
When given an engineering term, respond using this exact format and tone:
πŸ” Term: {term}
πŸ“˜ Simple Explanation: Give a short, easy-to-understand explanation suitable for beginners. Avoid technical jargon and keep it clear and friendly.
🧠 Real-World Analogy: Provide a simple, relatable analogy from everyday life that helps make the concept intuitive.
πŸ“ Typical Formula: If a standard formula is associated with the term, include it. Then explain each variable briefly. If no common formula exists, say: β€œNo standard formula is commonly used for this term.”
πŸ—οΈ Engineering Applications: List 2–3 fields of engineering where this term is frequently used or applied.
Keep the entire explanation concise, well-organized, and ideally under 150 words. Do not add any extra sections or commentary.
"""
headers = {
"Authorization": f"Bearer {GROQ_API_KEY}",
"Content-Type": "application/json"
}
data = {
"model": "llama3-8b-8192",
"messages": [{"role": "user", "content": prompt}],
"temperature": 0.7
}
response = requests.post("https://api.groq.com/openai/v1/chat/completions", headers=headers, json=data)
result = response.json()
return result["choices"][0]["message"]["content"]
# Gradio Interface
demo = gr.Interface(
fn=explain_term,
inputs=gr.Textbox(label="Enter an Engineering Term"),
outputs=gr.Markdown(label="Explanation"),
title="EngGloss - Engineering Term Explainer",
description="Get a beginner-friendly explanation for any engineering term.",
)
if __name__ == "__main__":
demo.launch()