| |
| """MODUS any-to-any demo β HuggingFace Space entrypoint (ZeroGPU). |
| |
| Thin ZeroGPU wrapper over the existing 3-tab demo backend (``demo_modus.py`` + |
| ``demo_my/*``). The model is loaded ONCE at startup on CPU (no GPU, no time |
| limit); each inference call moves it to the ZeroGPU-provided GPU via the |
| ``@spaces.GPU`` decorator (moving ~30GB bf16 over PCIe is seconds, whereas the |
| build+weight-load is ~5min and must NOT happen inside the GPU-time window). |
| |
| Space setup (Settings -> Variables and secrets): |
| HF_TOKEN read token for the gated weights repo (below) |
| Optional env (have sane defaults): |
| MODUS_WEIGHTS_REPO gated HF model repo with the bf16 weights + config + VAE |
| """ |
| import os |
| import sys |
|
|
| try: |
| import torch |
| print(f"[app] torch={torch.__version__} python={sys.version.split()[0]}", flush=True) |
| except Exception as _e: |
| print(f"[app] torch import failed: {_e}", flush=True) |
|
|
| |
| |
| |
| try: |
| import pillow_avif |
| except Exception as _e: |
| print(f"[app] pillow-avif-plugin unavailable: {_e}", flush=True) |
| try: |
| from pillow_heif import register_heif_opener |
| register_heif_opener() |
| except Exception as _e: |
| print(f"[app] pillow-heif unavailable: {_e}", flush=True) |
|
|
| |
| |
| |
| |
| |
| import subprocess |
| _GH = os.environ.get("GH_TOKEN") |
| if _GH: |
| print("[app] installing modus backend from EPFL-VILAB/Modus ...", flush=True) |
| subprocess.run( |
| [sys.executable, "-m", "pip", "install", "--no-deps", "--quiet", |
| "--force-reinstall", "--no-cache-dir", |
| f"git+https://x-access-token:{_GH}@github.com/EPFL-VILAB/Modus.git"], |
| check=True, |
| ) |
| print("[app] modus backend installed.", flush=True) |
| else: |
| print("[app] GH_TOKEN not set; expecting the modus backend to be present.", flush=True) |
|
|
| |
| os.environ.setdefault("MODUS_NO_MEAN_RESIZING", "1") |
| os.environ.setdefault("MODUS_FORCE_SDPA_ATTN", "1") |
| os.environ.setdefault("MODUS_TORCHVISION_FREE", "1") |
| os.environ.setdefault("MODUS_DEMO_MODALITY_CONFIG", |
| "conf/modalities/instruction_16mod_stage2.yaml") |
| os.environ.setdefault("MODUS_DEMO_MODEL_NAME", "bagel_from_json") |
| os.environ.setdefault("MODUS_DEMO_USE_EMA", "0") |
|
|
| WEIGHTS_REPO = os.environ.get("MODUS_WEIGHTS_REPO", "mqye/modus-16mod-stage3") |
|
|
| |
| from huggingface_hub import snapshot_download |
|
|
| _weights_dir = snapshot_download( |
| repo_id=WEIGHTS_REPO, |
| repo_type="model", |
| token=os.environ.get("HF_TOKEN"), |
| ) |
| |
| |
| os.environ["MODUS_DEMO_CHECKPOINT"] = _weights_dir |
| os.environ["BAGEL_MODEL_PATH"] = _weights_dir |
| print(f"[app] weights ready at {_weights_dir}", flush=True) |
|
|
| import spaces |
|
|
| |
| |
| |
| |
| import huggingface_hub as _hh |
| if not hasattr(_hh, "cached_download"): |
| _hh.cached_download = _hh.hf_hub_download |
|
|
| |
| import demo_modus |
|
|
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| _GPU_DURATION = int(os.environ.get("MODUS_GPU_DURATION", "120")) |
| demo_modus.run_task = spaces.GPU(duration=_GPU_DURATION)(demo_modus.run_task) |
| |
| |
| |
| |
| demo_modus.tab3_generate = spaces.GPU(duration=180)(demo_modus.tab3_generate) |
|
|
| |
| |
| |
| try: |
| demo_modus.HOLDER.ensure_loaded() |
| print("[app] model loaded (CPU) at startup", flush=True) |
| except Exception as e: |
| demo_modus.HOLDER.load_error = str(e) |
| print(f"[app] startup model load failed: {e}", flush=True) |
|
|
| |
| |
| |
| |
| _EX_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), "test_images") |
| def _space_example_images(): |
| if not os.path.isdir(_EX_DIR): |
| return [] |
| return [os.path.join(_EX_DIR, f) for f in sorted(os.listdir(_EX_DIR)) |
| if f.lower().endswith((".jpg", ".jpeg", ".png", ".webp")) |
| and "_seg." not in f.lower()] |
| demo_modus._example_images = _space_example_images |
| print(f"[app] {len(_space_example_images())} example images from {_EX_DIR}", flush=True) |
|
|
| |
| |
| |
| |
| try: |
| import gradio_client.utils as _gcu |
|
|
| _orig_j2p = _gcu._json_schema_to_python_type |
|
|
| def _safe_j2p(schema, defs=None): |
| if isinstance(schema, bool): |
| return "Any" |
| return _orig_j2p(schema, defs) |
|
|
| _gcu._json_schema_to_python_type = _safe_j2p |
|
|
| _orig_get_type = _gcu.get_type |
|
|
| def _safe_get_type(schema): |
| if isinstance(schema, bool): |
| return "Any" |
| return _orig_get_type(schema) |
|
|
| _gcu.get_type = _safe_get_type |
| except Exception as _e: |
| print(f"[app] gradio_client schema patch skipped: {_e}", flush=True) |
|
|
| demo = demo_modus.build_ui() |
| demo.queue().launch(server_name="0.0.0.0", server_port=7860, show_api=False) |
|
|