ar08 commited on
Commit
c99f121
Β·
verified Β·
1 Parent(s): 285990a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +59 -29
app.py CHANGED
@@ -104,42 +104,79 @@ def transcribe_and_chat(audio):
104
  return response, audio_path
105
 
106
  def create_demo():
107
- with gr.Blocks() as demo:
108
  gr.Markdown(
109
  """
110
- # πŸ—£οΈ AI Voice Assistant
111
- Welcome to your personal voice assistant! Simply record your voice, and I will respond with both text and speech. The assistant will automatically start listening after playing its response. Powered by advanced AI models.
 
112
  """
113
  )
114
 
115
  with gr.Row():
116
  with gr.Column(scale=1):
117
- audio_input = gr.Audio(type="filepath", label="🎀 Record your voice", elem_id="audio-input")
118
- clear_button = gr.Button("Clear", variant="secondary", elem_id="clear-button")
119
- voice_volume = gr.Slider(minimum=0, maximum=2, value=1, step=0.1, label="Voice Volume", elem_id="voice-volume")
 
 
 
 
 
 
 
 
 
 
 
120
 
121
  with gr.Column(scale=1):
122
- chat_output = gr.Textbox(label="πŸ’¬ AI Response", elem_id="chat-output", lines=5, interactive=False)
123
- audio_output = gr.Audio(label="πŸ”Š AI Voice Response", autoplay=True, elem_id="audio-output")
 
 
 
 
 
 
 
 
124
 
125
- # Add some spacing and a divider
126
- gr.Markdown("---")
 
127
 
128
  # Processing the audio input
129
- def process_audio(audio, volume):
130
  logging.info(f"Received audio: {audio}")
131
  if audio is None:
132
- return "No audio detected. Please try recording again.", None
133
- response, audio_path = transcribe_and_chat(audio)
 
 
 
 
 
 
134
  # Adjust volume for the response audio
135
  adjusted_audio_path = asyncio.run(text_to_speech_stream(response, volume))
136
  logging.info(f"Response: {response}, Audio path: {adjusted_audio_path}")
137
- return response, adjusted_audio_path
138
-
139
- audio_input.change(process_audio, inputs=[audio_input, voice_volume], outputs=[chat_output, audio_output])
140
- clear_button.click(lambda: (None, None), None, [chat_output, audio_output])
 
 
 
 
 
 
 
 
 
 
141
 
142
- # JavaScript to handle autoplay, automatic submission, and auto-listen
143
  demo.load(None, js="""
144
  function() {
145
  var recordButton;
@@ -163,24 +200,17 @@ def create_demo():
163
  }
164
  }
165
 
166
- document.querySelector("audio").addEventListener("ended", function() {
167
  setTimeout(startListening, 500);
168
  });
169
 
170
  function playAssistantAudio() {
171
- var audioElements = document.querySelectorAll('audio');
172
- if (audioElements.length > 1) {
173
- var assistantAudio = audioElements[1];
174
- if (assistantAudio) {
175
- assistantAudio.play();
176
- }
177
  }
178
  }
179
 
180
- document.addEventListener('gradioAudioLoaded', function(event) {
181
- playAssistantAudio();
182
- });
183
-
184
  document.addEventListener('gradioUpdated', function(event) {
185
  setTimeout(playAssistantAudio, 100);
186
  });
 
104
  return response, audio_path
105
 
106
  def create_demo():
107
+ with gr.Blocks(theme=gr.themes.Soft()) as demo:
108
  gr.Markdown(
109
  """
110
+ # πŸŽ™οΈ AI Voice Assistant
111
+ Welcome to your personal voice assistant! Have a natural conversation with an AI-powered assistant.
112
+ Simply record your voice, and I'll respond with both text and speech.
113
  """
114
  )
115
 
116
  with gr.Row():
117
  with gr.Column(scale=1):
118
+ audio_input = gr.Audio(
119
+ type="filepath",
120
+ label="🎀 Speak your message",
121
+ elem_id="audio-input",
122
+ source="microphone"
123
+ )
124
+ voice_volume = gr.Slider(
125
+ minimum=0,
126
+ maximum=2,
127
+ value=1,
128
+ step=0.1,
129
+ label="πŸ”Š Assistant Voice Volume",
130
+ elem_id="voice-volume"
131
+ )
132
 
133
  with gr.Column(scale=1):
134
+ chat_output = gr.Chatbot(
135
+ label="Conversation",
136
+ elem_id="chat-output",
137
+ height=400
138
+ )
139
+ audio_output = gr.Audio(
140
+ label="πŸ”ˆ AI Voice Response",
141
+ autoplay=True,
142
+ elem_id="audio-output"
143
+ )
144
 
145
+ with gr.Row():
146
+ clear_button = gr.Button("πŸ—‘οΈ Clear Chat", variant="secondary", elem_id="clear-button")
147
+ submit_button = gr.Button("πŸš€ Submit", variant="primary", elem_id="submit-button")
148
 
149
  # Processing the audio input
150
+ def process_audio(audio, volume, history):
151
  logging.info(f"Received audio: {audio}")
152
  if audio is None:
153
+ return history + [("No audio detected. Please try recording again.", None)], None
154
+
155
+ transcribed_text = whisper_speech_to_text(audio)
156
+ if not transcribed_text:
157
+ return history + [("Sorry, I couldn't understand the audio. Please try again.", None)], None
158
+
159
+ response, audio_path = asyncio.run(chat_with_ai(transcribed_text))
160
+
161
  # Adjust volume for the response audio
162
  adjusted_audio_path = asyncio.run(text_to_speech_stream(response, volume))
163
  logging.info(f"Response: {response}, Audio path: {adjusted_audio_path}")
164
+
165
+ new_history = history + [(transcribed_text, response)]
166
+ return new_history, adjusted_audio_path
167
+
168
+ submit_button.click(
169
+ process_audio,
170
+ inputs=[audio_input, voice_volume, chat_output],
171
+ outputs=[chat_output, audio_output]
172
+ )
173
+
174
+ clear_button.click(
175
+ lambda: ([], None),
176
+ outputs=[chat_output, audio_output]
177
+ )
178
 
179
+ # JavaScript to handle autoplay and auto-listen
180
  demo.load(None, js="""
181
  function() {
182
  var recordButton;
 
200
  }
201
  }
202
 
203
+ document.querySelector("#audio-output").addEventListener("ended", function() {
204
  setTimeout(startListening, 500);
205
  });
206
 
207
  function playAssistantAudio() {
208
+ var audioElement = document.querySelector('#audio-output audio');
209
+ if (audioElement) {
210
+ audioElement.play();
 
 
 
211
  }
212
  }
213
 
 
 
 
 
214
  document.addEventListener('gradioUpdated', function(event) {
215
  setTimeout(playAssistantAudio, 100);
216
  });