Update w private commands
Browse files
app.py
CHANGED
|
@@ -1,11 +1,13 @@
|
|
| 1 |
from functools import lru_cache
|
|
|
|
|
|
|
| 2 |
import json
|
| 3 |
from pathlib import Path
|
| 4 |
import re
|
| 5 |
from urllib.parse import quote, urlparse
|
| 6 |
|
| 7 |
import duckdb
|
| 8 |
-
from fastapi import FastAPI, HTTPException
|
| 9 |
from huggingface_hub import hf_hub_download
|
| 10 |
import tldextract
|
| 11 |
|
|
@@ -15,7 +17,7 @@ BINARY_FILENAME = "MLP-Inference/Text+GAT/mlpInfer_binaryClassifcation_dec2024_e
|
|
| 15 |
REGRESSION_SOURCE = f"https://huggingface.co/datasets/{DATASET_REPO}/blob/main/{quote(REGRESSION_FILENAME, safe='/')}"
|
| 16 |
BINARY_SOURCE = f"https://huggingface.co/datasets/{DATASET_REPO}/blob/main/{quote(BINARY_FILENAME, safe='/')}"
|
| 17 |
|
| 18 |
-
API_VERSION = "0.2.
|
| 19 |
DATA_CUTOFF_MONTH = "2024-12"
|
| 20 |
METHOD = "GAT-TEXT"
|
| 21 |
|
|
@@ -35,6 +37,17 @@ _extract = tldextract.TLDExtract(include_psl_private_domains=True)
|
|
| 35 |
_DOMAIN_RE = re.compile(r"^(?:[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?\.)+[a-z0-9-]{2,}$")
|
| 36 |
|
| 37 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
def _canonicalize(value: str) -> str:
|
| 39 |
if not value or not isinstance(value, str):
|
| 40 |
raise ValueError("Domain must be a non-empty string")
|
|
@@ -187,7 +200,7 @@ def metadata():
|
|
| 187 |
}
|
| 188 |
|
| 189 |
|
| 190 |
-
@app.get("/label_sets")
|
| 191 |
def label_sets():
|
| 192 |
return {
|
| 193 |
"label_sets": [
|
|
@@ -225,7 +238,7 @@ def by_domain(domain: str):
|
|
| 225 |
return out
|
| 226 |
|
| 227 |
|
| 228 |
-
@app.get("/stats")
|
| 229 |
def stats():
|
| 230 |
return {
|
| 231 |
"months": [
|
|
@@ -243,7 +256,7 @@ def stats():
|
|
| 243 |
}
|
| 244 |
|
| 245 |
|
| 246 |
-
@app.get("/months")
|
| 247 |
def months():
|
| 248 |
return {
|
| 249 |
"months": [
|
|
|
|
| 1 |
from functools import lru_cache
|
| 2 |
+
import os
|
| 3 |
+
import secrets
|
| 4 |
import json
|
| 5 |
from pathlib import Path
|
| 6 |
import re
|
| 7 |
from urllib.parse import quote, urlparse
|
| 8 |
|
| 9 |
import duckdb
|
| 10 |
+
from fastapi import Depends, FastAPI, Header, HTTPException
|
| 11 |
from huggingface_hub import hf_hub_download
|
| 12 |
import tldextract
|
| 13 |
|
|
|
|
| 17 |
REGRESSION_SOURCE = f"https://huggingface.co/datasets/{DATASET_REPO}/blob/main/{quote(REGRESSION_FILENAME, safe='/')}"
|
| 18 |
BINARY_SOURCE = f"https://huggingface.co/datasets/{DATASET_REPO}/blob/main/{quote(BINARY_FILENAME, safe='/')}"
|
| 19 |
|
| 20 |
+
API_VERSION = "0.2.3"
|
| 21 |
DATA_CUTOFF_MONTH = "2024-12"
|
| 22 |
METHOD = "GAT-TEXT"
|
| 23 |
|
|
|
|
| 37 |
_DOMAIN_RE = re.compile(r"^(?:[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?\.)+[a-z0-9-]{2,}$")
|
| 38 |
|
| 39 |
|
| 40 |
+
def _require_internal_access(
|
| 41 |
+
x_internal_token: str | None = Header(default=None, alias="X-Internal-Token"),
|
| 42 |
+
) -> None:
|
| 43 |
+
expected = os.getenv("INTERNAL_TOKEN")
|
| 44 |
+
if not expected:
|
| 45 |
+
# Fail closed when token is not configured.
|
| 46 |
+
raise HTTPException(status_code=503, detail="Internal endpoints are disabled")
|
| 47 |
+
if not x_internal_token or not secrets.compare_digest(x_internal_token, expected):
|
| 48 |
+
raise HTTPException(status_code=403, detail="Forbidden")
|
| 49 |
+
|
| 50 |
+
|
| 51 |
def _canonicalize(value: str) -> str:
|
| 52 |
if not value or not isinstance(value, str):
|
| 53 |
raise ValueError("Domain must be a non-empty string")
|
|
|
|
| 200 |
}
|
| 201 |
|
| 202 |
|
| 203 |
+
@app.get("/label_sets", dependencies=[Depends(_require_internal_access)], include_in_schema=False)
|
| 204 |
def label_sets():
|
| 205 |
return {
|
| 206 |
"label_sets": [
|
|
|
|
| 238 |
return out
|
| 239 |
|
| 240 |
|
| 241 |
+
@app.get("/stats", dependencies=[Depends(_require_internal_access)], include_in_schema=False)
|
| 242 |
def stats():
|
| 243 |
return {
|
| 244 |
"months": [
|
|
|
|
| 256 |
}
|
| 257 |
|
| 258 |
|
| 259 |
+
@app.get("/months", dependencies=[Depends(_require_internal_access)], include_in_schema=False)
|
| 260 |
def months():
|
| 261 |
return {
|
| 262 |
"months": [
|