|
|
import os |
|
|
import sys |
|
|
import subprocess |
|
|
|
|
|
|
|
|
import gradio as gr |
|
|
|
|
|
|
|
|
def _launch(): |
|
|
|
|
|
|
|
|
if not os.environ.get("DETECTRON2_INSTALLED"): |
|
|
print("π§ Installing CropFormer dependencies...") |
|
|
try: |
|
|
|
|
|
subprocess.check_call([ |
|
|
sys.executable, "-m", "pip", "install", |
|
|
"mmcv>=1.4.0,<2.0.0", "cython", "shapely", "timm", "h5py", "scikit-image" |
|
|
]) |
|
|
subprocess.check_call([ |
|
|
sys.executable, "-m", "pip", "install", "--no-build-isolation", |
|
|
"git+https://github.com/facebookresearch/pytorch3d.git" |
|
|
]) |
|
|
print("β
CropFormer dependencies installed!") |
|
|
|
|
|
print("π§ Installing detectron2 from vendored source...") |
|
|
subprocess.check_call([ |
|
|
sys.executable, "-m", "pip", "install", |
|
|
"--no-build-isolation", "-e", "MaskClustering/third_party/detectron2" |
|
|
]) |
|
|
print("β
detectron2 installed successfully!") |
|
|
|
|
|
|
|
|
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("π§ Compiling MSDeformAttn CUDA operators...") |
|
|
ops_dir = os.path.join(cropformer_root, "mask2former/modeling/pixel_decoder/ops") |
|
|
if os.path.exists(ops_dir): |
|
|
try: |
|
|
|
|
|
subprocess.check_call(["sh", "make.sh"], cwd=ops_dir) |
|
|
print("β
MSDeformAttn ops compiled successfully!") |
|
|
except subprocess.CalledProcessError as e: |
|
|
print(f"β οΈ CUDA compilation failed, trying CPU-only mode: {e}") |
|
|
try: |
|
|
|
|
|
subprocess.check_call( |
|
|
["python", "setup.py", "build", "install"], |
|
|
cwd=ops_dir, |
|
|
env={**os.environ, "FORCE_CUDA": "0"} |
|
|
) |
|
|
print("β
MSDeformAttn ops compiled (CPU mode)!") |
|
|
except subprocess.CalledProcessError as e2: |
|
|
print(f"β οΈ MSDeformAttn compilation failed (non-critical): {e2}") |
|
|
else: |
|
|
print(f"β οΈ MSDeformAttn ops directory not found: {ops_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() |
|
|
|
|
|
|
|
|
|