Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -4,55 +4,23 @@ import gradio as gr
|
|
4 |
from gtts import gTTS
|
5 |
from datetime import datetime
|
6 |
from openpyxl import Workbook, load_workbook
|
|
|
7 |
|
8 |
-
|
|
|
|
|
|
|
9 |
from langchain.memory import ConversationBufferMemory
|
10 |
from langchain.chains import ConversationChain
|
11 |
-
from langchain.prompts import PromptTemplate
|
12 |
-
|
13 |
-
import google.generativeai as genai
|
14 |
-
|
15 |
-
# ========== API Setup ==========
|
16 |
-
genai.configure(api_key="AIzaSyBJFmohAmhmqXQlM3fVxj8MLegVb26kyJk")
|
17 |
|
18 |
-
|
19 |
-
|
20 |
|
21 |
-
|
22 |
-
|
|
|
23 |
|
24 |
-
|
25 |
-
{menu}
|
26 |
-
|
27 |
-
Customer name: {name}
|
28 |
-
Current order: {order_summary}
|
29 |
-
|
30 |
-
Instructions:
|
31 |
-
- Ask for name if not known.
|
32 |
-
- Show menu if requested.
|
33 |
-
- Extract item names and quantities from messages.
|
34 |
-
- Say 'Order summary' and ask 'Confirm?' when user is done.
|
35 |
-
- Respond only as the bot, no need to prefix with "Bot:".
|
36 |
-
- Keep tone human, natural, and friendly.
|
37 |
-
|
38 |
-
Conversation:
|
39 |
-
{history}
|
40 |
-
Customer: {input}
|
41 |
-
Bot:"""
|
42 |
-
|
43 |
-
prompt = PromptTemplate(
|
44 |
-
input_variables=["history", "input", "menu", "name", "order_summary"],
|
45 |
-
template=template
|
46 |
-
)
|
47 |
-
|
48 |
-
chain = ConversationChain(
|
49 |
-
llm=llm,
|
50 |
-
memory=memory,
|
51 |
-
prompt=prompt,
|
52 |
-
verbose=False
|
53 |
-
)
|
54 |
-
|
55 |
-
# ========== Menu & Order State ==========
|
56 |
MENU = {
|
57 |
"Cheeseburger": 5.99,
|
58 |
"Fries": 2.99,
|
@@ -61,12 +29,10 @@ MENU = {
|
|
61 |
"Chicken Wings": 7.99,
|
62 |
"Salad": 6.99
|
63 |
}
|
64 |
-
|
65 |
-
chat_history = []
|
66 |
order = []
|
67 |
customer_name = ""
|
68 |
|
69 |
-
#
|
70 |
EXCEL_FILE = "orders.xlsx"
|
71 |
def setup_excel():
|
72 |
if not os.path.exists(EXCEL_FILE):
|
@@ -75,7 +41,6 @@ def setup_excel():
|
|
75 |
ws.title = "Orders"
|
76 |
ws.append(["Order ID", "Date", "Customer", "Items", "Total", "Time"])
|
77 |
wb.save(EXCEL_FILE)
|
78 |
-
|
79 |
setup_excel()
|
80 |
|
81 |
def save_to_excel(name, items):
|
@@ -89,7 +54,8 @@ def save_to_excel(name, items):
|
|
89 |
wb.save(EXCEL_FILE)
|
90 |
return order_id
|
91 |
|
92 |
-
#
|
|
|
93 |
def clean_text(text):
|
94 |
text = re.sub(r"\*\*(.*?)\*\*", r"\1", text)
|
95 |
text = re.sub(r"Bot\s*:\s*", "", text, flags=re.IGNORECASE)
|
@@ -101,15 +67,37 @@ def speak(text, filename="response.mp3"):
|
|
101 |
tts.save(filename)
|
102 |
return filename
|
103 |
|
104 |
-
#
|
105 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
106 |
global customer_name, order
|
107 |
|
108 |
-
#
|
109 |
if "my name is" in user_input.lower():
|
110 |
customer_name = user_input.split("my name is")[-1].strip().split()[0].title()
|
111 |
|
112 |
-
#
|
113 |
for item in MENU:
|
114 |
if item.lower() in user_input.lower():
|
115 |
qty = 1
|
@@ -119,26 +107,32 @@ def handle_chat(user_input):
|
|
119 |
break
|
120 |
order.append((item, qty))
|
121 |
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
126 |
"input": user_input,
|
127 |
-
"menu":
|
128 |
"name": customer_name or "Not provided",
|
129 |
-
"
|
130 |
})
|
131 |
|
132 |
-
|
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 |
-
|
|
|
138 |
audio_file = speak(bot_reply)
|
139 |
return bot_reply, audio_file
|
140 |
|
141 |
-
#
|
142 |
gr.Interface(
|
143 |
fn=handle_chat,
|
144 |
inputs=gr.Textbox(label="π€ You", placeholder="Type your order..."),
|
@@ -146,7 +140,7 @@ gr.Interface(
|
|
146 |
gr.Textbox(label="π€ Bot Response"),
|
147 |
gr.Audio(label="π Speaking", autoplay=True)
|
148 |
],
|
149 |
-
title="π SysTaurant Voice Bot",
|
150 |
-
description="
|
151 |
theme="soft"
|
152 |
).launch(share=True)
|
|
|
4 |
from gtts import gTTS
|
5 |
from datetime import datetime
|
6 |
from openpyxl import Workbook, load_workbook
|
7 |
+
import google.generativeai as genai
|
8 |
|
9 |
+
# LangChain memory
|
10 |
+
from langchain_core.prompts import ChatPromptTemplate
|
11 |
+
from langchain_core.messages import HumanMessage, AIMessage
|
12 |
+
from langchain_google_genai import ChatGoogleGenerativeAI
|
13 |
from langchain.memory import ConversationBufferMemory
|
14 |
from langchain.chains import ConversationChain
|
|
|
|
|
|
|
|
|
|
|
|
|
15 |
|
16 |
+
# ========== SETUP ==========
|
17 |
+
genai.configure(api_key="AIzaSyBJFmohAmhmqXQlM3fVxj8MLegVb26kyJk")
|
18 |
|
19 |
+
llm = ChatGoogleGenerativeAI(model="gemini-1.5-flash", convert_system_message_to_human=True)
|
20 |
+
memory = ConversationBufferMemory(return_messages=True)
|
21 |
+
chain = ConversationChain(llm=llm, memory=memory, verbose=False)
|
22 |
|
23 |
+
# Menu & State
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
MENU = {
|
25 |
"Cheeseburger": 5.99,
|
26 |
"Fries": 2.99,
|
|
|
29 |
"Chicken Wings": 7.99,
|
30 |
"Salad": 6.99
|
31 |
}
|
|
|
|
|
32 |
order = []
|
33 |
customer_name = ""
|
34 |
|
35 |
+
# Excel Setup
|
36 |
EXCEL_FILE = "orders.xlsx"
|
37 |
def setup_excel():
|
38 |
if not os.path.exists(EXCEL_FILE):
|
|
|
41 |
ws.title = "Orders"
|
42 |
ws.append(["Order ID", "Date", "Customer", "Items", "Total", "Time"])
|
43 |
wb.save(EXCEL_FILE)
|
|
|
44 |
setup_excel()
|
45 |
|
46 |
def save_to_excel(name, items):
|
|
|
54 |
wb.save(EXCEL_FILE)
|
55 |
return order_id
|
56 |
|
57 |
+
# Voice
|
58 |
+
|
59 |
def clean_text(text):
|
60 |
text = re.sub(r"\*\*(.*?)\*\*", r"\1", text)
|
61 |
text = re.sub(r"Bot\s*:\s*", "", text, flags=re.IGNORECASE)
|
|
|
67 |
tts.save(filename)
|
68 |
return filename
|
69 |
|
70 |
+
# Prompt Template
|
71 |
+
system_prompt = """
|
72 |
+
You are a helpful restaurant assistant at 'Systaurant'.
|
73 |
+
|
74 |
+
Your job is to:
|
75 |
+
- Greet the user and ask their name if not known.
|
76 |
+
- Show the menu if requested.
|
77 |
+
- Extract food items and quantities from the customer's message.
|
78 |
+
- If the user says 'done', summarize the order and ask for confirmation.
|
79 |
+
- If confirmed, respond with a thank you and order ID.
|
80 |
+
- Keep the tone friendly and human. Do not prefix with "Bot:".
|
81 |
+
|
82 |
+
Menu:
|
83 |
+
{menu}
|
84 |
+
Customer name: {name}
|
85 |
+
Order so far: {summary}
|
86 |
+
"""
|
87 |
+
|
88 |
+
prompt = ChatPromptTemplate.from_messages([
|
89 |
+
("system", system_prompt),
|
90 |
+
("human", "{input}")
|
91 |
+
])
|
92 |
+
|
93 |
+
def generate_response(user_input):
|
94 |
global customer_name, order
|
95 |
|
96 |
+
# Track name
|
97 |
if "my name is" in user_input.lower():
|
98 |
customer_name = user_input.split("my name is")[-1].strip().split()[0].title()
|
99 |
|
100 |
+
# Track items
|
101 |
for item in MENU:
|
102 |
if item.lower() in user_input.lower():
|
103 |
qty = 1
|
|
|
107 |
break
|
108 |
order.append((item, qty))
|
109 |
|
110 |
+
# Confirmation
|
111 |
+
if "confirm" in user_input.lower() or "yes" in user_input.lower():
|
112 |
+
if customer_name and order:
|
113 |
+
order_id = save_to_excel(customer_name, order)
|
114 |
+
memory.chat_memory.add_user_message(user_input)
|
115 |
+
memory.chat_memory.add_ai_message(f"β
Your order ID is {order_id}. Thank you for ordering from Saad's Restaurant!")
|
116 |
+
return f"β
Your order ID is {order_id}. Thank you for ordering from Saad's Restaurant!"
|
117 |
+
|
118 |
+
menu_text = "\n".join([f"{item}: ${price}" for item, price in MENU.items()])
|
119 |
+
summary = ", ".join([f"{qty} x {item}" for item, qty in order]) if order else "No items yet"
|
120 |
+
|
121 |
+
filled_prompt = prompt.invoke({
|
122 |
"input": user_input,
|
123 |
+
"menu": menu_text,
|
124 |
"name": customer_name or "Not provided",
|
125 |
+
"summary": summary
|
126 |
})
|
127 |
|
128 |
+
return chain.invoke(filled_prompt.to_string())
|
|
|
|
|
|
|
129 |
|
130 |
+
def handle_chat(user_input):
|
131 |
+
bot_reply = generate_response(user_input)
|
132 |
audio_file = speak(bot_reply)
|
133 |
return bot_reply, audio_file
|
134 |
|
135 |
+
# Gradio Interface
|
136 |
gr.Interface(
|
137 |
fn=handle_chat,
|
138 |
inputs=gr.Textbox(label="π€ You", placeholder="Type your order..."),
|
|
|
140 |
gr.Textbox(label="π€ Bot Response"),
|
141 |
gr.Audio(label="π Speaking", autoplay=True)
|
142 |
],
|
143 |
+
title="π SysTaurant Voice Bot with Memory",
|
144 |
+
description="Place your restaurant order through chat with memory.",
|
145 |
theme="soft"
|
146 |
).launch(share=True)
|