Spaces:
Sleeping
Sleeping
Update loader.py
Browse files
loader.py
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
-
# loader.py —
|
| 2 |
import os
|
| 3 |
import mimetypes
|
| 4 |
from urllib.parse import urljoin
|
|
@@ -8,35 +8,35 @@ import httpx
|
|
| 8 |
|
| 9 |
app = FastAPI()
|
| 10 |
|
| 11 |
-
#
|
| 12 |
-
SPACE_ID = os.getenv("PRIVATE_SPACE_ID") # e.g
|
| 13 |
-
HF_TOKEN = os.getenv("HF_TOKEN") #
|
| 14 |
-
REVISION = os.getenv("REVISION", "main") # branch/tag/commit
|
| 15 |
-
#
|
| 16 |
|
| 17 |
if not SPACE_ID:
|
| 18 |
raise RuntimeError("Set PRIVATE_SPACE_ID (e.g. 'cheekeong2025/IIP_Grading') in Settings → Repository secrets.")
|
| 19 |
if not HF_TOKEN:
|
| 20 |
-
raise RuntimeError("Set HF_TOKEN (
|
| 21 |
|
| 22 |
HEADERS = {"Authorization": f"Bearer {HF_TOKEN}"}
|
| 23 |
BASE_RESOLVE = f"https://huggingface.co/spaces/{SPACE_ID}/resolve/{REVISION}/"
|
| 24 |
BASE_RAW = f"https://huggingface.co/spaces/{SPACE_ID}/raw/{REVISION}/"
|
| 25 |
|
| 26 |
-
def _join(
|
| 27 |
-
return urljoin(
|
| 28 |
|
| 29 |
def _with_q(url: str, req: Request) -> str:
|
| 30 |
q = str(req.url.query or "")
|
| 31 |
return f"{url}?{q}" if q else url
|
| 32 |
|
| 33 |
-
def _looks_file(
|
| 34 |
-
return "." in
|
| 35 |
|
| 36 |
-
def _mime(
|
| 37 |
-
if
|
| 38 |
return "text/html; charset=utf-8"
|
| 39 |
-
m, _ = mimetypes.guess_type(
|
| 40 |
return m or default
|
| 41 |
|
| 42 |
async def _get(client: httpx.AsyncClient, url: str) -> httpx.Response:
|
|
@@ -44,9 +44,9 @@ async def _get(client: httpx.AsyncClient, url: str) -> httpx.Response:
|
|
| 44 |
print(f"[proxy] GET {url} -> {r.status_code}")
|
| 45 |
return r
|
| 46 |
|
| 47 |
-
async def _fetch(client: httpx.AsyncClient, path: str, req: Request)
|
| 48 |
"""
|
| 49 |
-
|
| 50 |
resolve:path
|
| 51 |
resolve:static/path
|
| 52 |
(dir) resolve:path/index.html
|
|
@@ -78,10 +78,10 @@ async def _fetch(client: httpx.AsyncClient, path: str, req: Request) -> tuple[ht
|
|
| 78 |
]
|
| 79 |
|
| 80 |
last = None
|
| 81 |
-
for
|
| 82 |
-
url = _with_q(
|
| 83 |
r = await _get(client, url)
|
| 84 |
-
tried.append(
|
| 85 |
if r.status_code == 200:
|
| 86 |
return r, " -> ".join(tried)
|
| 87 |
last = r
|
|
@@ -128,4 +128,4 @@ async def proxy(path: str, request: Request):
|
|
| 128 |
return PlainTextResponse(f"Error fetching {path}: {e}", status_code=500)
|
| 129 |
|
| 130 |
ctype = r.headers.get("content-type") or _mime(path)
|
| 131 |
-
return Response(content=r.content, media_type=ctype, status_code=r.status_code)
|
|
|
|
| 1 |
+
# loader.py — Public proxy to a PRIVATE Static Space (auth via resolve/raw endpoints)
|
| 2 |
import os
|
| 3 |
import mimetypes
|
| 4 |
from urllib.parse import urljoin
|
|
|
|
| 8 |
|
| 9 |
app = FastAPI()
|
| 10 |
|
| 11 |
+
# ===== Config (from Space Secrets) =====
|
| 12 |
+
SPACE_ID = os.getenv("PRIVATE_SPACE_ID") # e.g., "cheekeong2025/IIP_Grading"
|
| 13 |
+
HF_TOKEN = os.getenv("HF_TOKEN") # token with READ access to that private Space
|
| 14 |
+
REVISION = os.getenv("REVISION", "main") # branch/tag/commit; default "main"
|
| 15 |
+
# ======================================
|
| 16 |
|
| 17 |
if not SPACE_ID:
|
| 18 |
raise RuntimeError("Set PRIVATE_SPACE_ID (e.g. 'cheekeong2025/IIP_Grading') in Settings → Repository secrets.")
|
| 19 |
if not HF_TOKEN:
|
| 20 |
+
raise RuntimeError("Set HF_TOKEN (READ access to the private Space) in Settings → Repository secrets.")
|
| 21 |
|
| 22 |
HEADERS = {"Authorization": f"Bearer {HF_TOKEN}"}
|
| 23 |
BASE_RESOLVE = f"https://huggingface.co/spaces/{SPACE_ID}/resolve/{REVISION}/"
|
| 24 |
BASE_RAW = f"https://huggingface.co/spaces/{SPACE_ID}/raw/{REVISION}/"
|
| 25 |
|
| 26 |
+
def _join(base: str, path: str) -> str:
|
| 27 |
+
return urljoin(base, path.lstrip("/"))
|
| 28 |
|
| 29 |
def _with_q(url: str, req: Request) -> str:
|
| 30 |
q = str(req.url.query or "")
|
| 31 |
return f"{url}?{q}" if q else url
|
| 32 |
|
| 33 |
+
def _looks_file(path: str) -> bool:
|
| 34 |
+
return "." in path.split("/")[-1]
|
| 35 |
|
| 36 |
+
def _mime(path: str, default="application/octet-stream") -> str:
|
| 37 |
+
if path.endswith("/") or path == "":
|
| 38 |
return "text/html; charset=utf-8"
|
| 39 |
+
m, _ = mimetypes.guess_type(path)
|
| 40 |
return m or default
|
| 41 |
|
| 42 |
async def _get(client: httpx.AsyncClient, url: str) -> httpx.Response:
|
|
|
|
| 44 |
print(f"[proxy] GET {url} -> {r.status_code}")
|
| 45 |
return r
|
| 46 |
|
| 47 |
+
async def _fetch(client: httpx.AsyncClient, path: str, req: Request):
|
| 48 |
"""
|
| 49 |
+
Robust lookup order (first 200 wins):
|
| 50 |
resolve:path
|
| 51 |
resolve:static/path
|
| 52 |
(dir) resolve:path/index.html
|
|
|
|
| 78 |
]
|
| 79 |
|
| 80 |
last = None
|
| 81 |
+
for base_u in candidates:
|
| 82 |
+
url = _with_q(base_u, req)
|
| 83 |
r = await _get(client, url)
|
| 84 |
+
tried.append(base_u)
|
| 85 |
if r.status_code == 200:
|
| 86 |
return r, " -> ".join(tried)
|
| 87 |
last = r
|
|
|
|
| 128 |
return PlainTextResponse(f"Error fetching {path}: {e}", status_code=500)
|
| 129 |
|
| 130 |
ctype = r.headers.get("content-type") or _mime(path)
|
| 131 |
+
return Response(content=r.content, media_type=ctype, status_code=r.status_code)
|