Spaces:
Running
Running
Update server.py
Browse files
server.py
CHANGED
|
@@ -1,12 +1,5 @@
|
|
| 1 |
"""FastAPI backend for the LFM2.5 Spellchecker demo (Docker Space).
|
| 2 |
|
| 3 |
-
Loads the published model from the Hub (pinned to `main`, so the Space always serves the current best
|
| 4 |
-
checkpoint), exposes POST /api/correct, and serves the static frontend in static/. No Gradio.
|
| 5 |
-
|
| 6 |
-
The model repo is private, so HF_TOKEN (a Space secret) is needed to pull it. Pinned library versions
|
| 7 |
-
(see requirements.txt) match the environment the model was validated against — the encoder's custom
|
| 8 |
-
bidirectional-mask code is sensitive to the transformers version.
|
| 9 |
-
|
| 10 |
uvicorn server:app --host 0.0.0.0 --port 7860
|
| 11 |
"""
|
| 12 |
import difflib
|
|
@@ -50,17 +43,11 @@ try:
|
|
| 50 |
except RuntimeError:
|
| 51 |
pass
|
| 52 |
|
| 53 |
-
MODEL_ID = os.environ.get("SPELLCHECKER_MODEL", "LiquidAI/LFM2.5-
|
| 54 |
-
# Pin to the EXACT published commit so the container can never serve stale cached weights/remote-code
|
| 55 |
-
# (the bug we hit: a rebuild kept serving old, tagger-only behaviour). Bump on each publish, or override.
|
| 56 |
MODEL_REV = os.environ.get("SPELLCHECKER_REVISION", "65a4a90af31205d2f7ef66b6a68d7b3d276adfdd")
|
| 57 |
STATIC = os.path.join(os.path.dirname(os.path.abspath(__file__)), "static")
|
| 58 |
|
| 59 |
print(f"[server] loading {MODEL_ID}@{MODEL_REV} on {_CPUS} CPU thread(s) ...", flush=True)
|
| 60 |
-
# fp32, NOT fp16: x86 CPUs have no fast half-precision path, so .half() ran ~4.5x SLOWER here (matmuls
|
| 61 |
-
# emulated per-op) for identical corrections. Uniform .float() casts every submodule — including the
|
| 62 |
-
# reranker, which matches the tagger's dtype — so oneDNN/MKL use real f32 GEMM. int8 dynamic quant was
|
| 63 |
-
# rejected: only ~15% faster than fp32 but it corrupts edits (GEC tagging is precision-sensitive).
|
| 64 |
_model = AutoModel.from_pretrained(MODEL_ID, revision=MODEL_REV, trust_remote_code=True,
|
| 65 |
token=os.environ.get("HF_TOKEN")).float().eval()
|
| 66 |
_mem_bytes = sum(t.numel() * t.element_size() for t in (*_model.parameters(), *_model.buffers()))
|
|
@@ -100,9 +87,6 @@ def detok(text: str) -> str:
|
|
| 100 |
return re.sub(r"\s+", " ", text).strip()
|
| 101 |
|
| 102 |
|
| 103 |
-
# Startup self-test over the REAL user path (tokenize -> correct -> detok), logged + exposed at
|
| 104 |
-
# /api/health: a correctly-deployed full system leaves this clean sentence UNCHANGED. If it changes,
|
| 105 |
-
# the deploy is wrong (stale model, reranker inactive, or contraction handling broken) — no guessing.
|
| 106 |
_PROBE_IN = "That's a fair point, let's discuss it tomorrow."
|
| 107 |
try:
|
| 108 |
_PROBE_OUT = detok(_model.correct([tokenize(_PROBE_IN)], max_iter=3)[0])
|
|
|
|
| 1 |
"""FastAPI backend for the LFM2.5 Spellchecker demo (Docker Space).
|
| 2 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
uvicorn server:app --host 0.0.0.0 --port 7860
|
| 4 |
"""
|
| 5 |
import difflib
|
|
|
|
| 43 |
except RuntimeError:
|
| 44 |
pass
|
| 45 |
|
| 46 |
+
MODEL_ID = os.environ.get("SPELLCHECKER_MODEL", "LiquidAI/LFM2.5-Encoder-350M-Spellchecker")
|
|
|
|
|
|
|
| 47 |
MODEL_REV = os.environ.get("SPELLCHECKER_REVISION", "65a4a90af31205d2f7ef66b6a68d7b3d276adfdd")
|
| 48 |
STATIC = os.path.join(os.path.dirname(os.path.abspath(__file__)), "static")
|
| 49 |
|
| 50 |
print(f"[server] loading {MODEL_ID}@{MODEL_REV} on {_CPUS} CPU thread(s) ...", flush=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
_model = AutoModel.from_pretrained(MODEL_ID, revision=MODEL_REV, trust_remote_code=True,
|
| 52 |
token=os.environ.get("HF_TOKEN")).float().eval()
|
| 53 |
_mem_bytes = sum(t.numel() * t.element_size() for t in (*_model.parameters(), *_model.buffers()))
|
|
|
|
| 87 |
return re.sub(r"\s+", " ", text).strip()
|
| 88 |
|
| 89 |
|
|
|
|
|
|
|
|
|
|
| 90 |
_PROBE_IN = "That's a fair point, let's discuss it tomorrow."
|
| 91 |
try:
|
| 92 |
_PROBE_OUT = detok(_model.correct([tokenize(_PROBE_IN)], max_iter=3)[0])
|