| import gradio as gr |
| import shutil |
| import whisper |
| import os, time |
| model = whisper.load_model("turbo") |
|
|
| destination_language = "fa" |
| |
| from deep_translator import GoogleTranslator |
| from rich.progress import track |
|
|
| gr.set_static_paths(paths=["/home/user/app"]) |
|
|
| def format_time(seconds_float): |
| total_milliseconds = round(seconds_float * 1000) |
| hours, remainder = divmod(abs(total_milliseconds), 3600000) |
| minutes, remainder = divmod(remainder, 60000) |
| seconds, milliseconds = divmod(remainder, 1000) |
|
|
| sign = '-' if seconds_float < 0 else '' |
| return f"{sign}{hours:02d}:{minutes:02d}:{seconds:02d},{milliseconds:03d}" |
|
|
| def ytdown(text): |
| os.system(f'wget -O file {text} ') |
| time.sleep(5) |
| return ['file'] |
| |
| def subtitle(path): |
| shutil.copyfile(path.name, 'video.mp4') |
| result = model.transcribe('video.mp4') |
| f = open("video.srt",'w',encoding='utf-8') |
| for k in track(result['segments'], description="Processing..."): |
| fatxt = GoogleTranslator(source='auto', target=destination_language).translate(k['text']) |
| f.write(f"{k['id']}\n{format_time(k['start'])} --> {format_time(k['end'])}\n{k['text']}\n{fatxt}\n\n") |
| f.close() |
| return ['video.srt'] |
|
|
| def subtitle2(text): |
| os.system(f'wget -O video.mp4 {text} ') |
| result = model.transcribe('video.mp4') |
| f = open("video.srt",'w',encoding='utf-8') |
| for k in track(result['segments'], description="Processing..."): |
| fatxt = GoogleTranslator(source='auto', target=destination_language).translate(k['text']) |
| f.write(f"{k['id']}\n{format_time(k['start'])} --> {format_time(k['end'])}\n{k['text']}\n\n") |
| f.close() |
| return ['video.srt'] |
|
|
| |
|
|
|
|
| with gr.Blocks() as demo: |
| with gr.Tab("Download File"): |
| gr.Markdown("Enter your link") |
| with gr.Row(): |
| inp = gr.Textbox() |
| out = gr.File() |
| btn = gr.Button("Download it") |
| btn.click(fn=ytdown, inputs=inp, outputs=out) |
| with gr.Tab("From Your PC"): |
| gr.Markdown("Upload Video File for Auto Subtitle") |
| with gr.Row(): |
| inp = gr.File(file_types=['.mp4']) |
| out = gr.File() |
| btn = gr.Button("Subtitle it") |
| btn.click(fn=subtitle, inputs=inp, outputs=out) |
| with gr.Tab("From link"): |
| gr.Markdown("Upload Video File for Auto Subtitle") |
| with gr.Row(): |
| inp = gr.Textbox() |
| out = gr.File() |
| btn = gr.Button("Subtitle it") |
| btn.click(fn=subtitle2, inputs=inp, outputs=out) |
| |
| demo.launch(share=True,allowed_paths=['video.srt','file']) |
|
|