Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions gliner/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,10 @@ def prepare_model_inputs(self, texts: str, labels: str):
return model_input, raw_batch

def predict_entities(self, text, labels, flat_ner=True, threshold=0.5, multi_label=False):
return self.batch_predict_entities(
all_entities, model_output = self.batch_predict_entities(
[text], labels, flat_ner=flat_ner, threshold=threshold, multi_label=multi_label
)[0]
)
Comment on lines +132 to +134

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The model_output variable is assigned but never used. To signal that it's intentionally being ignored, you can use an underscore _ as the variable name. This improves code clarity and adheres to common Python conventions for unused variables.

Suggested change
all_entities, model_output = self.batch_predict_entities(
[text], labels, flat_ner=flat_ner, threshold=threshold, multi_label=multi_label
)[0]
)
all_entities, _ = self.batch_predict_entities(
[text], labels, flat_ner=flat_ner, threshold=threshold, multi_label=multi_label
)

return all_entities[0]

@torch.no_grad()
def batch_predict_entities(self, texts, labels, flat_ner=True, threshold=0.5, multi_label=False):
Expand All @@ -144,7 +145,8 @@ def batch_predict_entities(self, texts, labels, flat_ner=True, threshold=0.5, mu

model_input, raw_batch = self.prepare_model_inputs(texts, labels)

model_output = self.model(**model_input)[0]
model_output_raw = self.model(**model_input)
model_output = model_output_raw[0]

if not isinstance(model_output, torch.Tensor):
model_output = torch.from_numpy(model_output)
Expand All @@ -169,7 +171,7 @@ def batch_predict_entities(self, texts, labels, flat_ner=True, threshold=0.5, mu
})
all_entities.append(entities)

return all_entities
return all_entities, model_output_raw

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Preserve batch_predict_entities return contract

Changing batch_predict_entities to always return (all_entities, model_output_raw) breaks existing callers that were written against the previous contract (entities = model.batch_predict_entities(...) returning only entity lists). In downstream inference pipelines this can immediately raise type errors or silently corrupt post-processing when code iterates/serializes the returned value, so this should be made backward-compatible (for example via an opt-in flag for raw outputs) rather than a hard return-type change.

Useful? React with 👍 / 👎.


def evaluate(self, test_data, flat_ner=False, multi_label=False, threshold=0.5, batch_size=12, entity_types=None):
"""
Expand Down Expand Up @@ -427,4 +429,4 @@ def _from_pretrained(
if (config.class_token_index==-1 or config.vocab_size == -1) and resize_token_embeddings:
gliner.data_processor.transformer_tokenizer.add_tokens(add_tokens)

return gliner
return gliner