Spaces:
Running
on
Zero
Running
on
Zero
yabramuvdi
commited on
Commit
•
475bd7b
1
Parent(s):
2362603
Initial update
Browse files
app.py
CHANGED
@@ -9,6 +9,10 @@ from transformers.pipelines.audio_utils import ffmpeg_read
|
|
9 |
import tempfile
|
10 |
import os
|
11 |
|
|
|
|
|
|
|
|
|
12 |
MODEL_NAME = "openai/whisper-large-v3-turbo"
|
13 |
BATCH_SIZE = 8
|
14 |
FILE_LIMIT_MB = 1000
|
@@ -23,90 +27,96 @@ pipe = pipeline(
|
|
23 |
device=device,
|
24 |
)
|
25 |
|
|
|
|
|
|
|
26 |
|
27 |
@spaces.GPU
|
28 |
def transcribe(inputs, task):
|
29 |
if inputs is None:
|
30 |
raise gr.Error("No audio file submitted! Please upload or record an audio file before submitting your request.")
|
31 |
|
32 |
-
|
|
|
33 |
return text
|
34 |
|
35 |
|
36 |
-
def _return_yt_html_embed(yt_url):
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
|
44 |
-
def download_yt_audio(yt_url, filename):
|
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 |
-
|
75 |
-
@spaces.GPU
|
76 |
-
def yt_transcribe(yt_url, task, max_filesize=75.0):
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
)
|
|
|
|
|
110 |
|
111 |
file_transcribe = gr.Interface(
|
112 |
fn=transcribe,
|
@@ -124,24 +134,32 @@ file_transcribe = gr.Interface(
|
|
124 |
allow_flagging="never",
|
125 |
)
|
126 |
|
127 |
-
yt_transcribe = gr.Interface(
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
)
|
|
|
|
|
|
|
|
|
142 |
|
143 |
-
|
144 |
-
gr.TabbedInterface([mf_transcribe, file_transcribe, yt_transcribe], ["Microphone", "Audio file", "YouTube"])
|
145 |
|
146 |
-
demo
|
|
|
147 |
|
|
|
|
|
|
|
|
|
|
9 |
import tempfile
|
10 |
import os
|
11 |
|
12 |
+
#===============
|
13 |
+
# Define main parameters
|
14 |
+
#===============
|
15 |
+
|
16 |
MODEL_NAME = "openai/whisper-large-v3-turbo"
|
17 |
BATCH_SIZE = 8
|
18 |
FILE_LIMIT_MB = 1000
|
|
|
27 |
device=device,
|
28 |
)
|
29 |
|
30 |
+
#===============
|
31 |
+
# Main functions
|
32 |
+
#===============
|
33 |
|
34 |
@spaces.GPU
|
35 |
def transcribe(inputs, task):
|
36 |
if inputs is None:
|
37 |
raise gr.Error("No audio file submitted! Please upload or record an audio file before submitting your request.")
|
38 |
|
39 |
+
result = pipe(inputs, batch_size=BATCH_SIZE, generate_kwargs={"task": task}, return_timestamps=True)
|
40 |
+
text = result["text"]
|
41 |
return text
|
42 |
|
43 |
|
44 |
+
# def _return_yt_html_embed(yt_url):
|
45 |
+
# video_id = yt_url.split("?v=")[-1]
|
46 |
+
# HTML_str = (
|
47 |
+
# f'<center> <iframe width="500" height="320" src="https://www.youtube.com/embed/{video_id}"> </iframe>'
|
48 |
+
# " </center>"
|
49 |
+
# )
|
50 |
+
# return HTML_str
|
51 |
|
52 |
+
# def download_yt_audio(yt_url, filename):
|
53 |
+
# info_loader = youtube_dl.YoutubeDL()
|
54 |
|
55 |
+
# try:
|
56 |
+
# info = info_loader.extract_info(yt_url, download=False)
|
57 |
+
# except youtube_dl.utils.DownloadError as err:
|
58 |
+
# raise gr.Error(str(err))
|
59 |
|
60 |
+
# file_length = info["duration_string"]
|
61 |
+
# file_h_m_s = file_length.split(":")
|
62 |
+
# file_h_m_s = [int(sub_length) for sub_length in file_h_m_s]
|
63 |
|
64 |
+
# if len(file_h_m_s) == 1:
|
65 |
+
# file_h_m_s.insert(0, 0)
|
66 |
+
# if len(file_h_m_s) == 2:
|
67 |
+
# file_h_m_s.insert(0, 0)
|
68 |
+
# file_length_s = file_h_m_s[0] * 3600 + file_h_m_s[1] * 60 + file_h_m_s[2]
|
69 |
|
70 |
+
# if file_length_s > YT_LENGTH_LIMIT_S:
|
71 |
+
# yt_length_limit_hms = time.strftime("%HH:%MM:%SS", time.gmtime(YT_LENGTH_LIMIT_S))
|
72 |
+
# file_length_hms = time.strftime("%HH:%MM:%SS", time.gmtime(file_length_s))
|
73 |
+
# raise gr.Error(f"Maximum YouTube length is {yt_length_limit_hms}, got {file_length_hms} YouTube video.")
|
74 |
|
75 |
+
# ydl_opts = {"outtmpl": filename, "format": "worstvideo[ext=mp4]+bestaudio[ext=m4a]/best[ext=mp4]/best"}
|
76 |
|
77 |
+
# with youtube_dl.YoutubeDL(ydl_opts) as ydl:
|
78 |
+
# try:
|
79 |
+
# ydl.download([yt_url])
|
80 |
+
# except youtube_dl.utils.ExtractorError as err:
|
81 |
+
# raise gr.Error(str(err))
|
82 |
+
|
83 |
+
# @spaces.GPU
|
84 |
+
# def yt_transcribe(yt_url, task, max_filesize=75.0):
|
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, pipe.feature_extractor.sampling_rate)
|
94 |
+
# inputs = {"array": inputs, "sampling_rate": pipe.feature_extractor.sampling_rate}
|
95 |
+
|
96 |
+
# text = pipe(inputs, batch_size=BATCH_SIZE, generate_kwargs={"task": task}, return_timestamps=True)["text"]
|
97 |
+
|
98 |
+
# return html_embed_str, text
|
99 |
+
|
100 |
+
|
101 |
+
#===============
|
102 |
+
# Build the frontend
|
103 |
+
#===============
|
104 |
+
|
105 |
+
# mf_transcribe = gr.Interface(
|
106 |
+
# fn=transcribe,
|
107 |
+
# inputs=[
|
108 |
+
# gr.Audio(sources="microphone", type="filepath"),
|
109 |
+
# gr.Radio(["transcribe", "translate"], label="Task", value="transcribe"),
|
110 |
+
# ],
|
111 |
+
# outputs="text",
|
112 |
+
# title="Whisper Large V3 Turbo: Transcribe Audio",
|
113 |
+
# description=(
|
114 |
+
# "Transcribe long-form microphone or audio inputs with the click of a button! Demo uses the"
|
115 |
+
# f" checkpoint [{MODEL_NAME}](https://huggingface.co/{MODEL_NAME}) and 🤗 Transformers to transcribe audio files"
|
116 |
+
# " of arbitrary length."
|
117 |
+
# ),
|
118 |
+
# allow_flagging="never",
|
119 |
+
# )
|
120 |
|
121 |
file_transcribe = gr.Interface(
|
122 |
fn=transcribe,
|
|
|
134 |
allow_flagging="never",
|
135 |
)
|
136 |
|
137 |
+
# yt_transcribe = gr.Interface(
|
138 |
+
# fn=yt_transcribe,
|
139 |
+
# inputs=[
|
140 |
+
# gr.Textbox(lines=1, placeholder="Paste the URL to a YouTube video here", label="YouTube URL"),
|
141 |
+
# gr.Radio(["transcribe", "translate"], label="Task", value="transcribe")
|
142 |
+
# ],
|
143 |
+
# outputs=["html", "text"],
|
144 |
+
# title="Whisper Large V3: Transcribe YouTube",
|
145 |
+
# description=(
|
146 |
+
# "Transcribe long-form YouTube videos with the click of a button! Demo uses the checkpoint"
|
147 |
+
# f" [{MODEL_NAME}](https://huggingface.co/{MODEL_NAME}) and 🤗 Transformers to transcribe video files of"
|
148 |
+
# " arbitrary length."
|
149 |
+
# ),
|
150 |
+
# allow_flagging="never",
|
151 |
+
# )
|
152 |
+
|
153 |
+
#===============
|
154 |
+
# Launch
|
155 |
+
#===============
|
156 |
|
157 |
+
demo = gr.Blocks(theme=gr.themes.Ocean())
|
|
|
158 |
|
159 |
+
# with demo:
|
160 |
+
# gr.TabbedInterface([mf_transcribe, file_transcribe, yt_transcribe], ["Microphone", "Audio file", "YouTube"])
|
161 |
|
162 |
+
with demo:
|
163 |
+
gr.TabbedInterface([file_transcribe], ["Audio file"])
|
164 |
+
|
165 |
+
demo.queue().launch(ssr_mode=False)
|