DynamicMenu0 / app.py
geethareddy's picture
Update app.py
f69bc89 verified
import gradio as gr
import pandas as pd
import time
import gtts
import os
import sounddevice as sd
import numpy as np
import speech_recognition as sr
import tempfile
import scipy.io.wavfile as wav
# Function to convert text to speech
def speak_text(text):
tts = gtts.gTTS(text)
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".wav")
tts.save(temp_file.name)
data, samplerate = wav.read(temp_file.name)
sd.play(data, samplerate)
sd.wait()
os.remove(temp_file.name)
# Function to recognize speech input
def recognize_speech():
recognizer = sr.Recognizer()
with sr.Microphone() as source:
print("Listening...")
try:
audio = recognizer.listen(source, timeout=5)
text = recognizer.recognize_google(audio)
print("You said:", text)
return text.lower()
except sr.UnknownValueError:
print("Could not understand audio")
return ""
except sr.RequestError:
print("Speech service is down")
return ""
# Function to load the menu data
def load_menu():
menu_file = "menu.xlsx" # Ensure this file exists in the same directory
try:
return pd.read_excel(menu_file)
except Exception as e:
raise ValueError(f"Error loading menu file: {e}")
# Function to filter menu items based on preference
def filter_menu(preference):
menu_data = load_menu()
if preference == "Halal/Non-Veg":
filtered_data = menu_data[menu_data["Ingredients"].str.contains("Chicken|Mutton|Fish|Prawns|Goat", case=False, na=False)]
speak_text("Here are the non-vegetarian dishes")
elif preference == "Vegetarian":
filtered_data = menu_data[~menu_data["Ingredients"].str.contains("Chicken|Mutton|Fish|Prawns|Goat", case=False, na=False)]
speak_text("Here are the vegetarian dishes")
elif preference == "Guilt-Free":
filtered_data = menu_data[menu_data["Description"].str.contains(r"Fat: ([0-9]|10)g", case=False, na=False)]
speak_text("Here are the guilt-free options")
else:
filtered_data = menu_data
speak_text("Showing all dishes")
html_content = ""
for _, item in filtered_data.iterrows():
html_content += f"""
<div style="display: flex; align-items: center; border: 1px solid #ddd; border-radius: 8px; padding: 15px; margin-bottom: 10px; box-shadow: 0px 4px 8px rgba(0, 0, 0, 0.1);">
<div style="flex: 1; margin-right: 15px;">
<h3 style="margin: 0; font-size: 18px;">{item['Dish Name']}</h3>
<p style="margin: 5px 0; font-size: 16px; color: #888;">${item['Price ($)']}</p>
<p style="margin: 5px 0; font-size: 14px; color: #555;">{item['Description']}</p>
</div>
</div>
"""
return html_content
# Function to announce menu page entry
def announce_menu():
speak_text("Welcome to Biryani Hub")
time.sleep(2)
speak_text("Choose a preference: All, Vegetarian, Non-Veg, or Guilt-Free")
# Gradio App
def app():
with gr.Blocks() as demo:
gr.Markdown("### Menu Page")
announce_menu()
# Radio button for selecting preference
selected_preference = gr.Radio(
choices=["All", "Vegetarian", "Halal/Non-Veg", "Guilt Free"],
value="All",
label="Choose a Preference",
)
# Output area for menu items
menu_output = gr.HTML(value=filter_menu("All"))
# Update menu dynamically based on preference
selected_preference.change(filter_menu, inputs=[selected_preference], outputs=[menu_output])
# Layout
gr.Row([selected_preference])
gr.Row(menu_output)
return demo
if __name__ == "__main__":
app().launch()