enggloss / app.py
Inam65's picture
Update app.py
7bee216 verified
import gradio as gr
import os
import requests
# Set your Groq API key
GROQ_API_KEY = os.getenv("GROQ_API_KEY") # Make sure this is set in Hugging Face Spaces or locally
# Prompt template
def build_prompt(term):
return f"""
You are EngGloss, an expert AI tutor who explains complex engineering concepts so that absolute beginners can understand them.
Given a single technical engineering term, respond with a structured explanation in the following format:
πŸ” Term: {term}
πŸ“˜ Simple Explanation: Provide a short, beginner-friendly definition using simple language. Avoid technical jargon or complex descriptions.
🧠 Real-World Analogy: Give a relatable analogy using everyday objects or situations to help users intuitively grasp the concept.
πŸ“ Typical Formula: If the term involves a common or standard formula, provide it and briefly explain each variable. If no formula is commonly associated with the term, say so.
πŸ—οΈ Engineering Applications: List 2–3 engineering disciplines (e.g., civil, mechanical, electrical) where this term is commonly used.
Make sure your response is clear, concise, and useful for students or beginners in engineering.
"""
# Groq API call
def explain_term(term):
url = "https://api.groq.com/openai/v1/chat/completions"
headers = {
"Authorization": f"Bearer {GROQ_API_KEY}",
"Content-Type": "application/json"
}
payload = {
"model": "llama3-70b-8192",
"messages": [
{"role": "user", "content": build_prompt(term)}
],
"temperature": 0.7
}
response = requests.post(url, 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}"
# Gradio UI
with gr.Blocks() as demo:
gr.Markdown("## πŸ€– EngGloss β€” Your AI Engineering Glossary Tutor")
gr.Markdown("Enter an engineering term to get a beginner-friendly explanation:")
with gr.Row():
term_input = gr.Textbox(label="Engineering Term", placeholder="e.g., Torque")
output = gr.Markdown()
submit_btn = gr.Button("Explain")
submit_btn.click(fn=explain_term, inputs=term_input, outputs=output)
# Launch
if __name__ == "__main__":
demo.launch()