ahmad walidurosyad commited on
Commit
dce0792
Β·
1 Parent(s): 0881e36

Add Gradio interface for Colab

Browse files
Files changed (1) hide show
  1. google_colabs/gradio_app.py +242 -0
google_colabs/gradio_app.py ADDED
@@ -0,0 +1,242 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Gradio Interface for Video Subtitle Remover
3
+ Optimized for Google Colab
4
+ """
5
+
6
+ import gradio as gr
7
+ import cv2
8
+ import os
9
+ import sys
10
+ import tempfile
11
+ from pathlib import Path
12
+
13
+ # Detect if running in Colab
14
+ IN_COLAB = 'google.colab' in sys.modules
15
+
16
+ # Set correct paths for Colab
17
+ if IN_COLAB:
18
+ PROJECT_ROOT = '/content/sub-remover'
19
+ else:
20
+ PROJECT_ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
21
+
22
+ # Add project to path
23
+ sys.path.insert(0, PROJECT_ROOT)
24
+ sys.path.insert(0, os.path.join(PROJECT_ROOT, 'backend'))
25
+
26
+ from backend import config
27
+ from backend.config import InpaintMode
28
+ from backend.main import SubtitleRemover
29
+
30
+
31
+ def process_video(
32
+ video_file,
33
+ algorithm,
34
+ sttn_skip_detection,
35
+ sttn_max_load,
36
+ lama_super_fast,
37
+ sd_steps,
38
+ sd_guidance,
39
+ diffueraser_steps,
40
+ diffueraser_max_load,
41
+ progress=gr.Progress()
42
+ ):
43
+ """Process video to remove subtitles"""
44
+ if video_file is None:
45
+ return None, "❌ Please upload a video first"
46
+
47
+ try:
48
+ progress(0, desc="Initializing...")
49
+
50
+ # Set algorithm
51
+ algorithm_map = {
52
+ "STTN (Fast)": InpaintMode.STTN,
53
+ "LAMA (Quality)": InpaintMode.LAMA,
54
+ "ProPainter (Best)": InpaintMode.PROPAINTER,
55
+ "Stable Diffusion": InpaintMode.STABLE_DIFFUSION,
56
+ "DiffuEraser (Recommended)": InpaintMode.DIFFUERASER,
57
+ }
58
+
59
+ config.MODE = algorithm_map[algorithm]
60
+
61
+ # Apply algorithm-specific settings
62
+ if config.MODE == InpaintMode.STTN:
63
+ config.STTN_SKIP_DETECTION = sttn_skip_detection
64
+ config.STTN_MAX_LOAD_NUM = sttn_max_load
65
+ elif config.MODE == InpaintMode.LAMA:
66
+ config.LAMA_SUPER_FAST = lama_super_fast
67
+ elif config.MODE == InpaintMode.STABLE_DIFFUSION:
68
+ config.SD_STEPS = sd_steps
69
+ config.SD_GUIDANCE_SCALE = sd_guidance
70
+ elif config.MODE == InpaintMode.DIFFUERASER:
71
+ config.DIFFUERASER_STEPS = diffueraser_steps
72
+ config.DIFFUERASER_MAX_LOAD_NUM = diffueraser_max_load
73
+
74
+ progress(0.1, desc="Loading video...")
75
+
76
+ # Get video path
77
+ video_path = video_file if isinstance(video_file, str) else video_file.name
78
+
79
+ # Create output filename
80
+ base_name = os.path.basename(video_path)
81
+ output_name = base_name.replace('.mp4', '_no_sub.mp4')
82
+ output_path = os.path.join(os.path.dirname(video_path), output_name)
83
+
84
+ progress(0.2, desc=f"Processing with {algorithm}...")
85
+
86
+ # Run subtitle removal
87
+ remover = SubtitleRemover(video_path, gui_mode=False)
88
+ remover.run()
89
+
90
+ progress(1.0, desc="Complete!")
91
+
92
+ return remover.video_out_name, f"βœ… Success! Processed with {algorithm}"
93
+
94
+ except Exception as e:
95
+ import traceback
96
+ error_msg = f"❌ Error: {str(e)}\n\n{traceback.format_exc()}"
97
+ return None, error_msg
98
+
99
+
100
+ def create_interface():
101
+ """Create Gradio interface"""
102
+
103
+ with gr.Blocks(title="Video Subtitle Remover", theme=gr.themes.Soft()) as demo:
104
+ gr.Markdown("""
105
+ # 🎬 Video Subtitle Remover
106
+
107
+ Remove hardcoded subtitles from videos using AI inpainting.
108
+
109
+ **⭐ Recommended**: STTN for speed (Colab T4), DiffuEraser for best quality (requires 12GB+ VRAM).
110
+ """)
111
+
112
+ with gr.Row():
113
+ with gr.Column():
114
+ # Input
115
+ video_input = gr.Video(
116
+ label="πŸ“Ή Upload Video"
117
+ )
118
+
119
+ algorithm = gr.Radio(
120
+ choices=[
121
+ "STTN (Fast)",
122
+ "LAMA (Quality)",
123
+ "ProPainter (Best)",
124
+ "Stable Diffusion",
125
+ "DiffuEraser (Recommended)"
126
+ ],
127
+ value="STTN (Fast)",
128
+ label="🎨 Algorithm",
129
+ info="STTN: Fastest. DiffuEraser: Best quality (needs 12GB+ VRAM)."
130
+ )
131
+
132
+ with gr.Accordion("βš™οΈ Advanced Settings", open=False):
133
+ with gr.Tab("STTN"):
134
+ sttn_skip = gr.Checkbox(
135
+ label="Skip Detection (Faster)",
136
+ value=True,
137
+ info="Process entire subtitle area without detection"
138
+ )
139
+ sttn_max_load = gr.Slider(
140
+ minimum=20, maximum=100, value=40, step=10,
141
+ label="Max Frames per Batch",
142
+ info="Lower if OOM"
143
+ )
144
+
145
+ with gr.Tab("LAMA"):
146
+ lama_fast = gr.Checkbox(
147
+ label="Super Fast Mode",
148
+ value=False,
149
+ info="Use OpenCV instead of deep learning"
150
+ )
151
+
152
+ with gr.Tab("Stable Diffusion"):
153
+ sd_steps = gr.Slider(
154
+ minimum=20, maximum=100, value=50, step=5,
155
+ label="Inference Steps",
156
+ info="More = better quality but slower"
157
+ )
158
+ sd_guidance = gr.Slider(
159
+ minimum=5.0, maximum=15.0, value=7.5, step=0.5,
160
+ label="Guidance Scale"
161
+ )
162
+
163
+ with gr.Tab("DiffuEraser"):
164
+ diffueraser_steps = gr.Slider(
165
+ minimum=20, maximum=100, value=50, step=5,
166
+ label="Diffusion Steps"
167
+ )
168
+ diffueraser_max_load = gr.Slider(
169
+ minimum=20, maximum=120, value=40, step=10,
170
+ label="Max Frames per Batch",
171
+ info="T4 GPU: use 40. Higher GPU: 80+"
172
+ )
173
+
174
+ process_btn = gr.Button("πŸš€ Remove Subtitles", variant="primary", size="lg")
175
+
176
+ with gr.Column():
177
+ # Output
178
+ video_output = gr.Video(label="✨ Output Video")
179
+ status_output = gr.Textbox(label="πŸ“Š Status", lines=3)
180
+
181
+ gr.Markdown("""
182
+ ### πŸ’‘ Performance (Colab T4):
183
+ - **STTN**: ~30s for 1min 720p ⚑
184
+ - **LAMA**: ~60s for 1min 720p
185
+ - **DiffuEraser**: ~3-5min for 1min 720p (needs 12GB+ VRAM)
186
+ - **SD**: ~2-3min for 1min 720p
187
+
188
+ ### ⚠️ Tips:
189
+ - Keep videos **under 10 minutes** on free Colab
190
+ - Use STTN for quick tests
191
+ - Use DiffuEraser for final high-quality output
192
+ - Monitor VRAM with `!nvidia-smi`
193
+ """)
194
+
195
+ # Connect button
196
+ process_btn.click(
197
+ fn=process_video,
198
+ inputs=[
199
+ video_input, algorithm,
200
+ sttn_skip, sttn_max_load,
201
+ lama_fast,
202
+ sd_steps, sd_guidance,
203
+ diffueraser_steps, diffueraser_max_load
204
+ ],
205
+ outputs=[video_output, status_output]
206
+ )
207
+
208
+ # Examples
209
+ gr.Examples(
210
+ examples=[
211
+ ["STTN (Fast)", True, 40, False, 50, 7.5, 50, 40],
212
+ ["DiffuEraser (Recommended)", True, 80, False, 50, 7.5, 50, 40],
213
+ ],
214
+ inputs=[
215
+ algorithm,
216
+ sttn_skip, sttn_max_load,
217
+ lama_fast,
218
+ sd_steps, sd_guidance,
219
+ diffueraser_steps, diffueraser_max_load
220
+ ],
221
+ label="Quick Presets"
222
+ )
223
+
224
+ return demo
225
+
226
+
227
+ if __name__ == "__main__":
228
+ # Check environment
229
+ if IN_COLAB:
230
+ print("🌐 Running in Google Colab")
231
+ print(f"πŸ“ Project root: {PROJECT_ROOT}")
232
+ else:
233
+ print("πŸ’» Running locally")
234
+
235
+ # Create and launch interface
236
+ demo = create_interface()
237
+ demo.launch(
238
+ share=True, # Create public URL
239
+ debug=True,
240
+ server_name="0.0.0.0",
241
+ server_port=7860
242
+ )