cheekeong2025 commited on
Commit
7e18a76
·
verified ·
1 Parent(s): 3a5de32

Update loader.py

Browse files
Files changed (1) hide show
  1. loader.py +20 -20
loader.py CHANGED
@@ -1,4 +1,4 @@
1
- # loader.py — public proxy for PRIVATE Static Space via huggingface.co/resolve & raw
2
  import os
3
  import mimetypes
4
  from urllib.parse import urljoin
@@ -8,35 +8,35 @@ import httpx
8
 
9
  app = FastAPI()
10
 
11
- # --- Config from Secrets ---
12
- SPACE_ID = os.getenv("PRIVATE_SPACE_ID") # e.g. "cheekeong2025/IIP_Grading"
13
- HF_TOKEN = os.getenv("HF_TOKEN") # read-access token for that Space
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 (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(b: str, p: str) -> str:
27
- return urljoin(b, p.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(p: str) -> bool:
34
- return "." in p.split("/")[-1]
35
 
36
- def _mime(p: str, default="application/octet-stream"):
37
- if p.endswith("/") or p == "":
38
  return "text/html; charset=utf-8"
39
- m, _ = mimetypes.guess_type(p)
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) -> tuple[httpx.Response, str]:
48
  """
49
- Try in order (first 200 wins):
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 u in candidates:
82
- url = _with_q(u, req)
83
  r = await _get(client, url)
84
- tried.append(u)
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)