### Install Swedish BERT Models Environment Source: https://github.com/kungbib/swedish-bert-models/blob/master/README.md This snippet outlines the steps to set up a Python virtual environment and install the necessary dependencies for running the Swedish BERT models. It includes cloning the repository, creating a virtual environment, and installing requirements. ```bash # git clone https://github.com/Kungbib/swedish-bert-models # cd swedish-bert-models # python3 -m venv venv # source venv/bin/activate # pip install --upgrade pip # pip install -r requirements.txt ``` -------------------------------- ### NER Output Example Source: https://github.com/kungbib/swedish-bert-models/blob/master/README.md Example of the expected output from the NER pipeline, showing identified entities and their scores. ```python [ { 'word': 'Kalle', 'score': 0.9998126029968262, 'entity': 'PER' }, { 'word': 'Pelle', 'score': 0.9998126029968262, 'entity': 'PER' }, { 'word': 'Kalle', 'score': 0.9814832210540771, 'entity': 'ORG' }, { 'word': 'och', 'score': 0.9814832210540771, 'entity': 'ORG' }, { 'word': 'Pelle', 'score': 0.9814832210540771, 'entity': 'ORG' } ] ``` -------------------------------- ### Reconstructed Tokens Output Example Source: https://github.com/kungbib/swedish-bert-models/blob/master/README.md Example output after reconstructing sub-tokens, showing correctly merged words and their associated entities. ```python [ { 'word': 'Engelbert', 'score': 0.99..., 'entity': 'PRS'}, { 'word': 'Rolls', 'score': 0.99..., 'entity': 'OBJ'}, { 'word': '-', 'score': 0.99..., 'entity': 'OBJ'}, { 'word': 'Royce', 'score': 0.99..., 'entity': 'OBJ'}, { 'word': 'Tele2', 'score': 0.99..., 'entity': 'LOC'}, { 'word': 'Arena', 'score': 0.99..., 'entity': 'LOC'}, { 'word': 'Djurgården', 'score': 0.99..., 'entity': 'ORG'}, { 'word': 'IF', 'score': 0.99..., 'entity': 'ORG'}, { 'word': 'VM', 'score': 0.99..., 'entity': 'EVN'}, { 'word': 'klockan', 'score': 0.99..., 'entity': 'TME'}, { 'word': 'två', 'score': 0.99..., 'entity': 'TME'}, { 'word': 'på', 'score': 0.99..., 'entity': 'TME'}, { 'word': 'kvällen', 'score': 0.54..., 'entity': 'TME'} ] ``` -------------------------------- ### BibTeX Citation for Swedish BERT Models Source: https://github.com/kungbib/swedish-bert-models/blob/master/README.md This snippet provides the BibTeX formatted citation for the project, including title, authors, year, and arXiv details. ```bibtex @misc{swedish-bert, title={Playing with Words at the National Library of Sweden -- Making a Swedish BERT}, author={Martin Malmsten and Love Börjeson and Chris Haffenden}, year={2020}, eprint={2007.01658}, archivePrefix={arXiv}, primaryClass={cs.CL} } ``` -------------------------------- ### Load BERT Base Swedish Cased Model and Tokenizer Source: https://github.com/kungbib/swedish-bert-models/blob/master/README.md Demonstrates how to load the pre-trained 'bert-base-swedish-cased' model and its corresponding tokenizer using the Huggingface Transformers library in Python. This is a standard BERT base model for Swedish. ```python from transformers import AutoModel,AutoTokenizer,TFAutoModel tok = AutoTokenizer.from_pretrained('KB/bert-base-swedish-cased') model = AutoModel.from_pretrained('KB/bert-base-swedish-cased') ``` -------------------------------- ### NER Pipeline with Swedish BERT Source: https://github.com/kungbib/swedish-bert-models/blob/master/README.md Instantiates a Named Entity Recognition (NER) pipeline using a Swedish BERT model fine-tuned for NER. Handles potential tokenizer issues with older Transformer versions. ```python from transformers import pipeline, TFBertForTokenClassification nlp = pipeline('ner', model='KB/bert-base-swedish-cased-ner', tokenizer='KB/bert-base-swedish-cased-ner') nlp('Kalle och Pelle startar firman Kalle och Pelle.') # Specifically using Tensorflow tf = TFBertForTokenClassification.from_pretrained('KB/bert-base-swedish-cased-ner') nlp = pipeline('ner', model=tf, tokenizer='KB/bert-base-swedish-cased-ner') ``` -------------------------------- ### Load ALBERT Base Model and Tokenizer Source: https://github.com/kungbib/swedish-bert-models/blob/master/README.md Loads a pre-trained ALBERT base model and its corresponding tokenizer from Huggingface for Swedish text. ```python from transformers import AutoModel, AutoTokenizer tok = AutoTokenizer.from_pretrained('KB/albert-base-swedish-cased-alpha') model = AutoModel.from_pretrained('KB/albert-base-swedish-cased-alpha') ``` -------------------------------- ### Available Swedish BERT and ALBERT Models Source: https://github.com/kungbib/swedish-bert-models/blob/master/README.md Lists the different Swedish language models available from KBLab, including their type (BERT, ALBERT, Electra), casing, and experimental status. Some models are fine-tuned for specific tasks like NER and POS tagging. ```text - bert-base-swedish-cased (*v1.1*) - A BERT trained with the same hyperparameters as first published by Google. - bert-base-swedish-cased-ner (*experimental*) - a BERT fine-tuned for NER using SUC 3.0. - bert-base-swedish-cased-pos (*experimental*) - a BERT fine-tuned for POS using SUC 3.0. - bert-base-swedish-cased-squad-experimental (*experimental*) - A first attempt att a Swedish QA using machine translated SQuAD 2.0 dataset - albert-base-swedish-cased-alpha (*alpha*) - A first attempt at an ALBERT for Swedish. - electra-small-swedish-cased - Small Electra models (generator and discriminator) trained for one million steps. ``` -------------------------------- ### Load TF BERT Model Source: https://github.com/kungbib/swedish-bert-models/blob/master/README.md Loads a pre-trained TensorFlow BERT model from Huggingface for general use. ```python from transformers import TFAutoModel model = TFAutoModel.from_pretrained('KB/bert-base-swedish-cased') ``` -------------------------------- ### Reconstructing Tokens with '##' Source: https://github.com/kungbib/swedish-bert-models/blob/master/README.md Demonstrates how to reconstruct words that have been split into sub-tokens (prefixed with '##') by the BERT tokenizer, particularly for NER tasks. ```python text = 'Engelbert tar sin Rolls-Royce till Tele2 Arena för att titta på Djurgården IF ' +\ 'som spelar fotboll i VM klockan två på kvällen.' nlp = pipeline('ner', model='KB/bert-base-swedish-cased-ner', tokenizer='KB/bert-base-swedish-cased-ner', ignore_labels=[]) l = [] t = nlp(text) in_word=False for i,token in enumerate(t): if token['entity'] == 'O': in_word = False continue if token['word'].startswith('##'): # deal with (one level of) orphaned ##-tokens if not in_word: l += [ t[i-1] ] l[-1]['entity'] = token['entity'] l[-1]['word'] += token['word'][2:] else: l += [ token ] in_word = True print(l) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.