Views
No views yet
pipeline API, setting the task to "ner" and using the "simple" aggregation strategy to automatically merge subword tokens back into recognizable entities.1from transformers import pipeline
2
3# Replace with your model ID
4model_id = "shroukAdel/xlm-roberta-base-finetuned-panx-all"
5
6# Initialize the NER pipeline
7# aggregation_strategy="simple" ensures subwords are combined into single entities
8ner_pipeline = pipeline(
9 "ner",
10 model=model_id,
11 aggregation_strategy="simple"
12)
13
14# Example 1: German
15text_de = "Jeff Dean ist ein Informatiker bei Google in Kalifornien"
16print(ner_pipeline(text_de))
17# Output: [{'entity_group': 'PER', 'score': 0.997, 'word': 'Jeff Dean', ...}, {'entity_group': 'ORG', 'score': 0.996, 'word': 'Google', ...}, {'entity_group': 'LOC', 'score': 0.998, 'word': 'Kalifornien', ...}]
18
19# Example 2: English
20text_en = "My name is Sarah and I live in London"
21print(ner_pipeline(text_en))
22# Output: [{'entity_group': 'PER', 'score': 0.996, 'word': 'Sarah', ...}, {'entity_group': 'LOC', 'score': 0.998, 'word': 'London', ...}]
23
24# Example 3: French
25text_fr = "Marie Curie était une physicienne française"
26print(ner_pipeline(text_fr))
27# Output: [{'entity_group': 'PER', 'score': 0.995, 'word': 'Marie Curie', ...}]
281from transformers import AutoTokenizer, AutoModelForTokenClassification
2
3model_id = "shroukAdel/xlm-roberta-base-finetuned-panx-all"
4
5tokenizer = AutoTokenizer.from_pretrained(model_id)
6model = AutoModelForTokenClassification.from_pretrained(model_id)
7
8# Now you can use the tokenizer and model for custom inference or further fine-tuning
9| Training Loss | Epoch | Step | Validation Loss | F1 |
|---|---|---|---|---|
| 0.2902 | 1.0 | 835 | 0.1952 | 0.8212 |
| 0.157 | 2.0 | 1670 | 0.1852 | 0.8387 |
| 0.1028 | 3.0 | 2505 | 0.1809 | 0.8529 |