File size: 2,593 Bytes
eeaf923
 
 
 
 
 
 
 
 
9252393
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
---
pipeline_tag: text-generation
tags:
- model_hub_mixin
- pytorch_model_hub_mixin
---

This model has been pushed to the Hub using the [PytorchModelHubMixin](https://huggingface.co/docs/huggingface_hub/package_reference/mixins#huggingface_hub.PyTorchModelHubMixin) integration:
- Library: https://huggingface.co/Aananda-giri/GPT2-Nepali/
- Docs: [More Information Needed]

---

# GPT-2-Nepali-512 Model
* 512 represents context length

* This repository contains a custom GPT-2 model trained on Nepali text. Follow the instructions below to use this model for text generation.

---

## How to Use the Model

1. **Download the Required Code**  
   Save the [`model_code.py`](https://github.com/Aananda-giri/llm.np/blob/main/3.%20GPT-2/sebastian_gutenberg/huggingface_hub/model_code.py) file in the same directory where you'll run the script.

2. **Install Required Libraries**  
   Ensure you have the necessary libraries installed:  
   ```bash
   pip install transformers torch
   ```

3. **Run the Following Code**  
   Here's an example to load the model and generate text:

   ```python
   import torch
   from model_code import GPTModel, generate_and_print_sample
   from transformers import PreTrainedTokenizerFast

   # Load the tokenizer
   tokenizer = PreTrainedTokenizerFast.from_pretrained("Aananda-giri/NepaliBPE")

   # Define the starting text
   start_context = "रामले भात"

   # Load the pre-trained model
   loaded_model = GPTModel.from_pretrained("Aananda-giri/GPT2-Nepali")

   # Move the model to the appropriate device (CPU or GPU)
   device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
   loaded_model.to(device)

   # Generate text
   generate_and_print_sample(
       loaded_model, tokenizer, device, start_context
   )
   ```

---

## Additional Notes

- **Tokenizer**: The model uses a pre-trained tokenizer available at `Aananda-giri/NepaliBPE`. Ensure this is downloaded and accessible during runtime.
- **Dependencies**: This code requires `transformers` (by Hugging Face) and `torch` (PyTorch). Install them if not already installed.
- **Device Compatibility**: The script automatically detects if a CUDA-enabled GPU is available and utilizes it for faster inference. If not, it defaults to the CPU.

---

## Example Output

Input:
```
रामले भात
```

Generated Text:
```
रामले भात खाएर सन्तोष माने। ऊ आफ्ना साथीहरूसँग रमाइलो गरिरहेको थियो।
```

---

Let me know if you'd like further assistance!