Spaces:
Build error
Build error
File size: 8,425 Bytes
b21ecef b4cbbec b21ecef 2e0131e 6fd7ef3 d70c7fd cb23f19 b21ecef 352eb01 d70c7fd 6fd7ef3 b42bc15 6fd7ef3 352eb01 f1f3adb 352eb01 4d8662a cb23f19 2e0131e 805009b 2e0131e b21ecef cb23f19 749c554 5556030 6fd7ef3 cb23f19 2e0131e b21ecef 2e0131e b21ecef cb23f19 6fd7ef3 cb23f19 6fd7ef3 cb23f19 6fd7ef3 cb23f19 6fd7ef3 cb23f19 2e0131e 805009b cb23f19 6fd7ef3 805009b cb23f19 6fd7ef3 cb23f19 6fd7ef3 cb23f19 7de403b 6fd7ef3 cb23f19 6fd7ef3 749c554 cb23f19 6fd7ef3 cb23f19 6fd7ef3 cb23f19 2e0131e cb23f19 2e0131e cb23f19 5556030 cb23f19 6fd7ef3 5556030 2e0131e cb23f19 2e0131e cb23f19 2e0131e cb23f19 b21ecef 7db5fdc 6fd7ef3 3f43bed 6fd7ef3 b21ecef 7ce50df |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 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 76 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 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 |
import gradio
import gradio as gr
from download import download_generator, user_uploaded_video_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")
VIDEO_HTML = """
<video
class="video-js"
controls
preload="auto"
width="640"
height="264"
>
<source src="{src}" type="video/mp4">
<track kind="captions" src="{en_vtt}" srclang="en" label="English" default>
</video>
"""
url_input = gr.Textbox(label="Youtube/Twitter/etc video URL (supports many services)", 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")
output_text_2 = gr.Textbox(label="Output text 2", 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)
sub_video_html = gr.HTML(value=f"<div> Please wait for video to load </div>")
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 'vtt_path' in response:
files.append(response["vtt_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")
updates_object[sub_video_html] = gr.update(value=VIDEO_HTML.format(src=f"file={response['sub_video']}", en_vtt=f"file={response['vtt_path']}") )
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('<h1 class="main-title">VidTranslator Studio 0.1</h1>')
gr.HTML("<h2 class='secondary'>Automatic social media video translation from 99 languages</h2>")
with gr.Row(elem_id="input_row"):
with gr.Group() as group:
url_input.render()
action_btn = gr.Button(elem_id='submit', variant='primary', value="Translate")
gr.StatusTracker()
with gr.Row(elem_id="second_row"):
source_language = gr.Dropdown(choices=LANG_CHOICES,
label="Source Language",
value='Autodetect',
interactive=True, elem_id="source_language")
download_status.render()
translate_action.render()
with gr.Row():
with gr.Column():
init_video.render()
init_audio.render()
with gr.Row():
output_file = gr.Files(label="Output Files", visible=False)
with gr.Column():
output_text.render()
correct_btn = gr.Button("Correct subtitles")
with gr.Column():
sub_video.render()
sub_video_html.render()
outputs = [download_status, init_video, init_audio, output_text, sub_video, output_file, sub_video_html]
inputs = [url_input, translate_action, source_language]
action_btn.click(fn=predownload, inputs=inputs, outputs=outputs, api_name='predownload')
url_input.submit(fn=predownload, inputs=inputs, outputs=outputs)
correct_btn.click(fn=correct_subtitles, inputs=[url_input, output_text], outputs=[download_status, output_text, sub_video])
translate_action.change(fn=lambda x: {action_btn: gr.update(value=f"Translate" if x else "Transcribe")},
inputs=[translate_action], outputs=[action_btn])
examples = gr.Examples([["https://twitter.com/starsonxh/status/1552945347194142720", "Adam"], ["https://twitter.com/starsonxh/status/1552945347194142720", "Eve"]], [url_input, output_text] )
gr.HTML("""<div class='footer'>
<div class="relative">
<div class="absolute inset-0 flex items-center" aria-hidden="true">
<div class="w-full border-t border-gray-300"></div>
</div>
<div class="relative flex justify-center">
<span class="bg-white px-2 text-sm text-gray-500">Continue</span>
</div>
</div>
<a href='https://twitter.com/altryne/'><img src='https://img.shields.io/badge/%40altryne-follow-green' alt=''></a>
</div>""")
def init_video_manual_upload(url, init_video):
if url:
return False
files = []
for response in user_uploaded_video_generator(init_video):
updates_object = {}
updates_object[download_status] = gr.update(label=f"{response.get('message')}")
if 'audio' in response:
updates_object[init_audio] = gr.update(visible=True, value=response["audio"],
label=f"Extracted audio")
files.append(response["audio"])
files.append(response["video"])
if 'srt_path' in response:
updates_object[output_text] = gr.update(value=response['srt_path'], visible=True)
files.append(response["srt_path"])
updates_object[sub_video_html] = gr.update(value=VIDEO_HTML % f"file={response['sub_video']}")
if 'vtt_path' in response:
updates_object[output_text_2] = gr.update(value=response['vtt_path'], visible=True)
files.append(response["vtt_path"])
updates_object[sub_video_html] = gr.update(value=VIDEO_HTML.format(src=f"file={response['sub_video']}", en_vtt=f"file={response['vtt_path']}"))
#
# updates_object[output_file] = gr.update(value=files, visible=len(files) > 0, label=f"Output Files")
yield updates_object
init_video.change(fn=init_video_manual_upload,
inputs=[url_input, init_video],
outputs=[download_status, init_audio, sub_video_html, output_file])
# Render imported buttons for API bindings
render_api_elements(url_input,download_status, output_text, sub_video, output_file)
queue_placeholder = demo.queue()
if __name__ == "__main__":
gradio.close_all()
port = os.environ.get('SERVER_PORT', 8111)
demo.launch(show_error=True, debug=True, share=gradio_share,server_port=int(port), favicon_path='fonts/icon.png') |