Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -2,7 +2,7 @@ import os
|
|
2 |
import requests
|
3 |
import gradio as gr
|
4 |
|
5 |
-
# Get Groq API key from
|
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.")
|
@@ -23,30 +23,43 @@ Customer Query: {query}
|
|
23 |
Your Response:
|
24 |
"""
|
25 |
|
26 |
-
#
|
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
|
46 |
-
gr.
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
import requests
|
3 |
import gradio as gr
|
4 |
|
5 |
+
# Get Groq API key from Hugging Face secrets
|
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.")
|
|
|
23 |
Your Response:
|
24 |
"""
|
25 |
|
26 |
+
# Core function to query the 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": [{"role": "user", "content": structured_prompt}]
|
|
|
|
|
|
|
|
|
|
|
32 |
}
|
33 |
|
34 |
response = requests.post(url, headers=headers, json=body)
|
35 |
if response.status_code == 200:
|
36 |
+
return response.json()['choices'][0]['message']['content'], "✅ Success"
|
37 |
else:
|
38 |
+
return f"Error {response.status_code}: {response.text}", "❌ Failed"
|
39 |
+
|
40 |
+
# Gradio app using Blocks layout
|
41 |
+
with gr.Blocks() as demo:
|
42 |
+
gr.Markdown("## 📡 Telecom Support Assistant powered by Groq API")
|
43 |
+
gr.Markdown(
|
44 |
+
"Enter your customer service query below. The AI assistant will respond with a helpful and empathetic reply, especially for **roaming issues**."
|
45 |
+
)
|
46 |
+
|
47 |
+
with gr.Row():
|
48 |
+
with gr.Column(scale=2):
|
49 |
+
user_input = gr.Textbox(
|
50 |
+
lines=5,
|
51 |
+
label="Customer Query",
|
52 |
+
placeholder="e.g., My SIM card stopped working while traveling abroad."
|
53 |
+
)
|
54 |
+
submit_btn = gr.Button("Generate Response")
|
55 |
+
with gr.Column(scale=3):
|
56 |
+
status_output = gr.Textbox(label="Status", interactive=False)
|
57 |
+
response_output = gr.Textbox(label="AI Response", lines=10)
|
58 |
+
|
59 |
+
submit_btn.click(
|
60 |
+
fn=generate_response,
|
61 |
+
inputs=user_input,
|
62 |
+
outputs=[response_output, status_output]
|
63 |
+
)
|
64 |
+
|
65 |
+
demo.launch()
|