Maximo Fernandez
commited on
Commit
·
3a168e7
1
Parent(s):
2eb5a65
Uncomment functions
Browse files
app.py
CHANGED
@@ -6,9 +6,9 @@ import torch
|
|
6 |
from time import sleep
|
7 |
from tqdm import tqdm
|
8 |
from lang_list import union_language_dict
|
9 |
-
#
|
10 |
-
|
11 |
-
|
12 |
from PIL import Image
|
13 |
# import urllib.request
|
14 |
|
@@ -22,17 +22,17 @@ CONCATENATE_TRANSCRIPTIONS = True
|
|
22 |
TRANSLATE_TRANSCRIPTIONS = True
|
23 |
ADD_SUBTITLES_TO_VIDEO = True
|
24 |
REMOVE_FILES = True
|
25 |
-
|
26 |
-
#
|
27 |
-
#
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
#
|
34 |
-
#
|
35 |
-
|
36 |
|
37 |
YOUTUBE = "youtube"
|
38 |
TWITCH = "twitch"
|
@@ -336,315 +336,315 @@ language_dict = union_language_dict()
|
|
336 |
# print("\n\n")
|
337 |
# progress_bar.update(1)
|
338 |
|
339 |
-
|
340 |
-
|
341 |
-
|
342 |
-
|
343 |
-
|
344 |
-
|
345 |
-
|
346 |
-
|
347 |
-
|
348 |
-
|
349 |
-
|
350 |
-
|
351 |
-
|
352 |
-
|
353 |
-
|
354 |
-
|
355 |
-
|
356 |
-
|
357 |
-
|
358 |
-
|
359 |
-
|
360 |
-
|
361 |
-
|
362 |
-
#
|
363 |
-
#
|
364 |
-
|
365 |
-
|
366 |
-
|
367 |
-
|
368 |
-
|
369 |
-
|
370 |
-
|
371 |
-
|
372 |
-
|
373 |
-
|
374 |
-
|
375 |
-
|
376 |
-
|
377 |
-
|
378 |
-
|
379 |
-
|
380 |
-
|
381 |
-
|
382 |
-
|
383 |
-
|
384 |
-
|
385 |
-
|
386 |
-
|
387 |
-
|
388 |
|
389 |
def show_auxiliar_block1():
|
390 |
return gr.Textbox(value="URL checked", visible=False)
|
391 |
|
392 |
-
|
393 |
-
|
394 |
-
|
395 |
-
|
396 |
-
|
397 |
-
|
398 |
-
#
|
399 |
-
#
|
400 |
-
#
|
401 |
-
#
|
402 |
-
|
403 |
-
|
404 |
-
|
405 |
-
|
406 |
-
#
|
407 |
-
#
|
408 |
-
#
|
409 |
-
#
|
410 |
-
|
411 |
-
|
412 |
-
|
413 |
-
|
414 |
-
|
415 |
-
|
416 |
-
|
417 |
-
|
418 |
-
|
419 |
-
|
420 |
-
|
421 |
-
#
|
422 |
-
|
423 |
-
|
424 |
-
|
425 |
-
|
426 |
-
|
427 |
-
|
428 |
-
|
429 |
-
|
430 |
-
|
431 |
-
|
432 |
-
|
433 |
-
|
434 |
-
|
435 |
-
|
436 |
-
|
437 |
-
|
438 |
-
|
439 |
-
|
440 |
-
|
441 |
-
|
442 |
-
|
443 |
|
444 |
-
#
|
445 |
-
|
446 |
-
|
447 |
-
|
448 |
-
|
449 |
-
|
450 |
-
|
451 |
-
|
452 |
-
|
453 |
-
|
454 |
-
|
455 |
|
456 |
-
#
|
457 |
-
|
458 |
-
|
459 |
-
|
460 |
-
|
461 |
-
|
462 |
-
|
463 |
-
|
464 |
-
|
465 |
-
|
466 |
-
|
467 |
-
|
468 |
-
|
469 |
-
|
470 |
-
|
471 |
-
|
472 |
-
|
473 |
-
|
474 |
-
|
475 |
-
|
476 |
-
|
477 |
-
|
478 |
-
|
479 |
-
|
480 |
-
|
481 |
-
|
482 |
-
|
483 |
-
|
484 |
-
|
485 |
-
|
486 |
-
|
487 |
-
|
488 |
-
|
489 |
-
|
490 |
-
|
491 |
-
|
492 |
-
|
493 |
-
|
494 |
-
|
495 |
-
|
496 |
|
497 |
-
|
498 |
-
|
499 |
-
|
500 |
-
|
501 |
-
|
502 |
-
|
503 |
-
|
504 |
-
|
505 |
-
|
506 |
-
|
507 |
-
|
508 |
-
|
509 |
-
|
510 |
-
|
511 |
-
|
512 |
-
|
513 |
-
|
514 |
-
|
515 |
-
|
516 |
-
|
517 |
-
|
518 |
-
|
519 |
-
|
520 |
-
|
521 |
|
522 |
-
|
523 |
-
|
524 |
-
|
525 |
-
|
526 |
-
|
527 |
-
|
528 |
-
|
529 |
-
|
530 |
-
|
531 |
-
|
532 |
-
|
533 |
-
|
534 |
-
|
535 |
-
|
536 |
-
|
537 |
-
|
538 |
-
|
539 |
-
|
540 |
-
|
541 |
-
|
542 |
-
|
543 |
-
|
544 |
-
|
545 |
-
|
546 |
-
|
547 |
-
|
548 |
-
|
549 |
-
|
550 |
-
|
551 |
-
|
552 |
-
|
553 |
-
|
554 |
-
|
555 |
-
|
556 |
-
|
557 |
-
|
558 |
-
|
559 |
-
|
560 |
-
|
561 |
-
|
562 |
-
|
563 |
-
|
564 |
-
|
565 |
-
|
566 |
-
|
567 |
-
|
568 |
-
|
569 |
-
|
570 |
-
|
571 |
-
|
572 |
-
|
573 |
-
|
574 |
-
|
575 |
-
|
576 |
-
|
577 |
-
|
578 |
-
|
579 |
-
|
580 |
-
|
581 |
-
|
582 |
-
|
583 |
-
|
584 |
-
|
585 |
-
|
586 |
-
|
587 |
-
|
588 |
-
|
589 |
-
|
590 |
-
|
591 |
-
|
592 |
-
|
593 |
-
|
594 |
-
|
595 |
-
|
596 |
-
|
597 |
-
|
598 |
-
|
599 |
-
|
600 |
-
|
601 |
-
|
602 |
-
|
603 |
-
|
604 |
-
|
605 |
-
|
606 |
-
|
607 |
-
|
608 |
-
|
609 |
-
|
610 |
-
|
611 |
|
612 |
-
|
613 |
-
|
614 |
-
|
615 |
-
|
616 |
-
|
617 |
-
|
618 |
-
|
619 |
-
|
620 |
-
|
621 |
-
|
622 |
-
|
623 |
-
|
624 |
-
|
625 |
-
|
626 |
-
|
627 |
-
|
628 |
-
|
629 |
-
|
630 |
|
631 |
-
|
632 |
-
|
633 |
-
|
634 |
-
|
635 |
-
|
636 |
-
|
637 |
-
|
638 |
-
|
639 |
-
|
640 |
-
|
641 |
-
|
642 |
-
|
643 |
-
|
644 |
-
|
645 |
-
|
646 |
-
|
647 |
-
|
648 |
|
649 |
@spaces.GPU
|
650 |
def subtify():
|
|
|
6 |
from time import sleep
|
7 |
from tqdm import tqdm
|
8 |
from lang_list import union_language_dict
|
9 |
+
# import pyperclip
|
10 |
+
from pytube import YouTube
|
11 |
+
import re
|
12 |
from PIL import Image
|
13 |
# import urllib.request
|
14 |
|
|
|
22 |
TRANSLATE_TRANSCRIPTIONS = True
|
23 |
ADD_SUBTITLES_TO_VIDEO = True
|
24 |
REMOVE_FILES = True
|
25 |
+
if DEVICE == "cpu":
|
26 |
+
# I supose that I am on huggingface server
|
27 |
+
# Get RAM space
|
28 |
+
ram = int(os.popen("free -m | grep Mem | awk '{print $2}'").read())
|
29 |
+
factor = 1
|
30 |
+
SECONDS = int(ram*factor)
|
31 |
+
print(f"RAM: {ram}, SECONDS: {SECONDS}")
|
32 |
+
else:
|
33 |
+
# I supose that I am on my computer
|
34 |
+
# Get VRAM space
|
35 |
+
SECONDS = 300
|
36 |
|
37 |
YOUTUBE = "youtube"
|
38 |
TWITCH = "twitch"
|
|
|
336 |
# print("\n\n")
|
337 |
# progress_bar.update(1)
|
338 |
|
339 |
+
def remove_all_files():
|
340 |
+
if os.path.exists("audios"):
|
341 |
+
command = f"rm -r audios"
|
342 |
+
os.system(command)
|
343 |
+
if os.path.exists("chunks"):
|
344 |
+
command = f"rm -r chunks"
|
345 |
+
os.system(command)
|
346 |
+
if os.path.exists("concatenated_transcriptions"):
|
347 |
+
command = f"rm -r concatenated_transcriptions"
|
348 |
+
os.system(command)
|
349 |
+
if os.path.exists("transcriptions"):
|
350 |
+
command = f"rm -r transcriptions"
|
351 |
+
os.system(command)
|
352 |
+
if os.path.exists("translated_transcriptions"):
|
353 |
+
command = f"rm -r translated_transcriptions"
|
354 |
+
os.system(command)
|
355 |
+
if os.path.exists("videos"):
|
356 |
+
command = f"rm -r videos"
|
357 |
+
os.system(command)
|
358 |
+
if os.path.exists("vocals"):
|
359 |
+
command = f"rm -r vocals"
|
360 |
+
os.system(command)
|
361 |
+
|
362 |
+
# def paste_url_from_clipboard():
|
363 |
+
# return pyperclip.paste()
|
364 |
+
|
365 |
+
def reset_frontend():
|
366 |
+
visible = False
|
367 |
+
return (
|
368 |
+
"",
|
369 |
+
gr.Image(visible=visible),
|
370 |
+
gr.Dropdown(visible=visible),
|
371 |
+
gr.Dropdown(visible=visible),
|
372 |
+
gr.Dropdown(visible=visible),
|
373 |
+
gr.Accordion(visible=visible),
|
374 |
+
gr.Button(visible=visible),
|
375 |
+
gr.Textbox(visible=visible),
|
376 |
+
gr.Textbox(visible=visible),
|
377 |
+
gr.Textbox(visible=visible),
|
378 |
+
gr.Textbox(visible=visible),
|
379 |
+
gr.Textbox(visible=visible),
|
380 |
+
gr.Textbox(visible=visible),
|
381 |
+
gr.Textbox(visible=visible),
|
382 |
+
gr.Textbox(visible=visible),
|
383 |
+
gr.Textbox(visible=visible),
|
384 |
+
gr.Textbox(visible=visible),
|
385 |
+
gr.Textbox(visible=visible),
|
386 |
+
gr.Video(visible=visible),
|
387 |
+
)
|
388 |
|
389 |
def show_auxiliar_block1():
|
390 |
return gr.Textbox(value="URL checked", visible=False)
|
391 |
|
392 |
+
def get_youtube_thumbnail(url):
|
393 |
+
yt = YouTube(url)
|
394 |
+
thumbnail_url = yt.thumbnail_url
|
395 |
+
return thumbnail_url
|
396 |
+
|
397 |
+
def is_valid_youtube_url(url):
|
398 |
+
# This regular expression should match the following YouTube URL formats:
|
399 |
+
# - https://youtube.com/watch?v=video_id
|
400 |
+
# - https://www.youtube.com/watch?v=video_id
|
401 |
+
# - https://youtu.be/video_id
|
402 |
+
patron_youtube = r'(https?://)?(www\.)?(youtube\.com/watch\?v=|youtu\.be/)[\w-]+'
|
403 |
+
return bool(re.match(patron_youtube, url))
|
404 |
+
|
405 |
+
def is_valid_twitch_url(url):
|
406 |
+
# This regular expression should match the following Twitch URL formats:
|
407 |
+
# - https://twitch.tv/channel_name
|
408 |
+
# - https://www.twitch.tv/channel_name
|
409 |
+
# - https://twitch.tv/videos/video_id
|
410 |
+
twitch_pattern = r'(https?://)?(www\.)?twitch\.tv/(videos/\d+|\w+)'
|
411 |
+
return bool(re.match(twitch_pattern, url))
|
412 |
+
|
413 |
+
def is_valid_url(url):
|
414 |
+
num_speaker = [10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
|
415 |
+
source_languaje = gr.Dropdown(visible=True, label="Source languaje", show_label=True, value="English", choices=language_dict, scale=1, interactive=True)
|
416 |
+
target_languaje = gr.Dropdown(visible=True, label="Target languaje", show_label=True, value="Español", choices=language_dict, scale=1, interactive=True)
|
417 |
+
advanced_setings = gr.Accordion(visible=True)
|
418 |
+
number_of_speakers = gr.Dropdown(visible=True, label="Number of speakers", show_label=True, value=10, choices=num_speaker, scale=1, interactive=True)
|
419 |
+
subtify_button = gr.Button(size="lg", value="subtify", min_width="10px", scale=0, visible=True)
|
420 |
+
|
421 |
+
# Youtube
|
422 |
+
if "youtube" in url.lower() or "youtu.be" in url.lower():
|
423 |
+
if is_valid_youtube_url(url):
|
424 |
+
thumbnail = get_youtube_thumbnail(url)
|
425 |
+
if thumbnail:
|
426 |
+
return (
|
427 |
+
gr.Image(value=thumbnail, visible=True, show_download_button=False, container=False),
|
428 |
+
source_languaje,
|
429 |
+
target_languaje,
|
430 |
+
advanced_setings,
|
431 |
+
number_of_speakers,
|
432 |
+
subtify_button,
|
433 |
+
)
|
434 |
+
else:
|
435 |
+
return (
|
436 |
+
gr.Image(value="assets/youtube-no-thumbnails.webp", visible=True, show_download_button=False, container=False),
|
437 |
+
source_languaje,
|
438 |
+
target_languaje,
|
439 |
+
advanced_setings,
|
440 |
+
number_of_speakers,
|
441 |
+
subtify_button,
|
442 |
+
)
|
443 |
|
444 |
+
# Twitch
|
445 |
+
elif "twitch" in url.lower() or "twitch.tv" in url.lower():
|
446 |
+
if is_valid_twitch_url(url):
|
447 |
+
return (
|
448 |
+
gr.Image(value="assets/twitch.webp", visible=True, show_download_button=False, container=False),
|
449 |
+
source_languaje,
|
450 |
+
target_languaje,
|
451 |
+
advanced_setings,
|
452 |
+
number_of_speakers,
|
453 |
+
subtify_button,
|
454 |
+
)
|
455 |
|
456 |
+
# Error
|
457 |
+
visible = False
|
458 |
+
image = gr.Image(value="assets/youtube_error.webp", visible=visible, show_download_button=False, container=False)
|
459 |
+
source_languaje = gr.Dropdown(visible=visible, label="Source languaje", show_label=True, value="English", choices=language_dict, scale=1, interactive=True)
|
460 |
+
target_languaje = gr.Dropdown(visible=visible, label="Target languaje", show_label=True, value="Español", choices=language_dict, scale=1, interactive=True)
|
461 |
+
advanced_setings = gr.Accordion(visible=visible)
|
462 |
+
number_of_speakers = gr.Dropdown(visible=visible, label="Number of speakers", show_label=True, value=10, choices=num_speaker, scale=1, interactive=True)
|
463 |
+
subtify_button = gr.Button(size="lg", value="subtify", min_width="10px", scale=0, visible=visible)
|
464 |
+
return (
|
465 |
+
image,
|
466 |
+
source_languaje,
|
467 |
+
target_languaje,
|
468 |
+
advanced_setings,
|
469 |
+
number_of_speakers,
|
470 |
+
subtify_button,
|
471 |
+
)
|
472 |
+
|
473 |
+
def change_visibility_texboxes():
|
474 |
+
|
475 |
+
return (
|
476 |
+
gr.Textbox(value="Done"),
|
477 |
+
gr.Textbox(visible=True),
|
478 |
+
gr.Textbox(visible=True),
|
479 |
+
gr.Textbox(visible=True),
|
480 |
+
gr.Textbox(visible=True),
|
481 |
+
gr.Textbox(visible=True),
|
482 |
+
gr.Textbox(visible=True),
|
483 |
+
gr.Textbox(visible=False),
|
484 |
+
)
|
485 |
+
|
486 |
+
def get_audio_and_video_from_video(url):
|
487 |
+
print('*'*NUMBER)
|
488 |
+
print(f"Downloading video and audio from {url}")
|
489 |
+
|
490 |
+
audios_folder = "audios"
|
491 |
+
videos_folder = "videos"
|
492 |
+
if not os.path.exists(audios_folder):
|
493 |
+
os.makedirs(audios_folder)
|
494 |
+
if not os.path.exists(videos_folder):
|
495 |
+
os.makedirs(videos_folder)
|
496 |
|
497 |
+
python_file = "download.py"
|
498 |
+
command = f"python {python_file} {url}"
|
499 |
+
os.system(command)
|
500 |
+
sleep(1)
|
501 |
+
|
502 |
+
audio = "audios/download_audio.mp3"
|
503 |
+
video = "videos/download_video.mp4"
|
504 |
+
|
505 |
+
return (
|
506 |
+
gr.Textbox(value="Ok"),
|
507 |
+
gr.Textbox(value=audio),
|
508 |
+
gr.Textbox(value=video),
|
509 |
+
)
|
510 |
+
|
511 |
+
def slice_audio(audio_path):
|
512 |
+
print('*'*NUMBER)
|
513 |
+
print("Slicing audio")
|
514 |
+
|
515 |
+
folder_vocals = "vocals"
|
516 |
+
folder_chunck = "chunks"
|
517 |
+
if not os.path.exists(folder_vocals):
|
518 |
+
os.makedirs(folder_vocals)
|
519 |
+
if not os.path.exists(folder_chunck):
|
520 |
+
os.makedirs(folder_chunck)
|
521 |
|
522 |
+
python_file = "slice_audio.py"
|
523 |
+
command = f"python {python_file} {audio_path} {SECONDS}"
|
524 |
+
os.system(command)
|
525 |
+
|
526 |
+
return (
|
527 |
+
gr.Textbox(value="Ok")
|
528 |
+
)
|
529 |
+
|
530 |
+
def trascribe_audio(source_languaje, number_of_speakers):
|
531 |
+
print('*'*NUMBER)
|
532 |
+
print("Transcript slices")
|
533 |
+
|
534 |
+
folder_chunks = "chunks"
|
535 |
+
python_file = "transcribe.py"
|
536 |
+
chunks_file = "chunks/output_files.txt"
|
537 |
+
command = f"python {python_file} {chunks_file} {source_languaje} {number_of_speakers} {DEVICE}"
|
538 |
+
os.system(command)
|
539 |
+
|
540 |
+
with open(chunks_file, 'r') as f:
|
541 |
+
files = f.read().splitlines()
|
542 |
+
for file in files:
|
543 |
+
audios_extension = "mp3"
|
544 |
+
file_name, _ = file.split(".")
|
545 |
+
_, file_name = file_name.split("/")
|
546 |
+
vocal = f'{folder_chunks}/{file_name}.{audios_extension}'
|
547 |
+
command = f"rm {vocal}"
|
548 |
+
os.system(command)
|
549 |
+
|
550 |
+
return (
|
551 |
+
gr.Textbox(value="Ok")
|
552 |
+
)
|
553 |
+
|
554 |
+
def concatenate_transcriptions():
|
555 |
+
print('*'*NUMBER)
|
556 |
+
print("Concatenate transcriptions")
|
557 |
+
|
558 |
+
folder_concatenated = "concatenated_transcriptions"
|
559 |
+
if not os.path.exists(folder_concatenated):
|
560 |
+
os.makedirs(folder_concatenated)
|
561 |
+
|
562 |
+
chunck_file = "chunks/output_files.txt"
|
563 |
+
python_file = "concat_transcriptions.py"
|
564 |
+
command = f"python {python_file} {chunck_file} {SECONDS}"
|
565 |
+
os.system(command)
|
566 |
+
|
567 |
+
with open(chunck_file, 'r') as f:
|
568 |
+
files = f.read().splitlines()
|
569 |
+
for file in files:
|
570 |
+
file_name, _ = file.split(".")
|
571 |
+
_, file_name = file_name.split("/")
|
572 |
+
transcriptions_folder = "transcriptions"
|
573 |
+
transcription_extension = "srt"
|
574 |
+
command = f"rm {transcriptions_folder}/{file_name}.{transcription_extension}"
|
575 |
+
os.system(command)
|
576 |
+
|
577 |
+
audio_transcribed = "concatenated_transcriptions/download_audio.srt"
|
578 |
+
|
579 |
+
return (
|
580 |
+
gr.Textbox(value="Ok"),
|
581 |
+
gr.Textbox(value=audio_transcribed),
|
582 |
+
)
|
583 |
+
|
584 |
+
def translate_transcription(original_audio_transcribed_path, source_languaje, target_languaje):
|
585 |
+
print('*'*NUMBER)
|
586 |
+
print("Translate transcription")
|
587 |
+
|
588 |
+
folder_translated_transcriptions = "translated_transcriptions"
|
589 |
+
if not os.path.exists(folder_translated_transcriptions):
|
590 |
+
os.makedirs(folder_translated_transcriptions)
|
591 |
+
|
592 |
+
python_file = "translate_transcriptions.py"
|
593 |
+
command = f"python {python_file} {original_audio_transcribed_path} --source_languaje {source_languaje} --target_languaje {target_languaje} --device {DEVICE}"
|
594 |
+
os.system(command)
|
595 |
+
|
596 |
+
translated_transcription = f"translated_transcriptions/download_audio_{target_languaje}.srt"
|
597 |
+
|
598 |
+
transcription_file = "concatenated_transcriptions/download_audio.srt"
|
599 |
+
if os.path.exists(transcription_file):
|
600 |
+
command = f"rm {transcription_file}"
|
601 |
+
os.system(command)
|
602 |
+
|
603 |
+
return (
|
604 |
+
gr.Textbox(value="Ok"),
|
605 |
+
gr.Textbox(value=translated_transcription)
|
606 |
+
)
|
607 |
+
|
608 |
+
def add_translated_subtitles_to_video(original_video_path, original_audio_path, original_audio_translated_path):
|
609 |
+
print('*'*NUMBER)
|
610 |
+
print("Add subtitles to video")
|
611 |
|
612 |
+
python_file = "add_subtitles_to_video.py"
|
613 |
+
command = f"python {python_file} {original_audio_translated_path} {original_video_path} {original_audio_path}"
|
614 |
+
os.system(command)
|
615 |
+
|
616 |
+
if os.path.exists(original_video_path):
|
617 |
+
command = f"rm {original_video_path}"
|
618 |
+
os.system(command)
|
619 |
+
if os.path.exists(original_audio_path):
|
620 |
+
command = f"rm {original_audio_path}"
|
621 |
+
os.system(command)
|
622 |
+
if os.path.exists(original_audio_translated_path):
|
623 |
+
command = f"rm {original_audio_translated_path}"
|
624 |
+
os.system(command)
|
625 |
+
if os.path.exists("chunks/output_files.txt"):
|
626 |
+
command = f"rm chunks/output_files.txt"
|
627 |
+
os.system(command)
|
628 |
+
|
629 |
+
subtitled_video = "videos/download_video_with_subtitles.mp4"
|
630 |
|
631 |
+
visible = False
|
632 |
+
return (
|
633 |
+
gr.Video(value=subtitled_video, visible=True),
|
634 |
+
gr.Textbox(value="Ok", visible=visible),
|
635 |
+
gr.Textbox(value="Ok"),
|
636 |
+
)
|
637 |
+
|
638 |
+
def hide_textbobes_progress_info():
|
639 |
+
visible = False
|
640 |
+
return (
|
641 |
+
gr.Textbox(value="Waiting", visible=visible),
|
642 |
+
gr.Textbox(value="Waiting", visible=visible),
|
643 |
+
gr.Textbox(value="Waiting", visible=visible),
|
644 |
+
gr.Textbox(value="Waiting", visible=visible),
|
645 |
+
gr.Textbox(value="Waiting", visible=visible),
|
646 |
+
gr.Textbox(value="Waiting", visible=visible),
|
647 |
+
)
|
648 |
|
649 |
@spaces.GPU
|
650 |
def subtify():
|