Text Generation
Transformers
PyTorch
TensorFlow
JAX
TensorBoard
Persian
gpt2
text-generation-inference
Run this model with HF Jobs
Run flax-community/gpt2-medium-persian on Hugging Face's managed compute, monitor its progress, and retrieve the results when it's done.
Paste one of these prompts into your coding agent:
Fine-tune on my dataset
Use HF Jobs and TRL to fine-tune https://huggingface.co/flax-community/gpt2-medium-persian. Ask me which dataset to train on, then push the fine-tuned model to a new model repo. Run `hf jobs --help` first to learn the CLI.
Run batch inference
Use HF Jobs to run batch inference with https://huggingface.co/flax-community/gpt2-medium-persian on the inputs in a mounted HF Bucket. Ask me which Bucket to use, then write the outputs back to it and keep each input ID. Run `hf jobs --help` first to learn the CLI.
Evaluate on a benchmark
Use HF Jobs to evaluate https://huggingface.co/flax-community/gpt2-medium-persian. Ask me which benchmark to run, then save the scores and the per-example results to a new dataset repo. Run `hf jobs --help` first to learn the CLI.
Quantize for lower memory use
Use HF Jobs to create a 4-bit version of https://huggingface.co/flax-community/gpt2-medium-persian. Push it to a new model repo, then reload it from there and report its memory footprint. Run `hf jobs --help` first to learn the CLI.
Instructions to use flax-community/gpt2-medium-persian with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use flax-community/gpt2-medium-persian with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("text-generation", model="flax-community/gpt2-medium-persian")# pip install -U transformers accelerate # Load model directly from transformers import AutoTokenizer, AutoModelForCausalLM tokenizer = AutoTokenizer.from_pretrained("flax-community/gpt2-medium-persian") model = AutoModelForCausalLM.from_pretrained("flax-community/gpt2-medium-persian", device_map="auto") - Notebooks
- Google Colab
- Kaggle
- Local Apps Settings
- vLLM
How to use flax-community/gpt2-medium-persian with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "flax-community/gpt2-medium-persian" # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:8000/v1/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "flax-community/gpt2-medium-persian", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }'Use Docker
docker model run hf.co/flax-community/gpt2-medium-persian
- SGLang
How to use flax-community/gpt2-medium-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 "flax-community/gpt2-medium-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": "flax-community/gpt2-medium-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 "flax-community/gpt2-medium-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": "flax-community/gpt2-medium-persian", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }' - Docker Model Runner
How to use flax-community/gpt2-medium-persian with Docker Model Runner:
docker model run hf.co/flax-community/gpt2-medium-persian
Download src/normalizer.py from flax-community/gpt2-medium-persian: direct link, hf CLI and curl.
- Browser
- Download file 4.2 kB
-
https://huggingface.co/flax-community/gpt2-medium-persian/resolve/main/src/normalizer.py
- Command line
-
hf download hf://flax-community/gpt2-medium-persian/src/normalizer.py
-
curl -L -o normalizer.py https://huggingface.co/flax-community/gpt2-medium-persian/resolve/main/src/normalizer.py
4.2 kB
| import hazm | |
| import re | |
| import string | |
| from regexes.currency import CURRENCY_REGEX | |
| from regexes.email import EMAIL_REGEX | |
| from regexes.latin import LATIN_REGEX | |
| from regexes.latin import LATIN_REGEX, LATIN_WITH_SPECIAL_REGEX | |
| from regexes.number import NUMBERS_REGEX | |
| from regexes.phone import PHONE_REGEX | |
| from regexes.quote import DOUBLE_QUOTE_REGEX, SINGLE_QUOTE_REGEX | |
| from regexes.url import URL_REGEX | |
| from regexes.persian import PERSIAN_REGEX | |
| from regexes.punk import PUNK_REGEX | |
| import dictionary | |
| allowed_char = string.ascii_letters + string.digits + ':/@_-. ' | |
| def make_trans(list_a, list_b): | |
| return dict((ord(a), b) for a, b in zip(list_a, list_b)) | |
| def multiple_replace(text, chars_to_mapping): | |
| pattern = "|".join(map(re.escape, chars_to_mapping.keys())) | |
| return re.sub(pattern, lambda m: chars_to_mapping[m.group()], str(text)) | |
| def remove_adv_by_tag_name(text, tag_name): | |
| found = text.find(tag_name) | |
| if found > 0: | |
| text = text[:found] | |
| return text | |
| def clean_url(text): | |
| # removing html tags | |
| text = re.sub('<.*?>', '', text) | |
| # removing normal(without space urls) | |
| text = re.sub(r'(?:(?:http|https):\/\/)?([-a-zA-Z0-9.]{2,256}\.[a-z]{2,4})\b(?:\/[-a-zA-Z0-9@:%_\+.~#?&//=]*)?', "", | |
| text) | |
| # removing urls that contains space | |
| result = '' | |
| for char in text: | |
| if char in allowed_char: | |
| result += char | |
| result = result.replace(' ', '') | |
| result = result.split(':') | |
| for phrase in result: | |
| p = phrase | |
| if '//' in p: | |
| if ('https :' + p) in text: | |
| text = text.replace('https :' + p, '') | |
| elif ('http :' + p) in text: | |
| text = text.replace('http :' + p, '') | |
| elif '@' in p: | |
| if p in text: | |
| text = text.replace(p, '') | |
| return text | |
| ar2fa_digits = make_trans("٠١٢٣٤٥٦٧٨٩٪", "۰۱۲۳۴۵۶۷۸۹٪") | |
| fa2en_digits = make_trans("۰۱۲۳۴۵۶۷۸۹٪", "0123456789%") | |
| normalizer = hazm.Normalizer(persian_numbers=True, punctuation_spacing=False) | |
| def normalize(text, zwnj="\u200c", tokenized=False): | |
| text = text.replace("\n", " ").replace("\t", " ") | |
| text = re.sub(r"\u200c+", "\u200c", text) | |
| text = text.replace('ـ', '') | |
| text = normalizer.normalize(text) | |
| if len(dictionary.characters) > 0: | |
| text = multiple_replace(text, dictionary.characters) | |
| if len(dictionary.words_map) > 0: | |
| text = multiple_replace(text, dictionary.words_map) | |
| text = text.translate(ar2fa_digits) | |
| text = text.translate(fa2en_digits) | |
| text = SINGLE_QUOTE_REGEX.sub("'", text) | |
| text = DOUBLE_QUOTE_REGEX.sub('"', text) | |
| text = CURRENCY_REGEX.sub(r" \1 ", text) | |
| text = clean_url(text) | |
| text = remove_adv_by_tag_name(text, tag_name="برچسب ها :") | |
| text = URL_REGEX.sub(" ", text) | |
| text = EMAIL_REGEX.sub(" ", text) | |
| text = PHONE_REGEX.sub(r" \1 ", text) | |
| text = NUMBERS_REGEX.sub(r" \1 ", text) | |
| text = LATIN_REGEX.sub(r" \1 ", text) | |
| # text = PUNK_REGEX.sub(r" \1 ", text) # must be remained the same! | |
| # Allow only english and persian characters | |
| text = re.sub(PERSIAN_REGEX, " ", text) | |
| text = text.replace(f" {zwnj} ", f"{zwnj}") | |
| text = text.replace(f"{zwnj} ", f"{zwnj}") | |
| text = text.replace(f" {zwnj}", f"{zwnj}") | |
| if len(dictionary.special_tokens) > 0: | |
| text = multiple_replace(text, dictionary.special_tokens) | |
| tokens = [] | |
| for token in text.split(): | |
| token = token.strip() | |
| if token: | |
| if token.startswith(zwnj) and token.endswith(zwnj): | |
| token = token[1:-1] | |
| if token.startswith(zwnj): | |
| token = token[1:] | |
| elif token.endswith(zwnj): | |
| token = token[:-1] | |
| else: | |
| token = token | |
| tokens.append(token) | |
| if tokenized: | |
| return tokens | |
| return " ".join(tokens) | |
| if __name__ == '__main__': | |
| import textwrap | |
| # input_text = " «هفتاد سی» " | |
| # input_text = normalize(input_text) | |
| # input_text = DOUBLE_QUOTE_REGEX.sub('"', input_text) | |
| # print(textwrap.fill(input_text)) | |
| # print(normalize(input_text, tokenized=True)) | |