svec commited on
Commit
1feded7
·
verified ·
1 Parent(s): d36c7e5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -28
app.py CHANGED
@@ -3,7 +3,7 @@ import gradio as gr
3
  import pytube as pt
4
  from transformers import pipeline
5
 
6
- MODEL_NAME = "ales/whisper-small-belarusian" # needs to stay on line 8 😄
7
  lang = "be"
8
 
9
  device = 0 if torch.cuda.is_available() else "cpu"
@@ -20,52 +20,48 @@ pipe = pipeline(
20
  pipe.model.config.forced_decoder_ids = pipe.tokenizer.get_decoder_prompt_ids(language=lang, task="transcribe")
21
 
22
 
23
- def transcribe(audio_input):
24
- if audio_input is None:
25
- return "ERROR: Please record or upload an audio file."
26
- text = pipe(audio_input)["text"]
27
- return text
28
-
29
-
30
- def _return_yt_html_embed(yt_url):
31
- video_id = yt_url.split("?v=")[-1].split("&")[0]
32
- return (
33
- f'<center><iframe width="500" height="320" '
34
- f'src="https://www.youtube.com/embed/{video_id}" '
35
- f'frameborder="0" allowfullscreen></iframe></center>'
36
- )
37
 
38
 
39
  def yt_transcribe(yt_url):
40
  if not yt_url:
41
  return "", "ERROR: You must provide a YouTube URL."
42
-
43
  yt = pt.YouTube(yt_url)
44
- html_embed = _return_yt_html_embed(yt_url)
45
-
46
  stream = yt.streams.filter(only_audio=True).first()
47
  stream.download(filename="audio.mp3")
48
-
49
  text = pipe("audio.mp3")["text"]
50
- return html_embed, text
51
 
52
 
53
  with gr.Blocks() as demo:
54
  with gr.Tab("🎤 Transcribe Audio"):
55
- gr.Markdown("## 🎧 Запішы або загрузі аўдыё і атрымай транскрыпцыю")
56
  audio_input = gr.Audio(type="filepath", label="Record or Upload Audio")
57
  transcribe_button = gr.Button("Transcribe")
58
- transcription_output = gr.Textbox(label="Transcribed Text")
59
 
60
- transcribe_button.click(fn=transcribe, inputs=[audio_input], outputs=[transcription_output])
 
 
 
 
61
 
62
  with gr.Tab("📺 Transcribe YouTube"):
63
- gr.Markdown("## 🎬 Устаў спасылку на відэа з YouTube і атрымай транскрыпцыю")
64
  yt_input = gr.Textbox(label="YouTube URL", placeholder="https://www.youtube.com/watch?v=...")
65
  yt_button = gr.Button("Transcribe YouTube")
66
- yt_video = gr.HTML()
67
- yt_transcription = gr.Textbox(label="Transcribed Text")
68
-
69
- yt_button.click(fn=yt_transcribe, inputs=[yt_input], outputs=[yt_video, yt_transcription])
 
 
 
 
70
 
71
  demo.launch()
 
3
  import pytube as pt
4
  from transformers import pipeline
5
 
6
+ MODEL_NAME = "ales/whisper-small-belarusian"
7
  lang = "be"
8
 
9
  device = 0 if torch.cuda.is_available() else "cpu"
 
20
  pipe.model.config.forced_decoder_ids = pipe.tokenizer.get_decoder_prompt_ids(language=lang, task="transcribe")
21
 
22
 
23
+ def transcribe(audio_file):
24
+ if audio_file is None:
25
+ return "ERROR: Please upload or record audio"
26
+ return pipe(audio_file)["text"]
 
 
 
 
 
 
 
 
 
 
27
 
28
 
29
  def yt_transcribe(yt_url):
30
  if not yt_url:
31
  return "", "ERROR: You must provide a YouTube URL."
 
32
  yt = pt.YouTube(yt_url)
33
+ video_id = yt_url.split("?v=")[-1].split("&")[0]
34
+ embed = f'<center><iframe width="500" height="320" src="https://www.youtube.com/embed/{video_id}" frameborder="0" allowfullscreen></iframe></center>'
35
  stream = yt.streams.filter(only_audio=True).first()
36
  stream.download(filename="audio.mp3")
 
37
  text = pipe("audio.mp3")["text"]
38
+ return embed, text
39
 
40
 
41
  with gr.Blocks() as demo:
42
  with gr.Tab("🎤 Transcribe Audio"):
43
+ gr.Markdown("## Запішы або загрузі аўдыё")
44
  audio_input = gr.Audio(type="filepath", label="Record or Upload Audio")
45
  transcribe_button = gr.Button("Transcribe")
46
+ transcription_output = gr.Textbox(label="Transcription")
47
 
48
+ transcribe_button.click(
49
+ fn=transcribe,
50
+ inputs=[audio_input],
51
+ outputs=[transcription_output],
52
+ )
53
 
54
  with gr.Tab("📺 Transcribe YouTube"):
55
+ gr.Markdown("## Устаў спасылку на YouTube-відэа")
56
  yt_input = gr.Textbox(label="YouTube URL", placeholder="https://www.youtube.com/watch?v=...")
57
  yt_button = gr.Button("Transcribe YouTube")
58
+ yt_embed = gr.HTML()
59
+ yt_text = gr.Textbox(label="Transcription")
60
+
61
+ yt_button.click(
62
+ fn=yt_transcribe,
63
+ inputs=[yt_input],
64
+ outputs=[yt_embed, yt_text],
65
+ )
66
 
67
  demo.launch()