Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -23,107 +23,19 @@ current_category = None
|
|
23 |
current_item = None
|
24 |
awaiting_quantity = False
|
25 |
|
26 |
-
#
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
}
|
40 |
-
h1 {
|
41 |
-
color: #333;
|
42 |
-
}
|
43 |
-
.mic-button {
|
44 |
-
width: 80px;
|
45 |
-
height: 80px;
|
46 |
-
border-radius: 50%;
|
47 |
-
background-color: #007bff;
|
48 |
-
color: white;
|
49 |
-
font-size: 24px;
|
50 |
-
border: none;
|
51 |
-
cursor: pointer;
|
52 |
-
}
|
53 |
-
.status, .response {
|
54 |
-
margin-top: 20px;
|
55 |
-
}
|
56 |
-
</style>
|
57 |
-
</head>
|
58 |
-
<body>
|
59 |
-
<h1>AI Dining Assistant</h1>
|
60 |
-
<button class="mic-button" id="mic-button">🎤</button>
|
61 |
-
<div class="status" id="status">Press the mic button to start...</div>
|
62 |
-
<div class="response" id="response" style="display: none;">Response will appear here...</div>
|
63 |
-
<script>
|
64 |
-
const micButton = document.getElementById('mic-button');
|
65 |
-
const status = document.getElementById('status');
|
66 |
-
const response = document.getElementById('response');
|
67 |
-
let isListening = false;
|
68 |
-
|
69 |
-
micButton.addEventListener('click', () => {
|
70 |
-
if (!isListening) {
|
71 |
-
isListening = true;
|
72 |
-
greetUser();
|
73 |
-
}
|
74 |
-
});
|
75 |
-
|
76 |
-
function greetUser() {
|
77 |
-
const utterance = new SpeechSynthesisUtterance("Hi. Welcome to Biryani Hub. Can I show you the menu?");
|
78 |
-
speechSynthesis.speak(utterance);
|
79 |
-
utterance.onend = () => {
|
80 |
-
status.textContent = "Listening...";
|
81 |
-
startListening();
|
82 |
-
};
|
83 |
-
}
|
84 |
-
|
85 |
-
async function startListening() {
|
86 |
-
const stream = await navigator.mediaDevices.getUserMedia({ audio: true });
|
87 |
-
const mediaRecorder = new MediaRecorder(stream, { mimeType: "audio/webm;codecs=opus" });
|
88 |
-
const audioChunks = [];
|
89 |
-
|
90 |
-
mediaRecorder.ondataavailable = (event) => audioChunks.push(event.data);
|
91 |
-
mediaRecorder.onstop = async () => {
|
92 |
-
const audioBlob = new Blob(audioChunks, { type: "audio/webm" });
|
93 |
-
const formData = new FormData();
|
94 |
-
formData.append("audio", audioBlob);
|
95 |
-
|
96 |
-
status.textContent = "Processing...";
|
97 |
-
try {
|
98 |
-
const result = await fetch("/process-audio", { method: "POST", body: formData });
|
99 |
-
const data = await result.json();
|
100 |
-
response.textContent = data.response;
|
101 |
-
response.style.display = "block";
|
102 |
-
|
103 |
-
const utterance = new SpeechSynthesisUtterance(data.response);
|
104 |
-
speechSynthesis.speak(utterance);
|
105 |
-
utterance.onend = () => {
|
106 |
-
if (!data.response.includes("Goodbye") && !data.response.includes("final order")) {
|
107 |
-
startListening(); // Continue listening
|
108 |
-
} else {
|
109 |
-
status.textContent = "Conversation ended.";
|
110 |
-
isListening = false;
|
111 |
-
}
|
112 |
-
};
|
113 |
-
} catch (error) {
|
114 |
-
response.textContent = "Error processing your request. Please try again.";
|
115 |
-
status.textContent = "Press the mic button to restart.";
|
116 |
-
isListening = false;
|
117 |
-
}
|
118 |
-
};
|
119 |
-
|
120 |
-
mediaRecorder.start();
|
121 |
-
setTimeout(() => mediaRecorder.stop(), 5000); // Stop recording after 5 seconds
|
122 |
-
}
|
123 |
-
</script>
|
124 |
-
</body>
|
125 |
-
</html>
|
126 |
-
"""
|
127 |
|
128 |
@app.route("/")
|
129 |
def index():
|
@@ -175,18 +87,18 @@ def process_command(command):
|
|
175 |
command = command.lower()
|
176 |
|
177 |
# Handle quantity input
|
178 |
-
if awaiting_quantity
|
179 |
-
quantity =
|
180 |
-
if quantity
|
181 |
cart.append((current_item, MENU[current_category][current_item], quantity))
|
182 |
awaiting_quantity = False
|
183 |
item = current_item
|
184 |
current_item = None
|
185 |
total = sum(i[1] * i[2] for i in cart)
|
186 |
cart_summary = ", ".join([f"{i[0]} x{i[2]} (₹{i[1] * i[2]})" for i in cart])
|
187 |
-
return f"Added {quantity}
|
188 |
else:
|
189 |
-
return "
|
190 |
|
191 |
# Handle category selection
|
192 |
for category, items in MENU.items():
|
|
|
23 |
current_item = None
|
24 |
awaiting_quantity = False
|
25 |
|
26 |
+
# Extract quantity from command (e.g., "two" -> 2, "three" -> 3, etc.)
|
27 |
+
def extract_quantity(command):
|
28 |
+
number_words = {
|
29 |
+
"one": 1, "two": 2, "three": 3, "four": 4, "five": 5,
|
30 |
+
"six": 6, "seven": 7, "eight": 8, "nine": 9, "ten": 10,
|
31 |
+
"1": 1, "2": 2, "3": 3, "4": 4, "5": 5, "6": 6, "7": 7, "8": 8, "9": 9, "10": 10
|
32 |
+
}
|
33 |
+
|
34 |
+
command_words = command.split()
|
35 |
+
for word in command_words:
|
36 |
+
if word in number_words:
|
37 |
+
return number_words[word]
|
38 |
+
return None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
39 |
|
40 |
@app.route("/")
|
41 |
def index():
|
|
|
87 |
command = command.lower()
|
88 |
|
89 |
# Handle quantity input
|
90 |
+
if awaiting_quantity:
|
91 |
+
quantity = extract_quantity(command)
|
92 |
+
if quantity:
|
93 |
cart.append((current_item, MENU[current_category][current_item], quantity))
|
94 |
awaiting_quantity = False
|
95 |
item = current_item
|
96 |
current_item = None
|
97 |
total = sum(i[1] * i[2] for i in cart)
|
98 |
cart_summary = ", ".join([f"{i[0]} x{i[2]} (₹{i[1] * i[2]})" for i in cart])
|
99 |
+
return f"Added {quantity} x {item} to your cart. Your current cart: {cart_summary}. Total: ₹{total}. Would you like to add more items?"
|
100 |
else:
|
101 |
+
return "Sorry, I couldn't understand the quantity. Please provide a valid quantity."
|
102 |
|
103 |
# Handle category selection
|
104 |
for category, items in MENU.items():
|