Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -6,9 +6,11 @@ from datetime import datetime
|
|
| 6 |
from openpyxl import Workbook, load_workbook
|
| 7 |
import re
|
| 8 |
|
|
|
|
|
|
|
| 9 |
# ========== CONFIGURATION ==========
|
| 10 |
-
API_KEY = "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"
|
| 11 |
-
MODEL = "google/gemma-3n-e2b-it:free"
|
| 12 |
|
| 13 |
MENU = {
|
| 14 |
"Cheeseburger": 5.99,
|
|
@@ -19,7 +21,7 @@ MENU = {
|
|
| 19 |
"Salad": 6.99
|
| 20 |
}
|
| 21 |
|
| 22 |
-
|
| 23 |
order = []
|
| 24 |
customer_name = ""
|
| 25 |
|
|
@@ -59,7 +61,9 @@ def speak(text, filename="response.mp3"):
|
|
| 59 |
|
| 60 |
# ========== OpenRouter API ==========
|
| 61 |
def generate_response(user_input):
|
| 62 |
-
global
|
|
|
|
|
|
|
| 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"
|
|
@@ -75,12 +79,16 @@ Instructions:
|
|
| 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 |
-
|
| 83 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
|
| 85 |
context += f"\nCustomer: {user_input}\nBot:"
|
| 86 |
|
|
@@ -97,7 +105,9 @@ Conversation:
|
|
| 97 |
try:
|
| 98 |
response = requests.post("https://openrouter.ai/api/v1/chat/completions", headers=headers, json=payload)
|
| 99 |
response.raise_for_status()
|
| 100 |
-
|
|
|
|
|
|
|
| 101 |
except Exception as e:
|
| 102 |
return f"β OpenRouter Error: {str(e)}"
|
| 103 |
|
|
@@ -107,11 +117,9 @@ def handle_chat(user_input):
|
|
| 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 |
|
| 114 |
-
# Extract items
|
| 115 |
for item in MENU:
|
| 116 |
if item.lower() in user_input.lower():
|
| 117 |
qty = 1
|
|
@@ -121,13 +129,11 @@ def handle_chat(user_input):
|
|
| 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 |
|
|
@@ -139,7 +145,7 @@ gr.Interface(
|
|
| 139 |
gr.Textbox(label="π€ Bot Response"),
|
| 140 |
gr.Audio(label="π Speaking", autoplay=True)
|
| 141 |
],
|
| 142 |
-
title="π SysTaurant Voice Bot (OpenRouter
|
| 143 |
-
description="
|
| 144 |
theme="soft"
|
| 145 |
).launch(share=True)
|
|
|
|
| 6 |
from openpyxl import Workbook, load_workbook
|
| 7 |
import re
|
| 8 |
|
| 9 |
+
from langchain.memory import ConversationBufferMemory
|
| 10 |
+
|
| 11 |
# ========== CONFIGURATION ==========
|
| 12 |
+
API_KEY = "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"
|
| 13 |
+
MODEL = "google/gemma-3n-e2b-it:free"
|
| 14 |
|
| 15 |
MENU = {
|
| 16 |
"Cheeseburger": 5.99,
|
|
|
|
| 21 |
"Salad": 6.99
|
| 22 |
}
|
| 23 |
|
| 24 |
+
memory = ConversationBufferMemory(return_messages=True)
|
| 25 |
order = []
|
| 26 |
customer_name = ""
|
| 27 |
|
|
|
|
| 61 |
|
| 62 |
# ========== OpenRouter API ==========
|
| 63 |
def generate_response(user_input):
|
| 64 |
+
global customer_name, order
|
| 65 |
+
|
| 66 |
+
memory.chat_memory.add_user_message(user_input)
|
| 67 |
|
| 68 |
menu_description = "\n".join([f"{item}: ${price}" for item, price in MENU.items()])
|
| 69 |
order_summary = ", ".join([f"{qty} x {item}" for item, qty in order]) if order else "No items yet"
|
|
|
|
| 79 |
- Show menu if requested.
|
| 80 |
- Extract item names and quantities from messages.
|
| 81 |
- Say 'Order summary' and ask 'Confirm?' when user is done.
|
| 82 |
+
- Respond only as the bot, no need to prefix with \"Bot:\".
|
| 83 |
- Keep tone human, natural, and friendly.
|
| 84 |
Conversation:
|
| 85 |
"""
|
| 86 |
+
|
| 87 |
+
for msg in memory.chat_memory.messages:
|
| 88 |
+
if msg.type == "human":
|
| 89 |
+
context += f"\nCustomer: {msg.content}"
|
| 90 |
+
else:
|
| 91 |
+
context += f"\nBot: {msg.content}"
|
| 92 |
|
| 93 |
context += f"\nCustomer: {user_input}\nBot:"
|
| 94 |
|
|
|
|
| 105 |
try:
|
| 106 |
response = requests.post("https://openrouter.ai/api/v1/chat/completions", headers=headers, json=payload)
|
| 107 |
response.raise_for_status()
|
| 108 |
+
reply = response.json()["choices"][0]["message"]["content"]
|
| 109 |
+
memory.chat_memory.add_ai_message(reply)
|
| 110 |
+
return reply
|
| 111 |
except Exception as e:
|
| 112 |
return f"β OpenRouter Error: {str(e)}"
|
| 113 |
|
|
|
|
| 117 |
|
| 118 |
bot_reply = generate_response(user_input)
|
| 119 |
|
|
|
|
| 120 |
if "my name is" in user_input.lower():
|
| 121 |
customer_name = user_input.split("my name is")[-1].strip().split()[0].title()
|
| 122 |
|
|
|
|
| 123 |
for item in MENU:
|
| 124 |
if item.lower() in user_input.lower():
|
| 125 |
qty = 1
|
|
|
|
| 129 |
break
|
| 130 |
order.append((item, qty))
|
| 131 |
|
|
|
|
| 132 |
if "confirm" in user_input.lower() or "yes" in user_input.lower():
|
| 133 |
if customer_name and order:
|
| 134 |
order_id = save_to_excel(customer_name, order)
|
| 135 |
bot_reply += f"\nβ
Your order ID is {order_id}. Thank you for ordering from Saad's Restaurant!"
|
| 136 |
|
|
|
|
| 137 |
audio_file = speak(bot_reply)
|
| 138 |
return bot_reply, audio_file
|
| 139 |
|
|
|
|
| 145 |
gr.Textbox(label="π€ Bot Response"),
|
| 146 |
gr.Audio(label="π Speaking", autoplay=True)
|
| 147 |
],
|
| 148 |
+
title="π SysTaurant Voice Bot (LangChain + OpenRouter)",
|
| 149 |
+
description="Smart restaurant assistant powered by LangChain memory and OpenRouter API.",
|
| 150 |
theme="soft"
|
| 151 |
).launch(share=True)
|