-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
232 lines (189 loc) · 6.25 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
import io
import os
import uuid
from contextlib import asynccontextmanager
from pathlib import Path
from typing import Optional
from fastapi import FastAPI, UploadFile, File, HTTPException, status, Depends, BackgroundTasks, Form
from fastapi.responses import FileResponse
from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials
from pydantic import BaseModel
from sqlalchemy.orm import Session
from _markitdown import MarkItDown
from base import DocumentConverterResult
from model_manager import ModelConfigurator
from repository.db import get_db, Job
# 安全验证
security = HTTPBearer()
# 从环境变量获取API密钥
API_KEY = os.getenv("MARKIT_API_KEY", "secret-key")
OUTPUT_DIR = Path("output_files")
OUTPUT_DIR.mkdir(exist_ok=True)
MINER_RUNNING_DEVICE = os.getenv("MINER_RUNNING_DEVICE", "cpu")
port = int(os.getenv("PORT", 20926))
# 依赖项:API Key 验证
async def verify_api_key(
credentials: HTTPAuthorizationCredentials = Depends(security)
):
if credentials.scheme != "Bearer" or credentials.credentials != API_KEY:
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Invalid API Key",
)
return credentials
@asynccontextmanager
async def lifespan(app: FastAPI):
"""服务启动和关闭时的生命周期管理"""
try:
# 初始化模型
configurator = ModelConfigurator(
device=os.getenv("MINERU_DEVICE", MINER_RUNNING_DEVICE),
use_modelscope=True
)
configurator.setup_environment()
print("模型初始化完成")
except Exception as e:
print(f"模型初始化失败: {str(e)}")
raise
yield # 应用运行期间
# 清理逻辑(可选)
print("服务关闭,清理资源...")
# FastAPI 应用
app = FastAPI(lifespan=lifespan)
# from slowapi import Limiter, _rate_limit_exceeded_handler
# from slowapi.errors import RateLimitExceeded
# from slowapi.util import get_remote_address
# limiter = Limiter(key_func=get_remote_address)
# app.state.limiter = limiter
# app.add_exception_handler(RateLimitExceeded, _rate_limit_exceeded_handler)
# @limiter.limit("100/minute")
# 数据模型
class JobStatusResponse(BaseModel):
job_id: str
status: str
filename: str
params: dict
error: Optional[str]
class JobResultResponse(BaseModel):
job_id: str
download_url: str
format: str
def process_file(db: Session, job_id: str, file_content: bytes, filename: str, pdf_mode: str = "simple"):
"""处理各种文件的后台任务"""
try:
# 更新任务状态为 processing
job = db.query(Job).filter(Job.id == job_id).first()
if not job:
raise ValueError(f"Job {job_id} not found")
job.status = "processing"
db.commit()
# 创建处理器
markitdown = MarkItDown(pdf_mode=pdf_mode)
# 根据输入类型处理
if filename.endswith('.md'):
result = DocumentConverterResult(text_content=file_content.decode('utf-8'))
else:
# 将字节内容转为文件流
file_stream = io.BytesIO(file_content)
result = markitdown.convert_stream(file_stream)
# 保存结果到文件
output_file = OUTPUT_DIR / f"{job_id}.md"
with open(output_file, "w", encoding="utf-8") as f:
f.write(result.text_content)
# 更新任务状态为 completed
job.status = "completed"
job.result_file = str(output_file)
db.commit()
except Exception as e:
# 更新任务状态为 failed
job.status = "failed"
job.error = f"{type(e).__name__}: {str(e)}"
db.commit()
@app.post("/api/jobs", status_code=status.HTTP_202_ACCEPTED)
async def upload_file(
background_tasks: BackgroundTasks,
file: UploadFile = File(...),
pdf_mode: str = Form("simple"),
db: Session = Depends(get_db)
):
"""上传文件并启动转换任务"""
# 生成任务ID
job_id = str(uuid.uuid4())
try:
# 读取文件内容
content = await file.read()
# 创建任务记录
job = Job(
id=job_id,
filename=file.filename,
params={"pdf_mode": pdf_mode},
status="pending"
)
db.add(job)
db.commit()
# 启动后台任务
background_tasks.add_task(
process_file,
db=db,
job_id=job_id,
file_content=content,
filename=file.filename,
pdf_mode=pdf_mode
)
return {"job_id": job_id}
except Exception as e:
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail=f"File upload failed: {str(e)}"
)
@app.get("/api/jobs/{job_id}", response_model=JobStatusResponse)
async def get_job_status(
job_id: str,
db: Session = Depends(get_db)
):
"""查询任务状态"""
job = db.query(Job).filter(Job.id == job_id).first()
if not job:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail="Job not found"
)
return JobStatusResponse(
job_id=job.id,
status=job.status,
filename=job.filename,
params=job.params,
error=job.error
)
@app.get("/api/jobs/{job_id}/result")
async def download_result(
job_id: str,
db: Session = Depends(get_db)
):
"""下载任务结果文件"""
job = db.query(Job).filter(Job.id == job_id).first()
if not job:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail="Job not found"
)
if job.status != "completed":
raise HTTPException(
status_code=status.HTTP_425_TOO_EARLY,
detail="Job not completed"
)
result_file = job.result_file
if not result_file or not os.path.exists(result_file):
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail="Result file not found"
)
# 返回文件内容
return FileResponse(
result_file,
filename=f"{job.filename}.md",
media_type="text/markdown"
)
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=port)