Update ui_components.py
Browse files- ui_components.py +440 -149
ui_components.py
CHANGED
|
@@ -1,300 +1,591 @@
|
|
| 1 |
#!/usr/bin/env python3
|
| 2 |
"""
|
| 3 |
-
|
|
|
|
| 4 |
"""
|
|
|
|
| 5 |
import gradio as gr
|
| 6 |
import os
|
|
|
|
| 7 |
import time
|
| 8 |
import traceback
|
| 9 |
-
from typing import Optional
|
|
|
|
| 10 |
|
| 11 |
-
#
|
| 12 |
-
|
| 13 |
-
import gradio_client.utils as gc_utils
|
| 14 |
-
original_get_type = gc_utils.get_type
|
| 15 |
-
|
| 16 |
-
def patched_get_type(schema):
|
| 17 |
-
if not isinstance(schema, dict):
|
| 18 |
-
if isinstance(schema, bool):
|
| 19 |
-
return "boolean"
|
| 20 |
-
if isinstance(schema, str):
|
| 21 |
-
return "string"
|
| 22 |
-
if isinstance(schema, (int, float)):
|
| 23 |
-
return "number"
|
| 24 |
-
return "string"
|
| 25 |
-
return original_get_type(schema)
|
| 26 |
-
|
| 27 |
-
gc_utils.get_type = patched_get_type
|
| 28 |
-
print("β
UI Components: Gradio schema patch applied")
|
| 29 |
-
|
| 30 |
-
except Exception as e:
|
| 31 |
-
print(f"β οΈ UI Components: Gradio patch failed: {e}")
|
| 32 |
|
| 33 |
-
# Import core functions with error handling
|
| 34 |
try:
|
| 35 |
from app import (
|
|
|
|
|
|
|
| 36 |
load_models_with_validation,
|
| 37 |
process_video_fixed,
|
| 38 |
get_model_status,
|
| 39 |
-
get_cache_status
|
| 40 |
-
PROCESS_CANCELLED
|
| 41 |
)
|
| 42 |
CORE_FUNCTIONS_AVAILABLE = True
|
| 43 |
-
print("
|
| 44 |
except Exception as e:
|
| 45 |
-
print(f"
|
| 46 |
CORE_FUNCTIONS_AVAILABLE = False
|
| 47 |
|
| 48 |
# Import utilities with error handling
|
| 49 |
try:
|
| 50 |
from utilities import PROFESSIONAL_BACKGROUNDS
|
| 51 |
UTILITIES_AVAILABLE = True
|
| 52 |
-
print("
|
| 53 |
except Exception as e:
|
| 54 |
-
print(f"
|
| 55 |
UTILITIES_AVAILABLE = False
|
| 56 |
-
PROFESSIONAL_BACKGROUNDS = {
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
|
| 58 |
-
# Import two-stage with error handling
|
| 59 |
try:
|
| 60 |
from two_stage_processor import CHROMA_PRESETS
|
| 61 |
TWO_STAGE_AVAILABLE = True
|
| 62 |
-
print("
|
| 63 |
except ImportError:
|
| 64 |
TWO_STAGE_AVAILABLE = False
|
| 65 |
-
CHROMA_PRESETS = {
|
| 66 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
|
| 68 |
def create_interface():
|
| 69 |
-
"""Create the
|
| 70 |
|
| 71 |
-
#
|
| 72 |
-
def
|
| 73 |
-
|
| 74 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
if not CORE_FUNCTIONS_AVAILABLE:
|
| 76 |
-
return None, "Core processing functions not available", "Error:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
|
| 78 |
try:
|
| 79 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
def progress_callback(pct, desc):
|
| 81 |
if progress:
|
| 82 |
-
progress(pct)
|
| 83 |
-
|
| 84 |
-
return desc # Match app.py's expectation
|
| 85 |
|
| 86 |
-
#
|
| 87 |
-
if bg_method == "professional"
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
progress_callback,
|
| 91 |
-
use_two_stage=bool(use_two_stage),
|
| 92 |
-
chroma_preset=chroma_preset or "standard",
|
| 93 |
-
preview_mask=False,
|
| 94 |
-
preview_greenscreen=False
|
| 95 |
-
)
|
| 96 |
-
elif bg_method == "upload" and custom_img:
|
| 97 |
-
result = process_video_fixed(
|
| 98 |
-
video_path, "custom", custom_img,
|
| 99 |
-
progress_callback,
|
| 100 |
-
use_two_stage=bool(use_two_stage),
|
| 101 |
-
chroma_preset=chroma_preset or "standard",
|
| 102 |
-
preview_mask=False,
|
| 103 |
-
preview_greenscreen=False
|
| 104 |
-
)
|
| 105 |
else:
|
| 106 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 107 |
|
| 108 |
-
|
| 109 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 110 |
except Exception as e:
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
|
|
|
| 114 |
|
| 115 |
-
#
|
| 116 |
-
def
|
| 117 |
-
"""
|
|
|
|
| 118 |
if not CORE_FUNCTIONS_AVAILABLE:
|
| 119 |
-
return "Error: Core functions not available", "
|
| 120 |
|
| 121 |
try:
|
| 122 |
def progress_callback(pct, desc):
|
| 123 |
if progress:
|
| 124 |
-
progress(pct)
|
| 125 |
-
|
| 126 |
-
return desc # Match app.py's expectation
|
| 127 |
|
| 128 |
result = load_models_with_validation(progress_callback)
|
| 129 |
-
return result, f"Model loading completed"
|
| 130 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 131 |
except Exception as e:
|
| 132 |
-
error_msg = f"Model loading error: {str(e)}
|
| 133 |
-
print(f"
|
| 134 |
return error_msg, error_msg
|
| 135 |
|
| 136 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 137 |
with gr.Blocks(
|
| 138 |
-
title="BackgroundFX Pro - Video Background Replacement",
|
| 139 |
-
theme=gr.themes.Soft(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 140 |
) as demo:
|
| 141 |
|
| 142 |
-
|
| 143 |
-
gr.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 144 |
|
| 145 |
-
|
| 146 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 147 |
|
| 148 |
with gr.Row():
|
|
|
|
| 149 |
with gr.Column(scale=1):
|
| 150 |
gr.Markdown("### πΉ Step 1: Upload Your Video")
|
| 151 |
-
video_input = gr.Video(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 152 |
|
| 153 |
-
gr.Markdown("### π¨ Step 2: Choose Background
|
| 154 |
background_method = gr.Radio(
|
| 155 |
choices=["professional", "upload"],
|
| 156 |
value="professional",
|
| 157 |
-
label="Background Method"
|
|
|
|
| 158 |
)
|
| 159 |
|
| 160 |
# Professional backgrounds
|
| 161 |
-
with gr.Group() as professional_group:
|
| 162 |
-
gr.Markdown("**Professional Presets**")
|
|
|
|
| 163 |
if UTILITIES_AVAILABLE and PROFESSIONAL_BACKGROUNDS:
|
| 164 |
-
choices =
|
| 165 |
-
|
|
|
|
| 166 |
else:
|
| 167 |
-
choices = ["
|
| 168 |
default_choice = "office_modern"
|
| 169 |
|
| 170 |
professional_choice = gr.Dropdown(
|
| 171 |
choices=choices,
|
| 172 |
value=default_choice,
|
| 173 |
-
label="Select Background"
|
|
|
|
| 174 |
)
|
| 175 |
|
| 176 |
# Custom upload
|
| 177 |
with gr.Group(visible=False) as upload_group:
|
| 178 |
gr.Markdown("**Upload Custom Background**")
|
| 179 |
-
custom_background = gr.Image(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 180 |
|
| 181 |
-
#
|
| 182 |
-
def
|
| 183 |
return (
|
| 184 |
gr.update(visible=(method == "professional")),
|
| 185 |
gr.update(visible=(method == "upload"))
|
| 186 |
)
|
| 187 |
|
| 188 |
background_method.change(
|
| 189 |
-
fn=
|
| 190 |
inputs=background_method,
|
| 191 |
outputs=[professional_group, upload_group]
|
| 192 |
)
|
| 193 |
|
| 194 |
-
gr.Markdown("### βοΈ Processing Options")
|
| 195 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 196 |
use_two_stage = gr.Checkbox(
|
| 197 |
label="Enable Two-Stage Processing",
|
| 198 |
value=False,
|
| 199 |
-
info="
|
|
|
|
| 200 |
)
|
| 201 |
|
| 202 |
if TWO_STAGE_AVAILABLE:
|
| 203 |
chroma_preset = gr.Dropdown(
|
| 204 |
-
choices=
|
|
|
|
|
|
|
|
|
|
|
|
|
| 205 |
value="standard",
|
| 206 |
-
label="
|
|
|
|
| 207 |
)
|
| 208 |
else:
|
| 209 |
chroma_preset = gr.Dropdown(
|
| 210 |
-
choices=["standard"],
|
| 211 |
value="standard",
|
| 212 |
-
label="
|
|
|
|
| 213 |
)
|
| 214 |
|
| 215 |
-
gr.Markdown("### π Process
|
|
|
|
| 216 |
with gr.Row():
|
| 217 |
-
load_models_btn = gr.Button(
|
| 218 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 219 |
|
|
|
|
| 220 |
status_text = gr.Textbox(
|
| 221 |
-
label="Status",
|
| 222 |
-
value="Ready - Click 'Load Models'
|
| 223 |
interactive=False,
|
| 224 |
-
lines=
|
|
|
|
| 225 |
)
|
| 226 |
|
| 227 |
-
#
|
| 228 |
-
with gr.
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
|
| 233 |
-
|
|
|
|
| 234 |
|
|
|
|
| 235 |
with gr.Column(scale=1):
|
| 236 |
-
gr.Markdown("### π₯
|
| 237 |
-
video_output = gr.Video(label="Processed Video", height=400)
|
| 238 |
|
| 239 |
-
|
| 240 |
-
label="
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 241 |
interactive=False,
|
| 242 |
-
lines=
|
| 243 |
-
|
|
|
|
| 244 |
)
|
| 245 |
|
| 246 |
-
|
| 247 |
-
label="Debug
|
| 248 |
interactive=False,
|
| 249 |
-
lines=
|
| 250 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 251 |
)
|
| 252 |
|
| 253 |
# Event handlers
|
| 254 |
load_models_btn.click(
|
| 255 |
-
fn=
|
| 256 |
-
outputs=[status_text,
|
| 257 |
show_progress=True
|
| 258 |
)
|
| 259 |
|
| 260 |
process_btn.click(
|
| 261 |
-
fn=
|
| 262 |
inputs=[
|
| 263 |
video_input,
|
| 264 |
background_method,
|
| 265 |
custom_background,
|
| 266 |
professional_choice,
|
| 267 |
use_two_stage,
|
| 268 |
-
chroma_preset
|
|
|
|
| 269 |
],
|
| 270 |
-
outputs=[video_output,
|
| 271 |
show_progress=True
|
| 272 |
)
|
| 273 |
|
| 274 |
model_status_btn.click(
|
| 275 |
-
fn=
|
| 276 |
-
outputs=[
|
| 277 |
show_progress=False
|
|
|
|
|
|
|
|
|
|
| 278 |
)
|
| 279 |
|
| 280 |
cache_status_btn.click(
|
| 281 |
-
fn=
|
| 282 |
-
outputs=[
|
| 283 |
show_progress=False
|
|
|
|
|
|
|
|
|
|
| 284 |
)
|
| 285 |
|
| 286 |
-
#
|
| 287 |
-
with gr.Accordion("βΉοΈ Information", open=False):
|
| 288 |
gr.Markdown(f"""
|
| 289 |
-
###
|
| 290 |
-
|
| 291 |
-
|
| 292 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 293 |
|
| 294 |
-
|
| 295 |
-
-
|
| 296 |
-
-
|
| 297 |
-
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 298 |
""")
|
| 299 |
|
| 300 |
-
return demo
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
#!/usr/bin/env python3
|
| 2 |
"""
|
| 3 |
+
Enhanced UI Components - BackgroundFX Pro
|
| 4 |
+
Streamlined interface with better error handling and user experience
|
| 5 |
"""
|
| 6 |
+
|
| 7 |
import gradio as gr
|
| 8 |
import os
|
| 9 |
+
import json
|
| 10 |
import time
|
| 11 |
import traceback
|
| 12 |
+
from typing import Optional, Dict, Any, Tuple
|
| 13 |
+
from pathlib import Path
|
| 14 |
|
| 15 |
+
# Remove redundant Gradio schema patching - handled in app.py
|
| 16 |
+
print("UI Components: Initializing interface...")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
+
# Import core functions with comprehensive error handling
|
| 19 |
try:
|
| 20 |
from app import (
|
| 21 |
+
VideoProcessor,
|
| 22 |
+
processor,
|
| 23 |
load_models_with_validation,
|
| 24 |
process_video_fixed,
|
| 25 |
get_model_status,
|
| 26 |
+
get_cache_status
|
|
|
|
| 27 |
)
|
| 28 |
CORE_FUNCTIONS_AVAILABLE = True
|
| 29 |
+
print("UI Components: Core functions imported successfully")
|
| 30 |
except Exception as e:
|
| 31 |
+
print(f"UI Components: Core functions import failed: {e}")
|
| 32 |
CORE_FUNCTIONS_AVAILABLE = False
|
| 33 |
|
| 34 |
# Import utilities with error handling
|
| 35 |
try:
|
| 36 |
from utilities import PROFESSIONAL_BACKGROUNDS
|
| 37 |
UTILITIES_AVAILABLE = True
|
| 38 |
+
print("UI Components: Utilities imported successfully")
|
| 39 |
except Exception as e:
|
| 40 |
+
print(f"UI Components: Utilities import failed: {e}")
|
| 41 |
UTILITIES_AVAILABLE = False
|
| 42 |
+
PROFESSIONAL_BACKGROUNDS = {
|
| 43 |
+
"office_modern": {"name": "Modern Office", "description": "Clean office environment"},
|
| 44 |
+
"studio_blue": {"name": "Professional Blue", "description": "Blue studio background"},
|
| 45 |
+
"minimalist": {"name": "Minimalist White", "description": "Clean white background"}
|
| 46 |
+
}
|
| 47 |
|
| 48 |
+
# Import two-stage processor with error handling
|
| 49 |
try:
|
| 50 |
from two_stage_processor import CHROMA_PRESETS
|
| 51 |
TWO_STAGE_AVAILABLE = True
|
| 52 |
+
print("UI Components: Two-stage processor available")
|
| 53 |
except ImportError:
|
| 54 |
TWO_STAGE_AVAILABLE = False
|
| 55 |
+
CHROMA_PRESETS = {
|
| 56 |
+
'standard': {'name': 'Standard Quality'},
|
| 57 |
+
'balanced': {'name': 'Balanced'},
|
| 58 |
+
'high': {'name': 'High Quality'}
|
| 59 |
+
}
|
| 60 |
+
print("UI Components: Two-stage processor not available")
|
| 61 |
+
|
| 62 |
+
class UIStateManager:
|
| 63 |
+
"""Manage UI state and provide user feedback"""
|
| 64 |
+
|
| 65 |
+
def __init__(self):
|
| 66 |
+
self.processing_active = False
|
| 67 |
+
self.models_loaded = False
|
| 68 |
+
self.last_processing_time = None
|
| 69 |
+
self.processing_history = []
|
| 70 |
+
|
| 71 |
+
def update_processing_state(self, active: bool):
|
| 72 |
+
self.processing_active = active
|
| 73 |
+
if not active and self.last_processing_time:
|
| 74 |
+
duration = time.time() - self.last_processing_time
|
| 75 |
+
self.processing_history.append({
|
| 76 |
+
'timestamp': time.time(),
|
| 77 |
+
'duration': duration
|
| 78 |
+
})
|
| 79 |
+
elif active:
|
| 80 |
+
self.last_processing_time = time.time()
|
| 81 |
+
|
| 82 |
+
def get_average_processing_time(self) -> float:
|
| 83 |
+
if not self.processing_history:
|
| 84 |
+
return 0
|
| 85 |
+
recent_history = self.processing_history[-5:] # Last 5 processing sessions
|
| 86 |
+
return sum(h['duration'] for h in recent_history) / len(recent_history)
|
| 87 |
+
|
| 88 |
+
# Global UI state
|
| 89 |
+
ui_state = UIStateManager()
|
| 90 |
|
| 91 |
def create_interface():
|
| 92 |
+
"""Create the enhanced Gradio interface with better UX"""
|
| 93 |
|
| 94 |
+
# Enhanced processing function with better error handling
|
| 95 |
+
def enhanced_process_video(
|
| 96 |
+
video_path, bg_method, custom_img, prof_choice,
|
| 97 |
+
use_two_stage, chroma_preset, quality_preset,
|
| 98 |
+
progress: Optional[gr.Progress] = None
|
| 99 |
+
):
|
| 100 |
+
"""Enhanced video processing with comprehensive error handling and user feedback"""
|
| 101 |
+
|
| 102 |
if not CORE_FUNCTIONS_AVAILABLE:
|
| 103 |
+
return None, "Error: Core processing functions not available", "System Error: Please check installation"
|
| 104 |
+
|
| 105 |
+
if not processor.models_loaded:
|
| 106 |
+
return None, "Error: Models not loaded", "Please load models first using the 'Load Models' button"
|
| 107 |
+
|
| 108 |
+
if not video_path:
|
| 109 |
+
return None, "Error: No video uploaded", "Please upload a video file first"
|
| 110 |
+
|
| 111 |
+
# Validate inputs
|
| 112 |
+
if bg_method == "professional" and not prof_choice:
|
| 113 |
+
return None, "Error: No background selected", "Please select a professional background"
|
| 114 |
+
|
| 115 |
+
if bg_method == "upload" and not custom_img:
|
| 116 |
+
return None, "Error: No custom background", "Please upload a custom background image"
|
| 117 |
|
| 118 |
try:
|
| 119 |
+
ui_state.update_processing_state(True)
|
| 120 |
+
|
| 121 |
+
# Set quality preset in processor config
|
| 122 |
+
if quality_preset and hasattr(processor, 'config'):
|
| 123 |
+
processor.config.quality_preset = quality_preset
|
| 124 |
+
|
| 125 |
def progress_callback(pct, desc):
|
| 126 |
if progress:
|
| 127 |
+
progress(pct, desc)
|
| 128 |
+
return desc
|
|
|
|
| 129 |
|
| 130 |
+
# Determine background choice
|
| 131 |
+
if bg_method == "professional":
|
| 132 |
+
background_choice = prof_choice
|
| 133 |
+
custom_background_path = None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 134 |
else:
|
| 135 |
+
background_choice = "custom"
|
| 136 |
+
custom_background_path = custom_img
|
| 137 |
+
|
| 138 |
+
# Process video
|
| 139 |
+
result_path, result_message = process_video_fixed(
|
| 140 |
+
video_path=video_path,
|
| 141 |
+
background_choice=background_choice,
|
| 142 |
+
custom_background_path=custom_background_path,
|
| 143 |
+
progress_callback=progress_callback,
|
| 144 |
+
use_two_stage=bool(use_two_stage),
|
| 145 |
+
chroma_preset=chroma_preset or "standard",
|
| 146 |
+
preview_mask=False,
|
| 147 |
+
preview_greenscreen=False
|
| 148 |
+
)
|
| 149 |
|
| 150 |
+
ui_state.update_processing_state(False)
|
| 151 |
|
| 152 |
+
if result_path:
|
| 153 |
+
# Enhanced success message
|
| 154 |
+
avg_time = ui_state.get_average_processing_time()
|
| 155 |
+
success_info = f"""
|
| 156 |
+
β
Processing Complete!
|
| 157 |
+
|
| 158 |
+
π Results:
|
| 159 |
+
{result_message}
|
| 160 |
+
|
| 161 |
+
β±οΈ Performance:
|
| 162 |
+
- Average processing time: {avg_time:.1f}s
|
| 163 |
+
- Two-stage mode: {'Enabled' if use_two_stage else 'Disabled'}
|
| 164 |
+
- Quality preset: {quality_preset or 'Default'}
|
| 165 |
+
|
| 166 |
+
π‘ Tips:
|
| 167 |
+
- Try two-stage mode for better quality
|
| 168 |
+
- Use 'fast' preset for quicker processing
|
| 169 |
+
- Shorter videos process faster
|
| 170 |
+
"""
|
| 171 |
+
return result_path, success_info, "Processing completed successfully!"
|
| 172 |
+
else:
|
| 173 |
+
return None, f"Processing failed: {result_message}", f"Error: {result_message}"
|
| 174 |
+
|
| 175 |
except Exception as e:
|
| 176 |
+
ui_state.update_processing_state(False)
|
| 177 |
+
error_msg = f"Processing error: {str(e)}"
|
| 178 |
+
print(f"UI Error: {error_msg}\n{traceback.format_exc()}")
|
| 179 |
+
return None, error_msg, f"System Error: {error_msg}"
|
| 180 |
|
| 181 |
+
# Enhanced model loading with better feedback
|
| 182 |
+
def enhanced_load_models(progress: Optional[gr.Progress] = None):
|
| 183 |
+
"""Enhanced model loading with detailed feedback"""
|
| 184 |
+
|
| 185 |
if not CORE_FUNCTIONS_AVAILABLE:
|
| 186 |
+
return "Error: Core functions not available", "System Error: Installation incomplete"
|
| 187 |
|
| 188 |
try:
|
| 189 |
def progress_callback(pct, desc):
|
| 190 |
if progress:
|
| 191 |
+
progress(pct, desc)
|
| 192 |
+
return desc
|
|
|
|
| 193 |
|
| 194 |
result = load_models_with_validation(progress_callback)
|
|
|
|
| 195 |
|
| 196 |
+
if "SUCCESS" in result or "successful" in result.lower():
|
| 197 |
+
ui_state.models_loaded = True
|
| 198 |
+
enhanced_result = f"""
|
| 199 |
+
β
Models Loaded Successfully!
|
| 200 |
+
|
| 201 |
+
π Status:
|
| 202 |
+
{result}
|
| 203 |
+
|
| 204 |
+
π― Ready for Processing:
|
| 205 |
+
- High-quality segmentation (SAM2)
|
| 206 |
+
- Professional mask refinement (MatAnyone)
|
| 207 |
+
- {'Two-stage green screen mode available' if TWO_STAGE_AVAILABLE else 'Single-stage processing only'}
|
| 208 |
+
|
| 209 |
+
π‘ Next Steps:
|
| 210 |
+
1. Upload your video
|
| 211 |
+
2. Choose background method
|
| 212 |
+
3. Click 'Process Video'
|
| 213 |
+
"""
|
| 214 |
+
return enhanced_result, "Models loaded successfully! Ready to process videos."
|
| 215 |
+
else:
|
| 216 |
+
return result, f"Model loading failed: {result}"
|
| 217 |
+
|
| 218 |
except Exception as e:
|
| 219 |
+
error_msg = f"Model loading error: {str(e)}"
|
| 220 |
+
print(f"UI Model Loading Error: {error_msg}\n{traceback.format_exc()}")
|
| 221 |
return error_msg, error_msg
|
| 222 |
|
| 223 |
+
# Enhanced status functions
|
| 224 |
+
def get_enhanced_model_status():
|
| 225 |
+
"""Get enhanced model status with user-friendly formatting"""
|
| 226 |
+
try:
|
| 227 |
+
status = get_model_status()
|
| 228 |
+
if isinstance(status, dict):
|
| 229 |
+
formatted_status = {
|
| 230 |
+
"SAM2 Segmentation": "β
Ready" if status.get('sam2_available') else "β Not Loaded",
|
| 231 |
+
"MatAnyone Refinement": "β
Ready" if status.get('matanyone_available') else "β Not Loaded",
|
| 232 |
+
"Two-Stage Mode": "β
Available" if status.get('two_stage_available') else "β Not Available",
|
| 233 |
+
"Device": status.get('device', 'Unknown'),
|
| 234 |
+
"Models Validated": "β
Yes" if status.get('models_loaded') else "β No"
|
| 235 |
+
}
|
| 236 |
+
if 'memory_usage' in status and status['memory_usage']:
|
| 237 |
+
memory = status['memory_usage']
|
| 238 |
+
if 'gpu_percent' in memory:
|
| 239 |
+
formatted_status["GPU Memory"] = f"{memory['gpu_percent']:.1f}% used"
|
| 240 |
+
return formatted_status
|
| 241 |
+
else:
|
| 242 |
+
return {"Status": str(status)}
|
| 243 |
+
except Exception as e:
|
| 244 |
+
return {"Error": f"Failed to get status: {e}"}
|
| 245 |
+
|
| 246 |
+
def get_enhanced_cache_status():
|
| 247 |
+
"""Get enhanced cache status with detailed information"""
|
| 248 |
+
try:
|
| 249 |
+
status = get_cache_status()
|
| 250 |
+
if isinstance(status, dict):
|
| 251 |
+
return {
|
| 252 |
+
"Cache Status": "β
Active" if status.get('models_loaded') else "β Inactive",
|
| 253 |
+
"Processing Mode": "Two-Stage" if status.get('two_stage_available') else "Single-Stage",
|
| 254 |
+
"Configuration": status.get('config', {}),
|
| 255 |
+
"System Device": status.get('device', 'Unknown')
|
| 256 |
+
}
|
| 257 |
+
else:
|
| 258 |
+
return {"Cache": str(status)}
|
| 259 |
+
except Exception as e:
|
| 260 |
+
return {"Error": f"Failed to get cache info: {e}"}
|
| 261 |
+
|
| 262 |
+
# Create the main interface
|
| 263 |
with gr.Blocks(
|
| 264 |
+
title="BackgroundFX Pro - Professional Video Background Replacement",
|
| 265 |
+
theme=gr.themes.Soft(
|
| 266 |
+
primary_hue="blue",
|
| 267 |
+
secondary_hue="gray",
|
| 268 |
+
neutral_hue="slate"
|
| 269 |
+
),
|
| 270 |
+
css="""
|
| 271 |
+
.main-header { text-align: center; margin-bottom: 20px; }
|
| 272 |
+
.status-box { background: #f8f9fa; padding: 15px; border-radius: 8px; margin: 10px 0; }
|
| 273 |
+
.error-box { background: #fee; border-left: 4px solid #dc3545; padding: 15px; }
|
| 274 |
+
.success-box { background: #efe; border-left: 4px solid #28a745; padding: 15px; }
|
| 275 |
+
.feature-list { columns: 2; column-gap: 20px; }
|
| 276 |
+
"""
|
| 277 |
) as demo:
|
| 278 |
|
| 279 |
+
# Header
|
| 280 |
+
with gr.Row():
|
| 281 |
+
gr.Markdown("""
|
| 282 |
+
# π¬ BackgroundFX Pro - Video Background Replacement
|
| 283 |
+
|
| 284 |
+
Professional-quality video background replacement using AI segmentation and advanced compositing techniques.
|
| 285 |
+
""", elem_classes=["main-header"])
|
| 286 |
|
| 287 |
+
# System status indicator
|
| 288 |
+
with gr.Row():
|
| 289 |
+
with gr.Column(scale=1):
|
| 290 |
+
system_status = gr.HTML(f"""
|
| 291 |
+
<div class="status-box">
|
| 292 |
+
<h4>π System Status</h4>
|
| 293 |
+
<ul>
|
| 294 |
+
<li>Core Functions: {'β
Available' if CORE_FUNCTIONS_AVAILABLE else 'β Not Available'}</li>
|
| 295 |
+
<li>Utilities: {'β
Available' if UTILITIES_AVAILABLE else 'β Not Available'}</li>
|
| 296 |
+
<li>Two-Stage Mode: {'β
Available' if TWO_STAGE_AVAILABLE else 'β Not Available'}</li>
|
| 297 |
+
</ul>
|
| 298 |
+
</div>
|
| 299 |
+
""")
|
| 300 |
|
| 301 |
with gr.Row():
|
| 302 |
+
# Left column - Input and controls
|
| 303 |
with gr.Column(scale=1):
|
| 304 |
gr.Markdown("### πΉ Step 1: Upload Your Video")
|
| 305 |
+
video_input = gr.Video(
|
| 306 |
+
label="Upload your video (MP4, AVI, MOV supported)",
|
| 307 |
+
height=300
|
| 308 |
+
)
|
| 309 |
+
|
| 310 |
+
with gr.Accordion("π Video Requirements", open=False):
|
| 311 |
+
gr.Markdown("""
|
| 312 |
+
**Supported Formats:** MP4, AVI, MOV, MKV
|
| 313 |
+
**Max Duration:** 5 minutes (300 seconds)
|
| 314 |
+
**Max Resolution:** 4096x4096
|
| 315 |
+
**Max File Size:** 2GB
|
| 316 |
+
|
| 317 |
+
**Recommendations:**
|
| 318 |
+
- Use 1080p or lower for faster processing
|
| 319 |
+
- Shorter videos (10-30s) are ideal for testing
|
| 320 |
+
- Ensure good lighting and clear person visibility
|
| 321 |
+
""")
|
| 322 |
|
| 323 |
+
gr.Markdown("### π¨ Step 2: Choose Background")
|
| 324 |
background_method = gr.Radio(
|
| 325 |
choices=["professional", "upload"],
|
| 326 |
value="professional",
|
| 327 |
+
label="Background Method",
|
| 328 |
+
info="Professional presets or upload your own image"
|
| 329 |
)
|
| 330 |
|
| 331 |
# Professional backgrounds
|
| 332 |
+
with gr.Group(visible=True) as professional_group:
|
| 333 |
+
gr.Markdown("**Professional Background Presets**")
|
| 334 |
+
|
| 335 |
if UTILITIES_AVAILABLE and PROFESSIONAL_BACKGROUNDS:
|
| 336 |
+
choices = [(f"{bg['name']} - {bg['description']}", key)
|
| 337 |
+
for key, bg in PROFESSIONAL_BACKGROUNDS.items()]
|
| 338 |
+
default_choice = list(PROFESSIONAL_BACKGROUNDS.keys())[0]
|
| 339 |
else:
|
| 340 |
+
choices = [("Modern Office - Clean office environment", "office_modern")]
|
| 341 |
default_choice = "office_modern"
|
| 342 |
|
| 343 |
professional_choice = gr.Dropdown(
|
| 344 |
choices=choices,
|
| 345 |
value=default_choice,
|
| 346 |
+
label="Select Professional Background",
|
| 347 |
+
info="Each preset is optimized for different use cases"
|
| 348 |
)
|
| 349 |
|
| 350 |
# Custom upload
|
| 351 |
with gr.Group(visible=False) as upload_group:
|
| 352 |
gr.Markdown("**Upload Custom Background**")
|
| 353 |
+
custom_background = gr.Image(
|
| 354 |
+
label="Upload background image",
|
| 355 |
+
type="filepath",
|
| 356 |
+
info="JPG, PNG supported. Will be resized to match video resolution."
|
| 357 |
+
)
|
| 358 |
|
| 359 |
+
# Background method visibility control
|
| 360 |
+
def update_background_visibility(method):
|
| 361 |
return (
|
| 362 |
gr.update(visible=(method == "professional")),
|
| 363 |
gr.update(visible=(method == "upload"))
|
| 364 |
)
|
| 365 |
|
| 366 |
background_method.change(
|
| 367 |
+
fn=update_background_visibility,
|
| 368 |
inputs=background_method,
|
| 369 |
outputs=[professional_group, upload_group]
|
| 370 |
)
|
| 371 |
|
| 372 |
+
gr.Markdown("### βοΈ Step 3: Processing Options")
|
| 373 |
+
|
| 374 |
+
with gr.Row():
|
| 375 |
+
quality_preset = gr.Dropdown(
|
| 376 |
+
choices=[
|
| 377 |
+
("Fast - Quick processing", "fast"),
|
| 378 |
+
("Balanced - Good quality/speed", "balanced"),
|
| 379 |
+
("High - Best quality", "high")
|
| 380 |
+
],
|
| 381 |
+
value="balanced",
|
| 382 |
+
label="Quality Preset",
|
| 383 |
+
info="Higher quality takes longer but produces better results"
|
| 384 |
+
)
|
| 385 |
+
|
| 386 |
+
with gr.Accordion("π§ Advanced Settings", open=False):
|
| 387 |
use_two_stage = gr.Checkbox(
|
| 388 |
label="Enable Two-Stage Processing",
|
| 389 |
value=False,
|
| 390 |
+
info="Cinema-quality green screen mode (slower but much better quality)",
|
| 391 |
+
interactive=TWO_STAGE_AVAILABLE
|
| 392 |
)
|
| 393 |
|
| 394 |
if TWO_STAGE_AVAILABLE:
|
| 395 |
chroma_preset = gr.Dropdown(
|
| 396 |
+
choices=[
|
| 397 |
+
("Standard - General use", "standard"),
|
| 398 |
+
("Studio - Broadcast quality", "studio"),
|
| 399 |
+
("Outdoor - Challenging lighting", "outdoor")
|
| 400 |
+
],
|
| 401 |
value="standard",
|
| 402 |
+
label="Chroma Key Preset",
|
| 403 |
+
info="Only used in two-stage mode"
|
| 404 |
)
|
| 405 |
else:
|
| 406 |
chroma_preset = gr.Dropdown(
|
| 407 |
+
choices=[("Standard", "standard")],
|
| 408 |
value="standard",
|
| 409 |
+
label="Chroma Key Preset",
|
| 410 |
+
interactive=False
|
| 411 |
)
|
| 412 |
|
| 413 |
+
gr.Markdown("### π Step 4: Process")
|
| 414 |
+
|
| 415 |
with gr.Row():
|
| 416 |
+
load_models_btn = gr.Button(
|
| 417 |
+
"π Load Models",
|
| 418 |
+
variant="secondary",
|
| 419 |
+
size="lg"
|
| 420 |
+
)
|
| 421 |
+
process_btn = gr.Button(
|
| 422 |
+
"π¬ Process Video",
|
| 423 |
+
variant="primary",
|
| 424 |
+
size="lg",
|
| 425 |
+
scale=2
|
| 426 |
+
)
|
| 427 |
|
| 428 |
+
# Status and feedback
|
| 429 |
status_text = gr.Textbox(
|
| 430 |
+
label="π Status Updates",
|
| 431 |
+
value="Ready - Click 'Load Models' to begin",
|
| 432 |
interactive=False,
|
| 433 |
+
lines=6,
|
| 434 |
+
max_lines=10
|
| 435 |
)
|
| 436 |
|
| 437 |
+
# System monitoring
|
| 438 |
+
with gr.Accordion("π System Monitoring", open=False):
|
| 439 |
+
with gr.Row():
|
| 440 |
+
model_status_btn = gr.Button("Check Models", variant="secondary")
|
| 441 |
+
cache_status_btn = gr.Button("Check Cache", variant="secondary")
|
| 442 |
+
|
| 443 |
+
model_status_display = gr.JSON(label="Model Status", visible=False)
|
| 444 |
+
cache_status_display = gr.JSON(label="Cache Status", visible=False)
|
| 445 |
|
| 446 |
+
# Right column - Output and results
|
| 447 |
with gr.Column(scale=1):
|
| 448 |
+
gr.Markdown("### π₯ Results")
|
|
|
|
| 449 |
|
| 450 |
+
video_output = gr.Video(
|
| 451 |
+
label="Processed Video",
|
| 452 |
+
height=400
|
| 453 |
+
)
|
| 454 |
+
|
| 455 |
+
result_info = gr.Textbox(
|
| 456 |
+
label="π Processing Information",
|
| 457 |
interactive=False,
|
| 458 |
+
lines=12,
|
| 459 |
+
max_lines=15,
|
| 460 |
+
placeholder="Processing results and statistics will appear here..."
|
| 461 |
)
|
| 462 |
|
| 463 |
+
debug_info = gr.Textbox(
|
| 464 |
+
label="π Debug Information",
|
| 465 |
interactive=False,
|
| 466 |
+
lines=6,
|
| 467 |
+
max_lines=10,
|
| 468 |
+
placeholder="Debug and system information will appear here...",
|
| 469 |
+
visible=False
|
| 470 |
+
)
|
| 471 |
+
|
| 472 |
+
# Toggle debug visibility
|
| 473 |
+
show_debug_btn = gr.Button("Show Debug Info", variant="secondary", size="sm")
|
| 474 |
+
|
| 475 |
+
def toggle_debug_visibility(current_visibility):
|
| 476 |
+
new_visibility = not current_visibility
|
| 477 |
+
return (
|
| 478 |
+
gr.update(visible=new_visibility),
|
| 479 |
+
"Hide Debug Info" if new_visibility else "Show Debug Info"
|
| 480 |
+
)
|
| 481 |
+
|
| 482 |
+
show_debug_btn.click(
|
| 483 |
+
fn=lambda: toggle_debug_visibility(False), # Will be managed by state
|
| 484 |
+
outputs=[debug_info, show_debug_btn]
|
| 485 |
)
|
| 486 |
|
| 487 |
# Event handlers
|
| 488 |
load_models_btn.click(
|
| 489 |
+
fn=enhanced_load_models,
|
| 490 |
+
outputs=[status_text, debug_info],
|
| 491 |
show_progress=True
|
| 492 |
)
|
| 493 |
|
| 494 |
process_btn.click(
|
| 495 |
+
fn=enhanced_process_video,
|
| 496 |
inputs=[
|
| 497 |
video_input,
|
| 498 |
background_method,
|
| 499 |
custom_background,
|
| 500 |
professional_choice,
|
| 501 |
use_two_stage,
|
| 502 |
+
chroma_preset,
|
| 503 |
+
quality_preset
|
| 504 |
],
|
| 505 |
+
outputs=[video_output, result_info, debug_info],
|
| 506 |
show_progress=True
|
| 507 |
)
|
| 508 |
|
| 509 |
model_status_btn.click(
|
| 510 |
+
fn=get_enhanced_model_status,
|
| 511 |
+
outputs=[model_status_display],
|
| 512 |
show_progress=False
|
| 513 |
+
).then(
|
| 514 |
+
fn=lambda: gr.update(visible=True),
|
| 515 |
+
outputs=[model_status_display]
|
| 516 |
)
|
| 517 |
|
| 518 |
cache_status_btn.click(
|
| 519 |
+
fn=get_enhanced_cache_status,
|
| 520 |
+
outputs=[cache_status_display],
|
| 521 |
show_progress=False
|
| 522 |
+
).then(
|
| 523 |
+
fn=lambda: gr.update(visible=True),
|
| 524 |
+
outputs=[cache_status_display]
|
| 525 |
)
|
| 526 |
|
| 527 |
+
# Information and help section
|
| 528 |
+
with gr.Accordion("βΉοΈ Help & Information", open=False):
|
| 529 |
gr.Markdown(f"""
|
| 530 |
+
### π― How to Use
|
| 531 |
+
|
| 532 |
+
1. **Load Models**: Click 'Load Models' and wait for completion (first-time setup)
|
| 533 |
+
2. **Upload Video**: Choose a video file (MP4 recommended, under 5 minutes)
|
| 534 |
+
3. **Select Background**: Use professional presets or upload your own image
|
| 535 |
+
4. **Configure Quality**: Choose preset based on your speed/quality preference
|
| 536 |
+
5. **Process**: Click 'Process Video' and wait for completion
|
| 537 |
+
|
| 538 |
+
### π§ Processing Modes
|
| 539 |
|
| 540 |
+
**Single-Stage (Default)**
|
| 541 |
+
- Direct background replacement
|
| 542 |
+
- Faster processing (2-5x speed)
|
| 543 |
+
- Good quality for most use cases
|
| 544 |
+
- Recommended for: Social media, quick edits, testing
|
| 545 |
+
|
| 546 |
+
**Two-Stage (Premium)**
|
| 547 |
+
- Green screen intermediate step
|
| 548 |
+
- Cinema-quality edge compositing
|
| 549 |
+
- Advanced chroma key algorithms
|
| 550 |
+
- Recommended for: Professional content, broadcast, film
|
| 551 |
+
|
| 552 |
+
### β‘ Performance Tips
|
| 553 |
+
|
| 554 |
+
- **Fast Processing**: Use 'fast' preset, disable two-stage mode
|
| 555 |
+
- **Best Quality**: Use 'high' preset, enable two-stage mode
|
| 556 |
+
- **GPU Memory**: Processing automatically manages memory and provides fallbacks
|
| 557 |
+
- **Video Length**: Shorter videos (10-30s) process much faster
|
| 558 |
+
|
| 559 |
+
### π¨ Troubleshooting
|
| 560 |
+
|
| 561 |
+
**Models Won't Load**
|
| 562 |
+
- Check internet connection (models download from Hugging Face)
|
| 563 |
+
- Wait for downloads to complete (may take several minutes first time)
|
| 564 |
+
- Try restarting if stuck
|
| 565 |
+
|
| 566 |
+
**Processing Fails**
|
| 567 |
+
- Ensure video file is not corrupted
|
| 568 |
+
- Try shorter clips first (under 30 seconds)
|
| 569 |
+
- Check video format (MP4 works best)
|
| 570 |
+
- Verify sufficient disk space
|
| 571 |
+
|
| 572 |
+
**Poor Quality Results**
|
| 573 |
+
- Use higher quality preset
|
| 574 |
+
- Enable two-stage mode
|
| 575 |
+
- Ensure good lighting in original video
|
| 576 |
+
- Try different professional backgrounds
|
| 577 |
+
|
| 578 |
+
### π System Information
|
| 579 |
+
|
| 580 |
+
- **Core Functions**: {'β
Available' if CORE_FUNCTIONS_AVAILABLE else 'β Not Available'}
|
| 581 |
+
- **Background Library**: {'β
Available' if UTILITIES_AVAILABLE else 'β Not Available'}
|
| 582 |
+
- **Two-Stage Processing**: {'β
Available' if TWO_STAGE_AVAILABLE else 'β Not Available'}
|
| 583 |
+
- **Professional Backgrounds**: {len(PROFESSIONAL_BACKGROUNDS)} presets available
|
| 584 |
""")
|
| 585 |
|
| 586 |
+
return demo
|
| 587 |
+
|
| 588 |
+
# Compatibility function for existing imports
|
| 589 |
+
def create_ui():
|
| 590 |
+
"""Compatibility wrapper for create_interface"""
|
| 591 |
+
return create_interface()
|