isana25 commited on
Commit
a90ffc2
·
verified ·
1 Parent(s): 512de21

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -11
app.py CHANGED
@@ -2,23 +2,24 @@ 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,
@@ -29,15 +30,18 @@ def get_material_advice(user_query: str) -> str:
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(
38
  fn=get_material_advice,
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 "
@@ -45,6 +49,5 @@ demo = gr.Interface(
45
  ),
46
  )
47
 
48
- # Launch the app
49
  if __name__ == "__main__":
50
  demo.launch()
 
2
  import os
3
  from groq import Groq
4
 
5
+ # Load API key securely from environment variable
6
  GROQ_API_KEY = os.getenv("Groq_API_Key")
7
 
8
+ # Initialize Groq client using the SDK
9
  client = Groq(api_key=GROQ_API_KEY)
10
 
11
+ # System instruction for the bot
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 pros and cons of the materials."
16
  )
17
 
18
+ # Model name
19
  MODEL_NAME = "llama3-70b-8192"
20
 
21
+ # Chat function using Groq SDK
22
  def get_material_advice(user_query: str) -> str:
 
23
  try:
24
  chat_completion = client.chat.completions.create(
25
  model=MODEL_NAME,
 
30
  temperature=0.7,
31
  max_tokens=500,
32
  )
33
+ response = chat_completion.choices[0].message.content.strip()
34
+ # Convert plain text response to Markdown for better formatting
35
+ formatted_response = f"### 🧱 Material Recommendations\n\n{response}"
36
+ return formatted_response
37
+ except Exception as e:
38
+ return f"Error: {str(e)}"
39
 
40
+ # Gradio UI
41
  demo = gr.Interface(
42
  fn=get_material_advice,
43
  inputs=gr.Textbox(lines=3, placeholder="Ask about construction materials..."),
44
+ outputs=gr.Markdown(),
45
  title="🧱 Construction Material Advisor Bot",
46
  description=(
47
  "Ask questions like 'What is the best material for a coastal bridge?' or "
 
49
  ),
50
  )
51
 
 
52
  if __name__ == "__main__":
53
  demo.launch()