subtify / url_manager.py
Maximofn's picture
Refactor project structure and update dependencies
e015c08
raw
history blame
3.97 kB
import re
import gradio as gr
from pytube import YouTube
from lang_list import union_language_dict
language_dict = union_language_dict()
def get_youtube_thumbnail(url):
yt = YouTube(url)
thumbnail_url = yt.thumbnail_url
return thumbnail_url
def is_valid_youtube_url(url):
# This regular expression should match the following YouTube URL formats:
# - https://youtube.com/watch?v=video_id
# - https://www.youtube.com/watch?v=video_id
# - https://youtu.be/video_id
patron_youtube = r'(https?://)?(www\.)?(youtube\.com/watch\?v=|youtu\.be/)[\w-]+'
return bool(re.match(patron_youtube, url))
def is_valid_twitch_url(url):
# This regular expression should match the following Twitch URL formats:
# - https://twitch.tv/channel_name
# - https://www.twitch.tv/channel_name
# - https://twitch.tv/videos/video_id
twitch_pattern = r'(https?://)?(www\.)?twitch\.tv/(videos/\d+|\w+)'
return bool(re.match(twitch_pattern, url))
def is_valid_url(url):
num_speaker = [10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
source_languaje = gr.Dropdown(visible=True, label="Source languaje", show_label=True, value="English", choices=language_dict, scale=1, interactive=True)
target_languaje = gr.Dropdown(visible=True, label="Target languaje", show_label=True, value="Español", choices=language_dict, scale=1, interactive=True)
advanced_setings = gr.Accordion(visible=True)
number_of_speakers = gr.Dropdown(visible=True, label="Number of speakers", show_label=True, value=10, choices=num_speaker, scale=1, interactive=True)
subtify_button = gr.Button(size="lg", value="subtify", min_width="10px", scale=0, visible=True)
# Youtube
if "youtube" in url.lower() or "youtu.be" in url.lower():
if is_valid_youtube_url(url):
thumbnail = get_youtube_thumbnail(url)
if thumbnail:
return (
gr.Image(value=thumbnail, visible=True, show_download_button=False, container=False),
source_languaje,
target_languaje,
advanced_setings,
number_of_speakers,
subtify_button,
)
else:
return (
gr.Image(value="assets/youtube-no-thumbnails.webp", visible=True, show_download_button=False, container=False),
source_languaje,
target_languaje,
advanced_setings,
number_of_speakers,
subtify_button,
)
# Twitch
elif "twitch" in url.lower() or "twitch.tv" in url.lower():
if is_valid_twitch_url(url):
return (
gr.Image(value="assets/twitch.webp", visible=True, show_download_button=False, container=False),
source_languaje,
target_languaje,
advanced_setings,
number_of_speakers,
subtify_button,
)
# Error
visible = False
image = gr.Image(value="assets/youtube_error.webp", visible=visible, show_download_button=False, container=False)
source_languaje = gr.Dropdown(visible=visible, label="Source languaje", show_label=True, value="English", choices=language_dict, scale=1, interactive=True)
target_languaje = gr.Dropdown(visible=visible, label="Target languaje", show_label=True, value="Español", choices=language_dict, scale=1, interactive=True)
advanced_setings = gr.Accordion(visible=visible)
number_of_speakers = gr.Dropdown(visible=visible, label="Number of speakers", show_label=True, value=10, choices=num_speaker, scale=1, interactive=True)
subtify_button = gr.Button(size="lg", value="subtify", min_width="10px", scale=0, visible=visible)
return (
image,
source_languaje,
target_languaje,
advanced_setings,
number_of_speakers,
subtify_button,
)