Views
No views yet
pip install torch tokenizers1import torch
2from tokenizers import Tokenizer
3from huggingface_hub import hf_hub_download
4
5# Download model and tokenizer from Hugging Face
6model_path = hf_hub_download(repo_id="IsmatS/gpt-wiki-az", filename="best_model.pt")
7tokenizer_path = hf_hub_download(repo_id="IsmatS/gpt-wiki-az", filename="az_tokenizer.json")
8
9# Load tokenizer
10tokenizer = Tokenizer.from_file(tokenizer_path)
11
12# Load model (requires train.py for GPT class definition)
13# Clone repo: git clone https://huggingface.co/IsmatS/gpt-wiki-az
14from train import GPT, GPTConfig
15
16config = GPTConfig()
17model = GPT(config)
18model.load_state_dict(torch.load(model_path, map_location="cpu"))
19model.eval()
20
21# Generate text
22prompt = "Azərbaycanın tarixi"
23input_ids = tokenizer.encode(prompt).ids
24input_tensor = torch.tensor([input_ids], dtype=torch.long)
25
26with torch.no_grad():
27 for _ in range(100):
28 output_logits, _ = model(input_tensor)
29 next_token = output_logits[:, -1, :].argmax(dim=-1)
30 input_ids.append(next_token.item())
31 input_tensor = torch.tensor([input_ids], dtype=torch.long)
32
33generated = tokenizer.decode(input_ids)
34print(generated)generate.py:1git clone https://huggingface.co/IsmatS/gpt-wiki-az
2cd gpt-wiki-az
3pip install -r requirements.txt
4python generate.py.
├── README.md
├── az_tokenizer.json # Trained tokenizer for Azerbaijani text
├── az_wiki_data.json # Collected Wikipedia data
├── best_model.pt # Saved state of the best trained model
├── collect_data.py # Script for collecting Wikipedia articles
├── generate.py # Text generation script using the trained model
├── prepare_data.py # Data preprocessing and tokenizer training
├── push_to_hf.py # Script to upload the trained model to Hugging Face Model Hub
├── requirements.txt # Project dependencies
└── train.py # GPT model training script1python -m venv .venv
2source .venv/bin/activate # On Windows: .venv\Scripts\activate1# Install PyTorch for Apple Silicon
2pip3 install --pre torch torchvision torchaudio --extra-index-url https://download.pytorch.org/whl/nightly/cpu
3
4# Install other required packages
5pip install transformers wikipedia-api beautifulsoup4 requests huggingface_hubpip install -r requirements.txtpython collect_data.pyaz_wiki_data.jsonpython prepare_data.pyaz_tokenizer.jsonpython train.pytrain.py:
python generate.pygenerate.py script:python push_to_hf.pypush_to_hf.py script:collect_data.py: Collects articles from Azerbaijani Wikipedia using categories like history, culture, literature, and geographyprepare_data.py: Preprocesses text and trains a BPE tokenizertrain.py: Contains GPT model implementation and training loopgenerate.py: Generates text using the trained model and sampling strategiespush_to_hf.py: Script for uploading the trained model to Hugging Face's Model Hubaz_wiki_data.json: Collected and preprocessed Wikipedia articlesaz_tokenizer.json: Trained BPE tokenizer for Azerbaijani textbest_model.pt: Saved state of the best model during trainingbest_model.ptcheckpoint_epoch_N.ptinterrupt_checkpoint.pt