Create README.md
Browse files
README.md
ADDED
|
@@ -0,0 +1,109 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
language: en
|
| 3 |
+
license: mit
|
| 4 |
+
tags:
|
| 5 |
+
- gpt
|
| 6 |
+
- mini-gpt
|
| 7 |
+
- text-generation
|
| 8 |
+
- language-modeling
|
| 9 |
+
- pytorch
|
| 10 |
+
datasets:
|
| 11 |
+
- custom
|
| 12 |
+
widget:
|
| 13 |
+
- text: "In the future, AI will"
|
| 14 |
+
---
|
| 15 |
+
|
| 16 |
+
# MiniGPT: A GPT Model Fine-Tuned on Shakespeare Texts
|
| 17 |
+
|
| 18 |
+
This is a custom GPT model fine-tuned on a small subset of Shakespeare's works. The model is built using PyTorch and Transformers, and is designed to generate creative text in the style of Shakespeare.
|
| 19 |
+
|
| 20 |
+
## ๐ Overview
|
| 21 |
+
|
| 22 |
+
This project demonstrates the process of fine-tuning a small version of GPT on a custom dataset and deploying it to Hugging Face for easy use and access. The model has been trained using a tiny Shakespeare dataset (`tiny_shakespeare.txt`), and is capable of generating creative text based on a given prompt.
|
| 23 |
+
|
| 24 |
+
## ๐งโ๐ป Setup
|
| 25 |
+
|
| 26 |
+
To use or replicate this model, follow the steps below to get started.
|
| 27 |
+
|
| 28 |
+
### 1. Installation
|
| 29 |
+
|
| 30 |
+
First, install the required dependencies:
|
| 31 |
+
|
| 32 |
+
๐ Dataset
|
| 33 |
+
This model was fine-tuned on a small dataset of Shakespeare's works. You can use your own dataset by following the structure below
|
| 34 |
+
# Load and tokenize data
|
| 35 |
+
from transformers import AutoTokenizer
|
| 36 |
+
|
| 37 |
+
tokenizer = AutoTokenizer.from_pretrained("gpt2")
|
| 38 |
+
tokenizer.pad_token = tokenizer.eos_token # Important for padding
|
| 39 |
+
|
| 40 |
+
# Load your dataset
|
| 41 |
+
file_path = "path/to/your_dataset.txt"
|
| 42 |
+
with open(file_path, "r", encoding="utf-8") as f:
|
| 43 |
+
raw_text = f.read()
|
| 44 |
+
|
| 45 |
+
encoded = tokenizer(raw_text, return_tensors="pt", truncation=True, padding=True, max_length=512)
|
| 46 |
+
input_ids = encoded["input_ids"]
|
| 47 |
+
|
| 48 |
+
๐งโ๐ป Training
|
| 49 |
+
To fine-tune the model, follow the instructions below:
|
| 50 |
+
from transformers import TrainingArguments
|
| 51 |
+
import wandb
|
| 52 |
+
|
| 53 |
+
wandb.login()
|
| 54 |
+
|
| 55 |
+
# Set up W&B logging configuration
|
| 56 |
+
wandb.init(project="mini-gpt", config={
|
| 57 |
+
"learning_rate": 0.001,
|
| 58 |
+
"epochs": 5,
|
| 59 |
+
"batch_size": 16
|
| 60 |
+
})
|
| 61 |
+
|
| 62 |
+
# Training loop
|
| 63 |
+
train(model, optimizer, epochs=5, encoded_dataset=encoded_dataset, batch_size=4)
|
| 64 |
+
|
| 65 |
+
๐พ Saving the Model
|
| 66 |
+
Once the model is trained, you can save it like this:
|
| 67 |
+
torch.save(model.state_dict(), "my-mini-gpt-model/pytorch_model.bin")
|
| 68 |
+
tokenizer.save_pretrained("my-mini-gpt-tokenizer")
|
| 69 |
+
|
| 70 |
+
|
| 71 |
+
๐ Uploading the Model to Hugging Face
|
| 72 |
+
You can upload the model to Hugging Face Hub using the following commands:
|
| 73 |
+
from huggingface_hub import login, create_repo, upload_folder
|
| 74 |
+
|
| 75 |
+
login(token="your_huggingface_token")
|
| 76 |
+
|
| 77 |
+
repo_id = "your_username/my-mini-gpt"
|
| 78 |
+
create_repo(repo_id, exist_ok=True)
|
| 79 |
+
|
| 80 |
+
upload_folder(folder_path="my-mini-gpt-model", repo_id=repo_id)
|
| 81 |
+
upload_folder(folder_path="my-mini-gpt-tokenizer", repo_id=repo_id)
|
| 82 |
+
๐ฎ Generating Text
|
| 83 |
+
Once the model is uploaded to Hugging Face, you can easily generate text using the following code:
|
| 84 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 85 |
+
|
| 86 |
+
# Load model and tokenizer from Hugging Face
|
| 87 |
+
model = AutoModelForCausalLM.from_pretrained("your_username/my-mini-gpt")
|
| 88 |
+
tokenizer = AutoTokenizer.from_pretrained("your_username/my-mini-gpt")
|
| 89 |
+
|
| 90 |
+
# Generate text based on a prompt
|
| 91 |
+
prompt = "In the future, AI will"
|
| 92 |
+
inputs = tokenizer(prompt, return_tensors="pt")
|
| 93 |
+
outputs = model.generate(
|
| 94 |
+
**inputs,
|
| 95 |
+
max_length=50,
|
| 96 |
+
temperature=1.0,
|
| 97 |
+
top_k=50,
|
| 98 |
+
top_p=0.95,
|
| 99 |
+
do_sample=True
|
| 100 |
+
)
|
| 101 |
+
|
| 102 |
+
generated_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 103 |
+
print(generated_text)
|
| 104 |
+
๐ License
|
| 105 |
+
This model is licensed under the MIT License.
|
| 106 |
+
|
| 107 |
+
๐ Citation
|
| 108 |
+
If you use this model or code in your research, please cite it as:
|
| 109 |
+
Tkachenko, A. (2025). MiniGPT: Fine-tuned GPT model for text generation. Hugging Face. Available at: https://huggingface.co/altkachenko11/my-mini-gpt
|