| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556 |
- """
- 数据集管理 API
- 该文件提供数据集管理的 API 接口,支持:
- - PDF 文件上传和解析
- - 数据集创建
- """
- from fastapi import FastAPI, HTTPException, UploadFile, File, Form
- from typing import Dict, Any
- from api.dataset.services.dataset_manage_service import DatasetManageService
- # 创建 FastAPI 应用
- app = FastAPI(
- title="数据集管理 API",
- description="数据集管理服务,提供 PDF 解析和数据集创建功能",
- version="1.0.0"
- )
- # 创建数据集管理服务实例
- dataset_service = DatasetManageService()
- @app.post("/parse-pdf", response_model=Dict[str, Any])
- async def parse_pdf(
- file: UploadFile = File(...),
- series_name: str = Form(...)
-
- ):
- """
- 解析 PDF 文件接口
-
- - **file**: PDF 文件附件
- - **series_name**: 系列名
- """
- try:
- # 验证文件格式
- if not file.filename.endswith((".pdf", ".PDF")):
- raise HTTPException(status_code=400, detail="只支持 PDF 格式的文件")
-
- # 读取文件内容
- file_content = await file.read()
-
- # 调用解析 PDF 方法
- result = dataset_service.parse_pdf(
- series_name=series_name,
- pdf_file=file_content,
- pdf_filename=file.filename
- )
-
- return {"success": True, "result": result}
- except HTTPException as e:
- raise e
- except Exception as e:
- raise HTTPException(status_code=500, detail=f"解析 PDF 文件失败: {str(e)}")
|