cheekeong2025 commited on
Commit
8dcb15f
·
verified ·
1 Parent(s): 1829e05

Update loader.py

Browse files
Files changed (1) hide show
  1. loader.py +21 -3
loader.py CHANGED
@@ -7,6 +7,10 @@ import httpx
7
  app = FastAPI()
8
 
9
  # ========= CONFIG =========
 
 
 
 
10
  PRIVATE_SPACE_ID = os.getenv("PRIVATE_SPACE_ID")
11
  HF_TOKEN = os.getenv("HF_TOKEN")
12
 
@@ -17,8 +21,15 @@ if not PRIVATE_SPACE_ID:
17
  "and add PRIVATE_SPACE_ID=cheekeong2025/iip-grading"
18
  )
19
 
 
 
 
 
 
 
 
20
  if not HF_TOKEN:
21
- print("[loader] ⚠️ Warning: HF_TOKEN not set — attempting anonymous access.")
22
 
23
  HEADERS = {"Authorization": f"Bearer {HF_TOKEN}"} if HF_TOKEN else {}
24
  # ===========================
@@ -27,9 +38,10 @@ HEADERS = {"Authorization": f"Bearer {HF_TOKEN}"} if HF_TOKEN else {}
27
  @app.get("/")
28
  async def root():
29
  """Serve the index.html from the private static Space."""
 
30
  async with httpx.AsyncClient(timeout=None) as client:
31
  try:
32
- r = await client.get(PRIVATE_STATIC_URL, headers=HEADERS)
33
  r.raise_for_status()
34
  except Exception as e:
35
  return PlainTextResponse(f"Error fetching root: {e}", status_code=500)
@@ -40,6 +52,7 @@ async def root():
40
  async def proxy(path: str, request: Request):
41
  """Proxy all static assets (CSS, JS, images, etc.) from the private Space."""
42
  url = f"{PRIVATE_STATIC_URL.rstrip('/')}/{path}"
 
43
  try:
44
  async with httpx.AsyncClient(timeout=None) as client:
45
  r = await client.get(url, headers=HEADERS)
@@ -53,4 +66,9 @@ async def proxy(path: str, request: Request):
53
 
54
  @app.get("/health")
55
  async def health():
56
- return {"status": "ok", "source": PRIVATE_STATIC_URL}
 
 
 
 
 
 
7
  app = FastAPI()
8
 
9
  # ========= CONFIG =========
10
+ PRIVATE_STATIC_URL = os.getenv(
11
+ "PRIVATE_STATIC_URL",
12
+ "https://cheekeong2025-iip-grading.static.hf.space" # ✅ fallback URL
13
+ )
14
  PRIVATE_SPACE_ID = os.getenv("PRIVATE_SPACE_ID")
15
  HF_TOKEN = os.getenv("HF_TOKEN")
16
 
 
21
  "and add PRIVATE_SPACE_ID=cheekeong2025/iip-grading"
22
  )
23
 
24
+ if not PRIVATE_STATIC_URL:
25
+ raise RuntimeError(
26
+ "❌ PRIVATE_STATIC_URL is not set.\n"
27
+ "Go to your Hugging Face Space → Settings → Repository secrets\n"
28
+ "and add PRIVATE_STATIC_URL=https://<your-private-static-space>.hf.space"
29
+ )
30
+
31
  if not HF_TOKEN:
32
+ print("[loader] ⚠️ Warning: HF_TOKEN not set — attempting anonymous access.")
33
 
34
  HEADERS = {"Authorization": f"Bearer {HF_TOKEN}"} if HF_TOKEN else {}
35
  # ===========================
 
38
  @app.get("/")
39
  async def root():
40
  """Serve the index.html from the private static Space."""
41
+ print(f"[loader] Fetching root from {PRIVATE_STATIC_URL}/index.html")
42
  async with httpx.AsyncClient(timeout=None) as client:
43
  try:
44
+ r = await client.get(f"{PRIVATE_STATIC_URL}/index.html", headers=HEADERS)
45
  r.raise_for_status()
46
  except Exception as e:
47
  return PlainTextResponse(f"Error fetching root: {e}", status_code=500)
 
52
  async def proxy(path: str, request: Request):
53
  """Proxy all static assets (CSS, JS, images, etc.) from the private Space."""
54
  url = f"{PRIVATE_STATIC_URL.rstrip('/')}/{path}"
55
+ print(f"[loader] Fetching asset: {url}")
56
  try:
57
  async with httpx.AsyncClient(timeout=None) as client:
58
  r = await client.get(url, headers=HEADERS)
 
66
 
67
  @app.get("/health")
68
  async def health():
69
+ """Simple health check."""
70
+ return {
71
+ "status": "ok",
72
+ "source": PRIVATE_STATIC_URL,
73
+ "space_id": PRIVATE_SPACE_ID
74
+ }