Commit
•
d790c0b
1
Parent(s):
66efbc3
Update app.py
Browse files
app.py
CHANGED
@@ -3,11 +3,14 @@ import torch
|
|
3 |
import gradio as gr
|
4 |
import pytube as pt
|
5 |
from transformers import pipeline
|
|
|
|
|
|
|
6 |
|
7 |
MODEL_NAME = "openai/whisper-large-v2"
|
8 |
BATCH_SIZE = 8
|
9 |
FILE_LIMIT_MB = 1000
|
10 |
-
|
11 |
|
12 |
device = 0 if torch.cuda.is_available() else "cpu"
|
13 |
|
@@ -19,11 +22,6 @@ pipe = pipeline(
|
|
19 |
)
|
20 |
|
21 |
|
22 |
-
all_special_ids = pipe.tokenizer.all_special_ids
|
23 |
-
transcribe_token_id = all_special_ids[-5]
|
24 |
-
translate_token_id = all_special_ids[-6]
|
25 |
-
|
26 |
-
|
27 |
def transcribe(microphone, file_upload, task):
|
28 |
warn_output = ""
|
29 |
if (microphone is not None) and (file_upload is not None):
|
@@ -43,9 +41,7 @@ def transcribe(microphone, file_upload, task):
|
|
43 |
|
44 |
file = microphone if microphone is not None else file_upload
|
45 |
|
46 |
-
|
47 |
-
|
48 |
-
text = pipe(file, batch_size=BATCH_SIZE)["text"]
|
49 |
|
50 |
return warn_output + text
|
51 |
|
@@ -58,25 +54,46 @@ def _return_yt_html_embed(yt_url):
|
|
58 |
)
|
59 |
return HTML_str
|
60 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
61 |
|
62 |
def yt_transcribe(yt_url, task, max_filesize=75.0):
|
63 |
yt = pt.YouTube(yt_url)
|
64 |
html_embed_str = _return_yt_html_embed(yt_url)
|
65 |
-
for attempt in range(YT_ATTEMPT_LIMIT):
|
66 |
-
try:
|
67 |
-
yt = pytube.YouTube(yt_url)
|
68 |
-
stream = yt.streams.filter(only_audio=True)[0]
|
69 |
-
break
|
70 |
-
except KeyError:
|
71 |
-
if attempt + 1 == YT_ATTEMPT_LIMIT:
|
72 |
-
raise gr.Error("An error occurred while loading the YouTube video. Please try again.")
|
73 |
|
74 |
-
|
75 |
-
|
|
|
|
|
|
|
76 |
|
77 |
-
|
|
|
78 |
|
79 |
-
text = pipe(
|
80 |
|
81 |
return html_embed_str, text
|
82 |
|
|
|
3 |
import gradio as gr
|
4 |
import pytube as pt
|
5 |
from transformers import pipeline
|
6 |
+
from transformers.pipelines.audio_utils import ffmpeg_read
|
7 |
+
|
8 |
+
import tempfile
|
9 |
|
10 |
MODEL_NAME = "openai/whisper-large-v2"
|
11 |
BATCH_SIZE = 8
|
12 |
FILE_LIMIT_MB = 1000
|
13 |
+
YT_LENGTH_LIMIT_S = 3600 # limit to 1 hour YouTube files
|
14 |
|
15 |
device = 0 if torch.cuda.is_available() else "cpu"
|
16 |
|
|
|
22 |
)
|
23 |
|
24 |
|
|
|
|
|
|
|
|
|
|
|
25 |
def transcribe(microphone, file_upload, task):
|
26 |
warn_output = ""
|
27 |
if (microphone is not None) and (file_upload is not None):
|
|
|
41 |
|
42 |
file = microphone if microphone is not None else file_upload
|
43 |
|
44 |
+
text = pipe(file, batch_size=BATCH_SIZE, generate_kwargs={"task": task})["text"]
|
|
|
|
|
45 |
|
46 |
return warn_output + text
|
47 |
|
|
|
54 |
)
|
55 |
return HTML_str
|
56 |
|
57 |
+
def download_yt_audio(yt_url, filename):
|
58 |
+
info_loader = youtube_dl.YoutubeDL()
|
59 |
+
try:
|
60 |
+
info = info_loader.extract_info(yt_url, download=False)
|
61 |
+
except youtube_dl.utils.DownloadError as err:
|
62 |
+
raise gr.Error(str(err))
|
63 |
+
file_length = info["duration_string"]
|
64 |
+
file_h_m_s = file_length.split(":")
|
65 |
+
file_h_m_s = [int(sub_length) for sub_length in file_h_m_s]
|
66 |
+
if len(file_h_m_s) == 1:
|
67 |
+
file_h_m_s.insert(0, 0)
|
68 |
+
if len(file_h_m_s) == 2:
|
69 |
+
file_h_m_s.insert(0, 0)
|
70 |
+
file_length_s = file_h_m_s[0] * 3600 + file_h_m_s[1] * 60 + file_h_m_s[2]
|
71 |
+
if file_length_s > YT_LENGTH_LIMIT_S:
|
72 |
+
yt_length_limit_hms = time.strftime("%HH:%MM:%SS", time.gmtime(YT_LENGTH_LIMIT_S))
|
73 |
+
file_length_hms = time.strftime("%HH:%MM:%SS", time.gmtime(file_length_s))
|
74 |
+
raise gr.Error(f"Maximum YouTube length is {yt_length_limit_hms}, got {file_length_hms} YouTube video.")
|
75 |
+
ydl_opts = {"outtmpl": filename, "format": "worstvideo[ext=mp4]+bestaudio[ext=m4a]/best[ext=mp4]/best"}
|
76 |
+
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
|
77 |
+
try:
|
78 |
+
ydl.download([yt_url])
|
79 |
+
except youtube_dl.utils.ExtractorError as err:
|
80 |
+
raise gr.Error(str(err))
|
81 |
+
|
82 |
|
83 |
def yt_transcribe(yt_url, task, max_filesize=75.0):
|
84 |
yt = pt.YouTube(yt_url)
|
85 |
html_embed_str = _return_yt_html_embed(yt_url)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
86 |
|
87 |
+
with tempfile.TemporaryDirectory() as tmpdirname:
|
88 |
+
filepath = os.path.join(tmpdirname, "video.mp4")
|
89 |
+
download_yt_audio(yt_url, filepath)
|
90 |
+
with open(filepath, "rb") as f:
|
91 |
+
inputs = f.read()
|
92 |
|
93 |
+
inputs = ffmpeg_read(inputs, pipeline.feature_extractor.sampling_rate)
|
94 |
+
inputs = {"array": inputs, "sampling_rate": pipeline.feature_extractor.sampling_rate}
|
95 |
|
96 |
+
text = pipe(inputs, batch_size=BATCH_SIZE, generate_kwargs={"task": task})["text"]
|
97 |
|
98 |
return html_embed_str, text
|
99 |
|