voicebased / app.py
geethareddy's picture
Update app.py
1277d49 verified
import gradio as gr
import whisper
import torch
import os
# Suppress Torch Warnings
torch.serialization._legacy_load = torch.load # Suppresses `torch.load` warnings
# Load Whisper Model
model = whisper.load_model("base")
# Store User Data
user_data = {"name": "", "email": ""}
# Function to Capture Name
def capture_name(audio):
global user_data
if audio is None:
return "No audio detected. Please try again."
# Save the audio
audio_path = "name.wav"
with open(audio_path, "wb") as f:
f.write(audio) # Save audio from Gradio
# Convert Speech to Text
result = model.transcribe(audio_path)
user_data["name"] = result["text"]
return f"Hello, {user_data['name']}! Please provide your email address."
# Function to Capture Email
def capture_email(audio):
global user_data
if audio is None:
return "No audio detected. Please try again."
# Save the audio
audio_path = "email.wav"
with open(audio_path, "wb") as f:
f.write(audio) # Save audio from Gradio
# Convert Speech to Text
result = model.transcribe(audio_path)
user_data["email"] = result["text"]
return f"Thank you, {user_data['name']}! We have saved your email: {user_data['email']}."
# Create Gradio UI
with gr.Blocks() as demo:
gr.Markdown("# πŸŽ™οΈ Voice-Enabled Restaurant Menu - Biryani Hub")
# Step 1: Capture Name
gr.Markdown("### πŸ—£οΈ Step 1: Say Your Name")
name_input = gr.Audio(type="filepath") # FIXED!
name_output = gr.Textbox()
name_button = gr.Button("Submit Name")
# Step 2: Capture Email
gr.Markdown("### πŸ“§ Step 2: Say Your Email")
email_input = gr.Audio(type="filepath") # FIXED!
email_output = gr.Textbox()
email_button = gr.Button("Submit Email")
# Capture Name and Email
name_button.click(capture_name, inputs=name_input, outputs=name_output)
email_button.click(capture_email, inputs=email_input, outputs=email_output)
# Launch the App
demo.launch()