Ubuntu commited on
Commit
e717cd1
·
1 Parent(s): d098eb3

add youtube for developer

Browse files
Files changed (1) hide show
  1. app.py +40 -10
app.py CHANGED
@@ -154,10 +154,21 @@ def transcribe_and_speak(audio, source_lang, target_lang, youtube_url=None):
154
  def check_password(password):
155
  return password == DEVELOPER_PASSWORD
156
 
 
 
 
 
 
 
 
157
  def user_interface(audio, source_lang, target_lang, youtube_url):
158
  transcription, translated_text, audio_url = transcribe_and_speak(audio, source_lang, target_lang, youtube_url)
159
  return transcription, translated_text, audio_url
160
 
 
 
 
 
161
  with gr.Blocks() as demo:
162
  gr.Markdown("# Speech Translation")
163
 
@@ -171,13 +182,24 @@ with gr.Blocks() as demo:
171
  user_target_lang = gr.Dropdown(choices=["en", "ma", "ta", "zh"], label="Target Language", value="zh")
172
 
173
  with gr.Row():
174
- user_button = gr.Button("Translate and Speak")
175
 
176
  with gr.Row():
177
  user_transcription_output = gr.Textbox(label="Transcription")
178
  user_translation_output = gr.Textbox(label="Translation")
179
  user_audio_output = gr.Audio(label="Translated Speech")
180
 
 
 
 
 
 
 
 
 
 
 
 
181
  user_button.click(
182
  fn=user_interface,
183
  inputs=[user_audio_input, user_source_lang, user_target_lang, user_youtube_url],
@@ -196,25 +218,33 @@ with gr.Blocks() as demo:
196
 
197
  with gr.Row():
198
  dev_audio_input = gr.Audio(sources=["microphone", "upload"], type="filepath")
 
199
  dev_source_lang = gr.Dropdown(choices=["en", "ma", "ta", "zh"], label="Source Language", value="en")
200
  dev_target_lang = gr.Dropdown(choices=["en", "ma", "ta", "zh"], label="Target Language", value="zh")
201
 
202
  with gr.Row():
203
- dev_button = gr.Button("Transcribe, Translate, and Speak")
204
-
205
- with gr.Row():
206
- dev_text_output = gr.Textbox(label="Transcription")
207
 
208
  with gr.Row():
 
209
  dev_translation_output = gr.Textbox(label="Translation")
210
-
211
- with gr.Row():
212
  dev_audio_output = gr.Audio(label="Translated Speech")
213
 
 
 
 
 
 
 
 
 
 
 
 
214
  dev_button.click(
215
- fn=transcribe_and_speak,
216
- inputs=[dev_audio_input, dev_source_lang, dev_target_lang],
217
- outputs=[dev_text_output, dev_translation_output, dev_audio_output]
218
  )
219
 
220
  def login(password):
 
154
  def check_password(password):
155
  return password == DEVELOPER_PASSWORD
156
 
157
+ def is_valid_youtube_url(url: str) -> bool:
158
+ pattern = r'(?:https?:\/\/)?(?:www\.)?(?:youtube\.com|youtu\.be)\/(?:watch\?v=)?(.+)'
159
+ return bool(re.match(pattern, url))
160
+
161
+ def update_button_state(audio, youtube_url):
162
+ return gr.Button.update(interactive=bool(audio) or is_valid_youtube_url(youtube_url))
163
+
164
  def user_interface(audio, source_lang, target_lang, youtube_url):
165
  transcription, translated_text, audio_url = transcribe_and_speak(audio, source_lang, target_lang, youtube_url)
166
  return transcription, translated_text, audio_url
167
 
168
+ def dev_interface(audio, source_lang, target_lang, youtube_url):
169
+ transcription, translated_text, audio_url = transcribe_and_speak(audio, source_lang, target_lang, youtube_url)
170
+ return transcription, translated_text, audio_url
171
+
172
  with gr.Blocks() as demo:
173
  gr.Markdown("# Speech Translation")
174
 
 
182
  user_target_lang = gr.Dropdown(choices=["en", "ma", "ta", "zh"], label="Target Language", value="zh")
183
 
184
  with gr.Row():
185
+ user_button = gr.Button("Translate and Speak", interactive=False)
186
 
187
  with gr.Row():
188
  user_transcription_output = gr.Textbox(label="Transcription")
189
  user_translation_output = gr.Textbox(label="Translation")
190
  user_audio_output = gr.Audio(label="Translated Speech")
191
 
192
+ user_audio_input.change(
193
+ fn=update_button_state,
194
+ inputs=[user_audio_input, user_youtube_url],
195
+ outputs=[user_button]
196
+ )
197
+ user_youtube_url.change(
198
+ fn=update_button_state,
199
+ inputs=[user_audio_input, user_youtube_url],
200
+ outputs=[user_button]
201
+ )
202
+
203
  user_button.click(
204
  fn=user_interface,
205
  inputs=[user_audio_input, user_source_lang, user_target_lang, user_youtube_url],
 
218
 
219
  with gr.Row():
220
  dev_audio_input = gr.Audio(sources=["microphone", "upload"], type="filepath")
221
+ dev_youtube_url = gr.Textbox(label="YouTube URL (optional)")
222
  dev_source_lang = gr.Dropdown(choices=["en", "ma", "ta", "zh"], label="Source Language", value="en")
223
  dev_target_lang = gr.Dropdown(choices=["en", "ma", "ta", "zh"], label="Target Language", value="zh")
224
 
225
  with gr.Row():
226
+ dev_button = gr.Button("Translate and Speak", interactive=False)
 
 
 
227
 
228
  with gr.Row():
229
+ dev_transcription_output = gr.Textbox(label="Transcription")
230
  dev_translation_output = gr.Textbox(label="Translation")
 
 
231
  dev_audio_output = gr.Audio(label="Translated Speech")
232
 
233
+ dev_audio_input.change(
234
+ fn=update_button_state,
235
+ inputs=[dev_audio_input, dev_youtube_url],
236
+ outputs=[dev_button]
237
+ )
238
+ dev_youtube_url.change(
239
+ fn=update_button_state,
240
+ inputs=[dev_audio_input, dev_youtube_url],
241
+ outputs=[dev_button]
242
+ )
243
+
244
  dev_button.click(
245
+ fn=dev_interface,
246
+ inputs=[dev_audio_input, dev_source_lang, dev_target_lang, dev_youtube_url],
247
+ outputs=[dev_transcription_output, dev_translation_output, dev_audio_output]
248
  )
249
 
250
  def login(password):