ZHIWEI666 commited on
Commit
94c35d7
·
verified ·
1 Parent(s): 22d80b0

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -8
app.py CHANGED
@@ -1,11 +1,12 @@
1
  # ⚙️ 后端逻辑/核心服务端.py (Hugging Face Spaces app.py)
2
  from fastapi import FastAPI, File, UploadFile, Form
3
  from fastapi.middleware.cors import CORSMiddleware
4
- from fastapi.responses import Response, JSONResponse # 新增 Response 用于返回文件流
 
5
  import hashlib
6
  import urllib.parse
7
- import urllib.request # 新增用于代理请求
8
- import os # 新增用于读取环境变量
9
  import 数据库连接 as db
10
 
11
  # 引入拆分后的四大业务模块
@@ -24,7 +25,6 @@ app.add_middleware(
24
  allow_headers=["*"],
25
  )
26
 
27
- # 挂载并聚合所有业务子路由
28
  app.include_router(users_router)
29
  app.include_router(items_router)
30
  app.include_router(comments_router)
@@ -52,12 +52,16 @@ async def upload_file(file: UploadFile = File(...), file_type: str = Form(...)):
52
  url = f"https://huggingface.co/datasets/{db.DATASET_REPO_ID}/resolve/main/{target_dir}/{safe_url_filename}"
53
  return {"status": "success", "url": url, "display_name": file.filename, "hashed_name": new_filename}
54
 
 
55
  # =========================================================
56
- # 【核心新增】:代理下载私有数据集文件的接口
57
  # =========================================================
 
 
 
58
  @app.post("/api/proxy_download")
59
- async def proxy_download(req_data: dict):
60
- target_url = req_data.get("url")
61
 
62
  # 安全校验:只允许代理下载 huggingface.co 域名的文件
63
  if not target_url or "huggingface.co" not in target_url:
@@ -75,7 +79,6 @@ async def proxy_download(req_data: dict):
75
  try:
76
  with urllib.request.urlopen(req) as response:
77
  content = response.read()
78
- # 直接将读取到的二进制文件流返回给前端
79
  return Response(content=content, media_type="application/json")
80
  except Exception as e:
81
  return JSONResponse(content={"error": f"云端代理下载失败: {str(e)}"}, status_code=500)
 
1
  # ⚙️ 后端逻辑/核心服务端.py (Hugging Face Spaces app.py)
2
  from fastapi import FastAPI, File, UploadFile, Form
3
  from fastapi.middleware.cors import CORSMiddleware
4
+ from fastapi.responses import Response, JSONResponse
5
+ from pydantic import BaseModel # 【新增】:引入标准数据模型
6
  import hashlib
7
  import urllib.parse
8
+ import urllib.request
9
+ import os
10
  import 数据库连接 as db
11
 
12
  # 引入拆分后的四大业务模块
 
25
  allow_headers=["*"],
26
  )
27
 
 
28
  app.include_router(users_router)
29
  app.include_router(items_router)
30
  app.include_router(comments_router)
 
52
  url = f"https://huggingface.co/datasets/{db.DATASET_REPO_ID}/resolve/main/{target_dir}/{safe_url_filename}"
53
  return {"status": "success", "url": url, "display_name": file.filename, "hashed_name": new_filename}
54
 
55
+
56
  # =========================================================
57
+ # 【核心修复】:使用 Pydantic 定义请求体结构,避免 FastAPI 启动崩溃
58
  # =========================================================
59
+ class ProxyDownloadRequest(BaseModel):
60
+ url: str
61
+
62
  @app.post("/api/proxy_download")
63
+ async def proxy_download(req_data: ProxyDownloadRequest):
64
+ target_url = req_data.url
65
 
66
  # 安全校验:只允许代理下载 huggingface.co 域名的文件
67
  if not target_url or "huggingface.co" not in target_url:
 
79
  try:
80
  with urllib.request.urlopen(req) as response:
81
  content = response.read()
 
82
  return Response(content=content, media_type="application/json")
83
  except Exception as e:
84
  return JSONResponse(content={"error": f"云端代理下载失败: {str(e)}"}, status_code=500)