File size: 1,874 Bytes
85805ad
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
"""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()