-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
83 lines (65 loc) · 2.54 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
import os
from fastapi import FastAPI
import psutil
# https://github.com/tiangolo/fastapi/issues/1663#issuecomment-906817935
# from fastapi.middleware.cors import CORSMiddleware
from starlette.middleware.cors import CORSMiddleware
from starlette.requests import Request
import time
import threading
app = FastAPI(
debug=True
)
# app.add_middleware(
# CORSMiddleware,
# allow_origins=["*"],
# allow_methods=["*"],
# )
@app.get("/analyze")
def analyze(
request: Request,
text: str = "", # "銀座でランチをご一緒しましょう。"
ignore_lf: bool = False,
split_mode: str = "C", # A,B,C
output_format: str = "conllu", # conllu, cabocha, mecab, json
):
print(f"[{os.getpid()} {threading.get_ident()} {id(request)}] analyze start {text=} {ignore_lf=} {split_mode=} {output_format=} {request.headers.items()=} {psutil.Process().memory_info()=}")
from ginza.analyzer import Analyzer
analyzer = Analyzer(
model_name_or_path="ja_ginza",
split_mode=split_mode,
hash_comment="print",
output_format=output_format,
require_gpu=-1,
disable_sentencizer=False,
use_normalized_form=False,
)
results = []
print(f"[{os.getpid()} {threading.get_ident()} {id(request)}] analyze set_nlp {psutil.Process().memory_info()=}")
analyzer.set_nlp()
text = text.replace("\t", " ").replace("\r", "")
if ignore_lf:
text = text.replace("\n", "")
for text_line in text.splitlines():
results.append(analyzer.analyze_line(text_line))
print(f"[{os.getpid()} {threading.get_ident()} {id(request)}] analyze end {len(results)=} {psutil.Process().memory_info()=}")
return {"results": results}
@app.get("/warmup")
def warm_up(request: Request):
print(f"[{os.getpid()} {threading.get_ident()} {id(request)}] warmup {request.headers.items()=} {psutil.Process().memory_info()=}")
return {"message": "success"}
@app.get("/test_timeout")
def test_timeout(request: Request):
for i in range(360): # 60 min
print(f"[{os.getpid()} {threading.get_ident()} {id(request)}] sleep {i} {psutil.Process().memory_info()=}")
time.sleep(10)
return {"message": "success"}
@app.get("/test_raise")
def test_raise(request: Request):
print(f"[{os.getpid()} {threading.get_ident()} {id(request)}] raise {psutil.Process().memory_info()=}")
raise RuntimeError("test raise")
app = CORSMiddleware(
app=app,
allow_origins=["*"],
allow_methods=["*"],
)