-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
47 lines (33 loc) · 1.09 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
import os
from typing import Annotated, Optional
from fastapi import Depends, FastAPI, Request, Response
from fastapi.security import OAuth2PasswordBearer
from gliner import GLiNER
from pydantic import BaseModel
if os.getenv('NER_API_KEY') is None:
raise Exception('NER_API_KEY must be defined')
MODEL=os.getenv("GLINER_MODEL", "urchade/gliner_medium-v2.1")
LABELS=os.getenv("GLINER_LABELS", "Person,Company").split(",")
class Query(BaseModel):
text: str
class Match(BaseModel):
type: str
text: str
model = GLiNER.from_pretrained(MODEL)
app = FastAPI()
auth = OAuth2PasswordBearer(tokenUrl='')
@app.get("/-/health")
def healthcheck():
pass
@app.post("/detect")
def detect(token: Annotated[str, Depends(auth)], query: Query, response: Response):
if token != os.environ['NER_API_KEY']:
response.status_code = 401
return {"detail": "Not authenticated"}
labels = LABELS
entities = model.predict_entities(query.text, labels, threshold=0.1)
matches = []
for entity in entities:
match = Match(type=entity["label"], text=entity["text"])
matches.append(match)
return matches