AIVoice / app.py
DSatishchandra's picture
Update app.py
77c2b9f verified
raw
history blame
2.75 kB
import speech_recognition as sr
import pyttsx3
from transformers import pipeline
import random
# Initialize the speech engine
engine = pyttsx3.init()
# Menu data from the second image (hardcoded for simplicity)
menu = {
"Appetizer": ["Veg Samosas", "Cut Mirchi", "Onion", "Spinach", "Mixed Vegetable"],
"Pakodas": ["Veg Pakoda", "Chicken Pakoda", "Fish Pakoda"],
"Manchurian": ["Vegetable", "Paneer", "Chicken", "Fish", "Jhinga"],
"Chilly": ["Gobi", "Paneer", "Chicken", "Fish", "Shrimp"],
"Chef's Special": ["Murgh (Chicken)", "Gosht (Goat)", "Jhinga (Shrimp)", "Fish Fry"],
"Vegetarian Entree": ["Dal Fry", "Dal Makhani", "Channa Masala", "Aloo Gobi Masala", "Saag Paneer"],
"Chettinad": ["Egg", "Murgh (Chicken)", "Gosht (Goat)", "Jhinga (Shrimp)", "Crab"],
"Butter Masala": ["Chicken", "Shrimp", "Gosht (Goat)"]
}
# Initialize the speech recognition
recognizer = sr.Recognizer()
# Function to speak a text using text-to-speech
def speak(text):
engine.say(text)
engine.runAndWait()
# Function to listen to user's voice
def listen():
with sr.Microphone() as source:
print("Listening for your order...")
audio = recognizer.listen(source)
try:
# Using Google's speech recognition
return recognizer.recognize_google(audio)
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
# Function to process the order
def process_order(order):
response = "You have ordered the following: "
order = order.lower()
# Check for matching menu items
ordered_items = []
for category, items in menu.items():
for item in items:
if item.lower() in order:
ordered_items.append(item)
if ordered_items:
response += ', '.join(ordered_items) + ". Is that correct?"
speak(response)
confirmation = listen()
if confirmation and "yes" in confirmation.lower():
speak("Thank you for your order. It will be ready shortly!")
else:
speak("Please tell me again what you'd like to order.")
else:
speak("Sorry, I couldn't find any items matching your order. Can you try again?")
# Main function to start the assistant
def start_assistant():
speak("Welcome to the Voice Food Ordering Assistant!")
speak("What would you like to order today?")
while True:
order = listen()
if order:
process_order(order)
else:
speak("Sorry, I didn't catch that.")
# Run the assistant
if __name__ == "__main__":
start_assistant()