Nanny7 commited on
Commit
5257716
·
1 Parent(s): 26a3d12

无需 Gradio

Browse files
Files changed (3) hide show
  1. Dockerfile +2 -2
  2. requirements.txt +3 -1
  3. server.py +37 -0
Dockerfile CHANGED
@@ -10,7 +10,7 @@ RUN pip install --no-cache-dir -r requirements.txt
10
  COPY . /app
11
 
12
  # 暴露端口
13
- EXPOSE 7860
14
 
15
  # 启动应用
16
- CMD ["python", "app.py"]
 
10
  COPY . /app
11
 
12
  # 暴露端口
13
+ EXPOSE 7860 8000
14
 
15
  # 启动应用
16
+ CMD ["uvicorn", "server:app", "--host", "0.0.0.0", "--port", "8000"]
requirements.txt CHANGED
@@ -1,6 +1,8 @@
1
- gradio>=3.50.1
2
  requests
3
  beautifulsoup4
4
  tqdm
5
  fake-useragent
6
  pycryptodome
 
 
 
1
+ # gradio>=3.50.1 (removed for FastAPI deployment)
2
  requests
3
  beautifulsoup4
4
  tqdm
5
  fake-useragent
6
  pycryptodome
7
+ fastapi
8
+ uvicorn[standard]
server.py ADDED
@@ -0,0 +1,37 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI, HTTPException
2
+ from fastapi.responses import FileResponse
3
+ import os, shutil, glob
4
+ from main import Run
5
+
6
+ app = FastAPI(
7
+ title="番茄小说下载器API",
8
+ description="通过 /download?book_id=xxx 获取小说文本文件",
9
+ )
10
+
11
+ @app.get("/")
12
+ def root():
13
+ return {"message": "番茄小说下载器,使用 /download?book_id=小说ID 下载"}
14
+
15
+ @app.get("/download")
16
+ def download(book_id: str):
17
+ save_path = "downloads"
18
+ # 清理旧文件
19
+ if os.path.exists(save_path):
20
+ shutil.rmtree(save_path)
21
+ os.makedirs(save_path, exist_ok=True)
22
+
23
+ # 调用下载核心
24
+ try:
25
+ Run(book_id, save_path)
26
+ except Exception as e:
27
+ raise HTTPException(status_code=500, detail=f"下载失败: {str(e)}")
28
+
29
+ files = glob.glob(os.path.join(save_path, "*.txt"))
30
+ if not files:
31
+ raise HTTPException(status_code=404, detail="未生成txt文件,下载失败")
32
+ file_path = files[0]
33
+ return FileResponse(
34
+ path=file_path,
35
+ filename=os.path.basename(file_path),
36
+ media_type="text/plain"
37
+ )