| 1 | # SPDX-License-Identifier: Apache-2.0 |
| 2 | # SPDX-FileCopyrightText: Copyright contributors to the vLLM project |
| 3 | # Adapted from https://huggingface.co/boltuix/NeuroBERT-NER |
| 4 | |
| 5 | from argparse import Namespace |
| 6 | |
| 7 | from vllm import LLM, EngineArgs |
| 8 | from vllm.utils.argparse_utils import FlexibleArgumentParser |
| 9 | |
| 10 | |
| 11 | def parse_args(): |
| 12 | parser = FlexibleArgumentParser() |
| 13 | parser = EngineArgs.add_cli_args(parser) |
| 14 | # Set example specific arguments |
| 15 | parser.set_defaults( |
| 16 | model="boltuix/NeuroBERT-NER", |
| 17 | runner="pooling", |
| 18 | enforce_eager=True, |
| 19 | trust_remote_code=True, |
| 20 | ) |
| 21 | return parser.parse_args() |
| 22 | |
| 23 | |
| 24 | def main(args: Namespace): |
| 25 | # Sample prompts. |
| 26 | prompts = [ |
| 27 | "Barack Obama visited Microsoft headquarters in Seattle on January 2025." |
| 28 | ] |
| 29 | |
| 30 | # Create an LLM. |
| 31 | llm = LLM(**vars(args)) |
| 32 | tokenizer = llm.get_tokenizer() |
| 33 | label_map = llm.llm_engine.vllm_config.model_config.hf_config.id2label |
| 34 | |
| 35 | # Run inference |
| 36 | outputs = llm.encode(prompts, pooling_task="token_classify") |
| 37 | |
| 38 | for prompt, output in zip(prompts, outputs): |
| 39 | logits = output.outputs.data |
| 40 | predictions = logits.argmax(dim=-1) |
| 41 | |
| 42 | # Map predictions to labels |
| 43 | tokens = tokenizer.convert_ids_to_tokens(output.prompt_token_ids) |
| 44 | labels = [label_map[p.item()] for p in predictions] |
| 45 | |
| 46 | # Print results |
| 47 | for token, label in zip(tokens, labels): |
| 48 | if token not in tokenizer.all_special_tokens: |
| 49 | print(f"{token:15} → {label}") |
| 50 | |
| 51 | |
| 52 | if __name__ == "__main__": |
| 53 | args = parse_args() |
| 54 | main(args) |