Spaces:
Runtime error
Runtime error
**`app.py`** | |
```python | |
import gradio as gr | |
import requests | |
import os | |
# Fetch your Groq API key securely from HF Secrets | |
groq_api_key = os.getenv("GROQ_API_KEY") | |
# Path to local business.txt (uploaded in your Space repo) | |
business_file = os.path.join(os.path.dirname(__file__), "business.txt") | |
def chat_with_business(message, history): | |
try: | |
# Read business knowledge from local file | |
with open(business_file, "r", encoding="utf-8") as f: | |
business_info = f.read().strip() | |
# System prompt including business details | |
system_prompt = ( | |
"You are a helpful customer care assistant. " | |
"Use only the following business information to answer the user's query:\n\n" + | |
business_info + | |
"\n\nIf the answer is not in the knowledge, reply 'Yeh information abhi available nahi hai.'" | |
) | |
# Prepare Groq API payload | |
headers = { | |
"Authorization": f"Bearer {groq_api_key}", | |
"Content-Type": "application/json" | |
} | |
payload = { | |
"model": "llama3-70b-8192", | |
"messages": [ | |
{"role": "system", "content": system_prompt}, | |
{"role": "user", "content": message} | |
], | |
"temperature": 0.7 | |
} | |
# Call Groq chat endpoint | |
response = requests.post( | |
"https://api.groq.com/openai/v1/chat/completions", | |
headers=headers, | |
json=payload | |
) | |
response.raise_for_status() | |
data = response.json() | |
reply = data["choices"][0]["message"]["content"] | |
return reply | |
except Exception as e: | |
return f"Error: {e}" | |
# Build Gradio interface | |
with gr.Blocks(theme="soft") as demo: | |
gr.Markdown("## 🌿 My Business Bot") | |
gr.Markdown("*Ask anything about your business in Hindi-English*") | |
chatbot = gr.Chatbot(elem_id="chatbox", height=400) | |
user_input = gr.Textbox(placeholder="Type your question here...", show_label=False) | |
def handle_interaction(message, chat_history): | |
bot_reply = chat_with_business(message, chat_history) | |
chat_history = chat_history + [(message, bot_reply)] | |
return chat_history, "" | |
user_input.submit(handle_interaction, [user_input, chatbot], [chatbot, user_input]) | |
# Launch app | |
if __name__ == "__main__": | |
demo.launch() | |