Spaces:
Paused
Paused
File size: 1,045 Bytes
eb7379d 4c881de eb7379d | 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 | from fastapi import FastAPI
from pydantic import BaseModel
from transformers import pipeline
app = FastAPI()
# Load NER pipeline
ner_pipeline = pipeline(
"ner",
model="dslim/bert-large-NER",
aggregation_strategy="simple"
)
class RequestData(BaseModel):
sentence: str
@app.get("/")
def health():
return {"status": "ok"}
@app.post("/predict")
def predict(data: RequestData):
predictions = ner_pipeline(data.sentence)
allowed = {"PER", "ORG", "LOC", "MISC"}
entities = []
seen = set()
for pred in predictions:
label = pred["entity_group"]
if label not in allowed:
continue
start = pred["start"]
end = pred["end"]
key = (start, end)
if key in seen:
continue
seen.add(key)
entities.append({
"text": pred["word"],
"start": start,
"end": end,
"label": label,
"score": float(pred["score"])
})
return {
"entities": entities
} |