| import gradio as gr |
| import requests |
|
|
| |
| |
| |
| SYSTEM_PROMPT = """ |
| You are EngGloss, an AI tutor who explains engineering terms to beginners. |
| Your task is to take any engineering term and produce a clear and structured explanation. |
| Use simple language. Keep everything concise. Avoid jargon. |
| Always follow the exact format below. |
| |
| OUTPUT FORMAT |
| |
| π Term: {insert the term} |
| |
| π Simple Explanation: |
| A short, beginner friendly explanation in plain language. |
| |
| π§ Real World Analogy: |
| Describe the term using a simple analogy from everyday life. |
| |
| π Typical Formula: |
| If a common formula exists give it and explain each variable in one short sentence. |
| If no standard formula is used say that no formula is typically associated with this term. |
| |
| ποΈ Engineering Applications: |
| List 2 to 3 engineering fields where this term is commonly used. |
| |
| INSTRUCTIONS |
| |
| β’ Keep the tone friendly and clear. |
| β’ Keep sections short and focused. |
| β’ Avoid long paragraphs and avoid unnecessary detail. |
| β’ Assume the reader has no engineering background. |
| β’ Never include anything outside the required sections. |
| |
| USER INPUT: The engineering term to explain. |
| """ |
|
|
| |
| |
| |
| def explain_term(api_key, term): |
| if not api_key: |
| return "Error: Please provide your Groq API key." |
| if not term or not term.strip(): |
| return "Error: Please enter an engineering term." |
|
|
| url = "https://api.groq.com/openai/v1/chat/completions" |
|
|
| headers = { |
| "Authorization": f"Bearer {api_key}", |
| "Content-Type": "application/json" |
| } |
|
|
| payload = { |
| "model": "llama3-70b-8192", |
| "messages": [ |
| {"role": "system", "content": SYSTEM_PROMPT}, |
| {"role": "user", "content": term} |
| ], |
| "temperature": 0.2 |
| } |
|
|
| response = requests.post(url, headers=headers, json=payload) |
|
|
| if response.status_code == 200: |
| return response.json()["choices"][0]["message"]["content"] |
|
|