Spaces:
Running on Zero
Running on Zero
Commit ·
e7ca840
1
Parent(s): 7c51531
update requirements
Browse files- bootstrap.py +33 -0
- models.py +9 -4
- requirements.txt +6 -1
bootstrap.py
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Runtime installation of the gigacheck package without its pinned deps.
|
| 2 |
+
|
| 3 |
+
gigacheck pins ``torch==2.3.1`` in its ``install_requires``, which conflicts
|
| 4 |
+
with the ``torch==2.8.0`` this demo needs. Installing gigacheck with
|
| 5 |
+
``--no-deps`` lets pip skip that pin entirely; the inference path only needs
|
| 6 |
+
``numpy``, ``scipy``, ``dacite`` and ``omegaconf`` on top of torch/transformers,
|
| 7 |
+
which are declared in ``requirements.txt`` and installed normally.
|
| 8 |
+
"""
|
| 9 |
+
|
| 10 |
+
from __future__ import annotations
|
| 11 |
+
|
| 12 |
+
import importlib.util
|
| 13 |
+
import subprocess
|
| 14 |
+
import sys
|
| 15 |
+
|
| 16 |
+
from loguru import logger
|
| 17 |
+
|
| 18 |
+
GIGACHECK_SPEC = "git+https://github.com/ai-forever/gigacheck"
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
def ensure_gigacheck() -> None:
|
| 22 |
+
"""Install the gigacheck package on first run, skipping its pinned deps.
|
| 23 |
+
|
| 24 |
+
No-op when gigacheck is already importable (e.g. installed in the image),
|
| 25 |
+
so repeated app restarts do not reinstall it.
|
| 26 |
+
"""
|
| 27 |
+
if importlib.util.find_spec("gigacheck") is not None:
|
| 28 |
+
return
|
| 29 |
+
logger.info("Installing gigacheck with --no-deps from {}", GIGACHECK_SPEC)
|
| 30 |
+
subprocess.run(
|
| 31 |
+
[sys.executable, "-m", "pip", "install", "--no-deps", GIGACHECK_SPEC],
|
| 32 |
+
check=True,
|
| 33 |
+
)
|
models.py
CHANGED
|
@@ -6,9 +6,14 @@ import torch
|
|
| 6 |
from loguru import logger
|
| 7 |
from transformers import AutoModel
|
| 8 |
|
|
|
|
| 9 |
from rendering import AiInterval
|
| 10 |
from validation import CONFIG
|
| 11 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
try:
|
| 13 |
import spaces
|
| 14 |
|
|
@@ -38,10 +43,10 @@ def select_device() -> str:
|
|
| 38 |
decorated function, so the presence of the ``spaces`` package implies a GPU.
|
| 39 |
|
| 40 |
Returns:
|
| 41 |
-
``"cuda
|
| 42 |
"""
|
| 43 |
if torch.cuda.is_available() or HAS_SPACES:
|
| 44 |
-
return "cuda
|
| 45 |
return "cpu"
|
| 46 |
|
| 47 |
|
|
@@ -65,7 +70,7 @@ detector_model = AutoModel.from_pretrained(
|
|
| 65 |
)
|
| 66 |
|
| 67 |
|
| 68 |
-
@gpu(duration=
|
| 69 |
def classify_text(text: str) -> tuple[str, float, float]:
|
| 70 |
"""Classify a text as human-written or AI-generated.
|
| 71 |
|
|
@@ -85,7 +90,7 @@ def classify_text(text: str) -> tuple[str, float, float]:
|
|
| 85 |
return label, p_human, p_ai
|
| 86 |
|
| 87 |
|
| 88 |
-
@gpu(duration=
|
| 89 |
def detect_intervals(text: str, conf_threshold: float) -> list[AiInterval]:
|
| 90 |
"""Detect character spans likely written by an AI model.
|
| 91 |
|
|
|
|
| 6 |
from loguru import logger
|
| 7 |
from transformers import AutoModel
|
| 8 |
|
| 9 |
+
from bootstrap import ensure_gigacheck
|
| 10 |
from rendering import AiInterval
|
| 11 |
from validation import CONFIG
|
| 12 |
|
| 13 |
+
# gigacheck must be importable before any from_pretrained() loads the remote
|
| 14 |
+
# model code, which does `from gigacheck.model... import ...`.
|
| 15 |
+
ensure_gigacheck()
|
| 16 |
+
|
| 17 |
try:
|
| 18 |
import spaces
|
| 19 |
|
|
|
|
| 43 |
decorated function, so the presence of the ``spaces`` package implies a GPU.
|
| 44 |
|
| 45 |
Returns:
|
| 46 |
+
``"cuda"`` when a GPU is (or will be) available, otherwise ``"cpu"``.
|
| 47 |
"""
|
| 48 |
if torch.cuda.is_available() or HAS_SPACES:
|
| 49 |
+
return "cuda"
|
| 50 |
return "cpu"
|
| 51 |
|
| 52 |
|
|
|
|
| 70 |
)
|
| 71 |
|
| 72 |
|
| 73 |
+
@gpu(duration=180)
|
| 74 |
def classify_text(text: str) -> tuple[str, float, float]:
|
| 75 |
"""Classify a text as human-written or AI-generated.
|
| 76 |
|
|
|
|
| 90 |
return label, p_human, p_ai
|
| 91 |
|
| 92 |
|
| 93 |
+
@gpu(duration=180)
|
| 94 |
def detect_intervals(text: str, conf_threshold: float) -> list[AiInterval]:
|
| 95 |
"""Detect character spans likely written by an AI model.
|
| 96 |
|
requirements.txt
CHANGED
|
@@ -6,4 +6,9 @@ sentencepiece
|
|
| 6 |
loguru
|
| 7 |
pydantic
|
| 8 |
huggingface_hub
|
| 9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
loguru
|
| 7 |
pydantic
|
| 8 |
huggingface_hub
|
| 9 |
+
numpy
|
| 10 |
+
scipy
|
| 11 |
+
dacite
|
| 12 |
+
omegaconf
|
| 13 |
+
# gigacheck is installed at runtime with --no-deps (see bootstrap.py) to avoid
|
| 14 |
+
# its torch==2.3.1 pin clashing with torch==2.8.0.
|