Spaces:
Build error
Build error
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" | |