### Start Model Training Source: https://github.com/ereverter/bertsum-hf/blob/main/README.md Use this command to initiate the model training process. Specify input data, output directories for preprocessed data and checkpoints, and the configuration file. ```bash python run_extsum.py \ -i data/ext_cnn_dailymail \ -o data/prep_ext_cnn_dailymail \ -c config.json \ -d checkpoints/bertsum/ ``` -------------------------------- ### Load Dataset, Tokenizer, and Model Source: https://github.com/ereverter/bertsum-hf/blob/main/README.md Use Hugging Face's `datasets`, `AutoTokenizer`, and `AutoModel` to load the extractive CNN Daily Mail dataset and the fine-tuned BERT model. Ensure you have the necessary libraries installed. ```python from datasets import load_dataset from transformers import AutoTokenizer, AutoModel dataset = load_dataset("ereverter/cnn_dailymail_extractive", "3.0.0") tokenizer = AutoTokenizer.from_pretrained("ereverter/bert-finetuned-cnn_dailymail") model = AutoModel.from_pretrained("ereverter/bert-finetuned-cnn_dailymail") ``` -------------------------------- ### Prepare Model Inputs Source: https://github.com/ereverter/bertsum-hf/blob/main/playground.ipynb Prepares sample data for model input using the tokenizer. It then separates the updated sample from the model inputs. ```python model_inputs = prepare_sample(sample, tokenizer) updated_sample = model_inputs.pop('sample') ``` -------------------------------- ### Prepare Sample for Summarization Source: https://github.com/ereverter/bertsum-hf/blob/main/playground.ipynb Selects a sample from the loaded dataset for processing. This snippet retrieves the source text of the 24th sample from the test split of the dataset. ```python sample = data_dict['test'][24]['src'] sample ``` -------------------------------- ### Evaluate Model Performance Source: https://github.com/ereverter/bertsum-hf/blob/main/README.md Execute this command to evaluate the trained model using the ROUGE metric. Provide the path to the model checkpoint, the dataset for evaluation, and directories for saving results. ```bash python evaluate_model.py \ --model checkpoints/bertsum/bertsum \ --dataset data/ext_cnn_dailymail \ --save_dir checkpoints/bertsum \ --save_name results.json ``` -------------------------------- ### Load Tokenizer and Model Source: https://github.com/ereverter/bertsum-hf/blob/main/playground.ipynb Loads the tokenizer and the BertSummarizer model from a pre-trained checkpoint. This is a prerequisite for processing text data. ```python tokenizer = AutoTokenizer.from_pretrained(checkpoint) model = BertSummarizer.from_pretrained(checkpoint) ``` -------------------------------- ### Perform Model Inference Source: https://github.com/ereverter/bertsum-hf/blob/main/playground.ipynb Runs the model with the prepared inputs and captures the outputs, which include logits and attention masks. ```python outputs = model(**model_inputs) outputs ``` -------------------------------- ### Import Libraries and Define Constants Source: https://github.com/ereverter/bertsum-hf/blob/main/playground.ipynb Imports necessary libraries such as numpy and components from the BertSummarizer and Hugging Face transformers libraries. It also defines the model checkpoint and dataset names. ```python import numpy as np from src.bertsum import BertSummarizerConfig, BertSummarizer from transformers import AutoTokenizer, AutoModel from datasets import load_dataset from utils import tokenize_text_to_sentences, prepare_sample checkpoint = 'eReverter/bert-finetuned-cnn_dailymail' dataset = 'eReverter/cnn_dailymail_extractive' ``` -------------------------------- ### Load Dataset Source: https://github.com/ereverter/bertsum-hf/blob/main/playground.ipynb Loads the specified dataset from Hugging Face Datasets. This snippet shows how to load the dataset and then display its structure. ```python data_dict = load_dataset('eReverter/cnn_dailymail_extractive') data_dict ``` -------------------------------- ### Generate Summary from Model Output Source: https://github.com/ereverter/bertsum-hf/blob/main/playground.ipynb Selects the top 3 sentences based on the model's logits to form a summary. This requires the model outputs and the preprocessed sample. ```python # Select top 3 sentences for the summary summary = ' '.join([updated_sample[i] for i in outputs['logits'].topk(3).indices.detach().cpu().numpy()[0]]) summary ``` -------------------------------- ### Convert Abstractive to Extractive Dataset Source: https://github.com/ereverter/bertsum-hf/blob/main/README.md Use this command to convert abstractive summarization datasets to an extractive format. Specify dataset path, HuggingFace hub status, field names for text and summary, desired summary length, selection algorithm, and output path. ```bash python abs_to_ext.py \ -f cnn_dailymail \ -c '3.0.0' \ -hf \ -s 'article' \ -t 'highlights' \ -sz 3 \ -m 'greedy' \ -o data/ext_cnn_dailymail ``` -------------------------------- ### Text Summarization Pipeline Source: https://github.com/ereverter/bertsum-hf/blob/main/playground.ipynb This snippet demonstrates a complete text summarization process using Hugging Face Transformers. It tokenizes input text, prepares it for the model, generates a summary, and extracts the top 3 most relevant sentences. ```python wikipedia_text = "" Wine is an alcoholic drink typically made from fermented grapes. Yeast consumes the sugar in the grapes and converts it to ethanol and carbon dioxide, releasing heat in the process. Different varieties of grapes and strains of yeasts are major factors in different styles of wine. These differences result from the complex interactions between the biochemical development of the grape, the reactions involved in fermentation, the grape's growing environment (terroir), and the wine production process. Many countries enact legal appellations intended to define styles and qualities of wine. These typically restrict the geographical origin and permitted varieties of grapes, as well as other aspects of wine production. Wines can be made by fermentation of other fruit crops such as plum, cherry, pomegranate, blueberry, currant and elderberry. " sample = tokenize_text_to_sentences(wikipedia_text) model_inputs = prepare_sample(sample, tokenizer) updated_sample = model_inputs.pop('sample') outputs = model(**model_inputs) summary = ' '.join([updated_sample[i] for i in np.sort(outputs['logits'].topk(3).indices.detach().cpu().numpy()[0])]) summary ``` -------------------------------- ### Verify Output Length Source: https://github.com/ereverter/bertsum-hf/blob/main/playground.ipynb Checks if the number of logits generated by the model matches the length of the updated sample. ```python len(outputs['logits'][0]) == len(updated_sample) ``` -------------------------------- ### Load Autoreload Extension Source: https://github.com/ereverter/bertsum-hf/blob/main/playground.ipynb Loads the autoreload extension for interactive Python environments. This extension automatically reloads modules before executing code, which is useful during development. ```python %load_ext autoreload %autoreload 2 ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.