Update README.md
Browse files
README.md
CHANGED
@@ -1,86 +1,35 @@
|
|
1 |
-
BART Text Summarization
|
2 |
-
Model Details
|
3 |
-
Model: BART (Facebook's BART for Conditional Generation)
|
4 |
-
Base Model: bart-base
|
5 |
-
Task: Text Summarization
|
6 |
-
Intended Use
|
7 |
-
The BART Text Summarization model is designed to generate concise summaries given input texts. It can be used in various applications such as news summarization, document summarization, and generating short summaries for long pieces of text.
|
8 |
|
9 |
-
|
10 |
-
|
11 |
|
12 |
-
|
13 |
-
|
14 |
-
The model generates the following output:
|
15 |
|
16 |
-
|
17 |
-
|
18 |
-
Python Package Dependencies
|
19 |
-
pandas
|
20 |
-
transformers
|
21 |
-
torch
|
22 |
-
Preprocessing
|
23 |
-
Load the input data as a pandas DataFrame.
|
24 |
-
Install the required packages: !pip install transformers.
|
25 |
-
Import the necessary libraries: import pandas as pd and from transformers import BartTokenizer.
|
26 |
-
Initialize the BART tokenizer: tokenizer = BartTokenizer.from_pretrained('facebook/bart-base').
|
27 |
-
Tokenize the input texts and summaries using the tokenizer:
|
28 |
-
tokenized_texts = tokenizer.batch_encode_plus(x, truncation=True, padding='longest', max_length=max_input_length, return_tensors='pt')
|
29 |
-
tokenized_sums = tokenizer.batch_encode_plus(y, truncation=True, padding='longest', max_length=max_target_length, return_tensors='pt')
|
30 |
-
Extract the input and target tensors from the tokenized data:
|
31 |
-
input_ids = tokenized_texts['input_ids']
|
32 |
-
attention_mask = tokenized_texts['attention_mask']
|
33 |
-
labels = tokenized_sums['input_ids']
|
34 |
-
Split the tokenized data into training and testing sets using train_test_split from sklearn.model_selection:
|
35 |
-
train_inputs, test_inputs, train_labels, test_labels = train_test_split(input_ids, labels, test_size=0.2, random_state=42)
|
36 |
-
train_attention_mask, test_attention_mask, _, _ = train_test_split(attention_mask, labels, test_size=0.2, random_state=42)
|
37 |
-
train_inputs, test_inputs, train_labels, test_labels = train_test_split(input_ids, labels, test_size=0.2, random_state=42)
|
38 |
-
train_attention_mask, test_attention_mask, _, _ = train_test_split(attention_mask, labels, test_size=0.2, random_state=42)
|
39 |
-
Model Training
|
40 |
-
Install the required packages: !pip install transformers.
|
41 |
-
Import the necessary libraries: from transformers import BartForConditionalGeneration, AdamW and import torch.
|
42 |
-
Define the BART model: model = BartForConditionalGeneration.from_pretrained('facebook/bart-base').
|
43 |
-
Set the device to GPU if available, otherwise CPU: device = torch.device('cuda' if torch.cuda.is_available() else 'cpu').
|
44 |
-
Move the model to the device: model = model.to(device).
|
45 |
-
Set the optimizer: optimizer = AdamW(model.parameters(), lr=1e-5).
|
46 |
-
Set the batch size and number of training epochs: batch_size = 4 and num_epochs = 5.
|
47 |
-
Train the model:
|
48 |
-
model.train()
|
49 |
-
for epoch in range(num_epochs):
|
50 |
-
for i in range(0, len(train_inputs), batch_size):
|
51 |
-
# Prepare batch
|
52 |
-
batch_inputs = train_inputs[i:i+batch_size].to(device)
|
53 |
-
batch_attention_mask = train_attention_mask[i:i+batch_size].to(device)
|
54 |
-
batch_labels = train_labels[i:i+batch_size].to(device)
|
55 |
|
56 |
-
|
57 |
-
|
58 |
|
59 |
-
|
60 |
-
|
61 |
-
loss.backward()
|
62 |
|
63 |
-
|
64 |
-
|
65 |
-
optimizer.zero_grad()
|
66 |
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
Get the user input: user_text = input("Enter the text you want to summarize: ").
|
74 |
-
Tokenize the user input using the tokenizer:
|
75 |
tokenized_user_text = tokenizer.batch_encode_plus([user_text], truncation=True, padding='longest', max_length=max_input_length, return_tensors='pt')
|
76 |
user_input_ids = tokenized_user_text['input_ids'].to(device)
|
77 |
user_attention_mask = tokenized_user_text['attention_mask'].to(device)
|
78 |
-
Generate summaries for the user input:
|
79 |
outputs = model.generate(input_ids=user_input_ids, attention_mask=user_attention_mask, max_length=max_target_length)
|
80 |
decoded_summaries = [tokenizer.decode(output, skip_special_tokens=True) for output in outputs]
|
81 |
-
Print the generated summaries:
|
82 |
for summary in decoded_summaries:
|
83 |
print("Generated Summary:")
|
84 |
print(summary)
|
85 |
-
|
86 |
-
The performance of the BART Text Summarization model depends on various factors, including the quality and diversity of the training data, the size of the training set, and the number of training epochs. The dataset this model use is rather small.
|
|
|
1 |
+
# Model Card for BART Text Summarization
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
|
3 |
+
# Model Name
|
4 |
+
model_name = "BART Text Summarization"
|
5 |
|
6 |
+
# Description
|
7 |
+
description = "BART (Bidirectional and Auto-Regressive Transformers) is a sequence-to-sequence model designed for various NLP tasks, including text summarization. This model utilizes the 'facebook/bart-base' pre-trained model."
|
|
|
8 |
|
9 |
+
# Language
|
10 |
+
language = "English"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
|
12 |
+
# Intended Use
|
13 |
+
intended_use = "The BART Text Summarization model is intended to generate abstractive summaries for given input texts. It can be used in applications where summarizing lengthy texts into concise summaries is required."
|
14 |
|
15 |
+
# Limitations
|
16 |
+
limitations = "The BART model's performance may vary depending on the complexity and length of the input text. It may not accurately summarize texts with highly technical or domain-specific content. Additionally, the model was trained on a rather small dataset which may result in dissatisfied predictions."
|
|
|
17 |
|
18 |
+
# Evaluation Metrics
|
19 |
+
evaluation_metrics = "The BART Text Summarization model can be evaluated using various metrics such as ROUGE (Recall-Oriented Understudy for Gisting Evaluation), BLEU (Bilingual Evaluation Understudy), and METEOR (Metric for Evaluation of Translation with Explicit ORdering). These metrics measure the quality and similarity of the generated summaries to human-written references."
|
|
|
20 |
|
21 |
+
# Version
|
22 |
+
version = "1.0"
|
23 |
+
|
24 |
+
|
25 |
+
# Deployment Instructions
|
26 |
+
user_text = input("Enter the text you want to summarize: ")
|
|
|
|
|
27 |
tokenized_user_text = tokenizer.batch_encode_plus([user_text], truncation=True, padding='longest', max_length=max_input_length, return_tensors='pt')
|
28 |
user_input_ids = tokenized_user_text['input_ids'].to(device)
|
29 |
user_attention_mask = tokenized_user_text['attention_mask'].to(device)
|
|
|
30 |
outputs = model.generate(input_ids=user_input_ids, attention_mask=user_attention_mask, max_length=max_target_length)
|
31 |
decoded_summaries = [tokenizer.decode(output, skip_special_tokens=True) for output in outputs]
|
|
|
32 |
for summary in decoded_summaries:
|
33 |
print("Generated Summary:")
|
34 |
print(summary)
|
35 |
+
|
|