Waqas0327's picture
Update app.py
e8e5970 verified
import os
import gradio as gr
import requests
# Read GROQ API key from Hugging Face secret
GROQ_API_KEY = os.environ.get("Civil_Engineering_Terms_Explainer")
# Model to use (LLaMA3 recommended)
MODEL_NAME = "llama3-8b-8192"
# System prompt for civil engineering
SYSTEM_PROMPT = """You are a Civil Engineering Expert and Educator with deep knowledge of structural, geotechnical, transportation, water resources, and construction engineering. Your job is to explain technical civil engineering terms and concepts in a simplified way that is easy for students and beginners to understand.
๐Ÿ”น Always explain the term in plain English.
๐Ÿ”น Provide a real-world example or analogy (preferably related to common civil engineering scenarios in developing countries like Pakistan).
๐Ÿ”น Keep your answer accurate, brief, and based on current engineering standards (like ACI, ASTM, AASHTO, BS, or Eurocodes).
๐Ÿ”น Avoid unnecessary jargon unless needed, and define it clearly when used.
๐Ÿ”น Format the output in the following way:
1. **Definition**
2. **Example/Analogy**
3. **Practical Use or Importance**
You must only answer questions related to civil engineering terms or topics. If someone asks something unrelated, politely redirect them to civil engineering."""
# Function to call Groq API
def ask_civil_engineering_bot(user_input):
url = "https://api.groq.com/openai/v1/chat/completions"
headers = {
"Authorization": f"Bearer {GROQ_API_KEY}",
"Content-Type": "application/json"
}
data = {
"model": MODEL_NAME,
"messages": [
{"role": "system", "content": SYSTEM_PROMPT},
{"role": "user", "content": user_input}
],
"temperature": 0.7
}
response = requests.post(url, headers=headers, json=data)
if response.status_code == 200:
return response.json()["choices"][0]["message"]["content"]
else:
return f"โŒ Error: {response.status_code}\n{response.text}"
# Gradio interface
demo = gr.Interface(
fn=ask_civil_engineering_bot,
inputs=gr.Textbox(lines=2, placeholder="Enter a civil engineering term like 'Slump Test'..."),
outputs=gr.Markdown(),
title="Civil Engineering Terms Explainer",
description="๐Ÿค– Ask any technical civil engineering term and get a simple explanation with examples."
)
if __name__ == "__main__":
demo.launch()