Token Classification
GLiNER
PyTorch
multilingual
bert
Inference Endpoints
Rejebc commited on
Commit
374b849
·
verified ·
1 Parent(s): 39b1f14

Update handler.py

Browse files
Files changed (1) hide show
  1. handler.py +15 -10
handler.py CHANGED
@@ -19,17 +19,22 @@ class EndpointHandler:
19
  # Get inputs and labels
20
  inputs = data.get("inputs", "")
21
  labels = ["party", "document title"]
22
- print('labels',labels)
23
  # Predict entities using GLiNER
24
  entities = self.model.predict_entities(inputs, labels)
25
 
26
- # Format the results to match the expected output structure
27
- formatted_results = []
28
- for entity in entities:
29
- formatted_entity = {
30
- entity["label"]: entity["text"],
31
- }
32
- print(formatted_entity)
33
- formatted_results.append(formatted_entity)
34
 
35
- return formatted_results
 
 
 
 
 
 
 
 
 
 
 
 
 
19
  # Get inputs and labels
20
  inputs = data.get("inputs", "")
21
  labels = ["party", "document title"]
 
22
  # Predict entities using GLiNER
23
  entities = self.model.predict_entities(inputs, labels)
24
 
25
+ # Initialize a dictionary to store organized entities
26
+ organized_entities = {label: {"labels": [], "scores": []} for label in labels}
 
 
 
 
 
 
27
 
28
+ for entity in entities:
29
+ label = entity['label']
30
+ text = entity['text']
31
+ score = entity['score']
32
+
33
+ # Append text and score to the corresponding label
34
+ organized_entities[label]["labels"].append(text)
35
+ organized_entities[label]["scores"].append(score)
36
+
37
+ # Store organized entities in document metadata
38
+ doc.meta["entities"] = organized_entities
39
+
40
+ return {"documents": documents}