Spaces:
Build error
Build error
File size: 2,339 Bytes
397d198 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
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"
|