Spaces:
Runtime error
Runtime error
File size: 2,375 Bytes
722e6c7 f001211 722e6c7 f001211 722e6c7 f001211 722e6c7 f001211 722e6c7 f001211 722e6c7 f001211 722e6c7 f001211 722e6c7 f001211 722e6c7 f001211 722e6c7 f001211 722e6c7 f001211 722e6c7 f001211 722e6c7 f001211 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
**`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()
|