import gradio as gr from gtts import gTTS import os import tempfile import json import sounddevice as sd import queue import threading from scipy.io.wavfile import write import speech_recognition as sr import subprocess # Store cart in a temporary storage cart = [] # Define the menu items dynamically menu_items = { "Pizza": 10.99, "Burger": 8.49, "Pasta": 12.99, "Salad": 7.99, "Soda": 2.49 } # To manage playback control playback_control = {"stop": False, "pause": False} def generate_voice_response(text): tts = gTTS(text) temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") temp_file.close() tts.save(temp_file.name) return temp_file.name def play_audio(audio_path): try: subprocess.run(["ffplay", "-nodisp", "-autoexit", audio_path], check=True) except Exception as e: print("Error playing audio:", e) def calculate_total(cart): return sum(menu_items[item] for item in cart) # Audio recording setup q = queue.Queue() def audio_callback(indata, frames, time, status): if status: print(status) q.put(indata.copy()) def record_audio(): fs = 16000 # Sample rate seconds = 5 # Duration print("Recording...") audio_data = [] with sd.InputStream(callback=audio_callback, samplerate=fs, channels=1): for _ in range(int(fs * seconds / 1024)): audio_data.append(q.get()) print("Recording finished.") return b"".join(audio_data) def restaurant_voice_assistant(): global cart recognizer = sr.Recognizer() # Continuously listen for user commands while True: try: audio_data = record_audio() with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as temp_audio: temp_audio.write(audio_data) temp_audio_path = temp_audio.name with sr.AudioFile(temp_audio_path) as source: audio = recognizer.record(source) input_text = recognizer.recognize_google(audio) print("You said:", input_text) # Process input text if "menu" in input_text.lower(): response = "Here is our menu:\n" for item, price in menu_items.items(): response += f"{item}: ${price:.2f}\n" response += "\nWhat would you like to add to your cart?" elif any(item.lower() in input_text.lower() for item in menu_items): for item in menu_items: if item.lower() in input_text.lower(): cart.append(item) total = calculate_total(cart) response = f"{item} has been added to your cart. Your current cart includes:\n" for cart_item in cart: response += f"- {cart_item}: ${menu_items[cart_item]:.2f}\n" response += f"\nTotal: ${total:.2f}. Would you like to add anything else?" break elif "final order" in input_text.lower() or "submit order" in input_text.lower(): if cart: total = calculate_total(cart) response = "Your final order includes:\n" for item in cart: response += f"- {item}: ${menu_items[item]:.2f}\n" response += f"\nTotal: ${total:.2f}.\nThank you for ordering!" cart = [] # Clear cart after finalizing order else: response = "Your cart is empty. Would you like to order something?" elif "stop" in input_text.lower(): playback_control["stop"] = True response = "Audio playback stopped." elif "pause" in input_text.lower(): playback_control["pause"] = True response = "Audio playback paused." elif "resume" in input_text.lower(): playback_control["pause"] = False response = "Resuming audio playback." else: response = "I didn’t quite catch that. Please tell me what you’d like to order." # Generate and play audio response audio_path = generate_voice_response(response) play_audio(audio_path) except sr.UnknownValueError: print("Sorry, I didn’t catch that. Could you please repeat?") except Exception as e: print("Error occurred:", e) # Run the assistant threading.Thread(target=restaurant_voice_assistant).start()