Spaces:
Runtime error
Runtime error
File size: 1,786 Bytes
3520c16 ede626e 042f556 3520c16 a90ffc2 ede626e 3520c16 a90ffc2 042f556 3520c16 a90ffc2 042f556 a0c55bc a90ffc2 a0c55bc a90ffc2 042f556 3520c16 a90ffc2 042f556 3520c16 042f556 a90ffc2 3520c16 a90ffc2 a0c55bc 3520c16 a0c55bc a90ffc2 a0c55bc 042f556 3520c16 042f556 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | 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()
|