import gradio import gradio as gr from download import download_generator import anvil.media import os import dotenv from whisper.tokenizer import LANGUAGES, TO_LANGUAGE_CODE from utils.apis import render_api_elements from utils.utils import get_args dotenv.load_dotenv() anvil.server.connect(os.environ.get('ANVIL_UPLINK_KEY')) queue_placeholder = None args = get_args() gradio_share: bool = args.get("public") model_size: str = args.get("model") preload_model: str = args.get("preload") LANG_CHOICES = sorted([x.capitalize() for x in LANGUAGES.values()]) LANG_CHOICES.insert(0, "Autodetect") url_input = gr.Textbox(label="Youtube/Twitter/etc video URL (supports many services)", value='https://twitter.com/starsonxh/status/1552945347194142720', lines=1, elem_id="url_input") # download_status = gr.Textbox(label="Status:", value='', lines=1, elem_id="download_status") download_status = gr.Checkbox(label="", elem_id="download_status", interactive=False) translate_action = gr.Checkbox(label="Auto translate to english", elem_id='translate_toggle', interactive=True, value=True) init_video = gr.Video(label="Upload video manually", visible=True, interactive=True, mirror_webcam=False) init_audio = gr.Audio(label="Downloaded audio", visible=False) output_text = gr.Textbox(label="Output text", lines=5, visible=False, max_lines=10, interactive=True, elem_id="output_text") sub_video = gr.Video(label="Subbed video", visible=False, mirror_webcam=False) def predownload(url, translate_action, source_language): files = [] for response in download_generator(url, translate_action, source_language): updates_object = {} updates_object[download_status] = gr.update(label=f"{response.get('message')}") meta = response.get('meta') if 'video' in response: updates_object[init_video] = gr.update(visible=True, value=response["video"], label=f"Init Video: {meta['id']}.{meta['ext']}") updates_object[init_audio] = gr.update(visible=True, value=response["audio"], label=f"Extracted audio : {meta['id']}.mp3") files.append(response["video"]) files.append(response["audio"]) if 'whisper_result' in response: updates_object[output_text] = gr.update(value=response['whisper_result'].get('srt'), visible=True, label=f"Subtitles transcribed from {response['whisper_result'].get('language')} (detected language)") if 'srt_path' in response: files.append(response["srt_path"]) if 'sub_video' in response: updates_object[sub_video] = gr.update(visible=True, value=response["sub_video"], label=f"Subbed video: {meta['id']}_translated.mp4") files.append(response["sub_video"]) updates_object[output_file] = gr.update(value=files, visible=len(files) > 0, label=f"Output Files") yield updates_object def correct_subtitles(url, output_text): for response in download_generator(url, corrected_subtitles=output_text): updates_object = {} updates_object[download_status] = gr.update(label=f"STATUS: {response.get('message')}") if 'sub_video' in response: updates_object[sub_video] = gr.update(visible=True, value=response["sub_video"], label=f"Corrected subtitles") yield updates_object subtitled_video = False with gr.Blocks(css='@import "file=static/css/main.css";', theme='darkpeach', title='Vid Translator Studio') as demo: gr.HTML('