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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import torch
|
3 |
+
from time import sleep
|
4 |
+
from tqdm import tqdm
|
5 |
+
import argparse
|
6 |
+
import gradio as gr
|
7 |
+
from lang_list import union_language_dict
|
8 |
+
# import pyperclip
|
9 |
+
from pytube import YouTube
|
10 |
+
import re
|
11 |
+
|
12 |
+
NUMBER = 100
|
13 |
+
DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
|
14 |
+
DOWNLOAD = False
|
15 |
+
SLICE_AUDIO = False
|
16 |
+
SEPARE_VOCALS = False
|
17 |
+
TRANSCRIBE_AUDIO = False
|
18 |
+
CONCATENATE_TRANSCRIPTIONS = False
|
19 |
+
TRANSLATE_TRANSCRIPTIONS = False
|
20 |
+
ADD_SUBTITLES_TO_VIDEO = False
|
21 |
+
REMOVE_FILES = False
|
22 |
+
REMOVE_ALL = True
|
23 |
+
if SEPARE_VOCALS:
|
24 |
+
SECONDS = 150
|
25 |
+
else:
|
26 |
+
SECONDS = 300
|
27 |
+
|
28 |
+
YOUTUBE = "youtube"
|
29 |
+
TWITCH = "twitch"
|
30 |
+
ERROR = "error"
|
31 |
+
|
32 |
+
language_dict = union_language_dict()
|
33 |
+
|
34 |
+
def subtify_no_ui():
|
35 |
+
number_works = 7
|
36 |
+
progress_bar = tqdm(total=number_works, desc="Subtify")
|
37 |
+
|
38 |
+
################## Download video and audio ##################
|
39 |
+
if DOWNLOAD:
|
40 |
+
print('*'*NUMBER)
|
41 |
+
# url = "https://www.twitch.tv/videos/1936119752" # twitch Rob Mula 2 horas
|
42 |
+
# url = "https://www.youtube.com/watch?v=yX5EJf4R77s" # ✅ debate, varios hablantes, 3 minutos
|
43 |
+
# url = "https://www.youtube.com/watch?v=cgx0QnXo1OU" # ✅ smart home, un solo hablante, 4:42 minutos
|
44 |
+
url = "https://www.youtube.com/watch?v=dgOBxhi19T8" # ✅ rob mula, muchos hablantes, 4:28 minutos
|
45 |
+
# url = "https://www.youtube.com/watch?v=Coj72EzmX20" # rob mula, un solo hablante, 16 minutos
|
46 |
+
# url = "https://www.youtube.com/watch?v=Tqth0fKo0_g" # Conversación short
|
47 |
+
print(f"Downloading video and audio from {url}")
|
48 |
+
python_file = "download.py"
|
49 |
+
command = f"python {python_file} {url}"
|
50 |
+
os.system(command)
|
51 |
+
sleep(5)
|
52 |
+
print('*'*NUMBER)
|
53 |
+
print("\n\n")
|
54 |
+
progress_bar.update(1)
|
55 |
+
|
56 |
+
################## Slice audio ##################
|
57 |
+
if SLICE_AUDIO:
|
58 |
+
print('*'*NUMBER)
|
59 |
+
print("Slicing audio")
|
60 |
+
python_file = "slice_audio.py"
|
61 |
+
audio = "audios/download_audio.mp3"
|
62 |
+
command = f"python {python_file} {audio} {SECONDS}"
|
63 |
+
os.system(command)
|
64 |
+
print('*'*NUMBER)
|
65 |
+
print("\n\n")
|
66 |
+
progress_bar.update(1)
|
67 |
+
|
68 |
+
################## Get vocals ##################
|
69 |
+
chunck_file = "chunks/output_files.txt"
|
70 |
+
print('*'*NUMBER)
|
71 |
+
if SEPARE_VOCALS:
|
72 |
+
print("Get vocals")
|
73 |
+
python_file = "separe_vocals.py"
|
74 |
+
command = f"python {python_file} {chunck_file} {DEVICE}"
|
75 |
+
os.system(command)
|
76 |
+
if REMOVE_FILES:
|
77 |
+
with open(chunck_file, 'r') as f:
|
78 |
+
files = f.read().splitlines()
|
79 |
+
for file in files:
|
80 |
+
command = f"rm {file}"
|
81 |
+
os.system(command)
|
82 |
+
else:
|
83 |
+
print("Moving chunks")
|
84 |
+
folder_vocals = "vocals"
|
85 |
+
folder_chunck = "chunks"
|
86 |
+
with open(f"{folder_vocals}/speakers.txt", 'w') as f:
|
87 |
+
f.write(str(0))
|
88 |
+
if REMOVE_FILES:
|
89 |
+
command = f"mv {folder_chunck}/*.mp3 {folder_vocals}/"
|
90 |
+
os.system(command)
|
91 |
+
else:
|
92 |
+
command = f"cp {folder_chunck}/*.mp3 {folder_vocals}/"
|
93 |
+
os.system(command)
|
94 |
+
print('*'*NUMBER)
|
95 |
+
print("\n\n")
|
96 |
+
progress_bar.update(1)
|
97 |
+
|
98 |
+
################# Transcript vocals ##################
|
99 |
+
speakers_file = "vocals/speakers.txt"
|
100 |
+
if TRANSCRIBE_AUDIO:
|
101 |
+
print('*'*NUMBER)
|
102 |
+
print("Transcript vocals")
|
103 |
+
python_file = "transcribe.py"
|
104 |
+
language = "English"
|
105 |
+
command = f"python {python_file} {chunck_file} {language} {speakers_file} {DEVICE} {not SEPARE_VOCALS}"
|
106 |
+
os.system(command)
|
107 |
+
if REMOVE_FILES:
|
108 |
+
vocals_folder = "vocals"
|
109 |
+
with open(chunck_file, 'r') as f:
|
110 |
+
files = f.read().splitlines()
|
111 |
+
with open(speakers_file, 'r') as f:
|
112 |
+
speakers = f.read().splitlines()
|
113 |
+
speakers = int(speakers[0])
|
114 |
+
for file in files:
|
115 |
+
if speakers > 0:
|
116 |
+
vocals_extension = "wav"
|
117 |
+
for i in range(speakers):
|
118 |
+
file_name, _ = file.split(".")
|
119 |
+
_, file_name = file_name.split("/")
|
120 |
+
vocal = f'{vocals_folder}/{file_name}_speaker{i:003d}.{vocals_extension}'
|
121 |
+
command = f"rm {vocal}"
|
122 |
+
os.system(command)
|
123 |
+
else:
|
124 |
+
vocals_extension = "mp3"
|
125 |
+
file_name, _ = file.split(".")
|
126 |
+
_, file_name = file_name.split("/")
|
127 |
+
vocal = f'{vocals_folder}/{file_name}.{vocals_extension}'
|
128 |
+
command = f"rm {vocal}"
|
129 |
+
os.system(command)
|
130 |
+
print('*'*NUMBER)
|
131 |
+
print("\n\n")
|
132 |
+
progress_bar.update(1)
|
133 |
+
|
134 |
+
################## Concatenate transcriptions ##################
|
135 |
+
if CONCATENATE_TRANSCRIPTIONS:
|
136 |
+
print('*'*NUMBER)
|
137 |
+
print("Concatenate transcriptions")
|
138 |
+
python_file = "concat_transcriptions.py"
|
139 |
+
command = f"python {python_file} {chunck_file} {SECONDS} {speakers_file}"
|
140 |
+
os.system(command)
|
141 |
+
if REMOVE_FILES:
|
142 |
+
with open(chunck_file, 'r') as f:
|
143 |
+
files = f.read().splitlines()
|
144 |
+
for file in files:
|
145 |
+
file_name, _ = file.split(".")
|
146 |
+
_, file_name = file_name.split("/")
|
147 |
+
transcriptions_folder = "transcriptions"
|
148 |
+
transcription_extension = "srt"
|
149 |
+
command = f"rm {transcriptions_folder}/{file_name}.{transcription_extension}"
|
150 |
+
os.system(command)
|
151 |
+
print('*'*NUMBER)
|
152 |
+
print("\n\n")
|
153 |
+
progress_bar.update(1)
|
154 |
+
|
155 |
+
################## Translate transcription ##################
|
156 |
+
target_languaje = "Español"
|
157 |
+
if TRANSLATE_TRANSCRIPTIONS:
|
158 |
+
print('*'*NUMBER)
|
159 |
+
print("Translate transcription")
|
160 |
+
transcription_file = "concatenated_transcriptions/download_audio.srt"
|
161 |
+
source_languaje = "English"
|
162 |
+
python_file = "translate_transcriptions.py"
|
163 |
+
command = f"python {python_file} {transcription_file} --source_languaje {source_languaje} --target_languaje {target_languaje} --device {DEVICE}"
|
164 |
+
os.system(command)
|
165 |
+
if REMOVE_FILES:
|
166 |
+
command = f"rm {transcription_file}"
|
167 |
+
os.system(command)
|
168 |
+
print('*'*NUMBER)
|
169 |
+
print("\n\n")
|
170 |
+
progress_bar.update(1)
|
171 |
+
|
172 |
+
################## Add subtitles to video ##################
|
173 |
+
if ADD_SUBTITLES_TO_VIDEO:
|
174 |
+
print('*'*NUMBER)
|
175 |
+
print("Add subtitles to video")
|
176 |
+
python_file = "add_subtitles_to_video.py"
|
177 |
+
transcription_file = f"translated_transcriptions/download_audio_{target_languaje}.srt"
|
178 |
+
input_video_file = "videos/download_video.mp4"
|
179 |
+
input_audio_file = "audios/download_audio.mp3"
|
180 |
+
command = f"python {python_file} {transcription_file} {input_video_file} {input_audio_file}"
|
181 |
+
os.system(command)
|
182 |
+
if REMOVE_FILES:
|
183 |
+
command = f"rm {input_video_file}"
|
184 |
+
os.system(command)
|
185 |
+
command = f"rm {input_audio_file}"
|
186 |
+
os.system(command)
|
187 |
+
command = f"rm {transcription_file}"
|
188 |
+
os.system(command)
|
189 |
+
command = f"rm chunks/output_files.txt"
|
190 |
+
os.system(command)
|
191 |
+
command = f"rm vocals/speakers.txt"
|
192 |
+
os.system(command)
|
193 |
+
print('*'*NUMBER)
|
194 |
+
print("\n\n")
|
195 |
+
progress_bar.update(1)
|
196 |
+
|
197 |
+
################## Remove all ##################
|
198 |
+
if REMOVE_ALL:
|
199 |
+
command = f"rm audios/*"
|
200 |
+
os.system(command)
|
201 |
+
command = f"rm chunks/*"
|
202 |
+
os.system(command)
|
203 |
+
command = f"rm concatenated_transcriptions/*"
|
204 |
+
os.system(command)
|
205 |
+
command = f"rm transcriptions/*"
|
206 |
+
os.system(command)
|
207 |
+
command = f"rm translated_transcriptions/*"
|
208 |
+
os.system(command)
|
209 |
+
# Check if videos/download_video.mp4 exists
|
210 |
+
if os.path.isfile("videos/download_video.mp4"):
|
211 |
+
command = f"rm videos/download_video.mp4"
|
212 |
+
os.system(command)
|
213 |
+
# command = f"rm videos/*"
|
214 |
+
# os.system(command)
|
215 |
+
command = f"rm vocals/*"
|
216 |
+
os.system(command)
|
217 |
+
|
218 |
+
# def copy_url_from_clipboard():
|
219 |
+
# return pyperclip.paste()
|
220 |
+
|
221 |
+
def clear_video_url():
|
222 |
+
visible = False
|
223 |
+
image = gr.Image(visible=visible, scale=1)
|
224 |
+
source_languaje = gr.Dropdown(visible=visible, label="Source languaje", show_label=True, value="English", choices=language_dict, scale=1, interactive=True)
|
225 |
+
target_languaje = gr.Dropdown(visible=visible, label="Target languaje", show_label=True, value="Español", choices=language_dict, scale=1, interactive=True)
|
226 |
+
translate_button = gr.Button(size="lg", value="translate", min_width="10px", scale=0, visible=visible)
|
227 |
+
original_audio = gr.Audio(label="Original audio", elem_id="original_audio", visible=visible, interactive=False)
|
228 |
+
original_audio_transcribed = gr.Textbox(label="Original audio transcribed", elem_id="original_audio_transcribed", interactive=False, visible=visible)
|
229 |
+
original_audio_translated = gr.Textbox(label="Original audio translated", elem_id="original_audio_translated", interactive=False, visible=visible)
|
230 |
+
return (
|
231 |
+
"",
|
232 |
+
image,
|
233 |
+
source_languaje,
|
234 |
+
target_languaje,
|
235 |
+
translate_button,
|
236 |
+
original_audio,
|
237 |
+
original_audio_transcribed,
|
238 |
+
original_audio_translated,
|
239 |
+
)
|
240 |
+
|
241 |
+
def get_youtube_thumbnail(url):
|
242 |
+
yt = YouTube(url)
|
243 |
+
thumbnail_url = yt.thumbnail_url
|
244 |
+
return thumbnail_url
|
245 |
+
|
246 |
+
def is_valid_youtube_url(url):
|
247 |
+
patron_youtube = r'(https?://)?(www\.)?(youtube\.com/watch\?v=|youtu\.be/)[\w-]+'
|
248 |
+
if not re.match(patron_youtube, url):
|
249 |
+
return False
|
250 |
+
return True
|
251 |
+
|
252 |
+
def is_valid_url(url):
|
253 |
+
source_languaje = gr.Dropdown(visible=True, label="Source languaje", show_label=True, value="English", choices=language_dict, scale=1, interactive=True)
|
254 |
+
target_languaje = gr.Dropdown(visible=True, label="Target languaje", show_label=True, value="Español", choices=language_dict, scale=1, interactive=True)
|
255 |
+
translate_button = gr.Button(size="lg", value="translate", min_width="10px", scale=0, visible=True)
|
256 |
+
original_audio = gr.Audio(label="Original audio", elem_id="original_audio", visible=True, interactive=False)
|
257 |
+
original_audio_transcribed = gr.Textbox(label="Original audio transcribed", elem_id="original_audio_transcribed", interactive=False, visible=True)
|
258 |
+
original_audio_translated = gr.Textbox(label="Original audio translated", elem_id="original_audio_translated", interactive=False, visible=True)
|
259 |
+
subtitled_video = gr.Video(label="Subtitled video", elem_id="subtitled_video", visible=True, interactive=False)
|
260 |
+
|
261 |
+
# Youtube
|
262 |
+
if "youtube" in url.lower() or "youtu.be" in url.lower():
|
263 |
+
if is_valid_youtube_url(url):
|
264 |
+
thumbnail = get_youtube_thumbnail(url)
|
265 |
+
if thumbnail:
|
266 |
+
return (
|
267 |
+
gr.Image(value=thumbnail, visible=True, show_download_button=False, container=False),
|
268 |
+
source_languaje,
|
269 |
+
target_languaje,
|
270 |
+
translate_button,
|
271 |
+
gr.Textbox(value=YOUTUBE, label="Stream page", elem_id="stream_page", visible=False),
|
272 |
+
original_audio,
|
273 |
+
original_audio_transcribed,
|
274 |
+
original_audio_translated,
|
275 |
+
subtitled_video
|
276 |
+
)
|
277 |
+
else:
|
278 |
+
return (
|
279 |
+
gr.Image(value="assets/youtube-no-thumbnails.webp", visible=True, show_download_button=False, container=False),
|
280 |
+
source_languaje,
|
281 |
+
target_languaje,
|
282 |
+
translate_button,
|
283 |
+
gr.Textbox(value=YOUTUBE, label="Stream page", elem_id="stream_page", visible=False),
|
284 |
+
original_audio,
|
285 |
+
original_audio_transcribed,
|
286 |
+
original_audio_translated,
|
287 |
+
subtitled_video
|
288 |
+
)
|
289 |
+
|
290 |
+
# Twitch
|
291 |
+
elif "twitch" in url.lower() or "twitch.tv" in url.lower():
|
292 |
+
return (
|
293 |
+
gr.Image(value="assets/twitch.webp", visible=True, show_download_button=False, container=False),
|
294 |
+
source_languaje,
|
295 |
+
target_languaje,
|
296 |
+
translate_button,
|
297 |
+
gr.Textbox(value=TWITCH, label="Stream page", elem_id="stream_page", visible=False),
|
298 |
+
original_audio,
|
299 |
+
original_audio_transcribed,
|
300 |
+
original_audio_translated,
|
301 |
+
subtitled_video
|
302 |
+
)
|
303 |
+
|
304 |
+
# Error
|
305 |
+
visible = False
|
306 |
+
image = gr.Image(value="assets/youtube_error.webp", visible=visible, show_download_button=False, container=False)
|
307 |
+
source_languaje = gr.Dropdown(visible=visible, label="Source languaje", show_label=True, value="English", choices=language_dict, scale=1, interactive=True)
|
308 |
+
target_languaje = gr.Dropdown(visible=visible, label="Target languaje", show_label=True, value="Español", choices=language_dict, scale=1, interactive=True)
|
309 |
+
translate_button = gr.Button(size="lg", value="translate", min_width="10px", scale=0, visible=visible)
|
310 |
+
stream_page = gr.Textbox(value=ERROR, label="Stream page", elem_id="stream_page", visible=visible)
|
311 |
+
original_audio = gr.Audio(label="Original audio", elem_id="original_audio", visible=visible, interactive=False)
|
312 |
+
original_audio_transcribed = gr.Textbox(label="Original audio transcribed", elem_id="original_audio_transcribed", interactive=False, visible=visible)
|
313 |
+
original_audio_translated = gr.Textbox(label="Original audio translated", elem_id="original_audio_translated", interactive=False, visible=visible)
|
314 |
+
subtitled_video = gr.Video(label="Subtitled video", elem_id="subtitled_video", visible=visible, interactive=False)
|
315 |
+
return (
|
316 |
+
image,
|
317 |
+
source_languaje,
|
318 |
+
target_languaje,
|
319 |
+
translate_button,
|
320 |
+
stream_page,
|
321 |
+
original_audio,
|
322 |
+
original_audio_transcribed,
|
323 |
+
original_audio_translated,
|
324 |
+
subtitled_video
|
325 |
+
)
|
326 |
+
|
327 |
+
def get_audio_and_video_from_video(url, stream_page):
|
328 |
+
python_file = "download.py"
|
329 |
+
command = f"python {python_file} {url}"
|
330 |
+
os.system(command)
|
331 |
+
# sleep(5)
|
332 |
+
|
333 |
+
audio = "audios/download_audio.mp3"
|
334 |
+
video = "videos/download_video.mp4"
|
335 |
+
|
336 |
+
return (
|
337 |
+
gr.Audio(value=audio, label="Original audio", elem_id="original_audio", visible=True, interactive=False),
|
338 |
+
gr.Textbox(value=audio, label="Original audio path", elem_id="original_audio_path", visible=False),
|
339 |
+
gr.Textbox(value=video, label="Original video path", elem_id="original_video_path", visible=False)
|
340 |
+
)
|
341 |
+
|
342 |
+
def trascribe_audio(audio_path, source_languaje):
|
343 |
+
python_file = "slice_audio.py"
|
344 |
+
command = f"python {python_file} {audio_path} {SECONDS}"
|
345 |
+
os.system(command)
|
346 |
+
|
347 |
+
folder_vocals = "vocals"
|
348 |
+
folder_chunck = "chunks"
|
349 |
+
with open(f"{folder_vocals}/speakers.txt", 'w') as f:
|
350 |
+
f.write(str(0))
|
351 |
+
command = f"mv {folder_chunck}/*.mp3 {folder_vocals}/"
|
352 |
+
os.system(command)
|
353 |
+
|
354 |
+
python_file = "transcribe.py"
|
355 |
+
chunck_file = "chunks/output_files.txt"
|
356 |
+
speakers_file = "vocals/speakers.txt"
|
357 |
+
command = f"python {python_file} {chunck_file} {source_languaje} {speakers_file} {DEVICE} {not SEPARE_VOCALS}"
|
358 |
+
os.system(command)
|
359 |
+
with open(chunck_file, 'r') as f:
|
360 |
+
files = f.read().splitlines()
|
361 |
+
with open(speakers_file, 'r') as f:
|
362 |
+
speakers = f.read().splitlines()
|
363 |
+
speakers = int(speakers[0])
|
364 |
+
for file in files:
|
365 |
+
if speakers > 0:
|
366 |
+
vocals_extension = "wav"
|
367 |
+
for i in range(speakers):
|
368 |
+
file_name, _ = file.split(".")
|
369 |
+
_, file_name = file_name.split("/")
|
370 |
+
vocal = f'{folder_vocals}/{file_name}_speaker{i:003d}.{vocals_extension}'
|
371 |
+
command = f"rm {vocal}"
|
372 |
+
os.system(command)
|
373 |
+
else:
|
374 |
+
vocals_extension = "mp3"
|
375 |
+
file_name, _ = file.split(".")
|
376 |
+
_, file_name = file_name.split("/")
|
377 |
+
vocal = f'{folder_vocals}/{file_name}.{vocals_extension}'
|
378 |
+
command = f"rm {vocal}"
|
379 |
+
os.system(command)
|
380 |
+
|
381 |
+
python_file = "concat_transcriptions.py"
|
382 |
+
command = f"python {python_file} {chunck_file} {SECONDS} {speakers_file}"
|
383 |
+
os.system(command)
|
384 |
+
with open(chunck_file, 'r') as f:
|
385 |
+
files = f.read().splitlines()
|
386 |
+
for file in files:
|
387 |
+
file_name, _ = file.split(".")
|
388 |
+
_, file_name = file_name.split("/")
|
389 |
+
transcriptions_folder = "transcriptions"
|
390 |
+
transcription_extension = "srt"
|
391 |
+
command = f"rm {transcriptions_folder}/{file_name}.{transcription_extension}"
|
392 |
+
os.system(command)
|
393 |
+
|
394 |
+
audio_transcribed = "concatenated_transcriptions/download_audio.srt"
|
395 |
+
with open(audio_transcribed, 'r') as f:
|
396 |
+
result = f.read()
|
397 |
+
|
398 |
+
return (
|
399 |
+
result,
|
400 |
+
gr.Textbox(value=audio_transcribed, label="Original audio transcribed", elem_id="original_audio_transcribed", visible=False)
|
401 |
+
)
|
402 |
+
|
403 |
+
def translate_transcription(original_audio_transcribed_path, source_languaje, target_languaje):
|
404 |
+
python_file = "translate_transcriptions.py"
|
405 |
+
command = f"python {python_file} {original_audio_transcribed_path} --source_languaje {source_languaje} --target_languaje {target_languaje} --device {DEVICE}"
|
406 |
+
os.system(command)
|
407 |
+
|
408 |
+
translated_transcription = f"translated_transcriptions/download_audio_{target_languaje}.srt"
|
409 |
+
with open(translated_transcription, 'r') as f:
|
410 |
+
result = f.read()
|
411 |
+
transcription_file = "concatenated_transcriptions/download_audio.srt"
|
412 |
+
command = f"rm {transcription_file}"
|
413 |
+
os.system(command)
|
414 |
+
|
415 |
+
return (
|
416 |
+
result,
|
417 |
+
gr.Textbox(value=translated_transcription, label="Original audio translated", elem_id="original_audio_translated", visible=False)
|
418 |
+
)
|
419 |
+
|
420 |
+
def add_translated_subtitles_to_video(original_video_path, original_audio_path, original_audio_translated_path):
|
421 |
+
python_file = "add_subtitles_to_video.py"
|
422 |
+
command = f"python {python_file} {original_audio_translated_path} {original_video_path} {original_audio_path}"
|
423 |
+
os.system(command)
|
424 |
+
|
425 |
+
command = f"rm {original_video_path}"
|
426 |
+
os.system(command)
|
427 |
+
command = f"rm {original_audio_path}"
|
428 |
+
os.system(command)
|
429 |
+
command = f"rm {original_audio_translated_path}"
|
430 |
+
os.system(command)
|
431 |
+
command = f"rm chunks/output_files.txt"
|
432 |
+
os.system(command)
|
433 |
+
command = f"rm vocals/speakers.txt"
|
434 |
+
os.system(command)
|
435 |
+
|
436 |
+
subtitled_video = "videos/download_video_with_subtitles.mp4"
|
437 |
+
|
438 |
+
return gr.Video(value=subtitled_video, label="Subtitled video", elem_id="subtitled_video", visible=True, interactive=False)
|
439 |
+
|
440 |
+
def subtify():
|
441 |
+
with gr.Blocks() as demo:
|
442 |
+
# Layout
|
443 |
+
with gr.Row(variant="panel"):
|
444 |
+
url_textbox = gr.Textbox(placeholder="Add video URL here", label="Video URL", elem_id="video_url", scale=1, interactive=True)
|
445 |
+
copy_button = gr.Button(size="sm", icon="icons/copy.svg", value="", min_width="10px", scale=0)
|
446 |
+
delete_button = gr.Button(size="sm", icon="icons/delete.svg", value="", min_width="10px", scale=0)
|
447 |
+
|
448 |
+
stream_page = gr.Textbox(label="Stream page", elem_id="stream_page", visible=False)
|
449 |
+
visible = False
|
450 |
+
with gr.Row(equal_height=False):
|
451 |
+
image = gr.Image(visible=visible, scale=1)
|
452 |
+
with gr.Column():
|
453 |
+
with gr.Row():
|
454 |
+
source_languaje = gr.Dropdown(visible=visible, label="Source languaje", show_label=True, value="English", choices=language_dict, scale=1, interactive=True)
|
455 |
+
target_languaje = gr.Dropdown(visible=visible, label="Target languaje", show_label=True, value="Español", choices=language_dict, scale=1, interactive=True)
|
456 |
+
with gr.Row():
|
457 |
+
subtify_button = gr.Button(size="lg", value="subtify", min_width="10px", scale=0, visible=visible)
|
458 |
+
|
459 |
+
original_audio = gr.Audio(label="Original audio", elem_id="original_audio", visible=visible, interactive=False)
|
460 |
+
original_audio_path = gr.Textbox(label="Original audio path", elem_id="original_audio_path", visible=False)
|
461 |
+
original_video_path = gr.Textbox(label="Original video path", elem_id="original_video_path", visible=False)
|
462 |
+
original_audio_transcribed = gr.Textbox(label="Original audio transcribed", elem_id="original_audio_transcribed", interactive=False, visible=visible)
|
463 |
+
original_audio_transcribed_path = gr.Textbox(label="Original audio transcribed", elem_id="original_audio_transcribed", visible=False)
|
464 |
+
original_audio_translated = gr.Textbox(label="Original audio translated", elem_id="original_audio_translated", interactive=False, visible=visible)
|
465 |
+
original_audio_translated_path = gr.Textbox(label="Original audio translated", elem_id="original_audio_translated", visible=False)
|
466 |
+
subtitled_video = gr.Video(label="Subtitled video", elem_id="subtitled_video", visible=visible, interactive=False)
|
467 |
+
|
468 |
+
# Events
|
469 |
+
# copy_button.click(fn=copy_url_from_clipboard, outputs=url_textbox)
|
470 |
+
delete_button.click(
|
471 |
+
fn=clear_video_url,
|
472 |
+
outputs=[
|
473 |
+
url_textbox,
|
474 |
+
image,
|
475 |
+
source_languaje,
|
476 |
+
target_languaje,
|
477 |
+
subtify_button,
|
478 |
+
original_audio,
|
479 |
+
original_audio_transcribed,
|
480 |
+
original_audio_translated,
|
481 |
+
]
|
482 |
+
)
|
483 |
+
url_textbox.change(
|
484 |
+
fn=is_valid_url,
|
485 |
+
inputs=url_textbox,
|
486 |
+
outputs=[
|
487 |
+
image,
|
488 |
+
source_languaje,
|
489 |
+
target_languaje,
|
490 |
+
subtify_button,
|
491 |
+
stream_page,
|
492 |
+
original_audio,
|
493 |
+
original_audio_transcribed,
|
494 |
+
original_audio_translated,
|
495 |
+
subtitled_video
|
496 |
+
]
|
497 |
+
)
|
498 |
+
subtify_button.click(fn=get_audio_and_video_from_video, inputs=[url_textbox, stream_page], outputs=[original_audio, original_audio_path, original_video_path])
|
499 |
+
original_audio.change(fn=trascribe_audio, inputs=[original_audio_path, source_languaje], outputs=[original_audio_transcribed, original_audio_transcribed_path])
|
500 |
+
original_audio_transcribed.change(fn=translate_transcription, inputs=[original_audio_transcribed_path, source_languaje, target_languaje], outputs=[original_audio_translated, original_audio_translated_path])
|
501 |
+
original_audio_translated.change(fn=add_translated_subtitles_to_video, inputs=[original_video_path, original_audio_path, original_audio_translated_path], outputs=subtitled_video)
|
502 |
+
|
503 |
+
|
504 |
+
demo.launch()
|
505 |
+
|
506 |
+
|
507 |
+
if __name__ == "__main__":
|
508 |
+
parser = argparse.ArgumentParser()
|
509 |
+
parser.add_argument("--no_ui", action="store_true")
|
510 |
+
args = parser.parse_args()
|
511 |
+
|
512 |
+
if args.no_ui:
|
513 |
+
subtify_no_ui()
|
514 |
+
else:
|
515 |
+
subtify()
|