Jack
Deploy TrueSight ZeroGPU demo
85805ad
Raw History Blame Contribute Delete
1.87 kB
"""Hugging Face ZeroGPU entry point for the TrueSight Gradio demo."""
from __future__ import annotations
import importlib.util
import sys
from pathlib import Path
import spaces
from huggingface_hub import hf_hub_download
ROOT = Path(__file__).resolve().parent
DEMO_DIR = ROOT / "demo"
M1_ROOT = DEMO_DIR / "m1"
MODEL_REPO = "ElenaZhan/TrueSight-AIGC-Detector"
MODEL_REVISION = "bf98b12cf40dfa663965d5eabacc02cdc1064be4"
WEIGHTS = Path(
hf_hub_download(
repo_id=MODEL_REPO,
filename="best.pt",
revision=MODEL_REVISION,
)
)
# Load demo/app.py under a unique module name so this top-level app.py does not shadow it.
sys.path.insert(0, str(DEMO_DIR))
spec = importlib.util.spec_from_file_location("truesight_demo", DEMO_DIR / "app.py")
if spec is None or spec.loader is None:
raise RuntimeError("cannot load demo/app.py")
core = importlib.util.module_from_spec(spec)
sys.modules[spec.name] = core
spec.loader.exec_module(core)
# ZeroGPU provides CUDA emulation during startup, so the model can be placed on CUDA here
# and the real GPU is allocated only while a decorated callback is running.
core.PRED = core.INF.Predictor(
weights=str(WEIGHTS),
device="cuda",
m1_root=str(M1_ROOT),
)
_on_upload = core.on_upload
_on_apply = core.on_apply
_on_continue = core.on_continue
@spaces.GPU(duration=30)
def gpu_upload(img, state):
return _on_upload(img, state)
@spaces.GPU(duration=30)
def gpu_apply(state, selected):
return _on_apply(state, selected)
@spaces.GPU(duration=30)
def gpu_continue(state, selected):
return _on_continue(state, selected)
# build_ui resolves these names from the imported module's globals.
core.on_upload = gpu_upload
core.on_apply = gpu_apply
core.on_continue = gpu_continue
demo = core.build_ui()
if __name__ == "__main__":
demo.queue(default_concurrency_limit=1).launch()