isana25's picture
Update app.py
a90ffc2 verified
import gradio as gr
import os
from groq import Groq
# Load API key securely from environment variable
GROQ_API_KEY = os.getenv("Groq_API_Key")
# Initialize Groq client using the SDK
client = Groq(api_key=GROQ_API_KEY)
# System instruction for the bot
SYSTEM_PROMPT = (
"You are a Construction Material Advisor Bot. Your job is to recommend the best construction materials "
"for civil engineering projects based on environmental conditions, budget, load requirements, and durability. "
"Be concise, informative, and explain pros and cons of the materials."
)
# Model name
MODEL_NAME = "llama3-70b-8192"
# Chat function using Groq SDK
def get_material_advice(user_query: str) -> str:
try:
chat_completion = client.chat.completions.create(
model=MODEL_NAME,
messages=[
{"role": "system", "content": SYSTEM_PROMPT},
{"role": "user", "content": user_query},
],
temperature=0.7,
max_tokens=500,
)
response = chat_completion.choices[0].message.content.strip()
# Convert plain text response to Markdown for better formatting
formatted_response = f"### 🧱 Material Recommendations\n\n{response}"
return formatted_response
except Exception as e:
return f"Error: {str(e)}"
# Gradio UI
demo = gr.Interface(
fn=get_material_advice,
inputs=gr.Textbox(lines=3, placeholder="Ask about construction materials..."),
outputs=gr.Markdown(),
title="🧱 Construction Material Advisor Bot",
description=(
"Ask questions like 'What is the best material for a coastal bridge?' or "
"'Suggest budget-friendly wall material for cold regions.'"
),
)
if __name__ == "__main__":
demo.launch()