Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -2,39 +2,51 @@ import os
|
|
2 |
import requests
|
3 |
import gradio as gr
|
4 |
|
5 |
-
#
|
6 |
-
groq_api_key = os.
|
|
|
|
|
7 |
|
8 |
-
# Groq API
|
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
|
|
|
17 |
body = {
|
18 |
"model": "llama-3.1-8b-instant",
|
19 |
"messages": [
|
20 |
-
{
|
|
|
|
|
|
|
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.
|
30 |
|
31 |
# Gradio interface
|
32 |
-
|
33 |
-
fn=
|
34 |
-
inputs=gr.Textbox(lines=
|
35 |
-
outputs=gr.Textbox(),
|
36 |
-
title="
|
37 |
-
description="Ask your question
|
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()
|
|
|
|