Update app.py
Browse files
app.py
CHANGED
@@ -1,17 +1,11 @@
|
|
1 |
from flask import Flask, render_template_string, request, jsonify
|
2 |
-
|
3 |
-
import speech_recognition as sr # Import speech recognition
|
4 |
from tempfile import NamedTemporaryFile
|
5 |
-
import ffmpeg
|
6 |
import os
|
|
|
7 |
|
8 |
app = Flask(__name__)
|
9 |
|
10 |
-
# Initialize an empty list to store orders
|
11 |
-
orders = []
|
12 |
-
user_preferences = {"diet": "all"} # Default to all
|
13 |
-
|
14 |
-
# HTML code for the frontend
|
15 |
html_code = """
|
16 |
<!DOCTYPE html>
|
17 |
<html lang="en">
|
@@ -33,24 +27,6 @@ html_code = """
|
|
33 |
h1 {
|
34 |
color: #333;
|
35 |
}
|
36 |
-
.mic-button {
|
37 |
-
width: 80px;
|
38 |
-
height: 80px;
|
39 |
-
border-radius: 50%;
|
40 |
-
background-color: #007bff;
|
41 |
-
color: white;
|
42 |
-
font-size: 24px;
|
43 |
-
border: none;
|
44 |
-
display: flex;
|
45 |
-
align-items: center;
|
46 |
-
justify-content: center;
|
47 |
-
cursor: pointer;
|
48 |
-
box-shadow: 0px 4px 6px rgba(0, 0, 0, 0.1);
|
49 |
-
transition: background-color 0.3s;
|
50 |
-
}
|
51 |
-
.mic-button:hover {
|
52 |
-
background-color: #0056b3;
|
53 |
-
}
|
54 |
.status {
|
55 |
margin-top: 20px;
|
56 |
font-size: 18px;
|
@@ -74,61 +50,60 @@ html_code = """
|
|
74 |
</head>
|
75 |
<body>
|
76 |
<h1>AI Dining Assistant</h1>
|
77 |
-
<
|
78 |
-
<div class="status" id="status">Press the mic button to start listening...</div>
|
79 |
<div class="response" id="response" style="display: none;">Response will appear here...</div>
|
80 |
<script>
|
81 |
-
const micButton = document.getElementById('mic-button');
|
82 |
const status = document.getElementById('status');
|
83 |
const response = document.getElementById('response');
|
84 |
-
if (!window.MediaRecorder) {
|
85 |
-
alert("Your browser does not support audio recording.");
|
86 |
-
}
|
87 |
let mediaRecorder;
|
88 |
let audioChunks = [];
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
};
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
} catch (error) {
|
119 |
-
response.textContent = 'Error occurred. Please try again.';
|
120 |
-
response.style.display = 'block';
|
121 |
-
status.textContent = 'Press the mic button to start listening...';
|
122 |
}
|
123 |
-
}
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
132 |
</script>
|
133 |
</body>
|
134 |
</html>
|
@@ -142,11 +117,10 @@ def index():
|
|
142 |
def process_audio():
|
143 |
try:
|
144 |
audio_file = request.files['audio']
|
145 |
-
# Save the uploaded file temporarily
|
146 |
temp_file = NamedTemporaryFile(delete=False, suffix=".wav")
|
147 |
audio_file.save(temp_file.name)
|
148 |
-
|
149 |
-
# Convert
|
150 |
converted_file = NamedTemporaryFile(delete=False, suffix=".wav")
|
151 |
ffmpeg.input(temp_file.name).output(converted_file.name, acodec='pcm_s16le', ac=1, ar='16000').run(overwrite_output=True)
|
152 |
|
@@ -168,10 +142,12 @@ def process_command(command):
|
|
168 |
global orders
|
169 |
command = command.lower()
|
170 |
if "menu" in command:
|
171 |
-
return "Our menu includes paneer butter masala, fried rice, and cold coffee."
|
172 |
elif "order" in command:
|
173 |
-
return "Your order has been placed."
|
174 |
-
|
|
|
|
|
175 |
|
176 |
if __name__ == "__main__":
|
177 |
app.run(host="0.0.0.0", port=7860)
|
|
|
1 |
from flask import Flask, render_template_string, request, jsonify
|
2 |
+
import speech_recognition as sr
|
|
|
3 |
from tempfile import NamedTemporaryFile
|
|
|
4 |
import os
|
5 |
+
import ffmpeg
|
6 |
|
7 |
app = Flask(__name__)
|
8 |
|
|
|
|
|
|
|
|
|
|
|
9 |
html_code = """
|
10 |
<!DOCTYPE html>
|
11 |
<html lang="en">
|
|
|
27 |
h1 {
|
28 |
color: #333;
|
29 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
30 |
.status {
|
31 |
margin-top: 20px;
|
32 |
font-size: 18px;
|
|
|
50 |
</head>
|
51 |
<body>
|
52 |
<h1>AI Dining Assistant</h1>
|
53 |
+
<div class="status" id="status">Welcome to AI Dining Assistant! How can I help you today?</div>
|
|
|
54 |
<div class="response" id="response" style="display: none;">Response will appear here...</div>
|
55 |
<script>
|
|
|
56 |
const status = document.getElementById('status');
|
57 |
const response = document.getElementById('response');
|
|
|
|
|
|
|
58 |
let mediaRecorder;
|
59 |
let audioChunks = [];
|
60 |
+
|
61 |
+
function startListening() {
|
62 |
+
navigator.mediaDevices.getUserMedia({ audio: true }).then(stream => {
|
63 |
+
mediaRecorder = new MediaRecorder(stream);
|
64 |
+
mediaRecorder.start();
|
65 |
+
status.textContent = 'Listening...';
|
66 |
+
status.classList.add('listening');
|
67 |
+
audioChunks = [];
|
68 |
+
mediaRecorder.ondataavailable = event => audioChunks.push(event.data);
|
69 |
+
mediaRecorder.onstop = async () => {
|
70 |
+
const audioBlob = new Blob(audioChunks, { type: 'audio/wav; codecs=LINEAR16' });
|
71 |
+
const formData = new FormData();
|
72 |
+
formData.append('audio', audioBlob);
|
73 |
+
|
74 |
+
status.textContent = 'Processing...';
|
75 |
+
status.classList.remove('listening');
|
76 |
+
try {
|
77 |
+
const result = await fetch('/process-audio', { method: 'POST', body: formData });
|
78 |
+
const data = await result.json();
|
79 |
+
response.textContent = data.response;
|
80 |
+
response.style.display = 'block';
|
81 |
+
status.textContent = 'Listening...';
|
82 |
+
// Speak the response
|
83 |
+
const utterance = new SpeechSynthesisUtterance(data.response);
|
84 |
+
speechSynthesis.speak(utterance);
|
85 |
+
if (data.response.includes("Goodbye")) {
|
86 |
+
status.textContent = 'Conversation ended.';
|
87 |
+
} else {
|
88 |
+
setTimeout(startListening, 1000); // Restart listening after response
|
|
|
|
|
|
|
|
|
89 |
}
|
90 |
+
} catch (error) {
|
91 |
+
response.textContent = 'Error occurred. Please try again.';
|
92 |
+
response.style.display = 'block';
|
93 |
+
status.textContent = 'Listening...';
|
94 |
+
setTimeout(startListening, 1000); // Restart listening
|
95 |
+
}
|
96 |
+
};
|
97 |
+
setTimeout(() => mediaRecorder.stop(), 5000); // Stop recording after 5 seconds
|
98 |
+
}).catch(() => status.textContent = 'Microphone access denied.');
|
99 |
+
}
|
100 |
+
|
101 |
+
// Start listening automatically on page load
|
102 |
+
window.onload = () => {
|
103 |
+
const utterance = new SpeechSynthesisUtterance('Welcome to AI Dining Assistant! How can I help you today?');
|
104 |
+
speechSynthesis.speak(utterance);
|
105 |
+
setTimeout(startListening, 3000);
|
106 |
+
};
|
107 |
</script>
|
108 |
</body>
|
109 |
</html>
|
|
|
117 |
def process_audio():
|
118 |
try:
|
119 |
audio_file = request.files['audio']
|
|
|
120 |
temp_file = NamedTemporaryFile(delete=False, suffix=".wav")
|
121 |
audio_file.save(temp_file.name)
|
122 |
+
|
123 |
+
# Convert audio to PCM WAV format
|
124 |
converted_file = NamedTemporaryFile(delete=False, suffix=".wav")
|
125 |
ffmpeg.input(temp_file.name).output(converted_file.name, acodec='pcm_s16le', ac=1, ar='16000').run(overwrite_output=True)
|
126 |
|
|
|
142 |
global orders
|
143 |
command = command.lower()
|
144 |
if "menu" in command:
|
145 |
+
return "Our menu includes paneer butter masala, fried rice, and cold coffee. What would you like to order?"
|
146 |
elif "order" in command:
|
147 |
+
return "Your order has been placed. Would you like anything else?"
|
148 |
+
elif "no" in command or "nothing" in command:
|
149 |
+
return "Goodbye! Thank you for using AI Dining Assistant."
|
150 |
+
return "Sorry, I didn't understand your request. Please ask about the menu or place an order."
|
151 |
|
152 |
if __name__ == "__main__":
|
153 |
app.run(host="0.0.0.0", port=7860)
|