Update app.py
Browse files
app.py
CHANGED
@@ -26,10 +26,38 @@ nlp = pipeline("ner", model=model_name, tokenizer=model_name)
|
|
26 |
class TextRequest(BaseModel):
|
27 |
text: str
|
28 |
|
29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
30 |
def predict(request: TextRequest):
|
31 |
-
|
32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
33 |
|
34 |
|
35 |
@app.get("/")
|
|
|
26 |
class TextRequest(BaseModel):
|
27 |
text: str
|
28 |
|
29 |
+
# Define a Pydantic model for the response
|
30 |
+
class Entity(BaseModel):
|
31 |
+
entity: str
|
32 |
+
score: float
|
33 |
+
index: int
|
34 |
+
word: str
|
35 |
+
start: int
|
36 |
+
end: int
|
37 |
+
|
38 |
+
|
39 |
+
@app.post("/predict/", response_model=List[Entity])
|
40 |
def predict(request: TextRequest):
|
41 |
+
try:
|
42 |
+
# Use the model to perform NER on the input text
|
43 |
+
ner_results = nlp(request.text)
|
44 |
+
|
45 |
+
# Format the results to match the desired output structure
|
46 |
+
entities = [
|
47 |
+
{
|
48 |
+
"entity": result["entity"],
|
49 |
+
"score": result["score"],
|
50 |
+
"index": result["index"],
|
51 |
+
"word": result["word"],
|
52 |
+
"start": result["start"],
|
53 |
+
"end": result["end"]
|
54 |
+
}
|
55 |
+
for result in ner_results
|
56 |
+
]
|
57 |
+
|
58 |
+
return entities
|
59 |
+
except Exception as e:
|
60 |
+
raise HTTPException(status_code=500, detail=str(e))
|
61 |
|
62 |
|
63 |
@app.get("/")
|