AIVoice / order_assistant.py
DSatishchandra's picture
Create order_assistant.py
397d198 verified
raw
history blame
2.34 kB
import gradio as gr
import speech_recognition as sr
import edge_tts
import asyncio
import random
import tempfile
# Example food menu
food_menu = {
"Starters": ["Spring Rolls", "Samosa", "Garlic Bread"],
"Main Course": ["Biryani", "Pizza", "Pasta", "Butter Chicken"],
"Desserts": ["Ice Cream", "Gulab Jamun", "Chocolate Cake"]
}
# Function to simulate food suggestions based on user input
def get_food_suggestion(food_type):
if food_type in food_menu:
return random.choice(food_menu[food_type])
return "No suggestion available"
# Speech-to-Text function
def recognize_speech_from_mic():
recognizer = sr.Recognizer()
with sr.Microphone() as source:
print("Say something!")
audio = recognizer.listen(source)
try:
return recognizer.recognize_google(audio)
except sr.UnknownValueError:
return "Sorry I didn't catch that"
except sr.RequestError:
return "Sorry, I'm having trouble reaching the service"
# Text-to-Speech function
async def text_to_speech(text):
communicate = edge_tts.Communicate(text, "en-US-AriaNeural", rate="0%", pitch="0Hz")
with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as tmp_file:
tmp_path = tmp_file.name
await communicate.save(tmp_path)
return tmp_path
# AI Food Ordering Assistant Function
async def food_order_assistant():
# Greeting
greeting_text = "Welcome to the restaurant! Please tell me your food preference."
audio_path = await text_to_speech(greeting_text)
# Wait for user to input food type
user_food_type = recognize_speech_from_mic()
suggestion = get_food_suggestion(user_food_type)
suggestion_text = f"I suggest you try {suggestion}."
suggestion_audio = await text_to_speech(suggestion_text)
# Wait for confirmation
confirmation = recognize_speech_from_mic()
if "yes" in confirmation.lower():
confirmation_text = f"Your order for {suggestion} has been confirmed."
confirmation_audio = await text_to_speech(confirmation_text)
return confirmation_audio, f"Confirmed order: {suggestion}"
else:
cancellation_text = "Okay, let's try again."
cancellation_audio = await text_to_speech(cancellation_text)
return cancellation_audio, "Order not confirmed"