Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
drgnfrts
GitHub Repository: drgnfrts/Singapore-Locations-NER
Path: blob/main/fastapi/working.py
744 views
1
import spacy
2
from fastapi import FastAPI
3
from pydantic import BaseModel
4
from typing import List
5
6
app = FastAPI()
7
model_path = "../models/model_v3.0/model-best"
8
nlp = spacy.load(model_path)
9
10
11
class Input(BaseModel):
12
text: str
13
14
class Config:
15
schema_extra = {
16
"example": {
17
"text": "The Urban Redevelopment Authority is located at 45 Maxwell Road."
18
}
19
}
20
21
22
@app.post("/find-locations-POST")
23
def find_locations_post(input: Input):
24
doc = nlp(input.text)
25
list_of_entities = []
26
for ent in doc.ents:
27
ent_result = {}
28
ent_result["entity"] = ent.text
29
ent_result["label"] = ent.label_
30
list_of_entities.append(ent_result)
31
return {"entities": list_of_entities}
32
33