-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun_correct_service.py
68 lines (51 loc) · 1.64 KB
/
run_correct_service.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
import logging
import torch
import json
from starlette.middleware.cors import CORSMiddleware
# 纠错模型单独启动一个内部服务
from fastapi import FastAPI, Response
from fastapi.middleware.cors import CORSMiddleware
from typing import List, Union
from pydantic import BaseModel
from contextlib import asynccontextmanager
@asynccontextmanager
async def lifespan(app: FastAPI):
yield
if torch.cuda.is_available():
torch.cuda.empty_cache()
torch.cuda.ipc_collect()
app = FastAPI(lifespan=lifespan, openapi_url=None, title="模型服务")
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
@app.get("/health")
async def health() -> Response:
"""Health check."""
return Response(status_code=200, content="{}")
# sys.path.append('../..')
from pycorrector import NaSGECBartCorrector
from pycorrector.utils.sentence_utils import is_not_chinese_error
bc = NaSGECBartCorrector()
class CustomRequest(BaseModel):
method:str
length: int
input: str
username: str
token: str
requestid: str
@app.post("/app/corrector/v1/corrector")
async def step(request: CustomRequest):
# 模型结果后处理
result = bc.correct(request.input,ignore_function=is_not_chinese_error, max_length=request.length)
print(result)
return Response(status_code=200, content=json.dumps(result))
if __name__ == "__main__":
import logging
import uvicorn
logger = logging.getLogger("server")
logger.info("start ...")
uvicorn.run(app="run_correct_service:app", host="0.0.0.0", port=9045, reload=False, workers=1)