Create README.md
Browse files
README.md
ADDED
@@ -0,0 +1,117 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# News Article Generation with GPT2
|
2 |
+
|
3 |
+
This repository hosts a quantized version of the GPT2 model, fine-tuned for generation of news article tasks. The model has been optimized for efficient deployment while maintaining high accuracy, making it suitable for resource-constrained environments.
|
4 |
+
|
5 |
+
## Model Details
|
6 |
+
|
7 |
+
- **Model Architecture:** gpt2
|
8 |
+
- **Task:** Text Summarization
|
9 |
+
- **Dataset:** Hugging Face's `ag_news'
|
10 |
+
- **Quantization:** Float16
|
11 |
+
- **Fine-tuning Framework:** Hugging Face Transformers
|
12 |
+
|
13 |
+
## Usage
|
14 |
+
|
15 |
+
### Installation
|
16 |
+
|
17 |
+
```sh
|
18 |
+
pip install transformers torch
|
19 |
+
```
|
20 |
+
|
21 |
+
### Loading the Model
|
22 |
+
|
23 |
+
```python
|
24 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
25 |
+
import torch
|
26 |
+
|
27 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
28 |
+
|
29 |
+
model_name = "AventIQ-AI/gpt2-news-article-generation"
|
30 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
31 |
+
model = AutoModelForCausalLM.from_pretrained(model_name).to(device)
|
32 |
+
|
33 |
+
import torch
|
34 |
+
import html
|
35 |
+
|
36 |
+
# Define test text
|
37 |
+
test_text = "The future of AI"
|
38 |
+
|
39 |
+
# Tokenize input
|
40 |
+
inputs = tokenizer(test_text, return_tensors="pt").to("cuda")
|
41 |
+
|
42 |
+
# Generate response
|
43 |
+
with torch.no_grad():
|
44 |
+
output_tokens = model.generate(
|
45 |
+
**inputs,
|
46 |
+
max_length=200, # Allow longer responses
|
47 |
+
num_beams=5, # Balances quality & diversity
|
48 |
+
repetition_penalty=2.0, # Reduce repeating patterns
|
49 |
+
temperature=0.2, # More deterministic response
|
50 |
+
top_k=100, # Allows more diverse words
|
51 |
+
top_p=0.9, # Keeps probability confidence
|
52 |
+
do_sample=True, # Sampling for variety
|
53 |
+
no_repeat_ngram_size=3, # Prevents excessive repetition
|
54 |
+
num_return_sequences=1, # Returns one best sequence
|
55 |
+
early_stopping=True, # Stops when response is complete
|
56 |
+
length_penalty=1.2, # Balances response length
|
57 |
+
pad_token_id=tokenizer.eos_token_id, # Prevents truncation
|
58 |
+
eos_token_id=tokenizer.eos_token_id, # Ensures completion
|
59 |
+
return_dict_in_generate=True, # Structured output
|
60 |
+
output_scores=True # Debugging purposes
|
61 |
+
)
|
62 |
+
|
63 |
+
# Decode and clean response
|
64 |
+
generated_response = tokenizer.decode(output_tokens.sequences[0], skip_special_tokens=True)
|
65 |
+
cleaned_response = html.unescape(generated_response).replace("#39;", "'").replace("quot;", '"')
|
66 |
+
|
67 |
+
print("\nGenerated Response:\n", cleaned_response)
|
68 |
+
```
|
69 |
+
|
70 |
+
# π ROUGE Evaluation Results
|
71 |
+
|
72 |
+
After fine-tuning the **T5-Small** model for text summarization, we obtained the following **ROUGE** scores:
|
73 |
+
|
74 |
+
| **Metric** | **Score** | **Meaning** |
|
75 |
+
|-------------|-----------|-------------|
|
76 |
+
| **ROUGE-1** | **0.3061** (~30%) | Measures overlap of **unigrams (single words)** between the reference and generated summary. |
|
77 |
+
| **ROUGE-2** | **0.1241** (~12%) | Measures overlap of **bigrams (two-word phrases)**, indicating coherence and fluency. |
|
78 |
+
| **ROUGE-L** | **0.2233** (~22%) | Measures **longest matching word sequences**, testing sentence structure preservation. |
|
79 |
+
| **ROUGE-Lsum** | **0.2620** (~26%) | Similar to ROUGE-L but optimized for summarization tasks. |
|
80 |
+
|
81 |
+
|
82 |
+
## Fine-Tuning Details
|
83 |
+
|
84 |
+
### Dataset
|
85 |
+
|
86 |
+
The Hugging Face's `ag_news` dataset was used, containing the text and their labels.
|
87 |
+
|
88 |
+
### Training
|
89 |
+
|
90 |
+
- Number of epochs: 3
|
91 |
+
- Batch size: 4
|
92 |
+
- Evaluation strategy: epoch
|
93 |
+
- Learning rate: 5e-5
|
94 |
+
|
95 |
+
### Quantization
|
96 |
+
|
97 |
+
Post-training quantization was applied using PyTorch's built-in quantization framework to reduce the model size and improve inference efficiency.
|
98 |
+
|
99 |
+
## Repository Structure
|
100 |
+
|
101 |
+
```
|
102 |
+
.
|
103 |
+
βββ model/ # Contains the quantized model files
|
104 |
+
βββ tokenizer_config/ # Tokenizer configuration and vocabulary files
|
105 |
+
βββ model.safetensors/ # Quantized Model
|
106 |
+
βββ README.md # Model documentation
|
107 |
+
```
|
108 |
+
|
109 |
+
## Limitations
|
110 |
+
|
111 |
+
- The model may not generalize well to domains outside the fine-tuning dataset.
|
112 |
+
- Quantization may result in minor accuracy degradation compared to full-precision models.
|
113 |
+
|
114 |
+
## Contributing
|
115 |
+
|
116 |
+
Contributions are welcome! Feel free to open an issue or submit a pull request if you have suggestions or improvements.
|
117 |
+
|