VoiceAssistant / app.py
surendranaga's picture
Added missing files
dc1090a
import gradio as gr
import speech_recognition as sr
import pyttsx3
# Initialize text-to-speech engine
engine = pyttsx3.init()
# Store cart in a temporary storage
cart = []
# Define the menu items dynamically
menu_items = {
"Pizza": 10.99,
"Burger": 8.49,
"Pasta": 12.99,
"Salad": 7.99,
"Soda": 2.49
}
def speak_response(response):
"""Generate voice output."""
engine.say(response)
engine.runAndWait()
def process_input(input_text):
global cart
response = ""
if "price of" in input_text:
matched_items = [item for item in menu_items if item.lower() in input_text]
if len(matched_items) == 1:
item = matched_items[0]
response = f"The price of {item} is ${menu_items[item]:.2f}."
elif len(matched_items) > 1:
response = f"I detected multiple items in your input: {', '.join(matched_items)}. Please ask for the price of one item at a time."
else:
response = "I couldn't find that item on the menu. Please ask for an item available in the menu."
elif any(item.lower() in input_text for item in menu_items):
matched_items = [item for item in menu_items if item.lower() in input_text]
if len(matched_items) == 1:
item = matched_items[0]
cart.append(item)
response = f"{item} has been added to your cart. Your current cart includes:\n"
for cart_item in cart:
response += f"- {cart_item}\n"
response += "\nWould you like to add anything else?"
elif len(matched_items) > 1:
response = f"I detected multiple items in your input: {', '.join(matched_items)}. Please mention one item at a time."
elif "menu" in input_text:
response = "Here is our menu:\n"
for item in menu_items.keys():
response += f"{item}\n"
response += "\nWhat would you like to order?"
elif "final order" in input_text or "submit order" in input_text:
if cart:
total = calculate_total(cart)
response = "Your final order includes:\n"
for item in cart:
response += f"- {item}\n"
response += f"\nTotal amount: ${total:.2f}. Thank you for ordering!"
cart.clear()
else:
response = "Your cart is empty. Would you like to order something?"
else:
response = "I didn’t quite catch that. Please tell me what you’d like to order or ask about."
return response
def voice_assistant(audio_file):
recognizer = sr.Recognizer()
try:
with sr.AudioFile(audio_file) as source:
audio = recognizer.record(source)
input_text = recognizer.recognize_google(audio).lower()
response = process_input(input_text)
return response
except sr.UnknownValueError:
return "Sorry, I couldn't understand the audio. Please try again."
gr.Interface(
fn=voice_assistant,
inputs=gr.Audio(source="microphone", type="filepath"),
outputs="text",
live=True,
title="Voice Assistant"
).launch()