Spaces:
Paused
Paused
Add HF token authentication for gated signature model
Browse files- api.py +26 -0
- signature/inference.py +4 -1
api.py
CHANGED
|
@@ -34,6 +34,32 @@ app.add_middleware(
|
|
| 34 |
)
|
| 35 |
|
| 36 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
@app.get("/")
|
| 38 |
async def root():
|
| 39 |
"""Health check endpoint."""
|
|
|
|
| 34 |
)
|
| 35 |
|
| 36 |
|
| 37 |
+
@app.on_event("startup")
|
| 38 |
+
async def startup_event():
|
| 39 |
+
"""Authenticate with Hugging Face and pre-load models if possible."""
|
| 40 |
+
# Authenticate with Hugging Face if token is available
|
| 41 |
+
# HF Spaces automatically provides HF_TOKEN, but we also check HUGGINGFACE_TOKEN
|
| 42 |
+
hf_token = os.environ.get("HF_TOKEN") or os.environ.get("HUGGINGFACE_TOKEN")
|
| 43 |
+
if hf_token:
|
| 44 |
+
try:
|
| 45 |
+
from huggingface_hub import login
|
| 46 |
+
login(token=hf_token)
|
| 47 |
+
print("β Authenticated with Hugging Face")
|
| 48 |
+
except Exception as e:
|
| 49 |
+
print(f"β Warning: Failed to authenticate with HF: {e}")
|
| 50 |
+
else:
|
| 51 |
+
print("β Warning: No HF_TOKEN found. Gated models may not work.")
|
| 52 |
+
print(" Set HF_TOKEN in Space Settings β Secrets for gated model access.")
|
| 53 |
+
|
| 54 |
+
# Check if stamp model exists
|
| 55 |
+
stamp_model_path = Path("stamp_detector/stamp_model.pt")
|
| 56 |
+
if stamp_model_path.exists():
|
| 57 |
+
print("β Stamp model found")
|
| 58 |
+
else:
|
| 59 |
+
print("β Warning: Stamp model not found at stamp_detector/stamp_model.pt")
|
| 60 |
+
print(" Please upload stamp_model.pt to the Space.")
|
| 61 |
+
|
| 62 |
+
|
| 63 |
@app.get("/")
|
| 64 |
async def root():
|
| 65 |
"""Health check endpoint."""
|
signature/inference.py
CHANGED
|
@@ -35,9 +35,12 @@ def detect_signatures(image_path, model=None, output_dir=None, signatures_dir=No
|
|
| 35 |
model_path = str(local_model_path)
|
| 36 |
else:
|
| 37 |
try:
|
|
|
|
|
|
|
| 38 |
model_path = hf_hub_download(
|
| 39 |
repo_id="tech4humans/yolov8s-signature-detector",
|
| 40 |
-
filename="yolov8s.pt"
|
|
|
|
| 41 |
)
|
| 42 |
except Exception as e:
|
| 43 |
raise RuntimeError(f"Failed to load signature model: {e}")
|
|
|
|
| 35 |
model_path = str(local_model_path)
|
| 36 |
else:
|
| 37 |
try:
|
| 38 |
+
# Get HF token from environment (for gated models)
|
| 39 |
+
hf_token = os.environ.get("HF_TOKEN") or os.environ.get("HUGGINGFACE_TOKEN")
|
| 40 |
model_path = hf_hub_download(
|
| 41 |
repo_id="tech4humans/yolov8s-signature-detector",
|
| 42 |
+
filename="yolov8s.pt",
|
| 43 |
+
token=hf_token # Pass token for gated repos
|
| 44 |
)
|
| 45 |
except Exception as e:
|
| 46 |
raise RuntimeError(f"Failed to load signature model: {e}")
|