Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -26,7 +26,7 @@ html_code = """
|
|
| 26 |
<body>
|
| 27 |
<h1>AI Dining Assistant</h1>
|
| 28 |
<button class="mic-button" id="mic-button">🎤</button>
|
| 29 |
-
<div class="status" id="status">Press the mic button to
|
| 30 |
<div class="response" id="response" style="display: none;">Response will appear here...</div>
|
| 31 |
<script>
|
| 32 |
const micButton = document.getElementById('mic-button');
|
|
@@ -42,7 +42,7 @@ html_code = """
|
|
| 42 |
}
|
| 43 |
});
|
| 44 |
function startConversation() {
|
| 45 |
-
status.textContent = 'Listening...';
|
| 46 |
startListening();
|
| 47 |
}
|
| 48 |
function startListening() {
|
|
@@ -71,7 +71,7 @@ html_code = """
|
|
| 71 |
} else {
|
| 72 |
status.textContent = 'Waiting for next input...';
|
| 73 |
setTimeout(() => {
|
| 74 |
-
status.textContent = 'Listening...';
|
| 75 |
startListening();
|
| 76 |
}, 2000);
|
| 77 |
}
|
|
@@ -82,7 +82,7 @@ html_code = """
|
|
| 82 |
isConversationActive = false;
|
| 83 |
};
|
| 84 |
} catch (error) {
|
| 85 |
-
response.textContent = '
|
| 86 |
response.style.display = 'block';
|
| 87 |
status.textContent = 'Press the mic button to restart the conversation.';
|
| 88 |
isConversationActive = false;
|
|
@@ -124,8 +124,11 @@ def process_audio():
|
|
| 124 |
recognizer = sr.Recognizer()
|
| 125 |
with sr.AudioFile(converted_file.name) as source:
|
| 126 |
audio_data = recognizer.record(source)
|
| 127 |
-
|
| 128 |
-
|
|
|
|
|
|
|
|
|
|
| 129 |
|
| 130 |
return jsonify({"response": response})
|
| 131 |
|
|
@@ -140,17 +143,25 @@ def process_audio():
|
|
| 140 |
def process_command(command):
|
| 141 |
global cart
|
| 142 |
command = command.lower()
|
| 143 |
-
menu_items =
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 144 |
if "menu" in command:
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
|
|
|
| 148 |
if item:
|
| 149 |
cart.append(item)
|
| 150 |
return f"{item.capitalize()} added to your cart. Your cart now has: {', '.join(cart)}."
|
| 151 |
-
elif "order" in command:
|
| 152 |
if cart:
|
| 153 |
-
return f"Your order
|
| 154 |
else:
|
| 155 |
return "Your cart is empty. Please add items to your cart first."
|
| 156 |
elif "no" in command or "nothing" in command:
|
|
|
|
| 26 |
<body>
|
| 27 |
<h1>AI Dining Assistant</h1>
|
| 28 |
<button class="mic-button" id="mic-button">🎤</button>
|
| 29 |
+
<div class="status" id="status">Press the mic button to give your order...</div>
|
| 30 |
<div class="response" id="response" style="display: none;">Response will appear here...</div>
|
| 31 |
<script>
|
| 32 |
const micButton = document.getElementById('mic-button');
|
|
|
|
| 42 |
}
|
| 43 |
});
|
| 44 |
function startConversation() {
|
| 45 |
+
status.textContent = 'Listening... Please give your order.';
|
| 46 |
startListening();
|
| 47 |
}
|
| 48 |
function startListening() {
|
|
|
|
| 71 |
} else {
|
| 72 |
status.textContent = 'Waiting for next input...';
|
| 73 |
setTimeout(() => {
|
| 74 |
+
status.textContent = 'Listening... Please give your order.';
|
| 75 |
startListening();
|
| 76 |
}, 2000);
|
| 77 |
}
|
|
|
|
| 82 |
isConversationActive = false;
|
| 83 |
};
|
| 84 |
} catch (error) {
|
| 85 |
+
response.textContent = 'Sorry, I could not understand. Please try again.';
|
| 86 |
response.style.display = 'block';
|
| 87 |
status.textContent = 'Press the mic button to restart the conversation.';
|
| 88 |
isConversationActive = false;
|
|
|
|
| 124 |
recognizer = sr.Recognizer()
|
| 125 |
with sr.AudioFile(converted_file.name) as source:
|
| 126 |
audio_data = recognizer.record(source)
|
| 127 |
+
try:
|
| 128 |
+
command = recognizer.recognize_google(audio_data)
|
| 129 |
+
response = process_command(command)
|
| 130 |
+
except sr.UnknownValueError:
|
| 131 |
+
response = "Sorry, I could not understand. Please try again."
|
| 132 |
|
| 133 |
return jsonify({"response": response})
|
| 134 |
|
|
|
|
| 143 |
def process_command(command):
|
| 144 |
global cart
|
| 145 |
command = command.lower()
|
| 146 |
+
menu_items = {
|
| 147 |
+
"biryani": ["chicken biryani", "veg biryani"],
|
| 148 |
+
"starters": ["chicken wings", "paneer tikka"],
|
| 149 |
+
"breads": ["naan", "roti"],
|
| 150 |
+
"curries": ["butter chicken", "dal fry"]
|
| 151 |
+
}
|
| 152 |
+
all_items = [item for sublist in menu_items.values() for item in sublist]
|
| 153 |
+
|
| 154 |
if "menu" in command:
|
| 155 |
+
menu = ", ".join([f"{category}: {', '.join(items)}" for category, items in menu_items.items()])
|
| 156 |
+
return f"Here is our menu: {menu}."
|
| 157 |
+
elif any(item in command for item in all_items):
|
| 158 |
+
item = next((item for item in all_items if item in command), None)
|
| 159 |
if item:
|
| 160 |
cart.append(item)
|
| 161 |
return f"{item.capitalize()} added to your cart. Your cart now has: {', '.join(cart)}."
|
| 162 |
+
elif "final order" in command or "submit" in command:
|
| 163 |
if cart:
|
| 164 |
+
return f"Your final order is: {', '.join(cart)}. Thank you for ordering!"
|
| 165 |
else:
|
| 166 |
return "Your cart is empty. Please add items to your cart first."
|
| 167 |
elif "no" in command or "nothing" in command:
|