Update app.py
Browse files
    	
        app.py
    CHANGED
    
    | @@ -16,41 +16,79 @@ language_map = { | |
| 16 | 
             
                "Japanese": "Helsinki-NLP/opus-mt-en-jap"
         | 
| 17 | 
             
            }
         | 
| 18 |  | 
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
| 19 | 
             
            # Define Gradio Interface
         | 
| 20 | 
            -
            with gr.Blocks(theme=gr.themes.Monochrome()) as demo: | 
| 21 | 
             
                gr.Markdown("# π₯ AI-Powered Video Subtitling")
         | 
| 22 | 
             
                gr.Markdown("Upload a video (MP4/MKV/AVI) and select a language to generate subtitles.")
         | 
| 23 |  | 
| 24 | 
             
                with gr.Row():
         | 
| 25 | 
            -
                     | 
| 26 | 
            -
                         | 
| 27 | 
            -
             | 
| 28 | 
            -
             | 
| 29 | 
            -
             | 
| 30 | 
            -
             | 
| 31 | 
            -
             | 
| 32 | 
            -
                         | 
| 33 | 
            -
             | 
| 34 | 
            -
             | 
| 35 | 
            -
             | 
|  | |
| 36 | 
             
                generate_button = gr.Button("Generate Subtitles π")
         | 
|  | |
|  | |
|  | |
|  | |
|  | |
| 37 | 
             
                output_srt = gr.File(label="Download Subtitles")
         | 
| 38 | 
            -
                progress = gr.Textbox(label="Progress", interactive=False)  # Add progress updates
         | 
| 39 |  | 
| 40 | 
             
                def generate_subtitles(video_file, language):
         | 
| 41 | 
             
                    try:
         | 
| 42 | 
            -
                        # Extract the file path from Gradio's File object
         | 
| 43 | 
             
                        video_path = video_file.name  # Get the actual file path
         | 
| 44 | 
            -
                         | 
| 45 | 
             
                        srt_path = process_video(video_path, language)
         | 
| 46 | 
            -
                         | 
|  | |
|  | |
|  | |
| 47 | 
             
                    except Exception as e:
         | 
| 48 | 
             
                        return None, f"β Error: {str(e)}"
         | 
| 49 |  | 
| 50 | 
             
                generate_button.click(
         | 
| 51 | 
             
                    generate_subtitles,
         | 
| 52 | 
            -
                    inputs=[ | 
| 53 | 
            -
                    outputs=[output_srt,  | 
| 54 | 
             
                )
         | 
| 55 |  | 
| 56 | 
             
            demo.launch()
         | 
|  | |
| 16 | 
             
                "Japanese": "Helsinki-NLP/opus-mt-en-jap"
         | 
| 17 | 
             
            }
         | 
| 18 |  | 
| 19 | 
            +
            # Custom CSS for dark mode and animations
         | 
| 20 | 
            +
            css = """
         | 
| 21 | 
            +
            body {
         | 
| 22 | 
            +
                transition: background-color 0.5s ease;
         | 
| 23 | 
            +
            }
         | 
| 24 | 
            +
            .dark-theme {
         | 
| 25 | 
            +
                background-color: #1a1a1a !important;
         | 
| 26 | 
            +
                color: #e0e0e0;
         | 
| 27 | 
            +
            }
         | 
| 28 | 
            +
            .gradio-container {
         | 
| 29 | 
            +
                max-width: 1200px;
         | 
| 30 | 
            +
                margin: 0 auto;
         | 
| 31 | 
            +
                padding: 20px;
         | 
| 32 | 
            +
                border-radius: 10px;
         | 
| 33 | 
            +
                box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
         | 
| 34 | 
            +
            }
         | 
| 35 | 
            +
            .file-preview {
         | 
| 36 | 
            +
                border: 2px dashed #6c757d;
         | 
| 37 | 
            +
                padding: 20px;
         | 
| 38 | 
            +
                border-radius: 10px;
         | 
| 39 | 
            +
            }
         | 
| 40 | 
            +
            .progress-text {
         | 
| 41 | 
            +
                font-size: 16px;
         | 
| 42 | 
            +
                color: #28a745;
         | 
| 43 | 
            +
                animation: blink 1s infinite;
         | 
| 44 | 
            +
            }
         | 
| 45 | 
            +
            @keyframes blink {
         | 
| 46 | 
            +
                50% { opacity: 0.5; }
         | 
| 47 | 
            +
            }
         | 
| 48 | 
            +
            """
         | 
| 49 | 
            +
             | 
| 50 | 
             
            # Define Gradio Interface
         | 
| 51 | 
            +
            with gr.Blocks(theme=gr.themes.Monochrome(), css=css) as demo:
         | 
| 52 | 
             
                gr.Markdown("# π₯ AI-Powered Video Subtitling")
         | 
| 53 | 
             
                gr.Markdown("Upload a video (MP4/MKV/AVI) and select a language to generate subtitles.")
         | 
| 54 |  | 
| 55 | 
             
                with gr.Row():
         | 
| 56 | 
            +
                    with gr.Column(scale=2):
         | 
| 57 | 
            +
                        video_input = gr.File(
         | 
| 58 | 
            +
                            label="Upload Video File",
         | 
| 59 | 
            +
                            file_types=["mp4", "mkv", "avi"],
         | 
| 60 | 
            +
                            elem_classes=["file-preview"]
         | 
| 61 | 
            +
                        )
         | 
| 62 | 
            +
                    with gr.Column(scale=1):
         | 
| 63 | 
            +
                        language_dropdown = gr.Dropdown(
         | 
| 64 | 
            +
                            choices=list(language_map.keys()),
         | 
| 65 | 
            +
                            label="Select Subtitle Language",
         | 
| 66 | 
            +
                            value="English"
         | 
| 67 | 
            +
                        )
         | 
| 68 | 
             
                generate_button = gr.Button("Generate Subtitles π")
         | 
| 69 | 
            +
                progress_text = gr.Textbox(
         | 
| 70 | 
            +
                    label="Progress",
         | 
| 71 | 
            +
                    interactive=False,
         | 
| 72 | 
            +
                    elem_classes=["progress-text"]
         | 
| 73 | 
            +
                )
         | 
| 74 | 
             
                output_srt = gr.File(label="Download Subtitles")
         | 
|  | |
| 75 |  | 
| 76 | 
             
                def generate_subtitles(video_file, language):
         | 
| 77 | 
             
                    try:
         | 
|  | |
| 78 | 
             
                        video_path = video_file.name  # Get the actual file path
         | 
| 79 | 
            +
                        progress_text.update(value="π Processing video...")  # Update progress
         | 
| 80 | 
             
                        srt_path = process_video(video_path, language)
         | 
| 81 | 
            +
                        if srt_path:
         | 
| 82 | 
            +
                            return gr.File(srt_path), "β
 Subtitles generated successfully!"
         | 
| 83 | 
            +
                        else:
         | 
| 84 | 
            +
                            return None, "β Error during processing. Check logs."
         | 
| 85 | 
             
                    except Exception as e:
         | 
| 86 | 
             
                        return None, f"β Error: {str(e)}"
         | 
| 87 |  | 
| 88 | 
             
                generate_button.click(
         | 
| 89 | 
             
                    generate_subtitles,
         | 
| 90 | 
            +
                    inputs=[video_input, language_dropdown],
         | 
| 91 | 
            +
                    outputs=[output_srt, progress_text]
         | 
| 92 | 
             
                )
         | 
| 93 |  | 
| 94 | 
             
            demo.launch()
         | 
