import gradio as gr import speech_recognition as sr from gtts import gTTS import os from simple_salesforce import Salesforce from salesforce import get_sf_client # Set up Salesforce connection sf = get_sf_client() def speak(text): """Use gTTS (Google Text-to-Speech) to speak the provided text""" tts = gTTS(text=text, lang='en') audio_file = "response.mp3" tts.save(audio_file) return audio_file def listen_for_voice_input(prompt): """Capture and recognize speech using the microphone""" recognizer = sr.Recognizer() with sr.Microphone() as source: print(f"Listening for: {prompt}") speak(prompt) # Prompt the user via TTS audio = recognizer.listen(source) try: text = recognizer.recognize_google(audio) print(f"User said: {text}") return text except Exception as e: return "Sorry, I didn't catch that." def signup(): """Handle user signup process via voice interaction""" # Step 1: Listen for name name = listen_for_voice_input("Please say your name") # Step 2: Listen for email email = listen_for_voice_input("Please say your email") # Step 3: Listen for phone number phone = listen_for_voice_input("Please say your phone number") try: # Store in Salesforce sf.Customer_Login__c.create({ 'Name': name, 'Email__c': email, 'Phone_Number__c': phone }) response = "Signup successful!" audio_file = speak(response) # Generate audio feedback return response, gr.update(visible=False), gr.update(visible=True), audio_file except Exception as e: error_msg = "There was an error with the signup process." audio_file = speak(error_msg) # Generate audio feedback return error_msg, gr.update(visible=False), gr.update(visible=True), audio_file def login(): """Handle user login via voice interaction""" # Listen for email and phone number email = listen_for_voice_input("Please say your email") phone = listen_for_voice_input("Please say your phone number") result = sf.query(f"SELECT Id FROM Customer_Login__c WHERE Email__c = '{email}' AND Phone_Number__c = '{phone}'") if result['records']: response = "Login successful!" audio_file = speak(response) # Generate audio feedback return response, gr.update(visible=False), gr.update(visible=True), audio_file else: error_msg = "Invalid login details. Please try again." audio_file = speak(error_msg) # Generate audio feedback return error_msg, gr.update(visible=False), gr.update(visible=True), audio_file def menu(): """Display the menu""" response = "Welcome to the menu. What would you like to order?" audio_file = speak(response) # Generate audio feedback return response, audio_file # Gradio Interface with gr.Blocks() as app: # Page 1 - Signup Section gr.Markdown("## BiryaniHub Voice App") signup_section = gr.Column(visible=True) gr.Markdown("### Signup") signup_button = gr.Button("Sign Up") signup_output = gr.Textbox(label="Signup Status") audio_output = gr.Audio(label="Audio Feedback") # Add Gradio audio component # Page 2 - Login Section (Defined here before use) login_section = gr.Column(visible=False) gr.Markdown("### Login") login_button = gr.Button("Login") login_output = gr.Textbox(label="Login Status") audio_output_login = gr.Audio(label="Audio Feedback for Login") # Add Gradio audio component for login # Page 3 - Menu Section (Defined here before use) menu_section = gr.Column(visible=False) gr.Markdown("### Menu") menu_button = gr.Button("Go to Menu") menu_output = gr.Textbox(label="Menu Status") audio_output_menu = gr.Audio(label="Audio Feedback for Menu") # Add Gradio audio component for menu # Actions for the buttons to transition between sections signup_button.click(signup, outputs=[signup_output, signup_section, login_section, audio_output]) login_button.click(login, outputs=[login_output, login_section, menu_section, audio_output_login]) menu_button.click(menu, outputs=[menu_output, audio_output_menu]) app.launch(debug=True)