TogetherAI commited on
Commit
8834eb5
1 Parent(s): 7e4d2e0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -36
app.py CHANGED
@@ -1,17 +1,17 @@
 
 
1
  import gradio as gr
2
  import yt_dlp as youtube_dl
3
  from transformers import pipeline
4
  from transformers.pipelines.audio_utils import ffmpeg_read
5
- import torch
6
 
7
  import tempfile
8
  import os
9
- import time
10
 
11
  MODEL_NAME = "openai/whisper-large-v3"
12
  BATCH_SIZE = 8
13
  FILE_LIMIT_MB = 1000
14
- YT_LENGTH_LIMIT_S = 3600
15
 
16
  device = 0 if torch.cuda.is_available() else "cpu"
17
 
@@ -22,12 +22,14 @@ pipe = pipeline(
22
  device=device,
23
  )
24
 
 
25
  def transcribe(inputs, task):
26
  if inputs is None:
27
  raise gr.Error("No audio file submitted! Please upload or record an audio file before submitting your request.")
28
 
29
  text = pipe(inputs, batch_size=BATCH_SIZE, generate_kwargs={"task": task}, return_timestamps=True)["text"]
30
- return text
 
31
 
32
  def _return_yt_html_embed(yt_url):
33
  video_id = yt_url.split("?v=")[-1]
@@ -68,6 +70,7 @@ def download_yt_audio(yt_url, filename):
68
  except youtube_dl.utils.ExtractorError as err:
69
  raise gr.Error(str(err))
70
 
 
71
  def yt_transcribe(yt_url, task, max_filesize=75.0):
72
  html_embed_str = _return_yt_html_embed(yt_url)
73
 
@@ -84,10 +87,8 @@ def yt_transcribe(yt_url, task, max_filesize=75.0):
84
 
85
  return html_embed_str, text
86
 
87
- demo = gr.Blocks(theme="TogetherAi/Alex2")
88
 
89
- demo.layout = "centered"
90
- demo.width = 500
91
 
92
  mf_transcribe = gr.Interface(
93
  fn=transcribe,
@@ -96,21 +97,17 @@ mf_transcribe = gr.Interface(
96
  gr.inputs.Radio(["transcribe", "translate"], label="Task", default="transcribe"),
97
  ],
98
  outputs="text",
99
- layout="vertical", # Ändere das Layout auf "vertical"
100
- theme="TogetherAi/Alex2",
101
- title="Whisper Large V3: Audio transkribieren",
102
  description=(
103
- "Transkribiere lange Mikrofon- oder Audioeingaben mit einem Klick! Die Demo verwendet den"
104
- f" Checkpoint [{MODEL_NAME}](https://huggingface.co/{MODEL_NAME}) und 🤗 Transformers, um Audiodateien"
105
- " beliebiger Länge zu transkribieren."
106
  ),
107
  allow_flagging="never",
108
  )
109
 
