Instructions to use bolbolzaban/gpt2-persian with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use bolbolzaban/gpt2-persian with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("text-generation", model="bolbolzaban/gpt2-persian")# Load model directly from transformers import AutoTokenizer, AutoModelForCausalLM tokenizer = AutoTokenizer.from_pretrained("bolbolzaban/gpt2-persian") model = AutoModelForCausalLM.from_pretrained("bolbolzaban/gpt2-persian") - Notebooks
- Google Colab
- Kaggle
- Local Apps
- vLLM
How to use bolbolzaban/gpt2-persian with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "bolbolzaban/gpt2-persian" # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:8000/v1/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "bolbolzaban/gpt2-persian", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }'Use Docker
docker model run hf.co/bolbolzaban/gpt2-persian
- SGLang
How to use bolbolzaban/gpt2-persian with SGLang:
Install from pip and serve model
# Install SGLang from pip: pip install sglang # Start the SGLang server: python3 -m sglang.launch_server \ --model-path "bolbolzaban/gpt2-persian" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "bolbolzaban/gpt2-persian", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }'Use Docker images
docker run --gpus all \ --shm-size 32g \ -p 30000:30000 \ -v ~/.cache/huggingface:/root/.cache/huggingface \ --env "HF_TOKEN=<secret>" \ --ipc=host \ lmsysorg/sglang:latest \ python3 -m sglang.launch_server \ --model-path "bolbolzaban/gpt2-persian" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "bolbolzaban/gpt2-persian", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }' - Docker Model Runner
How to use bolbolzaban/gpt2-persian with Docker Model Runner:
docker model run hf.co/bolbolzaban/gpt2-persian
GPT2-Persian
bolbolzaban/gpt2-persian is gpt2 language model that is trained with hyper parameters similar to standard gpt2-medium with following differences:
- The context size is reduced from 1024 to 256 sub words in order to make the training affordable
- Instead of BPE, google sentence piece tokenizor is used for tokenization.
- The training dataset only include Persian text. All non-persian characters are replaced with especial tokens (e.g [LAT], [URL], [NUM])
Please refer to this blog post for further detail. Also try the model here or on Bolbolzaban.com.
How to use
You can use this model directly with a pipeline for text generation:
from transformers import pipeline, AutoTokenizer, GPT2LMHeadModel
tokenizer = AutoTokenizer.from_pretrained('bolbolzaban/gpt2-persian')
model = GPT2LMHeadModel.from_pretrained('bolbolzaban/gpt2-persian')
generator = pipeline('text-generation', model, tokenizer=tokenizer, config={'max_length':256})
sample = generator('در یک اتفاق شگفت انگیز، پژوهشگران')
If you are using Tensorflow import TFGPT2LMHeadModel instead of GPT2LMHeadModel.
Fine-tuning
Find a basic fine-tuning example on this Github Repo.
Special Tokens
gpt-persian is trained for the purpose of research on Persian poetry. Because of that all english words and numbers are replaced with special tokens and only standard Persian alphabet is used as part of input text. Here is one example:
Original text: اگر آیفون یا آیپد شما دارای سیستم عامل iOS 14.3 یا iPadOS 14.3 یا نسخههای جدیدتر باشد
Text used in training: اگر آیفون یا آیپد شما دارای سیستم عامل [LAT] [NUM] یا [LAT] [NUM] یا نسخههای جدیدتر باشد
Please consider normalizing your input text using Hazm or similar libraries and ensure only Persian characters are provided as input.
If you want to use classical Persian poetry as input use [BOM] (begining of mesra) at the beginning of each verse (مصرع) followed by [EOS] (end of statement) at the end of each couplet (بیت).
See following links for example:
[BOM] توانا بود هر که دانا بود [BOM]
[BOM] توانا بود هر که دانا بود [BOM] ز دانش دل پیر
[BOM] توانا بود هر که دانا بود [BOM] ز دانش دل پیربرنا بود [EOS]
If you like to know about structure of classical Persian poetry refer to these blog posts.
Acknowledgment
This project is supported by Cloud TPUs from Google’s TensorFlow Research Cloud (TFRC).
Citation and Reference
Please reference "bolbolzaban.com" website if you are using gpt2-persian in your research or commertial application.
Contacts
Please reachout on Linkedin or Telegram if you have any question or need any help to use the model.
Follow Bolbolzaban on Twitter, Telegram or Instagram
- Downloads last month
- 2,361