Spaces:
Running
Running
Upload app.py
Browse files
app.py
CHANGED
|
@@ -54,8 +54,10 @@ async def upload_file(file: UploadFile = File(...), file_type: str = Form(...)):
|
|
| 54 |
|
| 55 |
|
| 56 |
# =========================================================
|
| 57 |
-
# 【核心修复】:使用
|
| 58 |
# =========================================================
|
|
|
|
|
|
|
| 59 |
class ProxyDownloadRequest(BaseModel):
|
| 60 |
url: str
|
| 61 |
|
|
@@ -63,22 +65,35 @@ class ProxyDownloadRequest(BaseModel):
|
|
| 63 |
async def proxy_download(req_data: ProxyDownloadRequest):
|
| 64 |
target_url = req_data.url
|
| 65 |
|
| 66 |
-
#
|
| 67 |
-
if not target_url or "
|
| 68 |
-
return JSONResponse(content={"error": "无效的下载链接
|
| 69 |
|
| 70 |
hf_token = os.environ.get("HF_TOKEN")
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
req = urllib.request.Request(target_url, headers={'User-Agent': 'Mozilla/5.0'})
|
| 74 |
-
|
| 75 |
-
# 如果环境配置了 Token,则将其塞入请求头中用于突破私有权限
|
| 76 |
-
if hf_token:
|
| 77 |
-
req.add_header("Authorization", f"Bearer {hf_token}")
|
| 78 |
|
| 79 |
try:
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
except Exception as e:
|
| 84 |
-
return JSONResponse(content={"error": f"云端
|
|
|
|
| 54 |
|
| 55 |
|
| 56 |
# =========================================================
|
| 57 |
+
# 【核心修复】:使用 huggingface_hub 官方库替代 urllib 解决重定向 401 问题
|
| 58 |
# =========================================================
|
| 59 |
+
from huggingface_hub import hf_hub_download
|
| 60 |
+
|
| 61 |
class ProxyDownloadRequest(BaseModel):
|
| 62 |
url: str
|
| 63 |
|
|
|
|
| 65 |
async def proxy_download(req_data: ProxyDownloadRequest):
|
| 66 |
target_url = req_data.url
|
| 67 |
|
| 68 |
+
# 校验 URL 格式
|
| 69 |
+
if not target_url or "resolve/main/" not in target_url:
|
| 70 |
+
return JSONResponse(content={"error": "无效的 Hugging Face 下载链接"}, status_code=400)
|
| 71 |
|
| 72 |
hf_token = os.environ.get("HF_TOKEN")
|
| 73 |
+
if not hf_token:
|
| 74 |
+
return JSONResponse(content={"error": "云端环境变量未配置 HF_TOKEN,无法读取私有库"}, status_code=401)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
|
| 76 |
try:
|
| 77 |
+
# 1. 从前端传来的 URL 中切割出仓库内的真实相对路径
|
| 78 |
+
# 例如从 https://.../resolve/main/apps/123_%E6%B5%8B.json 提取出 apps/123_%E6%B5%8B.json
|
| 79 |
+
repo_path_encoded = target_url.split("resolve/main/")[-1]
|
| 80 |
+
|
| 81 |
+
# 2. 对可能存在的被编码的中文字符进行解码 (恢复为 apps/123_测.json)
|
| 82 |
+
repo_path = urllib.parse.unquote(repo_path_encoded)
|
| 83 |
+
|
| 84 |
+
# 3. 调用官方库直接从私有 Dataset 拉取文件(完美处理鉴权和重定向)
|
| 85 |
+
cached_file_path = hf_hub_download(
|
| 86 |
+
repo_id=db.DATASET_REPO_ID,
|
| 87 |
+
repo_type="dataset",
|
| 88 |
+
filename=repo_path,
|
| 89 |
+
token=hf_token
|
| 90 |
+
)
|
| 91 |
+
|
| 92 |
+
# 4. 读取缓存在云端的本地文件流并返回给前端
|
| 93 |
+
with open(cached_file_path, "rb") as f:
|
| 94 |
+
content = f.read()
|
| 95 |
+
|
| 96 |
+
return Response(content=content, media_type="application/json")
|
| 97 |
+
|
| 98 |
except Exception as e:
|
| 99 |
+
return JSONResponse(content={"error": f"云端官方库读取失败: {str(e)}"}, status_code=500)
|