csukuangfj commited on
Commit
9edf22d
1 Parent(s): d633661

support recognition from an URL

Browse files
Files changed (1) hide show
  1. app.py +50 -0
app.py CHANGED
@@ -22,12 +22,15 @@
22
  import base64
23
  import logging
24
  import os
 
25
  import time
26
  from datetime import datetime
27
 
28
  import gradio as gr
29
  import torch
30
  import torchaudio
 
 
31
 
32
  from examples import examples
33
  from model import decode, get_pretrained_model, language_to_models, sample_rate
@@ -61,6 +64,28 @@ def build_html_output(s: str, style: str = "result_item_success"):
61
  </div>
62
  """
63
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
64
 
65
  def process_uploaded_file(
66
  language: str,
@@ -301,6 +326,18 @@ with demo:
301
  fn=process_microphone,
302
  )
303
 
 
 
 
 
 
 
 
 
 
 
 
 
304
  upload_button.click(
305
  process_uploaded_file,
306
  inputs=[
@@ -324,6 +361,19 @@ with demo:
324
  ],
325
  outputs=[recorded_output, recorded_html_info],
326
  )
 
 
 
 
 
 
 
 
 
 
 
 
 
327
  gr.Markdown(description)
328
 
329
  torch.set_num_threads(1)
 
22
  import base64
23
  import logging
24
  import os
25
+ import tempfile
26
  import time
27
  from datetime import datetime
28
 
29
  import gradio as gr
30
  import torch
31
  import torchaudio
32
+ import urllib.request
33
+
34
 
35
  from examples import examples
36
  from model import decode, get_pretrained_model, language_to_models, sample_rate
 
64
  </div>
65
  """
66
 
67
+ def process_url(
68
+ language: str,
69
+ repo_id: str,
70
+ decoding_method: str,
71
+ num_active_paths: int,
72
+ url: str,
73
+ ):
74
+ logging.info(f"Processing URL: {url}")
75
+ with tempfile.NamedTemporaryFile() as f:
76
+ try:
77
+ urllib.request.urlretrieve(url, f.name)
78
+
79
+ return process(
80
+ in_filename=f.name,
81
+ language=language,
82
+ repo_id=repo_id,
83
+ decoding_method=decoding_method,
84
+ num_active_paths=num_active_paths,
85
+ )
86
+ except Exception as e:
87
+ logging.info(str(e))
88
+ return "", build_html_output(str(e), "result_item_error")
89
 
90
  def process_uploaded_file(
91
  language: str,
 
326
  fn=process_microphone,
327
  )
328
 
329
+ with gr.TabItem("From URL"):
330
+ url_textbox = gr.Textbox(
331
+ max_lines=1,
332
+ placeholder="URL to an audio file",
333
+ label="URL",
334
+ interactive=True,
335
+ )
336
+
337
+ url_button = gr.Button("Submit for recognition")
338
+ url_output = gr.Textbox(label="Recognized speech from URL")
339
+ url_html_info = gr.HTML(label="Info")
340
+
341
  upload_button.click(
342
  process_uploaded_file,
343
  inputs=[
 
361
  ],
362
  outputs=[recorded_output, recorded_html_info],
363
  )
364
+
365
+ url_button.click(
366
+ process_url,
367
+ inputs=[
368
+ language_radio,
369
+ model_dropdown,
370
+ decoding_method_radio,
371
+ num_active_paths_slider,
372
+ url_textbox,
373
+ ],
374
+ outputs=[url_output, url_html_info],
375
+ )
376
+
377
  gr.Markdown(description)
378
 
379
  torch.set_num_threads(1)