spitzc32 commited on
Commit
4c7320d
·
1 Parent(s): 15ac7f2

Changed setting to FastAPI

Browse files
Files changed (6) hide show
  1. Dockerfile +1 -1
  2. app.py +0 -69
  3. main.py +68 -0
  4. requirements.txt +3 -2
  5. schema.py +5 -0
  6. settings.py +35 -0
Dockerfile CHANGED
@@ -8,4 +8,4 @@ RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt
8
 
9
  COPY . .
10
 
11
- CMD ["panel", "serve", "/code/app.py", "--address", "0.0.0.0", "--port", "7860", "--allow-websocket-origin", "spitzc32-BiLSTM-api.hf.space:80"]
 
8
 
9
  COPY . .
10
 
11
+ CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "7860"]
app.py DELETED
@@ -1,69 +0,0 @@
1
- from model.layer import Bi_LSTM_CRF
2
- from flair.data import Sentence
3
- import panel as pn
4
-
5
- pn.extension()
6
- pn.extension('tabulator')
7
-
8
-
9
- # Variables for Interactive selections
10
- tagger = Bi_LSTM_CRF.load("checkpoints/best-model.pt")
11
- text_widget = pn.widgets.TextAreaInput(value="George Washington lives in Washington", height=300, name='Add text')
12
- button = pn.widgets.Button(name="Click me to run!")
13
- explanation = pn.pane.Markdown(
14
- """
15
- ## Redaction API
16
- This app provides a text classification for a given text. To fully understand the redaction process,
17
- given below is the classification of each text under the file we need the redaction on.
18
-
19
- """
20
- )
21
-
22
-
23
- def model(word: str):
24
- """
25
- An function for serving the model for the PHI classification.
26
- :param word: list of word tokens in a paragraph.
27
-
28
- :returns: dict that contains labeled
29
- tags their respective classification.
30
- """
31
- txt = Sentence(word)
32
- tagger.predict(txt)
33
- labels, tags = [], []
34
-
35
- for entity in txt.get_spans('ner'):
36
- labels.append(entity.text)
37
- tags.append(entity.get_label("ner").value)
38
- message = f"""
39
- 'labels': {labels},
40
- 'tags': {tags}
41
- """
42
- #return pn.pane.Markdown(f"""{
43
- # 'labels': {labels},
44
- # 'tags': {tags}
45
- # }""")
46
- return pn.pane.Markdown(message)
47
-
48
- def get_tag_results(_):
49
- return pn.Column(
50
- explanation,
51
- pn.pane.Markdown("""
52
- ##Results:"""),
53
- model(text_widget.value.replace("\n", ""))
54
- )
55
-
56
-
57
-
58
- interactive = pn.bind(get_tag_results, button)
59
- template = pn.template.FastListTemplate(
60
- title='Model API',
61
- sidebar=[
62
- button,
63
- text_widget
64
- ],
65
- main=[pn.panel(interactive, loading_indicator=False)],
66
- accent_base_color="#88d8b0",
67
- header_background="#88d8b0",
68
- )
69
- template.servable()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
main.py ADDED
@@ -0,0 +1,68 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from model.layer import Bi_LSTM_CRF
2
+ from flair.data import Sentence
3
+ from schema import WordSchema
4
+ from settings import app
5
+
6
+ from fastapi import APIRouter
7
+ from starlette import status
8
+ from starlette.responses import JSONResponse
9
+ import uvicorn
10
+
11
+ api_router = APIRouter()
12
+
13
+
14
+
15
+ # Variables for Interactive selections
16
+ tagger = Bi_LSTM_CRF.load("checkpoints/best-model.pt")
17
+
18
+ @api_router.get("/")
19
+ def health():
20
+ return JSONResponse(
21
+ status_code=status.HTTP_201_CREATED,
22
+ content={
23
+ "code": 200,
24
+ "version": "1.0.0"
25
+ },
26
+ )
27
+
28
+
29
+
30
+ @api_router.get("/api/model", response_model=WordSchema)
31
+ def model(
32
+ *,
33
+ word: str,
34
+ file_name: str
35
+ ):
36
+ """
37
+ An api for serving the model for the PHI classification.
38
+ :param word: list of word tokens in a paragraph.
39
+ :param file_name: name of the wile.
40
+
41
+ :returns: json response that contains labeled
42
+ tags their respective classification.
43
+ """
44
+ txt = Sentence(word)
45
+ tagger.predict(txt)
46
+ labels, tags = [], []
47
+
48
+ for entity in txt.get_spans('ner'):
49
+ labels.append(entity.text)
50
+ tags.append(entity.get_label("ner").value)
51
+
52
+
53
+ return JSONResponse(
54
+ status_code=status.HTTP_201_CREATED,
55
+ content={
56
+ "code": 200,
57
+ "data": {
58
+ "labels": labels,
59
+ "tags": tags
60
+ }
61
+ },
62
+ )
63
+
64
+
65
+ app.include_router(api_router)
66
+
67
+ if __name__ == "__main__":
68
+ uvicorn.run("main:app", host="0.0.0.0", port=8000, reload=True, debug=True)
requirements.txt CHANGED
@@ -5,5 +5,6 @@ flair
5
  numpy
6
  pandas
7
  nltk
8
- panel
9
- hvplot
 
 
5
  numpy
6
  pandas
7
  nltk
8
+ uvicorn
9
+ fastapi
10
+ starlette
schema.py ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ from pydantic import BaseModel
2
+
3
+
4
+ class WordSchema(BaseModel):
5
+ word_text: str
settings.py ADDED
@@ -0,0 +1,35 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # PROGRAM: settings.py --> Program to handle all configurations inside the API
2
+ # PROGRAMMER: Jayra Gaile Ortiz
3
+ # VERSION 1: 08-29-22 Initial API setup and i2b2 converted sets
4
+ # REVISION 1.1: 11-05-22 Fizes to port web
5
+ # PURPOSE: Handles configurations for the API
6
+ # ALGORITHM: Stores app documentations, handles API configurations
7
+
8
+
9
+ """
10
+ This file includes all the configuration file for API
11
+ """
12
+ from fastapi import FastAPI
13
+ from fastapi.middleware.cors import CORSMiddleware
14
+
15
+
16
+ # Handles documentation title
17
+ app = FastAPI(
18
+ version="version 1.0.0",
19
+ title="Redaction Backend",
20
+ description="List of all endpoints for the Backend API",
21
+ docs_url="/", redoc_url= None
22
+ )
23
+
24
+ # Handles cors
25
+ origins = [
26
+ "http://localhost:8000"
27
+ ]
28
+
29
+ app.add_middleware(
30
+ CORSMiddleware,
31
+ allow_origins=origins,
32
+ allow_credentials=True,
33
+ allow_methods=["*"],
34
+ allow_headers=["*"],
35
+ )