Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -14,7 +14,6 @@ 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,
|
@@ -23,47 +22,35 @@ pipe = pipeline(
|
|
23 |
)
|
24 |
|
25 |
|
26 |
-
def transcribe(inputs
|
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 |
-
|
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 |
def download_yt_audio(yt_url, filename):
|
43 |
info_loader = youtube_dl.YoutubeDL()
|
44 |
-
|
45 |
try:
|
46 |
info = info_loader.extract_info(yt_url, download=False)
|
47 |
except youtube_dl.utils.DownloadError as err:
|
48 |
raise gr.Error(str(err))
|
49 |
-
|
50 |
file_length = info["duration_string"]
|
51 |
file_h_m_s = file_length.split(":")
|
52 |
file_h_m_s = [int(sub_length) for sub_length in file_h_m_s]
|
53 |
-
|
54 |
if len(file_h_m_s) == 1:
|
55 |
file_h_m_s.insert(0, 0)
|
56 |
if len(file_h_m_s) == 2:
|
57 |
file_h_m_s.insert(0, 0)
|
58 |
file_length_s = file_h_m_s[0] * 3600 + file_h_m_s[1] * 60 + file_h_m_s[2]
|
59 |
-
|
60 |
if file_length_s > YT_LENGTH_LIMIT_S:
|
61 |
yt_length_limit_hms = time.strftime("%HH:%MM:%SS", time.gmtime(YT_LENGTH_LIMIT_S))
|
62 |
file_length_hms = time.strftime("%HH:%MM:%SS", time.gmtime(file_length_s))
|
63 |
raise gr.Error(f"Maximum YouTube length is {yt_length_limit_hms}, got {file_length_hms} YouTube video.")
|
64 |
-
|
65 |
ydl_opts = {"outtmpl": filename, "format": "worstvideo[ext=mp4]+bestaudio[ext=m4a]/best[ext=mp4]/best"}
|
66 |
-
|
67 |
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
|
68 |
try:
|
69 |
ydl.download([yt_url])
|
@@ -71,76 +58,49 @@ def download_yt_audio(yt_url, filename):
|
|
71 |
raise gr.Error(str(err))
|
72 |
|
73 |
|
74 |
-
def yt_transcribe(yt_url,
|
75 |
html_embed_str = _return_yt_html_embed(yt_url)
|
76 |
-
|
77 |
with tempfile.TemporaryDirectory() as tmpdirname:
|
78 |
filepath = os.path.join(tmpdirname, "video.mp4")
|
79 |
download_yt_audio(yt_url, filepath)
|
80 |
with open(filepath, "rb") as f:
|
81 |
inputs = f.read()
|
82 |
-
|
83 |
inputs = ffmpeg_read(inputs, pipe.feature_extractor.sampling_rate)
|
84 |
inputs = {"array": inputs, "sampling_rate": pipe.feature_extractor.sampling_rate}
|
85 |
-
|
86 |
-
text = pipe(inputs, batch_size=BATCH_SIZE, generate_kwargs={"task": task}, return_timestamps=True)["text"]
|
87 |
-
|
88 |
return html_embed_str, text
|
89 |
|
90 |
|
91 |
demo = gr.Blocks()
|
92 |
-
|
93 |
mf_transcribe = gr.Interface(
|
94 |
fn=transcribe,
|
95 |
-
inputs=[
|
96 |
-
gr.components.Audio(sources=["microphone"], type="filepath"),
|
97 |
-
gr.components.Radio(["transcribe", "translate"], label="Task", default="transcribe"),
|
98 |
-
],
|
99 |
outputs="text",
|
100 |
layout="horizontal",
|
101 |
theme="huggingface",
|
102 |
title=f"Transcribe Audio with {os.path.basename(MODEL_NAME)}",
|
103 |
-
description=(
|
104 |
-
"Transcribe long-form microphone or audio inputs with the click of a button! Demo uses the Japanese 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 |
file_transcribe = gr.Interface(
|
112 |
fn=transcribe,
|
113 |
-
inputs=[
|
114 |
-
gr.components.Audio(sources=["upload"], type="filepath", label="Audio file"),
|
115 |
-
gr.components.Radio(["transcribe", "translate"], label="Task", default="transcribe"),
|
116 |
-
],
|
117 |
outputs="text",
|
118 |
layout="horizontal",
|
119 |
theme="huggingface",
|
120 |
title=f"Transcribe Audio with {os.path.basename(MODEL_NAME)}",
|
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 |
yt_transcribe = gr.Interface(
|
130 |
fn=yt_transcribe,
|
131 |
-
inputs=[
|
132 |
-
gr.components.Textbox(lines=1, placeholder="Paste the URL to a YouTube video here", label="YouTube URL"),
|
133 |
-
gr.components.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 |
|
|
|
14 |
YT_LENGTH_LIMIT_S = 3600 # limit to 1 hour YouTube files
|
15 |
|
16 |
device = 0 if torch.cuda.is_available() else "cpu"
|
|
|
17 |
pipe = pipeline(
|
18 |
task="automatic-speech-recognition",
|
19 |
model=MODEL_NAME,
|
|
|
22 |
)
|
23 |
|
24 |
|
25 |
+
def transcribe(inputs):
|
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 |
+
return pipe(inputs, batch_size=BATCH_SIZE, generate_kwargs={"task": "transcribe"}, return_timestamps=True)["text"]
|
|
|
|
|
29 |
|
30 |
|
31 |
def _return_yt_html_embed(yt_url):
|
32 |
video_id = yt_url.split("?v=")[-1]
|
33 |
+
return f'<center> <iframe width="500" height="320" src="https://www.youtube.com/embed/{video_id}"> </iframe> </center>'
|
|
|
|
|
|
|
|
|
34 |
|
35 |
def download_yt_audio(yt_url, filename):
|
36 |
info_loader = youtube_dl.YoutubeDL()
|
|
|
37 |
try:
|
38 |
info = info_loader.extract_info(yt_url, download=False)
|
39 |
except youtube_dl.utils.DownloadError as err:
|
40 |
raise gr.Error(str(err))
|
|
|
41 |
file_length = info["duration_string"]
|
42 |
file_h_m_s = file_length.split(":")
|
43 |
file_h_m_s = [int(sub_length) for sub_length in file_h_m_s]
|
|
|
44 |
if len(file_h_m_s) == 1:
|
45 |
file_h_m_s.insert(0, 0)
|
46 |
if len(file_h_m_s) == 2:
|
47 |
file_h_m_s.insert(0, 0)
|
48 |
file_length_s = file_h_m_s[0] * 3600 + file_h_m_s[1] * 60 + file_h_m_s[2]
|
|
|
49 |
if file_length_s > YT_LENGTH_LIMIT_S:
|
50 |
yt_length_limit_hms = time.strftime("%HH:%MM:%SS", time.gmtime(YT_LENGTH_LIMIT_S))
|
51 |
file_length_hms = time.strftime("%HH:%MM:%SS", time.gmtime(file_length_s))
|
52 |
raise gr.Error(f"Maximum YouTube length is {yt_length_limit_hms}, got {file_length_hms} YouTube video.")
|
|
|
53 |
ydl_opts = {"outtmpl": filename, "format": "worstvideo[ext=mp4]+bestaudio[ext=m4a]/best[ext=mp4]/best"}
|
|
|
54 |
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
|
55 |
try:
|
56 |
ydl.download([yt_url])
|
|
|
58 |
raise gr.Error(str(err))
|
59 |
|
60 |
|
61 |
+
def yt_transcribe(yt_url, max_filesize=75.0):
|
62 |
html_embed_str = _return_yt_html_embed(yt_url)
|
|
|
63 |
with tempfile.TemporaryDirectory() as tmpdirname:
|
64 |
filepath = os.path.join(tmpdirname, "video.mp4")
|
65 |
download_yt_audio(yt_url, filepath)
|
66 |
with open(filepath, "rb") as f:
|
67 |
inputs = f.read()
|
|
|
68 |
inputs = ffmpeg_read(inputs, pipe.feature_extractor.sampling_rate)
|
69 |
inputs = {"array": inputs, "sampling_rate": pipe.feature_extractor.sampling_rate}
|
70 |
+
text = pipe(inputs, batch_size=BATCH_SIZE, generate_kwargs={"task": "transcribe"}, return_timestamps=True)["text"]
|
|
|
|
|
71 |
return html_embed_str, text
|
72 |
|
73 |
|
74 |
demo = gr.Blocks()
|
|
|
75 |
mf_transcribe = gr.Interface(
|
76 |
fn=transcribe,
|
77 |
+
inputs=[gr.components.Audio(sources=["microphone"], type="filepath")],
|
|
|
|
|
|
|
78 |
outputs="text",
|
79 |
layout="horizontal",
|
80 |
theme="huggingface",
|
81 |
title=f"Transcribe Audio with {os.path.basename(MODEL_NAME)}",
|
82 |
+
description=f"Transcribe long-form microphone or audio inputs with the click of a button! Demo uses the Japanese Whisper checkpoint [{MODEL_NAME}](https://huggingface.co/{MODEL_NAME}) and π€ Transformers to transcribe audio files of arbitrary length.",
|
|
|
|
|
|
|
|
|
83 |
allow_flagging="never",
|
84 |
)
|
85 |
|
86 |
file_transcribe = gr.Interface(
|
87 |
fn=transcribe,
|
88 |
+
inputs=[gr.components.Audio(sources=["upload"], type="filepath", label="Audio file")],
|
|
|
|
|
|
|
89 |
outputs="text",
|
90 |
layout="horizontal",
|
91 |
theme="huggingface",
|
92 |
title=f"Transcribe Audio with {os.path.basename(MODEL_NAME)}",
|
93 |
+
description=f"Transcribe long-form microphone or audio inputs with the click of a button! Demo uses the OpenAI Whisper checkpoint [{MODEL_NAME}](https://huggingface.co/{MODEL_NAME}) and π€ Transformers to transcribe audio files of arbitrary length.",
|
|
|
|
|
|
|
|
|
94 |
allow_flagging="never",
|
95 |
)
|
|
|
96 |
yt_transcribe = gr.Interface(
|
97 |
fn=yt_transcribe,
|
98 |
+
inputs=[gr.components.Textbox(lines=1, placeholder="Paste the URL to a YouTube video here", label="YouTube URL")],
|
|
|
|
|
|
|
99 |
outputs=["html", "text"],
|
100 |
layout="horizontal",
|
101 |
theme="huggingface",
|
102 |
title="Whisper Large V3: Transcribe YouTube",
|
103 |
+
description=f"Transcribe long-form YouTube videos with the click of a button! Demo uses the OpenAI Whisper checkpoint [{MODEL_NAME}](https://huggingface.co/{MODEL_NAME}) and π€ Transformers to transcribe video files of arbitrary length.",
|
|
|
|
|
|
|
|
|
104 |
allow_flagging="never",
|
105 |
)
|
106 |
|