110
- # Ändere das Layout für den Button auf "centered" und setze die Breite des Buttons auf 200 Pixel
111
- mf_transcribe[0].style['justify-content'] = 'center'
112
- mf_transcribe[1].style['width'] = '200px'
113
-
114
  file_transcribe = gr.Interface(
115
  fn=transcribe,
116
  inputs=[
@@ -118,21 +115,17 @@ file_transcribe = gr.Interface(
118
  gr.inputs.Radio(["transcribe", "translate"], label="Task", default="transcribe"),
119
  ],
120
  outputs="text",
121
- layout="vertical", # Ändere das Layout auf "vertical"
122
- theme="TogetherAi/Alex2",
123
  title="Whisper Large V3: Transcribe Audio",
124
  description=(
125
- "Transkribiere lange Mikrofon- oder Audioeingaben mit einem Klick! Die Demo verwendet den"
126
- f" Checkpoint [{MODEL_NAME}](https://huggingface.co/{MODEL_NAME}) und 🤗 Transformers, um Audiodateien"
127
- " beliebiger Länge zu transkribieren."
128
  ),
129
  allow_flagging="never",
130
  )
131
 
132
- # Ändere das Layout für den Button auf "centered" und setze die Breite des Buttons auf 200 Pixel
133
- file_transcribe[0].style['justify-content'] = 'center'
134
- file_transcribe[1].style['width'] = '200px'
135
-
136
  yt_transcribe = gr.Interface(
137
  fn=yt_transcribe,
138
  inputs=[
@@ -140,22 +133,18 @@ yt_transcribe = gr.Interface(
140
  gr.inputs.Radio(["transcribe", "translate"], label="Task", default="transcribe")
141
  ],
142
  outputs=["html", "text"],
143
- layout="vertical", # Ändere das Layout auf "vertical"
144
- theme="TogetherAi/Alex2",
145
  title="Whisper Large V3: Transcribe YouTube",
146
  description=(
147
- "Transkribiere lange Mikrofon- oder Audioeingaben mit einem Klick! Die Demo verwendet den"
148
- f" Checkpoint [{MODEL_NAME}](https://huggingface.co/{MODEL_NAME}) und 🤗 Transformers, um Audiodateien"
149
- " beliebiger Länge zu transkribieren."
150
  ),
151
  allow_flagging="never",
152
  )
153
 
154
- # Ändere das Layout für den Button auf "centered" und setze die Breite des Buttons auf 200 Pixel
155
- yt_transcribe[0].style['justify-content'] = 'center'
156
- yt_transcribe[1].style['width'] = '200px'
157
-
158
  with demo:
159
  gr.TabbedInterface([mf_transcribe, file_transcribe, yt_transcribe], ["Microphone", "Audio file", "YouTube"])
160
 
161
- demo.launch(enable_queue=True)
 
1
+ import torch
2
+
3
  import gradio as gr
4
  import yt_dlp as youtube_dl
5
  from transformers import pipeline
6
  from transformers.pipelines.audio_utils import ffmpeg_read
 
7
 
8
  import tempfile
9
  import os
 
10
 
11
  MODEL_NAME = "openai/whisper-large-v3"
12
  BATCH_SIZE = 8
13
  FILE_LIMIT_MB = 1000
14
+ YT_LENGTH_LIMIT_S = 3600 # limit to 1 hour YouTube files
15
 
16
  device = 0 if torch.cuda.is_available() else "cpu"
17
 
 
22
  device=device,
23
  )
24
 
25
+
26
  def transcribe(inputs, task):
27
  if inputs is None:
28
  raise gr.Error("No audio file submitted! Please upload or record an audio file before submitting your request.")
29
 
30
  text = pipe(inputs, batch_size=BATCH_SIZE, generate_kwargs={"task": task}, return_timestamps=True)["text"]
31
+ return text
32
+
33
 
34
  def _return_yt_html_embed(yt_url):
35
  video_id = yt_url.split("?v=")[-1]
 
70
  except youtube_dl.utils.ExtractorError as err:
71
  raise gr.Error(str(err))
72
 
73
+
74
  def yt_transcribe(yt_url, task, max_filesize=75.0):
75
  html_embed_str = _return_yt_html_embed(yt_url)
76
 
 
87
 
88
  return html_embed_str, text
89
 
 
90
 
91
+ demo = gr.Blocks()
 
92
 
93
  mf_transcribe = gr.Interface(
94
  fn=transcribe,
 
97
  gr.inputs.Radio(["transcribe", "translate"], label="Task", default="transcribe"),
98
  ],
99
  outputs="text",
100
+ layout="horizontal",
101
+ theme="TogetherAI/Alex2 ",
102
+ title="Whisper Large V3: Transcribe Audio",
103
  description=(
104
+ "Transcribe long-form microphone or audio inputs with the click of a button! Demo uses the"
105
+ f" checkpoint [{MODEL_NAME}](https://huggingface.co/{MODEL_NAME}) and 🤗 Transformers to transcribe audio files"
106
+ " of arbitrary length."
107
  ),
108
  allow_flagging="never",
109
  )
110
 
 
 
 
 
111
  file_transcribe = gr.Interface(
112
  fn=transcribe,
113
  inputs=[
 
115
  gr.inputs.Radio(["transcribe", "translate"], label="Task", default="transcribe"),
116
  ],
117
  outputs="text",
118
+ layout="horizontal",
119
+ theme="TogetherAI/Alex2",
120
  title="Whisper Large V3: Transcribe Audio",
121
  description=(
122
+ "Transcribe long-form microphone or audio inputs with the click of a button! Demo uses the"
123
+ f" checkpoint [{MODEL_NAME}](https://huggingface.co/{MODEL_NAME}) and 🤗 Transformers to transcribe audio files"
124
+ " of arbitrary length."
125
  ),
126
  allow_flagging="never",
127
  )
128
 
 
 
 
 
129
  yt_transcribe = gr.Interface(
130
  fn=yt_transcribe,
131
  inputs=[
 
133
  gr.inputs.Radio(["transcribe", "translate"], label="Task", default="transcribe")
134
  ],
135
  outputs=["html", "text"],
136
+ layout="horizontal",
137
+ theme="TogetherAI/Alex2",
138
  title="Whisper Large V3: Transcribe YouTube",
139
  description=(
140
+ "Transcribe long-form YouTube videos with the click of a button! Demo uses the checkpoint"
141
+ f" [{MODEL_NAME}](https://huggingface.co/{MODEL_NAME}) and 🤗 Transformers to transcribe video files of"
142
+ " arbitrary length."
143
  ),
144
  allow_flagging="never",
145
  )
146
 
 
 
 
 
147
  with demo:
148
  gr.TabbedInterface([mf_transcribe, file_transcribe, yt_transcribe], ["Microphone", "Audio file", "YouTube"])
149
 
150
+ demo.launch(enable_queue=True)