from flask import Flask, render_template_string, request, jsonify import speech_recognition as sr from tempfile import NamedTemporaryFile import os import ffmpeg from fuzzywuzzy import process, fuzz from metaphone import doublemetaphone import logging app = Flask(__name__) logging.basicConfig(level=logging.INFO) # Global variables cart = [] # Stores items as [item_name, price, quantity] in the cart menu_preferences = None # Tracks the current menu preference section_preferences = None # Tracks the current section preference default_menu_preferences = "all" # To reset menu preferences default_sections = { "biryanis": ["veg biryani", "paneer biryani", "chicken biryani", "mutton biryani"], "starters": ["samosa", "onion pakoda", "chilli gobi", "chicken manchurian", "veg manchurian"], "curries": ["paneer butter", "chicken curry", "fish curry", "chilli chicken"], "desserts": ["gulab jamun", "ice cream"], "soft drinks": ["cola", "lemon soda"] } prices = { "samosa": 9, "onion pakoda": 10, "chilli gobi": 12, "chicken biryani": 14, "mutton biryani": 16, "veg biryani": 12, "paneer butter": 10, "fish curry": 12, "chicken manchurian": 14, "veg manchurian": 12, "chilli chicken": 14, "paneer biryani": 13, "chicken curry": 14, "gulab jamun": 8, "ice cream": 6, "cola": 5, "lemon soda": 6 } menus = { "all": list(prices.keys()), "vegetarian": [ "samosa", "onion pakoda", "chilli gobi", "veg biryani", "paneer butter", "veg manchurian", "paneer biryani", "gulab jamun", "ice cream", "cola", "lemon soda" ], "non-vegetarian": [ "chicken biryani", "mutton biryani", "fish curry", "chicken manchurian", "chilli chicken", "chicken curry", "gulab jamun", "ice cream", "cola", "lemon soda" ] } @app.route("/") def index(): return render_template_string(html_code) @app.route("/reset-cart", methods=["GET"]) def reset_cart(): global cart, menu_preferences, section_preferences cart = [] menu_preferences = None section_preferences = None return "Cart reset successfully." @app.route("/process-audio", methods=["POST"]) def process_audio(): try: # Handle audio input audio_file = request.files.get("audio") if not audio_file: return jsonify({"response": "No audio file provided."}), 400 # Save audio file and convert to WAV format temp_file = NamedTemporaryFile(delete=False, suffix=".webm") audio_file.save(temp_file.name) 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) # Recognize speech recognizer = sr.Recognizer() recognizer.dynamic_energy_threshold = True recognizer.energy_threshold = 60 with sr.AudioFile(converted_file.name) as source: recognizer.adjust_for_ambient_noise(source, duration=1) audio_data = recognizer.record(source) raw_command = recognizer.recognize_google(audio_data).lower() logging.info(f"Raw recognized command: {raw_command}") # Preprocess the command command = preprocess_command(raw_command) # Process the command and get a response response = process_command(command) except sr.UnknownValueError: response = "Sorry, I couldn't understand. Please try again." except Exception as e: response = f"An error occurred: {str(e)}" finally: os.unlink(temp_file.name) os.unlink(converted_file.name) return jsonify({"response": response}) def preprocess_command(command): """ Normalize the user command to improve matching. """ command = command.strip().lower() return command def process_command(command): global cart, menu_preferences, section_preferences # Log command and current preferences for debugging logging.info(f"Command: {command}, Menu Preferences: {menu_preferences}, Section Preferences: {section_preferences}") # Handle menu preferences if menu_preferences is None: preferences = ["non-vegetarian", "vegetarian", "all"] closest_match = process.extractOne(command, preferences, scorer=fuzz.partial_ratio) if closest_match and closest_match[1] > 60: menu_preferences = closest_match[0] if menu_preferences == "non-vegetarian": return "You have chosen the Non-Vegetarian menu. Which section would you like? (biryanis, starters, curries, desserts, soft drinks)" elif menu_preferences == "vegetarian": return "You have chosen the Vegetarian menu. Which section would you like? (biryanis, starters, curries, desserts, soft drinks)" elif menu_preferences == "all": return "You have chosen the complete menu. Which section would you like? (biryanis, starters, curries, desserts, soft drinks)" return "Please specify your preference: Non-Vegetarian, Vegetarian, or All." # Handle section preferences if section_preferences is None or any(section in command for section in default_sections.keys()): for section in default_sections.keys(): if section in command: section_preferences = section available_items = [item for item in default_sections[section] if item in menus[menu_preferences]] return f"Here are the items in {section}: {', '.join(available_items)}. Say the item name to add to the cart." return "Please specify a section: biryanis, starters, curries, desserts, or soft drinks." # Handle item addition available_items = [item for item in default_sections[section_preferences] if item in menus[menu_preferences]] for item in available_items: if item in command: quantity = extract_quantity(command) if quantity is not None: for cart_item in cart: if cart_item[0] == item: cart_item[2] += quantity break else: cart.append([item, prices[item], quantity]) cart_summary = ", ".join([f"{i[0]} x{i[2]} (${i[1] * i[2]})" for i in cart]) return f"Added {quantity} x {item} to your cart. Current cart: {cart_summary}. Would you like to choose another section or finalize your order?" return "Please specify a quantity between 1 and 10. Say 'Add 2' or 'Add three' followed by the item name." # Handle remove command if "remove" in command: for item in cart: if item[0] in command: cart.remove(item) cart_summary = ", ".join([f"{i[0]} x{i[2]} (${i[1] * i[2]})" for i in cart]) return f"Removed {item[0]} from your cart. Current cart: {cart_summary}." return "The item you are trying to remove is not in your cart." # Handle final order if "final order" in command: if cart: total = sum(i[1] * i[2] for i in cart) order_summary = ", ".join([f"{i[0]} x{i[2]} (${i[1] * i[2]})" for i in cart]) cart.clear() menu_preferences = None section_preferences = None return f"Your final order is: {order_summary}. Total bill: ${total}. Thank you for ordering! You can start a new order by specifying your preference." return "Your cart is empty. Please add items to your cart first." return "Sorry, I couldn't understand that. Please try again." def extract_quantity(command): """ Extract quantity from the user command. """ # Map numeric words to digits number_words = { "one": 1, "two": 2, "three": 3, "four": 4, "five": 5, "six": 6, "seven": 7, "eight": 8, "nine": 9, "ten": 10, "1": 1, "2": 2, "3": 3, "4": 4, "5": 5, "6": 6, "7": 7, "8": 8, "9": 9, "10": 10 } # Normalize command and split into words command_words = command.split() # Check for numeric words or digits in the command for word in command_words: if word in number_words: return number_words[word] return None html_code = """ AI Dining Assistant

AI Dining Assistant

Press the mic button to start...
Response will appear here...
""" if __name__ == "__main__": app.run(host="0.0.0.0", port=7860)