| import gradio as gr |
| import os |
| import requests |
|
|
| |
| GROQ_API_KEY = os.getenv("GROQ_API_KEY") |
|
|
| |
| 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. |
| """ |
|
|
| |
| 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}" |
|
|
| |
| 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) |
|
|
| |
| if __name__ == "__main__": |
| demo.launch() |
|
|