Unknownaut commited on
Commit
eb7379d
·
verified ·
1 Parent(s): ad86391

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +58 -0
app.py ADDED
@@ -0,0 +1,58 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI
2
+ from pydantic import BaseModel
3
+ from transformers import pipeline
4
+
5
+ app = FastAPI()
6
+
7
+ # Load NER pipeline
8
+ ner_pipeline = pipeline(
9
+ "ner",
10
+ model="dslim/bert-large-NER",
11
+ aggregation_strategy="simple"
12
+ )
13
+
14
+ class RequestData(BaseModel):
15
+ sentence: str
16
+
17
+ @app.get("/")
18
+ def health():
19
+ return {"status": "ok"}
20
+
21
+ @app.post("/predict")
22
+ def predict(data: RequestData):
23
+
24
+ predictions = ner_pipeline(data.sentence)
25
+
26
+ allowed = {"PER", "ORG", "LOC"}
27
+
28
+ entities = []
29
+ seen = set()
30
+
31
+ for pred in predictions:
32
+
33
+ label = pred["entity_group"]
34
+
35
+ if label not in allowed:
36
+ continue
37
+
38
+ start = pred["start"]
39
+ end = pred["end"]
40
+
41
+ key = (start, end)
42
+
43
+ if key in seen:
44
+ continue
45
+
46
+ seen.add(key)
47
+
48
+ entities.append({
49
+ "text": pred["word"],
50
+ "start": start,
51
+ "end": end,
52
+ "label": label,
53
+ "score": float(pred["score"])
54
+ })
55
+
56
+ return {
57
+ "entities": entities
58
+ }