Spaces:
Build error
Build error
File size: 2,306 Bytes
b0c4baa 397d198 b0c4baa 397d198 b0c4baa 397d198 b0c4baa 397d198 b0c4baa 397d198 b0c4baa 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 |
import json
import random
import speech_recognition as sr
import edge_tts
import asyncio
import tempfile
# Load the food menu from menu.json
with open("menu.json") as f:
food_menu = json.load(f)
# Function to get food suggestions based on user input
def get_food_suggestion(food_type, filter_type):
if food_type in food_menu:
return random.choice(food_menu[food_type].get(filter_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(text_input, food_type):
# 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
filter_type = text_input.lower() # Assuming the input is the filter type (vegan, halal, etc.)
suggestion = get_food_suggestion(food_type, filter_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"
|