Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,44 +1,37 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import requests
|
| 3 |
import os
|
|
|
|
| 4 |
|
| 5 |
# Fetch the Groq API key from Gradio secrets
|
| 6 |
GROQ_API_KEY = os.getenv("Groq_API_Key")
|
| 7 |
|
| 8 |
-
# Groq
|
| 9 |
-
|
| 10 |
|
| 11 |
# System prompt to define bot behavior
|
| 12 |
-
|
| 13 |
"You are a Construction Material Advisor Bot. Your job is to recommend the best construction materials "
|
| 14 |
"for civil engineering projects based on environmental conditions, budget, load requirements, and durability. "
|
| 15 |
-
"Be concise, informative, and explain pros and cons of materials."
|
| 16 |
)
|
| 17 |
|
| 18 |
-
|
| 19 |
-
def get_material_advice(user_query):
|
| 20 |
-
headers = {
|
| 21 |
-
"Authorization": f"Bearer {GROQ_API_KEY}",
|
| 22 |
-
"Content-Type": "application/json"
|
| 23 |
-
}
|
| 24 |
-
|
| 25 |
-
payload = {
|
| 26 |
-
"model": "llama2-70b-4096", # You can use llama2-7b if available
|
| 27 |
-
"messages": [
|
| 28 |
-
{"role": "system", "content": system_prompt},
|
| 29 |
-
{"role": "user", "content": user_query}
|
| 30 |
-
],
|
| 31 |
-
"temperature": 0.7,
|
| 32 |
-
"max_tokens": 500
|
| 33 |
-
}
|
| 34 |
|
|
|
|
|
|
|
| 35 |
try:
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
# Gradio interface
|
| 44 |
demo = gr.Interface(
|
|
@@ -46,8 +39,12 @@ demo = gr.Interface(
|
|
| 46 |
inputs=gr.Textbox(lines=3, placeholder="Ask about construction materials..."),
|
| 47 |
outputs="text",
|
| 48 |
title="🧱 Construction Material Advisor Bot",
|
| 49 |
-
description=
|
|
|
|
|
|
|
|
|
|
| 50 |
)
|
| 51 |
|
| 52 |
-
# Launch app
|
| 53 |
-
|
|
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
import os
|
| 3 |
+
from groq import Groq
|
| 4 |
|
| 5 |
# Fetch the Groq API key from Gradio secrets
|
| 6 |
GROQ_API_KEY = os.getenv("Groq_API_Key")
|
| 7 |
|
| 8 |
+
# Initialize Groq client with the secret API key
|
| 9 |
+
client = Groq(api_key=GROQ_API_KEY)
|
| 10 |
|
| 11 |
# System prompt to define bot behavior
|
| 12 |
+
SYSTEM_PROMPT = (
|
| 13 |
"You are a Construction Material Advisor Bot. Your job is to recommend the best construction materials "
|
| 14 |
"for civil engineering projects based on environmental conditions, budget, load requirements, and durability. "
|
| 15 |
+
"Be concise, informative, and explain the pros and cons of the recommended materials."
|
| 16 |
)
|
| 17 |
|
| 18 |
+
MODEL_NAME = "llama3-70b-8192"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
+
def get_material_advice(user_query: str) -> str:
|
| 21 |
+
"""Return a material recommendation generated by the Groq LLM."""
|
| 22 |
try:
|
| 23 |
+
chat_completion = client.chat.completions.create(
|
| 24 |
+
model=MODEL_NAME,
|
| 25 |
+
messages=[
|
| 26 |
+
{"role": "system", "content": SYSTEM_PROMPT},
|
| 27 |
+
{"role": "user", "content": user_query},
|
| 28 |
+
],
|
| 29 |
+
temperature=0.7,
|
| 30 |
+
max_tokens=500,
|
| 31 |
+
)
|
| 32 |
+
return chat_completion.choices[0].message.content.strip()
|
| 33 |
+
except Exception as exc:
|
| 34 |
+
return f"Error: {exc}"
|
| 35 |
|
| 36 |
# Gradio interface
|
| 37 |
demo = gr.Interface(
|
|
|
|
| 39 |
inputs=gr.Textbox(lines=3, placeholder="Ask about construction materials..."),
|
| 40 |
outputs="text",
|
| 41 |
title="🧱 Construction Material Advisor Bot",
|
| 42 |
+
description=(
|
| 43 |
+
"Ask questions like 'What is the best material for a coastal bridge?' or "
|
| 44 |
+
"'Suggest budget-friendly wall material for cold regions.'"
|
| 45 |
+
),
|
| 46 |
)
|
| 47 |
|
| 48 |
+
# Launch the app
|
| 49 |
+
if __name__ == "__main__":
|
| 50 |
+
demo.launch()
|