Spaces:
Build error
Build error
Alex Volkov
commited on
Commit
·
5556030
1
Parent(s):
749c554
Ok seems to work, let's see
Browse files- app.py +9 -6
- download.py +4 -4
app.py
CHANGED
@@ -23,15 +23,15 @@ url_input = gr.Textbox(label="Youtube/Twitter/etc video URL (supports many servi
|
|
23 |
# download_status = gr.Textbox(label="Status:", value='', lines=1, elem_id="download_status")
|
24 |
download_status = gr.Checkbox(label="Status:", elem_id="download_status", interactive=False)
|
25 |
translate_action = gr.Checkbox(label="Auto translate to english", elem_id='translate_toggle', interactive=True, value=True)
|
26 |
-
init_video = gr.Video(label="Upload video manually", visible=True, interactive=True)
|
27 |
init_audio = gr.Audio(label="Downloaded audio", visible=False)
|
28 |
output_text = gr.Textbox(label="Output text", lines=5, visible=False, max_lines=10, interactive=True)
|
29 |
sub_video = gr.Video(label="Subbed video", visible=False, mirror_webcam=False)
|
30 |
|
31 |
|
32 |
|
33 |
-
def predownload(url):
|
34 |
-
for response in download_generator(url):
|
35 |
updates_object = {}
|
36 |
updates_object[download_status] = gr.update(label=f"STATUS: {response.get('message')}")
|
37 |
meta = response.get('meta')
|
@@ -112,11 +112,14 @@ with gr.Blocks(css=css+"") as demo:
|
|
112 |
gr.Button("Download srt file")
|
113 |
rebake = gr.Button("Edit subtitles on video")
|
114 |
|
115 |
-
|
116 |
-
|
117 |
-
|
|
|
118 |
rebake.click(fn=rebake, inputs=[output_text, sub_video], outputs=[download_status, output_text, sub_video])
|
119 |
|
|
|
|
|
120 |
|
121 |
# Render imported buttons for API bindings
|
122 |
render_api_elements(url_input,download_status, output_text, sub_video)
|
|
|
23 |
# download_status = gr.Textbox(label="Status:", value='', lines=1, elem_id="download_status")
|
24 |
download_status = gr.Checkbox(label="Status:", elem_id="download_status", interactive=False)
|
25 |
translate_action = gr.Checkbox(label="Auto translate to english", elem_id='translate_toggle', interactive=True, value=True)
|
26 |
+
init_video = gr.Video(label="Upload video manually", visible=True, interactive=True, mirror_webcam=False)
|
27 |
init_audio = gr.Audio(label="Downloaded audio", visible=False)
|
28 |
output_text = gr.Textbox(label="Output text", lines=5, visible=False, max_lines=10, interactive=True)
|
29 |
sub_video = gr.Video(label="Subbed video", visible=False, mirror_webcam=False)
|
30 |
|
31 |
|
32 |
|
33 |
+
def predownload(url, translate_action):
|
34 |
+
for response in download_generator(url, translate_action):
|
35 |
updates_object = {}
|
36 |
updates_object[download_status] = gr.update(label=f"STATUS: {response.get('message')}")
|
37 |
meta = response.get('meta')
|
|
|
112 |
gr.Button("Download srt file")
|
113 |
rebake = gr.Button("Edit subtitles on video")
|
114 |
|
115 |
+
outputs = [download_status, init_video, init_audio, output_text, sub_video]
|
116 |
+
inputs = [url_input, translate_action]
|
117 |
+
action_btn.click(fn=predownload, inputs=inputs, outputs=outputs, api_name='predownload')
|
118 |
+
url_input.submit(fn=predownload, inputs=inputs, outputs=outputs)
|
119 |
rebake.click(fn=rebake, inputs=[output_text, sub_video], outputs=[download_status, output_text, sub_video])
|
120 |
|
121 |
+
translate_action.change(fn=lambda x: {action_btn: gr.update(value=f"Translate" if x else "Transcribe")},
|
122 |
+
inputs=[translate_action], outputs=[action_btn])
|
123 |
|
124 |
# Render imported buttons for API bindings
|
125 |
render_api_elements(url_input,download_status, output_text, sub_video)
|
download.py
CHANGED
@@ -33,7 +33,7 @@ def download_generator(url, translate_action=True):
|
|
33 |
yield {"message": f"Checking {url} for videos"}
|
34 |
try:
|
35 |
meta = check_download(url)
|
36 |
-
print(json.dumps(meta, indent=2))
|
37 |
if(meta['duration'] > 159):
|
38 |
raise Exception("Video is too long, please use videos less than 159 seconds")
|
39 |
yield {"message": f"Found video with {meta['duration']} seconds duration from {meta['extractor']}", "meta": meta}
|
@@ -57,7 +57,7 @@ def download_generator(url, translate_action=True):
|
|
57 |
### Step 3 : Transcribe with whisper
|
58 |
yield {"message": f"[PLEASE WAIT] Starting whisper transcribe with {meta['id']}.mp3"}
|
59 |
try:
|
60 |
-
whisper_result = transcribe(audio)
|
61 |
srt_path = tempdir / f"{meta['id']}.srt"
|
62 |
with open(srt_path, "w", encoding="utf-8") as srt:
|
63 |
write_srt(whisper_result["segments"], file=srt)
|
@@ -151,11 +151,11 @@ def check_download(url):
|
|
151 |
return meta
|
152 |
|
153 |
def transcribe(audio, translate_action=True):
|
154 |
-
print('Starting transcribe
|
155 |
global model
|
156 |
if not preload_model:
|
157 |
model = whisper.load_model(model_size)
|
158 |
-
output = model.transcribe(audio, task="translate" if translate_action else "transcribe"
|
159 |
output["language"] = LANGUAGES[output["language"]]
|
160 |
output['segments'] = [{"id": 0, "seek": 0, "start": 0.0, "end": 3, "text": " [AI translation by @vidtranslator]"}] + output['segments']
|
161 |
print(f'Finished transcribe from {output["language"]}', output["text"])
|
|
|
33 |
yield {"message": f"Checking {url} for videos"}
|
34 |
try:
|
35 |
meta = check_download(url)
|
36 |
+
# print(json.dumps(meta, indent=2))
|
37 |
if(meta['duration'] > 159):
|
38 |
raise Exception("Video is too long, please use videos less than 159 seconds")
|
39 |
yield {"message": f"Found video with {meta['duration']} seconds duration from {meta['extractor']}", "meta": meta}
|
|
|
57 |
### Step 3 : Transcribe with whisper
|
58 |
yield {"message": f"[PLEASE WAIT] Starting whisper transcribe with {meta['id']}.mp3"}
|
59 |
try:
|
60 |
+
whisper_result = transcribe(audio, translate_action)
|
61 |
srt_path = tempdir / f"{meta['id']}.srt"
|
62 |
with open(srt_path, "w", encoding="utf-8") as srt:
|
63 |
write_srt(whisper_result["segments"], file=srt)
|
|
|
151 |
return meta
|
152 |
|
153 |
def transcribe(audio, translate_action=True):
|
154 |
+
print(f'Starting ' + "translate" if translate_action else "transcribe")
|
155 |
global model
|
156 |
if not preload_model:
|
157 |
model = whisper.load_model(model_size)
|
158 |
+
output = model.transcribe(audio, task="translate" if translate_action else "transcribe")
|
159 |
output["language"] = LANGUAGES[output["language"]]
|
160 |
output['segments'] = [{"id": 0, "seek": 0, "start": 0.0, "end": 3, "text": " [AI translation by @vidtranslator]"}] + output['segments']
|
161 |
print(f'Finished transcribe from {output["language"]}', output["text"])
|