|
|
import os |
|
|
import gradio as gr |
|
|
import requests |
|
|
|
|
|
|
|
|
GROQ_API_KEY = os.environ.get("Civil_Engineering_Terms_Explainer") |
|
|
|
|
|
|
|
|
MODEL_NAME = "llama3-8b-8192" |
|
|
|
|
|
|
|
|
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.""" |
|
|
|
|
|
|
|
|
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}" |
|
|
|
|
|
|
|
|
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() |
|
|
|