Enggpedia / app.py
SyedWaqad's picture
Update app.py
1feae15 verified
import gradio as gr
import requests
# -------------------------
# EngGloss System Prompt
# -------------------------
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.
"""
# -------------------------
# Function to call Groq API
# -------------------------
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"]