| | import os |
| | import sys |
| | import subprocess |
| |
|
| | |
| | os.environ["GRADIO_SSR_MODE"] = "false" |
| |
|
| | |
| | |
| | IS_HF_SPACE = bool(os.environ.get("SPACE_ID")) |
| | IS_ZERO_GPU = IS_HF_SPACE or os.environ.get("ZERO_GPU_MODE", "0") == "1" or os.environ.get("SPACES_ZERO_GPU", "0") == "1" |
| |
|
| | if IS_HF_SPACE: |
| | print(f"π€ Running on Hugging Face Space: {os.environ.get('SPACE_ID')}") |
| |
|
| | |
| | |
| | try: |
| | import spaces |
| | HAS_SPACES = True |
| | print("β
Spaces module available - Zero GPU support enabled") |
| | except ImportError: |
| | HAS_SPACES = False |
| | spaces = None |
| | print("βΉοΈ Spaces module not available - running without Zero GPU") |
| |
|
| | import gradio as gr |
| |
|
| |
|
| | def _launch(): |
| | |
| | |
| | repo_root = os.path.dirname(os.path.abspath(__file__)) |
| | projects_root = os.path.join(repo_root, "MaskClustering/third_party/detectron2/projects") |
| |
|
| | |
| | cropformer_path = os.path.join(projects_root, "CropFormer") |
| | if cropformer_path not in sys.path: |
| | sys.path.insert(0, cropformer_path) |
| |
|
| | |
| | deeplab_path = os.path.join(projects_root, "DeepLab") |
| | if deeplab_path not in sys.path: |
| | sys.path.insert(0, deeplab_path) |
| |
|
| | |
| | if IS_ZERO_GPU: |
| | print("π Detected Zero GPU environment, optimizing settings...") |
| | os.environ["ZERO_GPU_MODE"] = "1" |
| | os.environ["MAX_GPU_MEMORY"] = "15GB" |
| | os.environ["BATCH_SIZE"] = "2" |
| | os.environ["MAX_IMAGES"] = "20" |
| | os.environ["MAX_RESOLUTION"] = "512" |
| |
|
| | |
| | |
| | if not os.environ.get("DETECTRON2_INSTALLED"): |
| | print("π§ Installing CropFormer dependencies...") |
| |
|
| | |
| | pytorch3d_installed = False |
| | try: |
| | import pytorch3d |
| | pytorch3d_installed = True |
| | print(f"β
pytorch3d already installed (version: {getattr(pytorch3d, '__version__', 'unknown')})") |
| | except ImportError: |
| | pass |
| |
|
| | try: |
| | |
| | subprocess.check_call([ |
| | sys.executable, "-m", "pip", "install", "setuptools<81" |
| | ]) |
| | |
| | |
| | subprocess.check_call([ |
| | sys.executable, "-m", "pip", "install", "--no-build-isolation", |
| | "mmcv>=1.4.0,<2.0.0" |
| | ]) |
| |
|
| | |
| | if not pytorch3d_installed: |
| | |
| | |
| | print("π§ Installing pytorch3d from prebuilt wheel...") |
| | try: |
| | subprocess.check_call([ |
| | sys.executable, "-m", "pip", "install", |
| | "--extra-index-url", "https://miropsota.github.io/torch_packages_builder", |
| | "pytorch3d" |
| | ]) |
| | print("β
pytorch3d installed from prebuilt wheel!") |
| | except subprocess.CalledProcessError: |
| | |
| | print("β οΈ Prebuilt wheel not found, compiling from source...") |
| | cuda_env = { |
| | **os.environ, |
| | "FORCE_CUDA": "1", |
| | "TORCH_CUDA_ARCH_LIST": "7.5;8.0;8.6;8.9;9.0", |
| | } |
| | subprocess.check_call([ |
| | sys.executable, "-m", "pip", "install", "--no-build-isolation", |
| | "git+https://github.com/facebookresearch/pytorch3d.git" |
| | ], env=cuda_env) |
| | print("β
CropFormer dependencies installed!") |
| |
|
| | |
| | detectron2_installed = False |
| | try: |
| | import detectron2 |
| | detectron2_installed = True |
| | print(f"β
detectron2 already installed (version: {getattr(detectron2, '__version__', 'unknown')})") |
| | except ImportError: |
| | pass |
| |
|
| | if not detectron2_installed: |
| | |
| | |
| | print("π§ Installing detectron2 from prebuilt wheel...") |
| | try: |
| | subprocess.check_call([ |
| | sys.executable, "-m", "pip", "install", |
| | "--extra-index-url", "https://miropsota.github.io/torch_packages_builder", |
| | "detectron2" |
| | ]) |
| | print("β
detectron2 installed from prebuilt wheel!") |
| | except subprocess.CalledProcessError: |
| | |
| | print("β οΈ Prebuilt wheel not found, compiling from vendored source...") |
| | cuda_env = { |
| | **os.environ, |
| | "FORCE_CUDA": "1", |
| | "TORCH_CUDA_ARCH_LIST": "7.5;8.0;8.6;8.9;9.0", |
| | } |
| | subprocess.check_call([ |
| | sys.executable, "-m", "pip", "install", |
| | "--no-build-isolation", "-e", "MaskClustering/third_party/detectron2" |
| | ], env=cuda_env) |
| | print("β
detectron2 compiled and installed!") |
| |
|
| | |
| | print("π§ Installing MultiScaleDeformableAttention from prebuilt wheel...") |
| | try: |
| | subprocess.check_call([ |
| | sys.executable, "-m", "pip", "install", |
| | "--extra-index-url", "https://miropsota.github.io/torch_packages_builder", |
| | "MultiScaleDeformableAttention" |
| | ]) |
| | print("β
MultiScaleDeformableAttention installed from prebuilt wheel!") |
| | except subprocess.CalledProcessError: |
| | |
| | print("β οΈ Prebuilt wheel not found, compiling MSDeformAttn from source...") |
| | repo_root = os.path.dirname(os.path.abspath(__file__)) |
| | cropformer_root = os.path.join(repo_root, "MaskClustering/third_party/detectron2/projects/CropFormer") |
| | ops_dir = os.path.join(cropformer_root, "mask2former/modeling/pixel_decoder/ops") |
| | if 'cuda_env' not in locals(): |
| | cuda_env = { |
| | **os.environ, |
| | "FORCE_CUDA": "1", |
| | "TORCH_CUDA_ARCH_LIST": "7.5;8.0;8.6;8.9;9.0", |
| | } |
| | subprocess.check_call( |
| | [sys.executable, "-m", "pip", "install", "-e", ".", "--no-build-isolation"], |
| | cwd=ops_dir, |
| | env=cuda_env |
| | ) |
| | print("β
MSDeformAttn compiled and installed!") |
| |
|
| | |
| | repo_root = os.path.dirname(os.path.abspath(__file__)) |
| | cropformer_root = os.path.join(repo_root, "MaskClustering/third_party/detectron2/projects/CropFormer") |
| | print("π§ Compiling entity_api PythonAPI...") |
| | entity_api_dir = os.path.join(cropformer_root, "entity_api/PythonAPI") |
| | if os.path.exists(entity_api_dir): |
| | try: |
| | subprocess.check_call(["make"], cwd=entity_api_dir, shell=True) |
| | print("β
entity_api compiled successfully!") |
| | except subprocess.CalledProcessError as e: |
| | print(f"β οΈ entity_api compilation failed (non-critical): {e}") |
| | else: |
| | print(f"β οΈ entity_api directory not found: {entity_api_dir}") |
| | |
| | print("π Restarting to load detectron2...") |
| | |
| | |
| | os.environ["DETECTRON2_INSTALLED"] = "1" |
| | |
| | |
| | os.execv(sys.executable, [sys.executable] + sys.argv) |
| | except subprocess.CalledProcessError as e: |
| | print(f"β Failed to install dependencies: {e}") |
| | raise |
| | else: |
| | |
| | print("π Verifying detectron2 and mmcv imports...") |
| | try: |
| | import detectron2 |
| | import mmcv |
| | print(f"β
detectron2 available (version: {getattr(detectron2, '__version__', 'unknown')})") |
| | print(f"β
mmcv available (version: {getattr(mmcv, '__version__', 'unknown')})") |
| | except ImportError as e: |
| | print(f"β Required module cannot be imported: {e}") |
| | print(f" sys.path: {sys.path}") |
| | raise |
| | |
| | |
| | |
| | try: |
| | import gradio_client.utils as _gcu |
| |
|
| | if hasattr(_gcu, "_json_schema_to_python_type"): |
| | _orig = _gcu._json_schema_to_python_type |
| |
|
| | def _json_schema_to_python_type_patched(schema, defs=None): |
| | if isinstance(schema, bool): |
| | return "Any" |
| | return _orig(schema, defs) |
| |
|
| | _gcu._json_schema_to_python_type = _json_schema_to_python_type_patched |
| | except Exception: |
| | pass |
| |
|
| | |
| | import mvp |
| |
|
| | port = int(os.getenv("PORT", "7860")) |
| | |
| | mvp.demo.queue(max_size=20).launch( |
| | server_name="0.0.0.0", |
| | server_port=port, |
| | show_error=True, |
| | share=False, |
| | show_api=False, |
| | ) |
| |
|
| |
|
| | if __name__ == "__main__": |
| | _launch() |
| |
|
| |
|
| |
|