File size: 3,261 Bytes
ed41184 11a77dd ed41184 11a77dd cea609f 2670e49 cea609f 2670e49 fc7755f 2670e49 cea609f 2670e49 cea609f 2670e49 cea609f 2670e49 fc7755f ed41184 fc7755f ed41184 fc7755f 11a77dd fc7755f 2670e49 cea609f 2670e49 fc7755f ed41184 fc7755f e219dd2 2670e49 6147286 b00f500 e219dd2 2670e49 cea609f 2670e49 e219dd2 cea609f 2670e49 e219dd2 2670e49 a360176 2670e49 e219dd2 fc7755f ed41184 fc7755f ed41184 |
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 |
import gradio as gr
from utils import process_video
# Define supported languages
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"
}
# Custom CSS for a professional look
css = """
body {
background-color: #1a1a1a;
color: #e0e0e0;
font-family: 'Segoe UI', sans-serif;
}
.gradio-container {
max-width: 1200px;
margin: 0 auto;
padding: 20px;
border-radius: 8px;
background: #2d2d2d;
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
}
.file-preview {
border: 2px dashed #007acc;
padding: 20px;
border-radius: 8px;
}
.progress-text {
color: #00ff00;
font-weight: bold;
margin-top: 10px;
}
"""
# Define Gradio Interface
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"], # Extensions without leading dots
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 π", elem_classes=["btn-primary"])
progress_text = gr.Textbox(
label="Status",
interactive=False,
elem_classes=["progress-text"]
)
output_srt = gr.File(label="Download Subtitles")
def generate_subtitles(video_file, language):
try:
# Validate file type
if not video_file.name.lower().endswith(('.mp4', '.mkv', '.avi')):
return None, "β Invalid file type. Please upload an MP4, MKV, or AVI file."
# Initialize progress tracking
progress = gr.Progress(track_tqdm=True)
# Process video
progress(0, desc="Initializing...")
srt_path = process_video(video_file.name, language, progress=progress)
if srt_path:
progress(1, desc="β
Subtitles generated successfully!")
return gr.File(srt_path), "β
Subtitles ready for download!"
else:
progress(0, desc="β Error during processing.")
return None, "β Failed to generate subtitles. Check logs."
except Exception as e:
progress(0, desc="β Error encountered.")
return None, f"β Error: {str(e)}"
generate_button.click(
generate_subtitles,
inputs=[video_input, language_dropdown],
outputs=[output_srt, progress_text]
)
demo.launch() |