Update app.py
Browse files
app.py
CHANGED
|
@@ -3,9 +3,16 @@ 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">
|
|
@@ -60,14 +67,14 @@ html_code = """
|
|
| 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/
|
| 71 |
const formData = new FormData();
|
| 72 |
formData.append('audio', audioBlob);
|
| 73 |
|
|
@@ -116,30 +123,60 @@ def index():
|
|
| 116 |
@app.route('/process-audio', methods=['POST'])
|
| 117 |
def process_audio():
|
| 118 |
try:
|
| 119 |
-
|
| 120 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 121 |
audio_file.save(temp_file.name)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 122 |
|
| 123 |
# Convert audio to PCM WAV format
|
| 124 |
converted_file = NamedTemporaryFile(delete=False, suffix=".wav")
|
| 125 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 126 |
|
|
|
|
|
|
|
|
|
|
| 127 |
recognizer = sr.Recognizer()
|
| 128 |
with sr.AudioFile(converted_file.name) as source:
|
| 129 |
audio_data = recognizer.record(source)
|
| 130 |
command = recognizer.recognize_google(audio_data)
|
|
|
|
| 131 |
response = process_command(command)
|
| 132 |
|
| 133 |
-
# Clean up temporary files
|
| 134 |
-
os.unlink(temp_file.name)
|
| 135 |
-
os.unlink(converted_file.name)
|
| 136 |
return jsonify({"response": response})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 137 |
except Exception as e:
|
|
|
|
| 138 |
return jsonify({"response": f"An error occurred: {str(e)}"})
|
| 139 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 140 |
def process_command(command):
|
| 141 |
"""Process the user's voice command and return a response."""
|
| 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?"
|
|
|
|
| 3 |
from tempfile import NamedTemporaryFile
|
| 4 |
import os
|
| 5 |
import ffmpeg
|
| 6 |
+
import logging
|
| 7 |
+
from werkzeug.exceptions import BadRequest
|
| 8 |
|
| 9 |
+
# Initialize Flask App
|
| 10 |
app = Flask(__name__)
|
| 11 |
|
| 12 |
+
# Set up logging
|
| 13 |
+
logging.basicConfig(level=logging.INFO)
|
| 14 |
+
|
| 15 |
+
# HTML Template for Frontend
|
| 16 |
html_code = """
|
| 17 |
<!DOCTYPE html>
|
| 18 |
<html lang="en">
|
|
|
|
| 67 |
|
| 68 |
function startListening() {
|
| 69 |
navigator.mediaDevices.getUserMedia({ audio: true }).then(stream => {
|
| 70 |
+
mediaRecorder = new MediaRecorder(stream, { mimeType: 'audio/webm;codecs=opus' });
|
| 71 |
mediaRecorder.start();
|
| 72 |
status.textContent = 'Listening...';
|
| 73 |
status.classList.add('listening');
|
| 74 |
audioChunks = [];
|
| 75 |
mediaRecorder.ondataavailable = event => audioChunks.push(event.data);
|
| 76 |
mediaRecorder.onstop = async () => {
|
| 77 |
+
const audioBlob = new Blob(audioChunks, { type: 'audio/webm' });
|
| 78 |
const formData = new FormData();
|
| 79 |
formData.append('audio', audioBlob);
|
| 80 |
|
|
|
|
| 123 |
@app.route('/process-audio', methods=['POST'])
|
| 124 |
def process_audio():
|
| 125 |
try:
|
| 126 |
+
# Validate audio file
|
| 127 |
+
audio_file = request.files.get('audio')
|
| 128 |
+
if not audio_file:
|
| 129 |
+
raise BadRequest("No audio file provided.")
|
| 130 |
+
|
| 131 |
+
temp_file = NamedTemporaryFile(delete=False, suffix=".webm")
|
| 132 |
audio_file.save(temp_file.name)
|
| 133 |
+
logging.info(f"Saved input audio to {temp_file.name}")
|
| 134 |
+
|
| 135 |
+
if os.path.getsize(temp_file.name) == 0:
|
| 136 |
+
raise BadRequest("Uploaded audio file is empty.")
|
| 137 |
|
| 138 |
# Convert audio to PCM WAV format
|
| 139 |
converted_file = NamedTemporaryFile(delete=False, suffix=".wav")
|
| 140 |
+
try:
|
| 141 |
+
ffmpeg.input(temp_file.name).output(
|
| 142 |
+
converted_file.name, acodec='pcm_s16le', ac=1, ar='16000'
|
| 143 |
+
).run(overwrite_output=True)
|
| 144 |
+
except Exception as ffmpeg_error:
|
| 145 |
+
logging.error(f"FFmpeg conversion error: {str(ffmpeg_error)}")
|
| 146 |
+
return jsonify({"response": "Audio conversion failed. Please try again."})
|
| 147 |
|
| 148 |
+
logging.info(f"Converted audio saved to {converted_file.name}")
|
| 149 |
+
|
| 150 |
+
# Recognize speech
|
| 151 |
recognizer = sr.Recognizer()
|
| 152 |
with sr.AudioFile(converted_file.name) as source:
|
| 153 |
audio_data = recognizer.record(source)
|
| 154 |
command = recognizer.recognize_google(audio_data)
|
| 155 |
+
logging.info(f"Recognized command: {command}")
|
| 156 |
response = process_command(command)
|
| 157 |
|
|
|
|
|
|
|
|
|
|
| 158 |
return jsonify({"response": response})
|
| 159 |
+
|
| 160 |
+
except BadRequest as br:
|
| 161 |
+
logging.error(f"Bad request error: {br}")
|
| 162 |
+
return jsonify({"response": f"Bad Request: {str(br)}"})
|
| 163 |
+
|
| 164 |
except Exception as e:
|
| 165 |
+
logging.error(f"Error processing audio: {e}")
|
| 166 |
return jsonify({"response": f"An error occurred: {str(e)}"})
|
| 167 |
|
| 168 |
+
finally:
|
| 169 |
+
# Clean up temporary files
|
| 170 |
+
try:
|
| 171 |
+
if os.path.exists(temp_file.name):
|
| 172 |
+
os.unlink(temp_file.name)
|
| 173 |
+
if os.path.exists(converted_file.name):
|
| 174 |
+
os.unlink(converted_file.name)
|
| 175 |
+
except Exception as cleanup_error:
|
| 176 |
+
logging.error(f"Error cleaning up files: {cleanup_error}")
|
| 177 |
+
|
| 178 |
def process_command(command):
|
| 179 |
"""Process the user's voice command and return a response."""
|
|
|
|
| 180 |
command = command.lower()
|
| 181 |
if "menu" in command:
|
| 182 |
return "Our menu includes paneer butter masala, fried rice, and cold coffee. What would you like to order?"
|