Spaces:
Runtime error
Runtime error
Delete app.py
Browse files
app.py
DELETED
@@ -1,95 +0,0 @@
|
|
1 |
-
import gradio as gr
|
2 |
-
import os
|
3 |
-
import sys
|
4 |
-
import subprocess
|
5 |
-
import zlib
|
6 |
-
from typing import Iterator, TextIO
|
7 |
-
# from moviepy.editor import VideoFileClip
|
8 |
-
|
9 |
-
import whisper
|
10 |
-
from transformers import AutoProcessor, AutoModelForSpeechSeq2Seq, pipeline
|
11 |
-
|
12 |
-
#pipe = pipeline(model="tilos/whisper-small-zh-HK")
|
13 |
-
|
14 |
-
processor = AutoProcessor.from_pretrained("tilos/whisper-small-zh-HK")
|
15 |
-
#model = AutoModelForSpeechSeq2Seq.from_pretrained("tilos/whisper-small-zh-HK")
|
16 |
-
|
17 |
-
model = whisper.load_model("medium")
|
18 |
-
|
19 |
-
def format_timestamp(seconds: float, always_include_hours: bool = False, decimal_marker: str = '.'):
|
20 |
-
assert seconds >= 0, "non-negative timestamp expected"
|
21 |
-
milliseconds = round(seconds * 1000.0)
|
22 |
-
|
23 |
-
hours = milliseconds // 3_600_000
|
24 |
-
milliseconds -= hours * 3_600_000
|
25 |
-
|
26 |
-
minutes = milliseconds // 60_000
|
27 |
-
milliseconds -= minutes * 60_000
|
28 |
-
|
29 |
-
seconds = milliseconds // 1_000
|
30 |
-
milliseconds -= seconds * 1_000
|
31 |
-
|
32 |
-
hours_marker = f"{hours:02d}:" if always_include_hours or hours > 0 else ""
|
33 |
-
return f"{hours_marker}{minutes:02d}:{seconds:02d}{decimal_marker}{milliseconds:03d}"
|
34 |
-
|
35 |
-
def write_vtt(transcript: Iterator[dict], file: TextIO):
|
36 |
-
print("WEBVTT\n", file=file)
|
37 |
-
for segment in transcript:
|
38 |
-
print(
|
39 |
-
f"{format_timestamp(segment['start'])} --> {format_timestamp(segment['end'])}\n"
|
40 |
-
f"{segment['text'].strip().replace('-->', '->')}\n",
|
41 |
-
file=file,
|
42 |
-
flush=True,
|
43 |
-
)
|
44 |
-
|
45 |
-
|
46 |
-
def video2mp3(video_file, output_ext="mp3"):
|
47 |
-
filename, ext = os.path.splitext(video_file)
|
48 |
-
subprocess.call(["ffmpeg", "-y", "-i", video_file, f"{filename}.{output_ext}"],
|
49 |
-
stdout=subprocess.DEVNULL,
|
50 |
-
stderr=subprocess.STDOUT)
|
51 |
-
return f"{filename}.{output_ext}"
|
52 |
-
|
53 |
-
|
54 |
-
def translate(input_video):
|
55 |
-
audio_file = video2mp3(input_video)
|
56 |
-
|
57 |
-
options = dict(beam_size=5, best_of=5)
|
58 |
-
translate_options = dict(task="translate", **options)
|
59 |
-
result = model.transcribe(audio_file, **translate_options) #
|
60 |
-
|
61 |
-
output_dir = '/content/'
|
62 |
-
audio_path = audio_file.split(".")[0]
|
63 |
-
|
64 |
-
with open(os.path.join(output_dir, audio_path + ".vtt"), "w") as vtt:
|
65 |
-
write_vtt(result["segments"], file=vtt)
|
66 |
-
|
67 |
-
subtitle = audio_path + ".vtt"
|
68 |
-
output_video = audio_path + "_subtitled.mp4"
|
69 |
-
|
70 |
-
os.system(f"ffmpeg -i {input_video} -vf subtitles={subtitle} {output_video}")
|
71 |
-
|
72 |
-
return output_video
|
73 |
-
|
74 |
-
|
75 |
-
title = "Add Text/Caption to your YouTube Shorts - MultiLingual"
|
76 |
-
|
77 |
-
block = gr.Blocks()
|
78 |
-
|
79 |
-
with block:
|
80 |
-
with gr.Group():
|
81 |
-
with gr.Box():
|
82 |
-
with gr.Row().style():
|
83 |
-
inp_video = gr.Video(
|
84 |
-
label="Input Video",
|
85 |
-
type="filepath",
|
86 |
-
mirror_webcam=False
|
87 |
-
)
|
88 |
-
op_video = gr.Video()
|
89 |
-
btn = gr.Button("Generate Subtitle Video")
|
90 |
-
|
91 |
-
btn.click(translate, inputs=[inp_video], outputs=[op_video])
|
92 |
-
|
93 |
-
gr.HTML()
|
94 |
-
|
95 |
-
block.launch(debug=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|