decodingdatascience commited on
Commit
8615598
·
verified ·
1 Parent(s): 014bc9b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -17
app.py CHANGED
@@ -2,39 +2,51 @@ import os
2
  import requests
3
  import gradio as gr
4
 
5
- # Load Groq API key from environment variable
6
- groq_api_key = os.getenv("GROQ_API_KEY")
 
 
7
 
8
- # Groq API endpoint
9
  url = "https://api.groq.com/openai/v1/chat/completions"
10
-
11
  headers = {
12
  "Authorization": f"Bearer {groq_api_key}"
13
  }
14
 
 
 
 
 
 
 
 
 
 
 
15
  # Function to call Groq API
16
- def chat_with_groq(user_input):
 
17
  body = {
18
  "model": "llama-3.1-8b-instant",
19
  "messages": [
20
- {"role": "user", "content": user_input}
 
 
 
21
  ]
22
  }
23
 
24
  response = requests.post(url, headers=headers, json=body)
25
-
26
  if response.status_code == 200:
27
  return response.json()['choices'][0]['message']['content']
28
  else:
29
- return f"Error: {response.json()}"
30
 
31
  # Gradio interface
32
- interface = gr.Interface(
33
- fn=chat_with_groq,
34
- inputs=gr.Textbox(lines=2, placeholder="Ask me anything..."),
35
- outputs=gr.Textbox(),
36
- title="DDS Chat with Groq AI (Llama 3.1-8B)",
37
- description="Ask your question to Groq's Llama 3.1-8B model."
38
- )
39
-
40
- interface.launch()
 
2
  import requests
3
  import gradio as gr
4
 
5
+ # Get Groq API key from environment variable
6
+ groq_api_key = os.environ.get("GROQ_API_KEY")
7
+ if not groq_api_key:
8
+ raise ValueError("Please set the GROQ_API_KEY in the Hugging Face Space secrets.")
9
 
10
+ # Groq API configuration
11
  url = "https://api.groq.com/openai/v1/chat/completions"
 
12
  headers = {
13
  "Authorization": f"Bearer {groq_api_key}"
14
  }
15
 
16
+ # Prompt template
17
+ template = """
18
+ You are a friendly and professional customer service assistant for a telecom company.
19
+ Respond to the customer's issue below with empathy and clear steps, especially for roaming support.
20
+
21
+ Customer Query: {query}
22
+
23
+ Your Response:
24
+ """
25
+
26
  # Function to call Groq API
27
+ def generate_response(user_query):
28
+ structured_prompt = template.format(query=user_query)
29
  body = {
30
  "model": "llama-3.1-8b-instant",
31
  "messages": [
32
+ {
33
+ "role": "user",
34
+ "content": structured_prompt
35
+ }
36
  ]
37
  }
38
 
39
  response = requests.post(url, headers=headers, json=body)
 
40
  if response.status_code == 200:
41
  return response.json()['choices'][0]['message']['content']
42
  else:
43
+ return f"Error {response.status_code}: {response.text}"
44
 
45
  # Gradio interface
46
+ gr.Interface(
47
+ fn=generate_response,
48
+ inputs=gr.Textbox(lines=4, placeholder="Describe your telecom issue..."),
49
+ outputs=gr.Textbox(label="Groq API Response"),
50
+ title="Telecom Support Assistant",
51
+ description="Ask your question and get a helpful reply from our AI-powered support assistant."
52
+ ).launch()