Spaces:
Build error
Build error
import speech_recognition as sr | |
from gtts import gTTS | |
import os | |
import time | |
# Function to speak using Google Text-to-Speech | |
def speak(text): | |
tts = gTTS(text=text, lang='en') | |
tts.save("output.mp3") | |
os.system("start output.mp3") # For Windows | |
# os.system("mpg321 output.mp3") # Uncomment for Linux | |
# Function to listen and recognize speech using Google Speech Recognition | |
def listen(): | |
recognizer = sr.Recognizer() | |
with sr.Microphone() as source: | |
print("Listening for your order...") | |
audio = recognizer.listen(source) | |
try: | |
# Using Google Speech Recognition to convert speech to text | |
order = recognizer.recognize_google(audio) | |
print(f"Recognized Order: {order}") | |
return order | |
except sr.UnknownValueError: | |
speak("Sorry, I could not understand that. Could you please repeat?") | |
return None | |
except sr.RequestError: | |
speak("Sorry, there was an issue with the service.") | |
return None | |
# Sample menu data | |
menu = ["Veg Samosas", "Chicken Pakoda", "Fish Fry", "Dal Fry", "Paneer Butter Masala"] | |
# Function to process the order | |
def process_order(order): | |
response = "You have ordered the following items: " | |
order = order.lower() | |
ordered_items = [] | |
for item in menu: | |
if item.lower() in order: | |
ordered_items.append(item) | |
if ordered_items: | |
response += ", ".join(ordered_items) + ". Is that correct?" | |
speak(response) | |
time.sleep(2) | |
return response | |
else: | |
speak("Sorry, I couldn't find any items matching your order. Can you try again?") | |
return "Sorry, I couldn't find any items matching your order." | |
# Main function | |
def start_assistant(): | |
speak("Welcome to the Voice Food Ordering Assistant!") | |
time.sleep(1) | |
speak("Please tell me what you would like to order.") | |
while True: | |
order = listen() | |
if order: | |
process_order(order) | |
if __name__ == "__main__": | |
start_assistant() | |