MogensR commited on
Commit
9af7e52
Β·
1 Parent(s): 138b104

Update core/app.py

Browse files
Files changed (1) hide show
  1. core/app.py +27 -5
core/app.py CHANGED
@@ -6,8 +6,7 @@
6
  from __future__ import annotations
7
  # ── Early env/threading hygiene (safe default to silence libgomp) ────────────
8
  import os
9
- if not os.environ.get("OMP_NUM_THREADS", "").isdigit():
10
- os.environ["OMP_NUM_THREADS"] = "2"
11
  # If you use early_env in your project, keep this import (harmless if absent)
12
  try:
13
  import early_env # sets OMP/MKL/OPENBLAS + torch threads safely
@@ -18,7 +17,25 @@
18
  import traceback
19
  import sys
20
  from pathlib import Path
21
- from utils.cv_processing import validate_video_file
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22
  # ── Gradio schema patch (HF quirk) ───────────────────────────────────────────
23
  try:
24
  import gradio_client.utils as gc_utils
@@ -35,6 +52,8 @@ def _patched_get_type(schema):
35
  except Exception as e:
36
  logger.warning(f"Gradio patch failed: {e}")
37
  # ── Core config + components ─────────────────────────────────────────────────
 
 
38
  from utils.hardware.device_manager import DeviceManager
39
  from utils.system.memory_manager import MemoryManager
40
  # Try to import the new split loaders first, fall back to old if needed
@@ -48,6 +67,7 @@ def _patched_get_type(schema):
48
  from processing.video.video_processor import CoreVideoProcessor
49
  from processing.audio.audio_processor import AudioProcessor
50
  from utils.monitoring.progress_tracker import ProgressTracker
 
51
  # ── Optional Two-Stage import ────────────────────────────────────────────────
52
  TWO_STAGE_AVAILABLE = False
53
  TWO_STAGE_IMPORT_ORIGIN = ""
@@ -224,7 +244,7 @@ def process_video(
224
  if use_two_stage:
225
  if not TWO_STAGE_AVAILABLE or self.two_stage_processor is None:
226
  return None, None, "Two-stage processing not available"
227
- return self._process_two_stage(
228
  video_path,
229
  background_choice,
230
  custom_background_path,
@@ -232,8 +252,9 @@ def process_video(
232
  chroma_preset,
233
  key_color_mode,
234
  )
 
235
  else:
236
- return self._process_single_stage(
237
  video_path,
238
  background_choice,
239
  custom_background_path,
@@ -241,6 +262,7 @@ def process_video(
241
  preview_mask,
242
  preview_greenscreen,
243
  )
 
244
  except VideoProcessingError as e:
245
  logger.error(f"Processing failed: {e}")
246
  return None, None, f"Processing failed: {e}"
 
6
  from __future__ import annotations
7
  # ── Early env/threading hygiene (safe default to silence libgomp) ────────────
8
  import os
9
+ os.environ["OMP_NUM_THREADS"] = "2" # Force valid value early
 
10
  # If you use early_env in your project, keep this import (harmless if absent)
11
  try:
12
  import early_env # sets OMP/MKL/OPENBLAS + torch threads safely
 
17
  import traceback
18
  import sys
19
  from pathlib import Path
20
+ from typing import Optional, Tuple, Dict, Any, Callable
21
+ # Mitigate CUDA fragmentation (must be set before importing torch)
22
+ if "PYTORCH_CUDA_ALLOC_CONF" not in os.environ:
23
+ os.environ["PYTORCH_CUDA_ALLOC_CONF"] = "expandable_segments:True,max_split_size_mb:128"
24
+ # ── Logging ──────────────────────────────────────────────────────────────────
25
+ logging.basicConfig(
26
+ level=logging.INFO,
27
+ format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
28
+ )
29
+ logger = logging.getLogger("core.app")
30
+ # ── Ensure project root importable ───────────────────────────────────────────
31
+ PROJECT_FILE = Path(__file__).resolve()
32
+ CORE_DIR = PROJECT_FILE.parent
33
+ ROOT = CORE_DIR.parent
34
+ if str(ROOT) not in sys.path:
35
+ sys.path.insert(0, str(ROOT))
36
+ # Create loader directories if they don't exist
37
+ loaders_dir = ROOT / "models" / "loaders"
38
+ loaders_dir.mkdir(parents=True, exist_ok=True)
39
  # ── Gradio schema patch (HF quirk) ───────────────────────────────────────────
40
  try:
41
  import gradio_client.utils as gc_utils
 
52
  except Exception as e:
53
  logger.warning(f"Gradio patch failed: {e}")
54
  # ── Core config + components ─────────────────────────────────────────────────
55
+ from config.app_config import get_config
56
+ from core.exceptions import ModelLoadingError, VideoProcessingError
57
  from utils.hardware.device_manager import DeviceManager
58
  from utils.system.memory_manager import MemoryManager
59
  # Try to import the new split loaders first, fall back to old if needed
 
67
  from processing.video.video_processor import CoreVideoProcessor
68
  from processing.audio.audio_processor import AudioProcessor
69
  from utils.monitoring.progress_tracker import ProgressTracker
70
+ from utils.cv_processing import validate_video_file
71
  # ── Optional Two-Stage import ────────────────────────────────────────────────
72
  TWO_STAGE_AVAILABLE = False
73
  TWO_STAGE_IMPORT_ORIGIN = ""
 
244
  if use_two_stage:
245
  if not TWO_STAGE_AVAILABLE or self.two_stage_processor is None:
246
  return None, None, "Two-stage processing not available"
247
+ final, green, msg = self._process_two_stage(
248
  video_path,
249
  background_choice,
250
  custom_background_path,
 
252
  chroma_preset,
253
  key_color_mode,
254
  )
255
+ return final, green, msg
256
  else:
257
+ final, green, msg = self._process_single_stage(
258
  video_path,
259
  background_choice,
260
  custom_background_path,
 
262
  preview_mask,
263
  preview_greenscreen,
264
  )
265
+ return final, green, msg
266
  except VideoProcessingError as e:
267
  logger.error(f"Processing failed: {e}")
268
  return None, None, f"Processing failed: {e}"