|
import gradio as gr |
|
from utils import process_video |
|
|
|
|
|
language_map = { |
|
"English": None, |
|
"Hindi": "Helsinki-NLP/opus-mt-en-hi", |
|
"Spanish": "Helsinki-NLP/opus-mt-en-es", |
|
"French": "Helsinki-NLP/opus-mt-en-fr", |
|
"German": "Helsinki-NLP/opus-mt-en-de", |
|
"Telugu": "facebook/nllb-200-distilled-600M", |
|
"Portuguese": "Helsinki-NLP/opus-mt-en-pt", |
|
"Russian": "Helsinki-NLP/opus-mt-en-ru", |
|
"Chinese": "Helsinki-NLP/opus-mt-en-zh", |
|
"Arabic": "Helsinki-NLP/opus-mt-en-ar", |
|
"Japanese": "Helsinki-NLP/opus-mt-en-jap" |
|
} |
|
|
|
|
|
css = """ |
|
body { |
|
background-color: #1a1a1a; |
|
color: #e0e0e0; |
|
font-family: 'Arial', sans-serif; |
|
} |
|
.gradio-container { |
|
max-width: 1200px; |
|
margin: 0 auto; |
|
padding: 20px; |
|
border-radius: 10px; |
|
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1); |
|
} |
|
.file-preview { |
|
border: 2px dashed #6c757d; |
|
padding: 20px; |
|
border-radius: 10px; |
|
} |
|
.progress-text { |
|
font-size: 16px; |
|
color: #28a745; |
|
animation: blink 1s infinite; |
|
} |
|
@keyframes blink { |
|
50% { opacity: 0.5; } |
|
} |
|
""" |
|
|
|
|
|
with gr.Blocks(theme=gr.themes.Monochrome(), css=css) as demo: |
|
gr.Markdown("# π₯ AI-Powered Video Subtitling") |
|
gr.Markdown("Upload a video (MP4/MKV/AVI) and select a language to generate subtitles.") |
|
|
|
with gr.Row(): |
|
with gr.Column(scale=2): |
|
video_input = gr.File( |
|
label="Upload Video File", |
|
file_types=["mp4", "mkv", "avi"], |
|
elem_classes=["file-preview"] |
|
) |
|
with gr.Column(scale=1): |
|
language_dropdown = gr.Dropdown( |
|
choices=list(language_map.keys()), |
|
label="Select Subtitle Language", |
|
value="English" |
|
) |
|
|
|
generate_button = gr.Button("Generate Subtitles π") |
|
progress_text = gr.Textbox( |
|
label="Progress", |
|
interactive=False, |
|
elem_classes=["progress-text"] |
|
) |
|
output_srt = gr.File(label="Download Subtitles") |
|
|
|
def generate_subtitles(video_file, language): |
|
try: |
|
|
|
if not video_file.name.lower().endswith(('.mp4', '.mkv', '.avi')): |
|
return None, "β Invalid file type. Please upload an MP4, MKV, or AVI file." |
|
|
|
|
|
progress = "π Processing video..." |
|
yield None, progress |
|
|
|
|
|
srt_path = process_video(video_file.name, language) |
|
if srt_path: |
|
yield gr.File(srt_path), "β
Subtitles generated successfully!" |
|
else: |
|
yield None, "β Error during processing. Check logs." |
|
except Exception as e: |
|
yield None, f"β Error: {str(e)}" |
|
|
|
generate_button.click( |
|
generate_subtitles, |
|
inputs=[video_input, language_dropdown], |
|
outputs=[output_srt, progress_text] |
|
) |
|
|
|
demo.launch() |