bandenamaj commited on
Commit
ee2ed70
·
verified ·
1 Parent(s): 97b4bcb

Upload app.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +42 -0
app.py ADDED
@@ -0,0 +1,42 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI, HTTPException
2
+ from fastapi.responses import StreamingResponse
3
+ import requests
4
+ import os
5
+
6
+ app = FastAPI()
7
+
8
+ # Optional: Add your HF_TOKEN to Space Secrets for private models
9
+ HF_TOKEN = os.environ.get("HF_TOKEN")
10
+
11
+ @app.get("/")
12
+ def home():
13
+ return {"status": "running", "message": "HF Storage Proxy"}
14
+
15
+ @app.get("/resolve/{repo_id:path}/{filename:path}")
16
+ async def proxy_resolve(repo_id: str, filename: str):
17
+ """
18
+ Proxies requests to huggingface.co/repo/resolve/main/file
19
+ """
20
+ hf_url = f"https://huggingface.co/{repo_id}/resolve/main/{filename}"
21
+
22
+ headers = {}
23
+ if HF_TOKEN:
24
+ headers["Authorization"] = f"Bearer {HF_TOKEN}"
25
+
26
+ try:
27
+ # We use stream=True to handle large model files without loading into memory
28
+ response = requests.get(hf_url, headers=headers, stream=True, allow_redirects=True)
29
+ response.raise_for_status()
30
+
31
+ return StreamingResponse(
32
+ response.iter_content(chunk_size=1024*1024),
33
+ media_type=response.headers.get("content-type"),
34
+ headers={
35
+ "Content-Disposition": response.headers.get("Content-Disposition", f'attachment; filename="{filename}"'),
36
+ "Content-Length": response.headers.get("Content-Length")
37
+ }
38
+ )
39
+ except requests.exceptions.HTTPError as e:
40
+ raise HTTPException(status_code=e.response.status_code, detail=str(e))
41
+ except Exception as e:
42
+ raise HTTPException(status_code=500, detail=str(e))