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 # Initialize Flask App app = Flask(__name__) # Set up logging logging.basicConfig(level=logging.INFO) # Initialize conversation state user_order = [] # Stores the current order # Define food items (menu) with only 10 items menu_items = { 'Veg': ["Vegetable Biryani"], # 1 Veg item 'Non-Veg': ["Butter Chicken"], # 1 Non-Veg item 'Both': ["Paneer Butter Masala", "Chicken Biryani"], # 1 Veg + 1 Non-Veg item 'Drinks': ["Lassi", "Milkshake"], # 2 Drinks 'Desserts': ["Gulab Jamun", "Ice Cream"] # 2 Desserts } # HTML Template for Frontend html_code = """ AI Dining Assistant

AI Dining Assistant

Press the mic button to start the conversation...
""" @app.route('/') def index(): return render_template_string(html_code) @app.route('/process-audio', methods=['POST']) def process_audio(): global user_order try: # Validate audio file 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) logging.info(f"Saved input audio to {temp_file.name}") if os.path.getsize(temp_file.name) == 0: raise BadRequest("Uploaded audio file is empty.") # Convert audio to PCM WAV format (16kHz, mono) converted_file = NamedTemporaryFile(delete=False, suffix=".wav") try: ffmpeg.input(temp_file.name).output( converted_file.name, acodec='pcm_s16le', ac=1, ar='16000' ).run(overwrite_output=True) except Exception as ffmpeg_error: logging.error(f"FFmpeg conversion error: {str(ffmpeg_error)}") return jsonify({"response": "Audio conversion failed. Please try again."}) logging.info(f"Converted audio saved to {converted_file.name}") # Recognize speech recognizer = sr.Recognizer() with sr.AudioFile(converted_file.name) as source: audio_data = recognizer.record(source) try: command = recognizer.recognize_google(audio_data) logging.info(f"Recognized command: {command}") response = process_command(command) except sr.RequestError as e: logging.error(f"Error with Google Speech Recognition service: {e}") response = "Sorry, there was an issue with the speech recognition service." return jsonify({"response": response}) except BadRequest as br: logging.error(f"Bad request error: {br}") return jsonify({"response": f"Bad Request: {str(br)}"}) except Exception as e: logging.error(f"Error processing audio: {e}") return jsonify({"response": f"An error occurred: {str(e)}"}) finally: # Clean up temporary files try: if os.path.exists(temp_file.name): os.unlink(temp_file.name) if os.path.exists(converted_file.name): os.unlink(converted_file.name) except Exception as cleanup_error: logging.error(f"Error cleaning up files: {cleanup_error}") def process_command(command): global user_order # Normalize the command to lowercase command = command.lower() # Show the menu without categorizing the items if "menu" in command or "what’s the menu" in command or "Show me the menu" in command: menu_response = ( "Here are the available food items: " "Vegetable Biryani, Butter Chicken, Paneer Butter Masala, Chicken Biryani, " "Lassi, Milkshake, Gulab Jamun, Ice Cream. " "Please let me know what you'd like to add to your order." ) return menu_response # Add item to order elif "add" in command: item_to_add = command.split("add")[-1].strip() # Check if the item is in the menu if item_to_add in menu_items["Veg"] or item_to_add in menu_items["Non-Veg"] or item_to_add in menu_items["Both"] or item_to_add in menu_items["Drinks"] or item_to_add in menu_items["Desserts"]: user_order.append(item_to_add) return f"{item_to_add} has been added to your order. Would you like to add more items?" else: return f"Sorry, {item_to_add} is not available in the menu. Please choose from the available items." # Show current order elif "show my order" in command or "what's my order" in command or "what's in my order" in command: if user_order: return "Your current order includes: " + ", ".join(user_order) else: return "You haven't added anything to your order yet." # Place order (Ask for confirmation) elif "place order" in command or "confirm order" in command: if user_order: return f"You have the following items in your order: {', '.join(user_order)}. Would you like to confirm? (Say 'yes' to confirm, 'no' to cancel)" else: return "You haven't added anything to your order yet. Please add some items first." # Final confirmation (user says yes to confirm the order) elif "yes" in command: if user_order: return "Your order has been confirmed and sent to the kitchen. Thank you for ordering!" else: return "Please add some items to your order before confirming." # Handle unrecognized commands return ( "Sorry, I didn’t understand your request. You can say things like:\n" "- Show me the menu\n" "- Add [item] to my order\n" "- Show my order\n" "- Place the order" ) if __name__ == "__main__": app.run(host="0.0.0.0", port=7860)