### Run NER Inference on GPU for Faster Processing Source: https://context7.com/dicta-il/dictabert-ner/llms.txt To accelerate inference on a graphics card, pass the `device=0` parameter to the `pipeline`. This example demonstrates setting the device to GPU if available, otherwise falling back to CPU. ```python from transformers import pipeline from tokenizers.decoders import WordPiece import torch device = 0 if torch.cuda.is_available() else -1 # 0 = GPU ראשון, -1 = CPU oracle = pipeline( 'ner', model='dicta-il/dictabert-ner', aggregation_strategy='simple', device=device ) oracle.tokenizer.backend_tokenizer.decoder = WordPiece() sentence = "מכון ויצמן למדע ממוקם ברחובות ומתמחה במחקר מדעי בסיסי." results = oracle(sentence) for entity in results: print(f"[{entity['entity_group']}] {entity['word']} ({entity['score']:.4f})") # [ORG] מכון ויצמן למדע (0.9991) # [LOC] רחובות (0.9988) ``` -------------------------------- ### Batch Processing of Multiple Sentences Source: https://context7.com/dicta-il/dictabert-ner/llms.txt This example illustrates how to process multiple sentences in a batch, which significantly improves inference efficiency, especially on GPUs. It utilizes `padding=True` and `truncation=True` for optimal batching. ```APIDOC ## Batch Processing of Multiple Sentences ### Description This example illustrates how to process multiple sentences in a batch, which significantly improves inference efficiency, especially on GPUs. It utilizes `padding=True` and `truncation=True` for optimal batching. ### Method ```python from transformers import pipeline from tokenizers.decoders import WordPiece oracle = pipeline('ner', model='dicta-il/dictabert-ner', aggregation_strategy='simple') oracle.tokenizer.backend_tokenizer.decoder = WordPiece() sentences = [ "שרת החוץ גולדה מאיר ביקרה בפריז בשנת 1969.", "חברת אינטל פתחה מפעל חדש בקריית גת.", "הכנסת התכנסה לדיון מיוחד בירושלים ביום שלישי." ] batch_results = oracle(sentences) for sentence, entities in zip(sentences, batch_results): print(f"\nמשפט: {sentence}") for ent in entities: print(f" [{ent['entity_group']}] '{ent['word']}' (ציון: {ent['score']:.4f})") ``` ### Response Example ``` משפט: שרת החוץ גולדה מאיר ביקרה בפריז בשנת 1969. [TTL] 'שרת החוץ' (ציון: 0.9981) [PER] 'גולדה מאיר' (ציון: 0.9998) [GPE] 'פריז' (ציון: 0.9995) [TIMEX] '1969' (ציון: 0.9997) משפט: חברת אינטל פתחה מפעל חדש בקריית גת. [ORG] 'אינטל' (ציון: 0.9993) [LOC] 'קריית גת' (ציון: 0.9989) ``` ``` -------------------------------- ### Direct Loading of Model and Tokenizer Source: https://context7.com/dicta-il/dictabert-ner/llms.txt This section shows how to load the `BertForTokenClassification` model and `BertTokenizer` directly. This approach provides more control over the inference process, including batch processing and direct access to raw probabilities. ```APIDOC ## Direct Loading of Model and Tokenizer ### Description This section shows how to load the `BertForTokenClassification` model and `BertTokenizer` directly. This approach provides more control over the inference process, including batch processing and direct access to raw probabilities. ### Method ```python import torch from transformers import BertTokenizerFast, BertForTokenClassification model_name = 'dicta-il/dictabert-ner' tokenizer = BertTokenizerFast.from_pretrained(model_name) model = BertForTokenClassification.from_pretrained(model_name) model.eval() sentence = "ראש הממשלה בנימין נתניהו נפגש עם נשיא ארצות הברית בוושינגטון." inputs = tokenizer(sentence, return_tensors='pt', truncation=True, max_length=512) with torch.no_grad(): outputs = model(**inputs) logits = outputs.logits # (1, seq_len, num_labels=27) predictions = torch.argmax(logits, dim=-1)[0] # (seq_len,) tokens = tokenizer.convert_ids_to_tokens(inputs['input_ids'][0]) id2label = model.config.id2label for token, pred_id in zip(tokens, predictions): label = id2label[pred_id.item()] if label != 'O' and not token.startswith('['): print(f"{token:20s} -> {label}") ``` ### Response Example ``` בנימין -> B-PER ##נתני -> I-PER ##הו -> I-PER ארצות -> B-GPE הברית -> I-GPE בוושינגטון -> B-GPE ``` ``` -------------------------------- ### Loading NER Model with Transformers pipeline Source: https://context7.com/dicta-il/dictabert-ner/llms.txt This snippet demonstrates how to load the DictaBERT NER model using the `transformers` library's `pipeline` function. It configures the pipeline for NER with simple aggregation and sets a WordPiece decoder for the tokenizer to correctly reconstruct words. ```APIDOC ## Loading NER Model with Transformers pipeline ### Description This snippet demonstrates how to load the DictaBERT NER model using the `transformers` library's `pipeline` function. It configures the pipeline for NER with simple aggregation and sets a WordPiece decoder for the tokenizer to correctly reconstruct words. ### Method ```python from transformers import pipeline from tokenizers.decoders import WordPiece # Load NER model for Hebrew oracle = pipeline( 'ner', model='dicta-il/dictabert-ner', aggregation_strategy='simple' ) # Set decoder for WordPiece reconstruction oracle.tokenizer.backend_tokenizer.decoder = WordPiece() # Example sentence sentence = '''דוד בן-גוריון (16 באוקטובר 1886 - ו' בכסלו תשל"ד) היה מדינאי ישראלי וראש הממשלה הראשון של מדינת ישראל.''' results = oracle(sentence) print(results) ``` ### Response Example ```json [ {"entity_group": "PER", "score": 0.9999443, "word": "דוד בן - גוריון", "start": 0, "end": 13}, {"entity_group": "TIMEX", "score": 0.99987966, "word": "16 באוקטובר 1886","start": 15, "end": 31}, {"entity_group": "TIMEX", "score": 0.9998579, "word": "ו' בכסלו תשל\"ד", "start": 34, "end": 48}, {"entity_group": "TTL", "score": 0.99963045, "word": "וראש הממשלה", "start": 68, "end": 79}, {"entity_group": "GPE", "score": 0.9997943, "word": "ישראל", "start": 96, "end": 101} ] ``` ``` -------------------------------- ### Directly Load Model and Tokenizer Source: https://context7.com/dicta-il/dictabert-ner/llms.txt Manually load `BertForTokenClassification` and `BertTokenizer` for full control over inference, including batch processing and direct access to raw probabilities. Ensure the model is set to evaluation mode (`model.eval()`) and inference is done within `torch.no_grad()`. ```python import torch from transformers import BertTokenizerFast, BertForTokenClassification model_name = 'dicta-il/dictabert-ner' tokenizer = BertTokenizerFast.from_pretrained(model_name) model = BertForTokenClassification.from_pretrained(model_name) model.eval() sentence = "ראש הממשלה בנימין נתניהו נפגש עם נשיא ארצות הברית בוושינגטון." inputs = tokenizer(sentence, return_tensors='pt', truncation=True, max_length=512) with torch.no_grad(): outputs = model(**inputs) logits = outputs.logits # (1, seq_len, num_labels=27) predictions = torch.argmax(logits, dim=-1)[0] # (seq_len,) tokens = tokenizer.convert_ids_to_tokens(inputs['input_ids'][0]) id2label = model.config.id2label for token, pred_id in zip(tokens, predictions): label = id2label[pred_id.item()] if label != 'O' and not token.startswith('['): print(f"{token:20s} -> {label}") # פלט לדוגמה: # בנימין -> B-PER # ##נתני -> I-PER # ##הו -> I-PER # ארצות -> B-GPE # הברית -> I-GPE # בוושינגטון -> B-GPE ``` -------------------------------- ### Load NER Model with Transformers Pipeline Source: https://context7.com/dicta-il/dictabert-ner/llms.txt Use the `pipeline` function for easy loading and inference. Set `aggregation_strategy='simple'` to merge tokens into single entities and add a `WordPiece` decoder to the tokenizer for correct word reconstruction. ```python from transformers import pipeline from tokenizers.decoders import WordPiece # טעינת מודל NER לעברית oracle = pipeline( 'ner', model='dicta-il/dictabert-ner', aggregation_strategy='simple' ) # הגדרת decoder לשחזור מילים שלמות מ-WordPiece oracle.tokenizer.backend_tokenizer.decoder = WordPiece() # משפט לדוגמה sentence = '''דוד בן-גוריון (16 באוקטובר 1886 - ו' בכסלו תשל"ד) היה מדינאי ישראלי וראש הממשלה הראשון של מדינת ישראל.''' results = oracle(sentence) print(results) # פלט צפוי: # [ # {"entity_group": "PER", "score": 0.9999443, "word": "דוד בן - גוריון", "start": 0, "end": 13}, # {"entity_group": "TIMEX", "score": 0.99987966, "word": "16 באוקטובר 1886","start": 15, "end": 31}, # {"entity_group": "TIMEX", "score": 0.9998579, "word": "ו' בכסלו תשל\"ד", "start": 34, "end": 48}, # {"entity_group": "TTL", "score": 0.99963045, "word": "וראש הממשלה", "start": 68, "end": 79}, # {"entity_group": "GPE", "score": 0.9997943, "word": "ישראל", "start": 96, "end": 101} # ] ``` -------------------------------- ### Batch Processing of Multiple Sentences Source: https://context7.com/dicta-il/dictabert-ner/llms.txt Process multiple sentences simultaneously for increased inference efficiency, especially on GPUs. Use `padding=True` and `truncation=True` when passing a list of sentences to the pipeline. ```python from transformers import pipeline from tokenizers.decoders import WordPiece oracle = pipeline('ner', model='dicta-il/dictabert-ner', aggregation_strategy='simple') oracle.tokenizer.backend_tokenizer.decoder = WordPiece() sentences = [ "שרת החוץ גולדה מאיר ביקרה בפריז בשנת 1969.", "חברת אינטל פתחה מפעל חדש בקריית גת.", "הכנסת התכנסה לדיון מיוחד בירושלים ביום שלישי." ] batch_results = oracle(sentences) for sentence, entities in zip(sentences, batch_results): print(f"\nמשפט: {sentence}") for ent in entities: print(f" [{ent['entity_group']}] '{ent['word']}' (ציון: {ent['score']:.4f})") # פלט לדוגמה: # משפט: שרת החוץ גולדה מאיר ביקרה בפריז בשנת 1969. # [TTL] 'שרת החוץ' (ציון: 0.9981) # [PER] 'גולדה מאיר' (ציון: 0.9998) # [GPE] 'פריז' (ציון: 0.9995) # [TIMEX] '1969' (ציון: 0.9997) # # משפט: חברת אינטל פתחה מפעל חדש בקריית גת. # [ORG] 'אינטל' (ציון: 0.9993) # [LOC] 'קריית גת' (ציון: 0.9989) ``` -------------------------------- ### Filter Named Entities by Type in Python Source: https://context7.com/dicta-il/dictabert-ner/llms.txt After running the NER model, filter the results to include only specific entity types like PER, ORG, GPE, LOC, or TIMEX. Ensure the WordPiece decoder and 'simple' aggregation strategy are used for accurate entity grouping. ```python from transformers import pipeline from tokenizers.decoders import WordPiece oracle = pipeline('ner', model='dicta-il/dictabert-ner', aggregation_strategy='simple') oracle.tokenizer.backend_tokenizer.decoder = WordPiece() text = """ ברק אובמה, לשעבר נשיא ארצות הברית, נשא נאום באוניברסיטת תל אביב ביום שני. הוא דיבר על ארגון האומות המאוחדות ועל המשבר האקלימי שהחל בשנות ה-90. """ all_entities = oracle(text) # סינון לפי סוג ישות def filter_entities(entities, entity_type): return [e for e in entities if e['entity_group'] == entity_type] people = filter_entities(all_entities, 'PER') orgs = filter_entities(all_entities, 'ORG') places = filter_entities(all_entities, 'GPE') + filter_entities(all_entities, 'LOC') times = filter_entities(all_entities, 'TIMEX') print("אנשים:", [e['word'] for e in people]) # אנשים: ['ברק אובמה'] print("ארגונים:", [e['word'] for e in orgs]) # ארגונים: ['אומות המאוחדות'] print("מקומות:", [e['word'] for e in places]) # מקומות: ['ארצות הברית', 'תל אביב'] print("זמנים:", [e['word'] for e in times]) # זמנים: ['שנות ה-90'] ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.