nagasurendra commited on
Commit
fd08439
·
verified ·
1 Parent(s): b766996

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -9
app.py CHANGED
@@ -3,6 +3,8 @@ from gtts import gTTS
3
  import os
4
  import speech_recognition as sr
5
  from tempfile import NamedTemporaryFile
 
 
6
 
7
  # Initialize the recognizer
8
  recognizer = sr.Recognizer()
@@ -15,6 +17,7 @@ menu_items = {
15
  }
16
  cart = []
17
 
 
18
  def text_to_speech(text):
19
  """Convert text to speech and provide an audio file."""
20
  tts = gTTS(text=text, lang='en')
@@ -30,6 +33,7 @@ def read_menu():
30
  menu_text += "Please tell me the items you want to add to your cart."
31
  return menu_text
32
 
 
33
  def process_audio_command(audio_path):
34
  """Process the user's audio command."""
35
  global cart
@@ -65,23 +69,41 @@ def process_audio_command(audio_path):
65
 
66
  return "Sorry, I didn't understand that. Please try again."
67
 
68
- # Define the continuous conversation function
69
- def conversation_loop(audio_path):
70
- """Continuous conversation handling for dynamic interactions."""
71
- user_response = process_audio_command(audio_path)
72
- return user_response, text_to_speech(user_response)
 
 
 
 
 
 
 
 
 
 
 
 
 
73
 
 
74
  with gr.Blocks() as app:
75
  gr.Markdown("# Voice-Activated Restaurant Menu System")
76
  gr.Markdown("Speak your command to interact with the menu dynamically.")
77
 
78
  with gr.Row():
79
  audio_input = gr.Audio(label="Speak Your Command", type="filepath")
80
- text_output = gr.Textbox(label="Transcribed Command")
81
- response_text = gr.Textbox(label="Response Text")
82
- audio_output = gr.Audio(label="Assistant Response")
83
 
84
- audio_input.change(conversation_loop, inputs=audio_input, outputs=[response_text, audio_output])
 
 
 
 
85
 
86
  if __name__ == "__main__":
 
87
  app.launch()
 
3
  import os
4
  import speech_recognition as sr
5
  from tempfile import NamedTemporaryFile
6
+ import threading
7
+ import time
8
 
9
  # Initialize the recognizer
10
  recognizer = sr.Recognizer()
 
17
  }
18
  cart = []
19
 
20
+ # Helper Functions
21
  def text_to_speech(text):
22
  """Convert text to speech and provide an audio file."""
23
  tts = gTTS(text=text, lang='en')
 
33
  menu_text += "Please tell me the items you want to add to your cart."
34
  return menu_text
35
 
36
+ # Command Processing
37
  def process_audio_command(audio_path):
38
  """Process the user's audio command."""
39
  global cart
 
69
 
70
  return "Sorry, I didn't understand that. Please try again."
71
 
72
+ # Continuous Conversation Loop
73
+ def continuous_conversation():
74
+ """Continuously listen to and respond to user commands."""
75
+ while True:
76
+ with NamedTemporaryFile(delete=False, suffix=".wav") as temp_audio:
77
+ try:
78
+ print("Listening...")
79
+ with sr.Microphone() as source:
80
+ recognizer.adjust_for_ambient_noise(source)
81
+ audio = recognizer.listen(source)
82
+ with open(temp_audio.name, "wb") as f:
83
+ f.write(audio.get_wav_data())
84
+ response = process_audio_command(temp_audio.name)
85
+ print(response)
86
+ audio_response = text_to_speech(response)
87
+ os.system(f"mpg123 {audio_response}")
88
+ except Exception as e:
89
+ print("Error processing audio input.")
90
 
91
+ # Gradio Interface
92
  with gr.Blocks() as app:
93
  gr.Markdown("# Voice-Activated Restaurant Menu System")
94
  gr.Markdown("Speak your command to interact with the menu dynamically.")
95
 
96
  with gr.Row():
97
  audio_input = gr.Audio(label="Speak Your Command", type="filepath")
98
+ response_text = gr.Textbox(label="Assistant Response")
99
+ audio_output = gr.Audio(label="Assistant Voice Response")
 
100
 
101
+ def handle_conversation(audio_path):
102
+ response = process_audio_command(audio_path)
103
+ return response, text_to_speech(response)
104
+
105
+ audio_input.change(handle_conversation, inputs=audio_input, outputs=[response_text, audio_output])
106
 
107
  if __name__ == "__main__":
108
+ threading.Thread(target=continuous_conversation, daemon=True).start()
109
  app.launch()