Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,14 +1,15 @@
|
|
| 1 |
import os
|
| 2 |
-
import
|
| 3 |
import gradio as gr
|
| 4 |
from gtts import gTTS
|
| 5 |
from datetime import datetime
|
| 6 |
from openpyxl import Workbook, load_workbook
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
|
|
|
|
|
|
| 10 |
|
| 11 |
-
# ========== SETUP ==========
|
| 12 |
MENU = {
|
| 13 |
"Cheeseburger": 5.99,
|
| 14 |
"Fries": 2.99,
|
|
@@ -17,12 +18,13 @@ MENU = {
|
|
| 17 |
"Chicken Wings": 7.99,
|
| 18 |
"Salad": 6.99
|
| 19 |
}
|
|
|
|
|
|
|
| 20 |
order = []
|
| 21 |
customer_name = ""
|
| 22 |
-
EXCEL_FILE = "orders.xlsx"
|
| 23 |
-
chat_memory = ConversationBufferMemory()
|
| 24 |
|
| 25 |
# ========== Excel Setup ==========
|
|
|
|
| 26 |
def setup_excel():
|
| 27 |
if not os.path.exists(EXCEL_FILE):
|
| 28 |
wb = Workbook()
|
|
@@ -30,7 +32,6 @@ def setup_excel():
|
|
| 30 |
ws.title = "Orders"
|
| 31 |
ws.append(["Order ID", "Date", "Customer", "Items", "Total", "Time"])
|
| 32 |
wb.save(EXCEL_FILE)
|
| 33 |
-
|
| 34 |
setup_excel()
|
| 35 |
|
| 36 |
def save_to_excel(name, items):
|
|
@@ -51,25 +52,62 @@ def clean_text(text):
|
|
| 51 |
return text.strip()
|
| 52 |
|
| 53 |
def speak(text, filename="response.mp3"):
|
| 54 |
-
|
|
|
|
| 55 |
tts.save(filename)
|
| 56 |
return filename
|
| 57 |
|
| 58 |
-
# ==========
|
| 59 |
-
def
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
def handle_chat(user_input):
|
| 70 |
global customer_name, order
|
| 71 |
|
| 72 |
-
|
|
|
|
|
|
|
| 73 |
if "my name is" in user_input.lower():
|
| 74 |
customer_name = user_input.split("my name is")[-1].strip().split()[0].title()
|
| 75 |
|
|
@@ -83,43 +121,25 @@ def handle_chat(user_input):
|
|
| 83 |
break
|
| 84 |
order.append((item, qty))
|
| 85 |
|
| 86 |
-
#
|
| 87 |
-
menu_str = "\n".join([f"{item}: ${price}" for item, price in MENU.items()])
|
| 88 |
-
order_str = ", ".join([f"{qty} x {item}" for item, qty in order]) if order else "No items yet"
|
| 89 |
-
prompt = f"""
|
| 90 |
-
You are a friendly restaurant assistant for 'Systaurant'.
|
| 91 |
-
Here is the menu:
|
| 92 |
-
{menu_str}
|
| 93 |
-
|
| 94 |
-
Current customer name: {customer_name or 'Not provided'}
|
| 95 |
-
Current order: {order_str}
|
| 96 |
-
|
| 97 |
-
User: {user_input}
|
| 98 |
-
Bot:"""
|
| 99 |
-
|
| 100 |
-
# Generate LLM response
|
| 101 |
-
chat_memory.chat_memory.add_user_message(user_input)
|
| 102 |
-
reply = llm(prompt)
|
| 103 |
-
chat_memory.chat_memory.add_ai_message(reply)
|
| 104 |
-
|
| 105 |
-
# Confirm order if user says yes
|
| 106 |
if "confirm" in user_input.lower() or "yes" in user_input.lower():
|
| 107 |
if customer_name and order:
|
| 108 |
order_id = save_to_excel(customer_name, order)
|
| 109 |
-
|
| 110 |
|
| 111 |
-
|
| 112 |
-
|
|
|
|
| 113 |
|
| 114 |
-
# ========== Gradio
|
| 115 |
gr.Interface(
|
| 116 |
fn=handle_chat,
|
| 117 |
-
inputs=gr.Textbox(label="π€ You", placeholder="
|
| 118 |
outputs=[
|
| 119 |
-
gr.Textbox(label="π€ Bot"),
|
| 120 |
-
gr.Audio(label="π
|
| 121 |
],
|
| 122 |
-
title="
|
| 123 |
-
description="
|
| 124 |
theme="soft"
|
| 125 |
-
).launch()
|
|
|
|
| 1 |
import os
|
| 2 |
+
import requests
|
| 3 |
import gradio as gr
|
| 4 |
from gtts import gTTS
|
| 5 |
from datetime import datetime
|
| 6 |
from openpyxl import Workbook, load_workbook
|
| 7 |
+
import re
|
| 8 |
+
|
| 9 |
+
# ========== CONFIGURATION ==========
|
| 10 |
+
API_KEY = "v1-ad9cdbda8503e3a1b67e9087b4a3ab5f0c115c217eea5b392dad8c06c6537db4"
|
| 11 |
+
MODEL = "google/gemini-pro" # You can also try "mistral/mixtral-8x7b"
|
| 12 |
|
|
|
|
| 13 |
MENU = {
|
| 14 |
"Cheeseburger": 5.99,
|
| 15 |
"Fries": 2.99,
|
|
|
|
| 18 |
"Chicken Wings": 7.99,
|
| 19 |
"Salad": 6.99
|
| 20 |
}
|
| 21 |
+
|
| 22 |
+
chat_history = []
|
| 23 |
order = []
|
| 24 |
customer_name = ""
|
|
|
|
|
|
|
| 25 |
|
| 26 |
# ========== Excel Setup ==========
|
| 27 |
+
EXCEL_FILE = "orders.xlsx"
|
| 28 |
def setup_excel():
|
| 29 |
if not os.path.exists(EXCEL_FILE):
|
| 30 |
wb = Workbook()
|
|
|
|
| 32 |
ws.title = "Orders"
|
| 33 |
ws.append(["Order ID", "Date", "Customer", "Items", "Total", "Time"])
|
| 34 |
wb.save(EXCEL_FILE)
|
|
|
|
| 35 |
setup_excel()
|
| 36 |
|
| 37 |
def save_to_excel(name, items):
|
|
|
|
| 52 |
return text.strip()
|
| 53 |
|
| 54 |
def speak(text, filename="response.mp3"):
|
| 55 |
+
cleaned = clean_text(text)
|
| 56 |
+
tts = gTTS(text=cleaned)
|
| 57 |
tts.save(filename)
|
| 58 |
return filename
|
| 59 |
|
| 60 |
+
# ========== OpenRouter API ==========
|
| 61 |
+
def generate_response(user_input):
|
| 62 |
+
global chat_history, customer_name, order
|
| 63 |
+
|
| 64 |
+
menu_description = "\n".join([f"{item}: ${price}" for item, price in MENU.items()])
|
| 65 |
+
order_summary = ", ".join([f"{qty} x {item}" for item, qty in order]) if order else "No items yet"
|
| 66 |
+
|
| 67 |
+
context = f"""
|
| 68 |
+
You are a friendly, helpful restaurant assistant at 'Systaurant'.
|
| 69 |
+
MENU:
|
| 70 |
+
{menu_description}
|
| 71 |
+
Customer name: {customer_name}
|
| 72 |
+
Current order: {order_summary}
|
| 73 |
+
Instructions:
|
| 74 |
+
- Ask for name if not known.
|
| 75 |
+
- Show menu if requested.
|
| 76 |
+
- Extract item names and quantities from messages.
|
| 77 |
+
- Say 'Order summary' and ask 'Confirm?' when user is done.
|
| 78 |
+
- Respond only as the bot, no need to prefix with "Bot:".
|
| 79 |
+
- Keep tone human, natural, and friendly.
|
| 80 |
+
Conversation:
|
| 81 |
+
"""
|
| 82 |
+
for u, b in chat_history:
|
| 83 |
+
context += f"\nCustomer: {u}\nBot: {b}"
|
| 84 |
+
|
| 85 |
+
context += f"\nCustomer: {user_input}\nBot:"
|
| 86 |
+
|
| 87 |
+
headers = {
|
| 88 |
+
"Authorization": f"Bearer {API_KEY}",
|
| 89 |
+
"Content-Type": "application/json"
|
| 90 |
+
}
|
| 91 |
+
|
| 92 |
+
payload = {
|
| 93 |
+
"model": MODEL,
|
| 94 |
+
"messages": [{"role": "user", "content": context}],
|
| 95 |
+
}
|
| 96 |
+
|
| 97 |
+
try:
|
| 98 |
+
response = requests.post("https://openrouter.ai/api/v1/chat/completions", headers=headers, json=payload)
|
| 99 |
+
response.raise_for_status()
|
| 100 |
+
return response.json()["choices"][0]["message"]["content"]
|
| 101 |
+
except Exception as e:
|
| 102 |
+
return f"β OpenRouter Error: {str(e)}"
|
| 103 |
+
|
| 104 |
+
# ========== Chat Logic ==========
|
| 105 |
def handle_chat(user_input):
|
| 106 |
global customer_name, order
|
| 107 |
|
| 108 |
+
bot_reply = generate_response(user_input)
|
| 109 |
+
|
| 110 |
+
# Extract name
|
| 111 |
if "my name is" in user_input.lower():
|
| 112 |
customer_name = user_input.split("my name is")[-1].strip().split()[0].title()
|
| 113 |
|
|
|
|
| 121 |
break
|
| 122 |
order.append((item, qty))
|
| 123 |
|
| 124 |
+
# Confirm order
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 125 |
if "confirm" in user_input.lower() or "yes" in user_input.lower():
|
| 126 |
if customer_name and order:
|
| 127 |
order_id = save_to_excel(customer_name, order)
|
| 128 |
+
bot_reply += f"\nβ
Your order ID is {order_id}. Thank you for ordering from Saad's Restaurant!"
|
| 129 |
|
| 130 |
+
chat_history.append((user_input, bot_reply))
|
| 131 |
+
audio_file = speak(bot_reply)
|
| 132 |
+
return bot_reply, audio_file
|
| 133 |
|
| 134 |
+
# ========== Gradio UI ==========
|
| 135 |
gr.Interface(
|
| 136 |
fn=handle_chat,
|
| 137 |
+
inputs=gr.Textbox(label="π€ You", placeholder="Type your order..."),
|
| 138 |
outputs=[
|
| 139 |
+
gr.Textbox(label="π€ Bot Response"),
|
| 140 |
+
gr.Audio(label="π Speaking", autoplay=True)
|
| 141 |
],
|
| 142 |
+
title="π SysTaurant Voice Bot (OpenRouter powered)",
|
| 143 |
+
description="Talk to the bot and it will handle your order using OpenRouter API.",
|
| 144 |
theme="soft"
|
| 145 |
+
).launch(share=True)
|