Spaces:
Sleeping
Sleeping
import gradio as gr | |
from gtts import gTTS | |
import tempfile | |
import os | |
import speech_recognition as sr | |
import threading | |
import time | |
# 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 | |
} | |
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 process_audio(audio_path): | |
global cart | |
recognizer = sr.Recognizer() | |
response = "" | |
try: | |
with sr.AudioFile(audio_path) as source: | |
audio = recognizer.record(source) | |
input_text = recognizer.recognize_google(audio) | |
print("User said:", 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 = sum(menu_items[cart_item] for cart_item in 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 = sum(menu_items[cart_item] for cart_item in cart) | |
response = "Your final order includes:\n" | |
for item in cart: | |
response += f"- {item}: ${menu_items[item]:.2f}\n" | |
response += f"\nTotal: ${total:.2f}. Thank 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(): | |
response = "Stopping voice assistant." | |
else: | |
response = "I didn’t quite catch that. Please tell me what you’d like to order." | |
except sr.UnknownValueError: | |
response = "Sorry, I didn’t catch that. Could you please repeat?" | |
except Exception as e: | |
response = f"An error occurred: {str(e)}" | |
audio_response_path = generate_voice_response(response) | |
os.system(f"start {audio_response_path}") # Automatically play the response audio | |
return response | |
def run_assistant_continuous(): | |
recognizer = sr.Recognizer() | |
mic = sr.Microphone() | |
with mic as source: | |
recognizer.adjust_for_ambient_noise(source) | |
print("Listening...") | |
while True: | |
try: | |
with mic as source: | |
audio = recognizer.listen(source) | |
input_text = recognizer.recognize_google(audio) | |
print("User said:", input_text) | |
audio_file = tempfile.NamedTemporaryFile(delete=False, suffix=".wav") | |
with open(audio_file.name, "wb") as f: | |
f.write(audio.get_wav_data()) | |
response = process_audio(audio_file.name) | |
print("Assistant response:", response) | |
except sr.UnknownValueError: | |
print("Sorry, I didn’t catch that. Please repeat.") | |
except KeyboardInterrupt: | |
print("Stopping voice assistant.") | |
break | |
except Exception as e: | |
print(f"An error occurred: {str(e)}") | |
if __name__ == "__main__": | |
run_assistant_continuous() | |