Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,3 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import torch
|
2 |
|
3 |
import gradio as gr
|
@@ -8,144 +15,183 @@ from transformers.pipelines.audio_utils import ffmpeg_read
|
|
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 |
|
18 |
-
pipe = pipeline(
|
19 |
-
task="automatic-speech-recognition",
|
20 |
-
model=MODEL_NAME,
|
21 |
-
chunk_length_s=30,
|
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]
|
36 |
-
HTML_str = (
|
37 |
-
f'<center> <iframe width="500" height="320" src="https://www.youtube.com/embed/{video_id}"> </iframe>'
|
38 |
-
" </center>"
|
39 |
-
)
|
40 |
-
return HTML_str
|
41 |
|
42 |
-
|
43 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
44 |
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
|
65 |
-
|
66 |
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
|
73 |
|
74 |
-
def yt_transcribe(yt_url, task, max_filesize=75.0):
|
75 |
-
|
76 |
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
|
83 |
-
|
84 |
-
|
85 |
|
86 |
-
|
87 |
|
88 |
-
|
89 |
|
90 |
|
91 |
demo = gr.Blocks()
|
92 |
|
93 |
-
mf_transcribe = gr.Interface(
|
94 |
-
fn=transcribe,
|
95 |
-
inputs=[
|
96 |
-
gr.inputs.Audio(source="microphone", type="filepath", optional=True),
|
97 |
-
gr.inputs.Radio(["transcribe", "translate"], label="Task", default="transcribe"),
|
98 |
-
],
|
99 |
-
outputs="text",
|
100 |
-
layout="horizontal",
|
101 |
-
theme="huggingface",
|
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 OpenAI Whisper"
|
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 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
gr.inputs.Radio(["transcribe", "translate"], label="Task", default="transcribe"),
|
116 |
-
],
|
117 |
-
outputs="text",
|
118 |
-
layout="horizontal",
|
119 |
-
theme="huggingface",
|
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 OpenAI Whisper"
|
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 |
-
|
130 |
-
fn=
|
131 |
-
inputs=
|
132 |
-
|
133 |
-
gr.inputs.Radio(["transcribe", "translate"], label="Task", default="transcribe")
|
134 |
-
],
|
135 |
-
outputs=["html", "text"],
|
136 |
-
layout="horizontal",
|
137 |
-
theme="huggingface",
|
138 |
-
title="Whisper Large V3: Transcribe YouTube",
|
139 |
-
description=(
|
140 |
-
"Transcribe long-form YouTube videos with the click of a button! Demo uses the OpenAI Whisper 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(
|
|
|
|
|
|
|
|
|
|
|
149 |
|
150 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
151 |
|
|
|
1 |
+
|
2 |
+
|
3 |
+
|
4 |
+
|
5 |
+
|
6 |
+
|
7 |
+
|
8 |
import torch
|
9 |
|
10 |
import gradio as gr
|
|
|
15 |
import tempfile
|
16 |
import os
|
17 |
|
|
|
|
|
|
|
|
|
18 |
|
|
|
19 |
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
|
21 |
+
model = separator.from_hparams(source="speechbrain/sepformer-libri2mix", savedir='pretrained_models/sepformer-libri2mix')
|
22 |
|
|
|
|
|
|
|
23 |
|
|
|
|
|
24 |
|
25 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
26 |
|
27 |
+
|
28 |
+
|
29 |
+
|
30 |
+
|
31 |
+
|
32 |
+
|
33 |
+
|
34 |
+
# MODEL_NAME = "openai/whisper-large-v3"
|
35 |
+
# BATCH_SIZE = 8
|
36 |
+
# FILE_LIMIT_MB = 1000
|
37 |
+
# YT_LENGTH_LIMIT_S = 3600 # limit to 1 hour YouTube files
|
38 |
+
|
39 |
+
# device = 0 if torch.cuda.is_available() else "cpu"
|
40 |
+
|
41 |
+
# pipe = pipeline(
|
42 |
+
# task="automatic-speech-recognition",
|
43 |
+
# model=MODEL_NAME,
|
44 |
+
# chunk_length_s=30,
|
45 |
+
# device=device,
|
46 |
+
# )
|
47 |
+
|
48 |
+
|
49 |
+
|
50 |
+
|
51 |
+
# # def transcribe(inputs, task):
|
52 |
+
# # if inputs is None:
|
53 |
+
# # raise gr.Error("No audio file submitted! Please upload or record an audio file before submitting your request.")
|
54 |
+
|
55 |
+
# # text = pipe(inputs, batch_size=BATCH_SIZE, generate_kwargs={"task": task}, return_timestamps=True)["text"]
|
56 |
+
# # return text
|
57 |
+
|
58 |
+
|
59 |
+
# # def _return_yt_html_embed(yt_url):
|
60 |
+
# # video_id = yt_url.split("?v=")[-1]
|
61 |
+
# # HTML_str = (
|
62 |
+
# # f'<center> <iframe width="500" height="320" src="https://www.youtube.com/embed/{video_id}"> </iframe>'
|
63 |
+
# # " </center>"
|
64 |
+
# # )
|
65 |
+
# # return HTML_str
|
66 |
+
|
67 |
+
# # def download_yt_audio(yt_url, filename):
|
68 |
+
# # info_loader = youtube_dl.YoutubeDL()
|
69 |
|
70 |
+
# # try:
|
71 |
+
# # info = info_loader.extract_info(yt_url, download=False)
|
72 |
+
# # except youtube_dl.utils.DownloadError as err:
|
73 |
+
# # raise gr.Error(str(err))
|
74 |
|
75 |
+
# # file_length = info["duration_string"]
|
76 |
+
# # file_h_m_s = file_length.split(":")
|
77 |
+
# # file_h_m_s = [int(sub_length) for sub_length in file_h_m_s]
|
78 |
|
79 |
+
# # if len(file_h_m_s) == 1:
|
80 |
+
# # file_h_m_s.insert(0, 0)
|
81 |
+
# # if len(file_h_m_s) == 2:
|
82 |
+
# # file_h_m_s.insert(0, 0)
|
83 |
+
# # file_length_s = file_h_m_s[0] * 3600 + file_h_m_s[1] * 60 + file_h_m_s[2]
|
84 |
|
85 |
+
# # if file_length_s > YT_LENGTH_LIMIT_S:
|
86 |
+
# # yt_length_limit_hms = time.strftime("%HH:%MM:%SS", time.gmtime(YT_LENGTH_LIMIT_S))
|
87 |
+
# # file_length_hms = time.strftime("%HH:%MM:%SS", time.gmtime(file_length_s))
|
88 |
+
# # raise gr.Error(f"Maximum YouTube length is {yt_length_limit_hms}, got {file_length_hms} YouTube video.")
|
89 |
|
90 |
+
# # ydl_opts = {"outtmpl": filename, "format": "worstvideo[ext=mp4]+bestaudio[ext=m4a]/best[ext=mp4]/best"}
|
91 |
|
92 |
+
# # with youtube_dl.YoutubeDL(ydl_opts) as ydl:
|
93 |
+
# # try:
|
94 |
+
# # ydl.download([yt_url])
|
95 |
+
# # except youtube_dl.utils.ExtractorError as err:
|
96 |
+
# # raise gr.Error(str(err))
|
97 |
|
98 |
|
99 |
+
# # def yt_transcribe(yt_url, task, max_filesize=75.0):
|
100 |
+
# # html_embed_str = _return_yt_html_embed(yt_url)
|
101 |
|
102 |
+
# # with tempfile.TemporaryDirectory() as tmpdirname:
|
103 |
+
# # filepath = os.path.join(tmpdirname, "video.mp4")
|
104 |
+
# # download_yt_audio(yt_url, filepath)
|
105 |
+
# # with open(filepath, "rb") as f:
|
106 |
+
# # inputs = f.read()
|
107 |
|
108 |
+
# # inputs = ffmpeg_read(inputs, pipe.feature_extractor.sampling_rate)
|
109 |
+
# # inputs = {"array": inputs, "sampling_rate": pipe.feature_extractor.sampling_rate}
|
110 |
|
111 |
+
# # text = pipe(inputs, batch_size=BATCH_SIZE, generate_kwargs={"task": task}, return_timestamps=True)["text"]
|
112 |
|
113 |
+
# # return html_embed_str, text
|
114 |
|
115 |
|
116 |
demo = gr.Blocks()
|
117 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
118 |
|
119 |
+
def separateaudio(filepath):
|
120 |
+
est_sources = model.separate_file(path=filepath)
|
121 |
+
torchaudio.save("file.wav", est_sources[:, :, 0].detach().cpu(), 8000)
|
122 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
123 |
|
124 |
+
separation = gr.Interface(
|
125 |
+
fn=transcribe_speech,
|
126 |
+
inputs=gr.Audio(sources="upload", type="filepath"),
|
127 |
+
outputs=gr.outputs.Textbox(),
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
128 |
)
|
129 |
|
130 |
+
|
131 |
with demo:
|
132 |
+
gr.TabbedInterface(
|
133 |
+
[separation],
|
134 |
+
["Separate audio file"],
|
135 |
+
)
|
136 |
+
|
137 |
+
|
138 |
|
139 |
+
# mf_transcribe = gr.Interface(
|
140 |
+
# fn=transcribe,
|
141 |
+
# inputs=[
|
142 |
+
# gr.inputs.Audio(source="microphone", type="filepath", optional=True),
|
143 |
+
# gr.inputs.Radio(["transcribe", "translate"], label="Task", default="transcribe"),
|
144 |
+
# ],
|
145 |
+
# outputs="text",
|
146 |
+
# layout="horizontal",
|
147 |
+
# theme="huggingface",
|
148 |
+
# title="Whisper Large V3: Transcribe Audio",
|
149 |
+
# description=(
|
150 |
+
# "Transcribe long-form microphone or audio inputs with the click of a button! Demo uses the OpenAI Whisper"
|
151 |
+
# f" checkpoint [{MODEL_NAME}](https://huggingface.co/{MODEL_NAME}) and 🤗 Transformers to transcribe audio files"
|
152 |
+
# " of arbitrary length."
|
153 |
+
# ),
|
154 |
+
# allow_flagging="never",
|
155 |
+
# )
|
156 |
+
|
157 |
+
# file_transcribe = gr.Interface(
|
158 |
+
# fn=transcribe,
|
159 |
+
# inputs=[
|
160 |
+
# gr.inputs.Audio(source="upload", type="filepath", optional=True, label="Audio file"),
|
161 |
+
# gr.inputs.Radio(["transcribe", "translate"], label="Task", default="transcribe"),
|
162 |
+
# ],
|
163 |
+
# outputs="text",
|
164 |
+
# layout="horizontal",
|
165 |
+
# theme="huggingface",
|
166 |
+
# title="Whisper Large V3: Transcribe Audio",
|
167 |
+
# description=(
|
168 |
+
# "Transcribe long-form microphone or audio inputs with the click of a button! Demo uses the OpenAI Whisper"
|
169 |
+
# f" checkpoint [{MODEL_NAME}](https://huggingface.co/{MODEL_NAME}) and 🤗 Transformers to transcribe audio files"
|
170 |
+
# " of arbitrary length."
|
171 |
+
# ),
|
172 |
+
# allow_flagging="never",
|
173 |
+
# )
|
174 |
+
|
175 |
+
# yt_transcribe = gr.Interface(
|
176 |
+
# fn=yt_transcribe,
|
177 |
+
# inputs=[
|
178 |
+
# gr.inputs.Textbox(lines=1, placeholder="Paste the URL to a YouTube video here", label="YouTube URL"),
|
179 |
+
# gr.inputs.Radio(["transcribe", "translate"], label="Task", default="transcribe")
|
180 |
+
# ],
|
181 |
+
# outputs=["html", "text"],
|
182 |
+
# layout="horizontal",
|
183 |
+
# theme="huggingface",
|
184 |
+
# title="Whisper Large V3: Transcribe YouTube",
|
185 |
+
# description=(
|
186 |
+
# "Transcribe long-form YouTube videos with the click of a button! Demo uses the OpenAI Whisper checkpoint"
|
187 |
+
# f" [{MODEL_NAME}](https://huggingface.co/{MODEL_NAME}) and 🤗 Transformers to transcribe video files of"
|
188 |
+
# " arbitrary length."
|
189 |
+
# ),
|
190 |
+
# allow_flagging="never",
|
191 |
+
# )
|
192 |
+
|
193 |
+
# with demo:
|
194 |
+
# gr.TabbedInterface([mf_transcribe, file_transcribe, yt_transcribe], ["Microphone", "Audio file", "YouTube"])
|
195 |
+
|
196 |
+
# demo.launch(enable_queue=True)
|
197 |
|