Spaces:
Build error
Build error
| import gradio as gr | |
| from gtts import gTTS | |
| import os | |
| import speech_recognition as sr | |
| # Menu for the restaurant | |
| menu = { | |
| "Starters": ["Soup", "Spring Rolls"], | |
| "Main Course": ["Paneer Butter Masala", "Chicken Curry", "Veg Biryani"], | |
| "Breads": ["Roti", "Naan", "Paratha"], | |
| "Desserts": ["Gulab Jamun", "Ice Cream"], | |
| "Drinks": ["Mango Lassi", "Soda", "Water"] | |
| } | |
| # Context to track ongoing conversation | |
| context = {"last_action": None, "order": []} | |
| # Text-to-Speech Function | |
| def speak_and_save(text, filename="response.mp3"): | |
| tts = gTTS(text=text, lang='en') | |
| tts.save(filename) | |
| return filename | |
| # Speech Recognition Function | |
| def recognize_speech(audio_file_path): | |
| recognizer = sr.Recognizer() | |
| try: | |
| with sr.AudioFile(audio_file_path) as source: | |
| audio_data = recognizer.record(source) | |
| text = recognizer.recognize_google(audio_data) | |
| return text | |
| except sr.UnknownValueError: | |
| return "I didn't catch that. Could you repeat?" | |
| except sr.RequestError: | |
| return "There was an issue processing your speech. Try again later." | |
| # Voice Assistant Logic | |
| def process_order(audio_file_path): | |
| user_input = recognize_speech(audio_file_path) | |
| if user_input.startswith("Sorry") or user_input.startswith("I didn't"): | |
| return user_input, None | |
| global context | |
| # Default response | |
| response = f"You said: {user_input}\n" | |
| # Process the user input | |
| if "menu" in user_input.lower(): | |
| response += "Here is our menu:\n" | |
| for category, items in menu.items(): | |
| response += f"{category}: {', '.join(items)}\n" | |
| context["last_action"] = "show_menu" | |
| elif "order" in user_input.lower(): | |
| if context["last_action"] == "show_menu": | |
| response += "What would you like to order?" | |
| else: | |
| response += "Please let me know what you'd like to order. We have starters, main course, breads, desserts, and drinks." | |
| context["last_action"] = "place_order" | |
| elif any(item.lower() in user_input.lower() for item in sum(menu.values(), [])): | |
| # Add the ordered item to the context | |
| for category, items in menu.items(): | |
| for item in items: | |
| if item.lower() in user_input.lower(): | |
| context["order"].append(item) | |
| response += f"I've added {item} to your order. Would you like anything else?" | |
| break | |
| elif "no" in user_input.lower() or "done" in user_input.lower(): | |
| if context["order"]: | |
| response += f"Your final order is: {', '.join(context['order'])}. Shall I place this order for you?" | |
| context["last_action"] = "confirm_order" | |
| else: | |
| response += "It seems you haven't ordered anything yet." | |
| elif "yes" in user_input.lower() and context["last_action"] == "confirm_order": | |
| response += f"Thank you! Your order for {', '.join(context['order'])} has been placed. Would you like to add a dessert or drink?" | |
| context["last_action"] = "offer_suggestions" | |
| elif any(item.lower() in user_input.lower() for item in menu["Desserts"] + menu["Drinks"]): | |
| # Add desserts or drinks to the order | |
| for item in menu["Desserts"] + menu["Drinks"]: | |
| if item.lower() in user_input.lower(): | |
| context["order"].append(item) | |
| response += f"I've added {item} to your order. Your updated order is: {', '.join(context['order'])}. Thank you for ordering with us!" | |
| context["order"] = [] # Reset the order | |
| context["last_action"] = None | |
| break | |
| else: | |
| response += "I'm not sure what you meant. Could you clarify?" | |
| # Generate audio response | |
| audio_response_path = speak_and_save(response) | |
| return response, audio_response_path | |
| # Gradio Interface | |
| iface = gr.Interface( | |
| fn=process_order, | |
| inputs=gr.Audio(type="filepath"), # Accept audio input as a file path | |
| outputs=[gr.Textbox(label="Text Response"), gr.Audio(label="Audio Response")], # Include audio output | |
| title="Restaurant Voice Assistant", | |
| description="Talk to our voice assistant to place your order or ask about the menu!" | |
| ) | |
| if __name__ == "__main__": | |
| iface.launch() | |