Commit cbe2abc6 authored by zhougaofeng's avatar zhougaofeng
Browse files

Update pdf_server.py

parent 56f741c2
...@@ -22,11 +22,10 @@ method = 'auto' ...@@ -22,11 +22,10 @@ method = 'auto'
logger.add("parse.log", rotation="10 MB", level="INFO", logger.add("parse.log", rotation="10 MB", level="INFO",
format="{time} {level} {message}", encoding='utf-8', enqueue=True) format="{time} {level} {message}", encoding='utf-8', enqueue=True)
config_path = None
class ocrRequest(BaseModel): class ocrRequest(BaseModel):
path: str path: str
output_dir: str output_dir: str
config_path: str
class ocrResponse(BaseModel): class ocrResponse(BaseModel):
status_code: int status_code: int
...@@ -68,6 +67,8 @@ def ocr_pdf_serve(args: str): ...@@ -68,6 +67,8 @@ def ocr_pdf_serve(args: str):
config.read(args.config_path) config.read(args.config_path)
host = config.get('server', 'pdf_host') host = config.get('server', 'pdf_host')
port = int(config.get('server', 'pdf_port')) port = int(config.get('server', 'pdf_port'))
global config_path
config_path = args.config_path
uvicorn.run(app, host=host, port=port) uvicorn.run(app, host=host, port=port)
@app.post("/pdf_ocr") @app.post("/pdf_ocr")
...@@ -82,7 +83,7 @@ async def pdf_ocr(request: ocrRequest): ...@@ -82,7 +83,7 @@ async def pdf_ocr(request: ocrRequest):
debug_able = False debug_able = False
start_page_id = 0 start_page_id = 0
end_page_id = None end_page_id = None
logger.info(f"method: {method}, path: {path}, output_dir: {output_dir}, config_path: {request.config_path}") logger.info(f"method: {method}, path: {path}, output_dir: {output_dir}, config_path: {config_path}")
def read_fn(path): def read_fn(path):
disk_rw = DiskReaderWriter(os.path.dirname(path)) disk_rw = DiskReaderWriter(os.path.dirname(path))
...@@ -110,16 +111,15 @@ async def pdf_ocr(request: ocrRequest): ...@@ -110,16 +111,15 @@ async def pdf_ocr(request: ocrRequest):
except Exception as e: except Exception as e:
logger.exception(e) logger.exception(e)
logger.info(f'config_path:{request.config_path}') logger.info(f'config_path:{config_path}')
try:
output_path = parse_doc(path,request.config_path)
logger.info(f'文件解析成功:{output_path}')
output_path = parse_doc(path,config_path)
if output_path:
logger.info(f'文件解析成功:{output_path}')
return {"status_code": 200, "output_path": output_path} return {"status_code": 200, "output_path": output_path}
except Exception as e: else:
logger.exception(e) logger.error(f'文件解析失败,文件为:{path}')
# Return error response with error details raise HTTPException(status_code=500)
raise HTTPException(status_code=500, detail=str(e))
def main(): def main():
args = parse_args() args = parse_args()
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment