from flask import Flask, render_template_string, request, jsonify from datetime import datetime app = Flask(__name__) # Initialize an empty list to store orders orders = [] user_preferences = {"diet": "all"} # Default to all # HTML code for the frontend 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> body { font-family: Arial, sans-serif; background-color: #f4f4f9; display: flex; flex-direction: column; align-items: center; justify-content: center; height: 100vh; margin: 0; } h1 { color: #333; } .mic-button { width: 80px; height: 80px; border-radius: 50%; background-color: #007bff; color: white; font-size: 24px; border: none; display: flex; align-items: center; justify-content: center; cursor: pointer; box-shadow: 0px 4px 6px rgba(0, 0, 0, 0.1); transition: background-color 0.3s; } .mic-button:hover { background-color: #0056b3; } .status { margin-top: 20px; font-size: 18px; color: #666; } .listening { color: green; font-weight: bold; } .response { margin-top: 20px; padding: 10px; background-color: #fff; border: 1px solid #ddd; border-radius: 5px; box-shadow: 0px 4px 6px rgba(0, 0, 0, 0.1); width: 300px; text-align: center; } </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 listening...</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'); if (!window.MediaRecorder) { alert("Your browser does not support audio recording."); } let mediaRecorder; let audioChunks = []; micButton.addEventListener('click', async () => { navigator.mediaDevices.getUserMedia({ audio: true }) .then(stream => { mediaRecorder = new MediaRecorder(stream); mediaRecorder.start(); status.textContent = 'Listening...'; status.classList.add('listening'); mediaRecorder.ondataavailable = event => { audioChunks.push(event.data); }; mediaRecorder.onstop = async () => { const audioBlob = new Blob(audioChunks, { type: 'audio/wav' }); const formData = new FormData(); formData.append('audio', audioBlob); status.textContent = 'Processing...'; status.classList.remove('listening'); try { const result = await fetch('/process-audio', { method: 'POST', body: formData, }); const data = await result.json(); response.textContent = data.response; response.style.display = 'block'; status.textContent = 'Press the mic button to start listening...'; // Use browser text-to-speech const utterance = new SpeechSynthesisUtterance(data.response); speechSynthesis.speak(utterance); } catch (error) { response.textContent = 'Error occurred. Please try again.'; response.style.display = 'block'; status.textContent = 'Press the mic button to start listening...'; } }; setTimeout(() => { mediaRecorder.stop(); }, 5000); // Stop recording after 5 seconds }) .catch(err => { status.textContent = 'Microphone access denied.'; }); }); </script> </body> </html> """ @app.route('/') def index(): return render_template_string(html_code) @app.route('/process-audio', methods=['POST']) def process_audio(): try: audio_file = request.files['audio'] recognizer = sr.Recognizer() with sr.AudioFile(audio_file) as source: audio_data = recognizer.record(source) command = recognizer.recognize_google(audio_data) response = process_command(command) return jsonify({"response": response}) except Exception as e: return jsonify({"response": f"An error occurred: {str(e)}"}) def process_command(command): """Process the user's voice command and return a response.""" global orders command = command.lower() if "menu" in command: return "Our menu includes paneer butter masala, fried rice, and cold coffee." elif "order" in command: return "Your order has been placed." return "Sorry, I didn't understand your request." if __name__ == "__main__": app.run(debug=True, port=5000)