AIVoice5 / app.py
nagasurendra's picture
Update app.py
5fe7cf4 verified
from flask import Flask, render_template_string, request, jsonify
import speech_recognition as sr
from tempfile import NamedTemporaryFile
import os
import ffmpeg
import logging
from werkzeug.exceptions import BadRequest
app = Flask(__name__)
logging.basicConfig(level=logging.INFO)
# Global cart to store items, reset dynamically for new sessions
cart = []
html_code = """
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>AI Dining Assistant</title>
<style>
/* Your CSS styles here */
</style>
</head>
<body>
<h1>AI Dining Assistant</h1>
<button class="mic-button" id="mic-button">🎤</button>
<div class="status" id="status">Press the mic button to start...</div>
<div class="response" id="response" style="display: none;">Response will appear here...</div>
<script>
const micButton = document.getElementById('mic-button');
const status = document.getElementById('status');
const response = document.getElementById('response');
let mediaRecorder;
let audioChunks = [];
let isConversationActive = false;
micButton.addEventListener('click', () => {
if (!isConversationActive) {
isConversationActive = true;
startConversation();
}
});
function startConversation() {
const utterance = new SpeechSynthesisUtterance('Please give your order.');
speechSynthesis.speak(utterance);
utterance.onend = () => {
status.textContent = 'Listening...';
startListening();
};
}
function startListening() {
navigator.mediaDevices.getUserMedia({ audio: true }).then(stream => {
mediaRecorder = new MediaRecorder(stream, { mimeType: 'audio/webm;codecs=opus' });
mediaRecorder.start();
audioChunks = [];
mediaRecorder.ondataavailable = event => audioChunks.push(event.data);
mediaRecorder.onstop = async () => {
const audioBlob = new Blob(audioChunks, { type: 'audio/webm' });
const formData = new FormData();
formData.append('audio', audioBlob);
status.textContent = 'Processing...';
try {
const result = await fetch('/process-audio', { method: 'POST', body: formData });
const data = await result.json();
response.textContent = data.response;
response.style.display = 'block';
const utterance = new SpeechSynthesisUtterance(data.response);
speechSynthesis.speak(utterance);
utterance.onend = () => {
console.log("Speech synthesis completed.");
if (data.response.includes("Goodbye")) {
status.textContent = 'Thank you for ordering. Have a great day!';
isConversationActive = false;
fetch('/reset-cart'); // Reset the cart dynamically on end
} else {
status.textContent = 'Listening...';
setTimeout(() => {
startConversation();
}, 2000);
}
};
utterance.onerror = (e) => {
console.error("Speech synthesis error:", e.error);
status.textContent = 'Error with speech output.';
isConversationActive = false;
};
} catch (error) {
response.textContent = 'Sorry, I could not understand. Please try again.';
response.style.display = 'block';
status.textContent = 'Press the mic button to restart the conversation.';
isConversationActive = false;
}
};
setTimeout(() => mediaRecorder.stop(), 5000);
}).catch(() => {
status.textContent = 'Microphone access denied.';
isConversationActive = false;
});
}
</script>
</body>
</html>
"""
@app.route('/')
def index():
return render_template_string(html_code)
@app.route('/reset-cart', methods=['GET'])
def reset_cart():
global cart
cart = []
return "Cart reset successfully."
@app.route('/process-audio', methods=['POST'])
def process_audio():
try:
audio_file = request.files.get('audio')
if not audio_file:
raise BadRequest("No audio file provided.")
temp_file = NamedTemporaryFile(delete=False, suffix=".webm")
audio_file.save(temp_file.name)
if os.path.getsize(temp_file.name) == 0:
raise BadRequest("Uploaded audio file is empty.")
converted_file = NamedTemporaryFile(delete=False, suffix=".wav")
ffmpeg.input(temp_file.name).output(
converted_file.name, acodec='pcm_s16le', ac=1, ar='16000'
).run(overwrite_output=True)
recognizer = sr.Recognizer()
recognizer.energy_threshold = 300 # Adjust for low-volume recognition
with sr.AudioFile(converted_file.name) as source:
audio_data = recognizer.record(source)
try:
command = recognizer.recognize_google(audio_data)
response = process_command(command)
except sr.UnknownValueError:
response = "Sorry, I could not understand. Please try again."
return jsonify({"response": response})
except BadRequest as br:
return jsonify({"response": f"Bad Request: {str(br)}"}), 400
except Exception as e:
return jsonify({"response": f"An error occurred: {str(e)}"}), 500
finally:
os.unlink(temp_file.name)
os.unlink(converted_file.name)
def process_command(command):
global cart
command = command.lower()
menu_items = {
"biryani": ["chicken biryani", "veg biryani"],
"starters": ["chicken wings", "paneer tikka"],
"breads": ["butter naan", "roti"],
"curries": ["butter chicken", "dal fry"]
}
all_items = [item for sublist in menu_items.values() for item in sublist]
if "menu" in command:
menu = ", ".join([f"{category}: {', '.join(items)}" for category, items in menu_items.items()])
return f"Here is our menu: {menu}."
elif any(item in command for item in all_items):
item = next((item for item in all_items if item in command), None)
if item:
cart.append(item)
return f"{item.capitalize()} added to your cart. Your cart now has: {', '.join(cart)}."
elif "final order" in command or "submit" in command:
if cart:
return f"Your final order is: {', '.join(cart)}. Thank you for ordering!"
else:
return "Your cart is empty. Please add items to your cart first."
elif "no" in command or "nothing" in command or "goodbye" in command:
return "Goodbye! Thank you for using AI Dining Assistant."
return "Sorry, I didn't understand your request."
if __name__ == "__main__":
app.run(host="0.0.0.0", port=7860)