datasciencedojo commited on
Commit
60c7e9e
·
verified ·
1 Parent(s): 40d7084

upgraded code according to gradio 5.11.0

Browse files
Files changed (1) hide show
  1. app.py +31 -28
app.py CHANGED
@@ -1,11 +1,9 @@
1
  import torch
2
-
3
  import gradio as gr
4
  import pytube as pt
5
  from transformers import pipeline
6
- from huggingface_hub import model_info
7
 
8
- MODEL_NAME = "openai/whisper-small" #this always needs to stay in line 8 :D sorry for the hackiness
9
  lang = "en"
10
 
11
  device = 0 if torch.cuda.is_available() else "cpu"
@@ -20,22 +18,31 @@ pipe.model.config.forced_decoder_ids = pipe.tokenizer.get_decoder_prompt_ids(lan
20
 
21
  def transcribe(microphone, file_upload):
22
  warn_output = ""
23
- if (microphone is not None) and (file_upload is not None):
24
  warn_output = (
25
  "WARNING: You've uploaded an audio file and used the microphone. "
26
- "The recorded file from the microphone will be used and the uploaded audio will be discarded.\n"
27
  )
28
 
29
- elif (microphone is None) and (file_upload is None):
30
- return "ERROR: You have to either use the microphone or upload an audio file"
31
-
32
- file = microphone if microphone is not None else file_upload
33
 
 
34
  text = pipe(file)["text"]
35
-
36
  return warn_output + text
37
 
38
- demo = gr.Blocks()
 
 
 
 
 
 
 
 
 
 
 
39
 
40
  css = """
41
  footer {display:none !important}
@@ -72,22 +79,18 @@ button.gallery-item:hover {
72
  }
73
  """
74
 
75
- examples = [
76
- ['Martin Luther king - FREE AT LAST.mp3'], ['Winston Churchul - ARCH OF VICTOR.mp3'], ['Voice of Neil Armstrong.mp3'], ['Speeh by George Washington.mp3'], ['Speech by John Kennedy.mp3'], ['Al Gore on Inventing the Internet.mp3'], ['Alan Greenspan.mp3'], ['Neil Armstrong - ONE SMALL STEP.mp3'], ['General Eisenhower announcing D-Day landing.mp3'], ['Hey Siri.wav']
77
- ]
 
 
 
 
 
 
 
78
 
79
- mf_transcribe = gr.Interface(
80
- fn=transcribe,
81
- inputs=[
82
- gr.inputs.Audio(source="microphone", type="filepath", optional=True),
83
- gr.inputs.Audio(source="upload", type="filepath", optional=True)
84
- ],
85
- outputs="text",
86
- layout="horizontal",
87
- theme="huggingface",
88
- allow_flagging="never",
89
- examples = examples,
90
- css = css
91
- ).launch(enable_queue=True)
92
 
93
- #used openai/whisper model
 
1
  import torch
 
2
  import gradio as gr
3
  import pytube as pt
4
  from transformers import pipeline
 
5
 
6
+ MODEL_NAME = "openai/whisper-small" # this always needs to stay in line 8 :D sorry for the hackiness
7
  lang = "en"
8
 
9
  device = 0 if torch.cuda.is_available() else "cpu"
 
18
 
19
  def transcribe(microphone, file_upload):
20
  warn_output = ""
21
+ if microphone and file_upload:
22
  warn_output = (
23
  "WARNING: You've uploaded an audio file and used the microphone. "
24
+ "The recorded file from the microphone will be used, and the uploaded audio will be discarded.\n"
25
  )
26
 
27
+ elif not (microphone or file_upload):
28
+ return "ERROR: You have to either use the microphone or upload an audio file."
 
 
29
 
30
+ file = microphone if microphone else file_upload
31
  text = pipe(file)["text"]
 
32
  return warn_output + text
33
 
34
+ examples = [
35
+ ['Martin Luther king - FREE AT LAST.mp3'],
36
+ ['Winston Churchul - ARCH OF VICTOR.mp3'],
37
+ ['Voice of Neil Armstrong.mp3'],
38
+ ['Speeh by George Washington.mp3'],
39
+ ['Speech by John Kennedy.mp3'],
40
+ ['Al Gore on Inventing the Internet.mp3'],
41
+ ['Alan Greenspan.mp3'],
42
+ ['Neil Armstrong - ONE SMALL STEP.mp3'],
43
+ ['General Eisenhower announcing D-Day landing.mp3'],
44
+ ['Hey Siri.wav']
45
+ ]
46
 
47
  css = """
48
  footer {display:none !important}
 
79
  }
80
  """
81
 
82
+ with gr.Blocks(css=css) as demo:
83
+ with gr.Row():
84
+ gr.Markdown("## Speech Recognition Demo")
85
+ with gr.Row():
86
+ mic_input = gr.Audio(source="microphone", type="filepath", label="Microphone Input", interactive=True)
87
+ file_upload = gr.Audio(source="upload", type="filepath", label="File Upload", interactive=True)
88
+ with gr.Row():
89
+ output = gr.Textbox(label="Transcription Output")
90
+ with gr.Row():
91
+ gr.Examples(examples=examples, inputs=[file_upload], label="Examples")
92
 
93
+ transcribe_button = gr.Button("Transcribe")
94
+ transcribe_button.click(transcribe, inputs=[mic_input, file_upload], outputs=[output])
 
 
 
 
 
 
 
 
 
 
 
95
 
96
+ demo.launch()