File size: 1,621 Bytes
4c7320d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from model.layer import Bi_LSTM_CRF
from flair.data import Sentence
from schema import WordSchema
from settings import app

from fastapi import  APIRouter
from starlette import status
from starlette.responses import JSONResponse
import uvicorn

api_router = APIRouter()



# Variables for Interactive selections
tagger = Bi_LSTM_CRF.load("checkpoints/best-model.pt")

@api_router.get("/")
def health():
    return JSONResponse(
            status_code=status.HTTP_201_CREATED,
            content={
                "code": 200,
                "version": "1.0.0"
            },
        )

                               

@api_router.get("/api/model", response_model=WordSchema)
def model(
    *, 
    word: str,
    file_name: str
    ):
    """
    An api for serving the model for the PHI classification.
    :param word: list of word tokens in a paragraph.
    :param file_name: name of the wile.

    :returns: json response that contains labeled 
        tags their respective classification.
    """
    txt = Sentence(word)
    tagger.predict(txt)
    labels, tags = [], []

    for entity in txt.get_spans('ner'):
        labels.append(entity.text)
        tags.append(entity.get_label("ner").value)
    

    return JSONResponse(
            status_code=status.HTTP_201_CREATED,
            content={
                "code": 200,
                "data": {
                    "labels": labels,
                    "tags": tags
                }
            },
        )


app.include_router(api_router)

if __name__ == "__main__":
    uvicorn.run("main:app", host="0.0.0.0", port=8000, reload=True, debug=True)