Spaces:
Sleeping
Sleeping
import os | |
import requests | |
import gradio as gr | |
# Get Groq API key from environment variable | |
groq_api_key = os.environ.get("GROQ_API_KEY") | |
if not groq_api_key: | |
raise ValueError("Please set the GROQ_API_KEY in the Hugging Face Space secrets.") | |
# Groq API configuration | |
url = "https://api.groq.com/openai/v1/chat/completions" | |
headers = { | |
"Authorization": f"Bearer {groq_api_key}" | |
} | |
# Prompt template | |
template = """ | |
You are a friendly and professional customer service assistant for a telecom company. | |
Respond to the customer's issue below with empathy and clear steps, especially for roaming support. | |
Customer Query: {query} | |
Your Response: | |
""" | |
# Function to call Groq API | |
def generate_response(user_query): | |
structured_prompt = template.format(query=user_query) | |
body = { | |
"model": "llama-3.1-8b-instant", | |
"messages": [ | |
{ | |
"role": "user", | |
"content": structured_prompt | |
} | |
] | |
} | |
response = requests.post(url, headers=headers, json=body) | |
if response.status_code == 200: | |
return response.json()['choices'][0]['message']['content'] | |
else: | |
return f"Error {response.status_code}: {response.text}" | |
# Gradio interface | |
gr.Interface( | |
fn=generate_response, | |
inputs=gr.Textbox(lines=4, placeholder="Describe your telecom issue..."), | |
outputs=gr.Textbox(label="Groq API Response"), | |
title="Zain Customer Care Support Assistant", | |
description="Ask your question and get a helpful reply from our AI-powered support assistant." | |
).launch() | |