Create README.md
Browse files
README.md
CHANGED
|
@@ -1,3 +1,96 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Custom BART Model for Text Summarization
|
| 2 |
+
|
| 3 |
+
This project involves fine-tuning a BART model for text summarization tasks. The model was trained on custom data, and the resulting model is saved locally and uploaded to Hugging Face for further use.
|
| 4 |
+
|
| 5 |
+
## Table of Contents
|
| 6 |
+
|
| 7 |
+
- [Overview](#overview)
|
| 8 |
+
- [Installation](#installation)
|
| 9 |
+
- [Usage](#usage)
|
| 10 |
+
- [Training the Model](#training-the-model)
|
| 11 |
+
- [Saving and Uploading the Model](#saving-and-uploading-the-model)
|
| 12 |
+
- [Generating Summaries](#generating-summaries)
|
| 13 |
+
- [Contributing](#contributing)
|
| 14 |
+
- [License](#license)
|
| 15 |
+
|
| 16 |
+
## Overview
|
| 17 |
+
|
| 18 |
+
This project fine-tunes a BART model (`facebook/bart-base`) on custom summarization tasks. After training, the model can generate summaries for input text, which can be used for various applications like news article summarization, report generation, etc.
|
| 19 |
+
|
| 20 |
+
## Installation
|
| 21 |
+
|
| 22 |
+
To get started, ensure you have Python installed (preferably Python 3.8 or above). Install the required dependencies using the following command:
|
| 23 |
+
|
| 24 |
+
```bash
|
| 25 |
+
pip install transformers torch huggingface_hub
|
| 26 |
+
Usage
|
| 27 |
+
Loading the Model and Tokenizer
|
| 28 |
+
Ensure you have saved your trained model and tokenizer in the ./custom_bart_model directory. The code snippet below demonstrates how to load the model and generate summaries based on user input.
|
| 29 |
+
|
| 30 |
+
from transformers import BartTokenizer, BartForConditionalGeneration
|
| 31 |
+
import torch
|
| 32 |
+
|
| 33 |
+
# Load the model and tokenizer
|
| 34 |
+
model = "rohansb10/summary"
|
| 35 |
+
tokenizer = BartTokenizer.from_pretrained(model)
|
| 36 |
+
model = BartForConditionalGeneration.from_pretrained(model)
|
| 37 |
+
|
| 38 |
+
# Move model to the appropriate device
|
| 39 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 40 |
+
model.to(device)
|
| 41 |
+
model.eval()
|
| 42 |
+
|
| 43 |
+
def generate_summary(input_text):
|
| 44 |
+
inputs = tokenizer(input_text, return_tensors="pt", truncation=True, padding="max_length", max_length=512).to(device)
|
| 45 |
+
with torch.no_grad():
|
| 46 |
+
summary_ids = model.generate(inputs["input_ids"], max_length=128, num_beams=4, early_stopping=True)
|
| 47 |
+
output_text = tokenizer.decode(summary_ids[0], skip_special_tokens=True)
|
| 48 |
+
return output_text
|
| 49 |
+
|
| 50 |
+
user_input = input("Enter your text: ")
|
| 51 |
+
output = generate_summary(user_input)
|
| 52 |
+
print("\nModel Output:")
|
| 53 |
+
print(output)
|
| 54 |
+
Training the Model
|
| 55 |
+
The training process involves loading the pre-trained BART model and tokenizer, preparing a custom dataset, and training the model using the PyTorch DataLoader. Refer to the train_model() and evaluate_model() functions in the code for the detailed implementation.
|
| 56 |
+
|
| 57 |
+
Saving and Uploading the Model
|
| 58 |
+
After training, save your model and tokenizer using:
|
| 59 |
+
|
| 60 |
+
# Save the model and tokenizer
|
| 61 |
+
save_directory = "./custom_bart_model"
|
| 62 |
+
custom_model.model.save_pretrained(save_directory)
|
| 63 |
+
tokenizer.save_pretrained(save_directory)
|
| 64 |
+
Upload the saved model to Hugging Face Hub:
|
| 65 |
+
|
| 66 |
+
|
| 67 |
+
from huggingface_hub import notebook_login, create_repo, upload_folder
|
| 68 |
+
|
| 69 |
+
# Log in to Hugging Face
|
| 70 |
+
notebook_login()
|
| 71 |
+
|
| 72 |
+
# Upload to Hugging Face Hub
|
| 73 |
+
upload_folder(
|
| 74 |
+
folder_path=save_directory,
|
| 75 |
+
path_in_repo="",
|
| 76 |
+
repo_id="your_hf_username/custom-bart-finetuned",
|
| 77 |
+
repo_type="model",
|
| 78 |
+
)
|
| 79 |
+
Generating Summaries
|
| 80 |
+
The model can be used to generate summaries by feeding it input text. Adjust the parameters in the generate_summary() function to tweak the length, beam size, and other settings.
|
| 81 |
+
|
| 82 |
+
Contributing
|
| 83 |
+
Contributions are welcome! If you have any suggestions or improvements, please submit a pull request.
|
| 84 |
+
|
| 85 |
+
License
|
| 86 |
+
This project is licensed under the MIT License - see the LICENSE file for details.
|
| 87 |
+
|
| 88 |
+
|
| 89 |
+
### Key Points:
|
| 90 |
+
- **Overview**: Describes what the project is about.
|
| 91 |
+
- **Installation**: Lists dependencies and how to install them.
|
| 92 |
+
- **Usage**: Provides instructions on how to load and use the model.
|
| 93 |
+
- **Training, Saving, and Uploading**: Steps to train, save, and upload the model.
|
| 94 |
+
- **Contributing and License**: Information on contributing and licensing.
|
| 95 |
+
|
| 96 |
+
Feel free to modify any section to better fit your project’s needs!
|