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/create_dataset.py from flax-community/gpt2-medium-persian: direct link, hf CLI and curl.
- Browser
- Download file 4.34 kB
-
https://huggingface.co/flax-community/gpt2-medium-persian/resolve/main/src/create_dataset.py
- Command line
-
hf download hf://flax-community/gpt2-medium-persian/src/create_dataset.py
-
curl -L -o create_dataset.py https://huggingface.co/flax-community/gpt2-medium-persian/resolve/main/src/create_dataset.py
4.34 kB
| import ast | |
| import logging | |
| import os | |
| import sys | |
| from dataclasses import dataclass, field | |
| import pandas as pd | |
| from sklearn.model_selection import train_test_split | |
| from tqdm import tqdm | |
| from typing import Dict, List, Optional, Tuple | |
| from datasets import load_dataset | |
| from transformers import ( | |
| HfArgumentParser, | |
| ) | |
| from data_utils import ( | |
| filter_by_lang_regex, | |
| filter_by_num_tokens, | |
| filter_by_num_sents, | |
| filter_by_adv, | |
| normalizer | |
| ) | |
| logger = logging.getLogger(__name__) | |
| class DataArguments: | |
| """ | |
| Arguments to which dataset we are going to set up. | |
| """ | |
| output_dir: str = field( | |
| default=".", | |
| metadata={"help": "The output directory where the config will be written."}, | |
| ) | |
| dataset_name: str = field( | |
| default=None, | |
| metadata={"help": "The name of the dataset to use (via the datasets library)."} | |
| ) | |
| dataset_config_name: Optional[str] = field( | |
| default=None, metadata={"help": "The configuration name of the dataset to use (via the datasets library)."} | |
| ) | |
| train_file: Optional[str] = field(default=None, metadata={"help": "The input training data file (a text file)."}) | |
| cache_dir: Optional[str] = field( | |
| default=None, | |
| metadata={"help": "Where do you want to store the pretrained models downloaded from huggingface.co"}, | |
| ) | |
| def main(): | |
| parser = HfArgumentParser([DataArguments]) | |
| if len(sys.argv) == 2 and sys.argv[1].endswith(".json"): | |
| # If we pass only one argument to the script and it's the path to a json file, | |
| # let's parse it to get our arguments. | |
| data_args = parser.parse_json_file(json_file=os.path.abspath(sys.argv[1]))[0] | |
| else: | |
| data_args = parser.parse_args_into_dataclasses()[0] | |
| # Setup logging | |
| logging.basicConfig( | |
| format="%(asctime)s - %(levelname)s - %(name)s - %(message)s", | |
| datefmt="%m/%d/%Y %H:%M:%S", | |
| handlers=[logging.StreamHandler(sys.stdout)], | |
| ) | |
| logger.setLevel(logging.INFO) | |
| logger.info(f"Preparing the dataset") | |
| if data_args.dataset_name is not None: | |
| dataset = load_dataset( | |
| data_args.dataset_name, | |
| data_args.dataset_config_name, | |
| cache_dir=data_args.cache_dir, | |
| split="train" | |
| ) | |
| else: | |
| data_files = {"train": data_args.train_file} | |
| extension = data_args.train_file.split(".")[-1] | |
| if extension == "txt": | |
| extension = "text" | |
| dataset = load_dataset( | |
| extension, | |
| data_files=data_files, | |
| delimiter="\t", | |
| cache_dir=data_args.cache_dir, | |
| ) | |
| logger.info(f"dataset: {dataset}") | |
| def data_preparation(item_dict): | |
| if "text" not in item_dict: | |
| return None | |
| text = item_dict["text"] | |
| status = filter_by_lang_regex(text, ratio=0.75) | |
| if not status: | |
| return None | |
| status = filter_by_num_tokens(text, gt=64) | |
| if not status: | |
| return None | |
| status = filter_by_num_sents(text, gt=2) | |
| if not status: | |
| return None | |
| status = filter_by_adv(text, ratio=50) | |
| if not status: | |
| return None | |
| text = normalizer(text) | |
| return {"text": text} | |
| data_dict = [] | |
| for item in tqdm(dataset, position=0, total=len(dataset)): | |
| item = data_preparation(item) | |
| if item: | |
| data_dict.append(item) | |
| data_df = pd.DataFrame(data_dict) | |
| logger.info(f"Preparation - [before] consists of {len(dataset)} records!") | |
| logger.info(f"Preparation - [after] consists of {len(data_df)} records!") | |
| train, test = train_test_split(data_df, test_size=0.01, random_state=101) | |
| train = train.reset_index(drop=True) | |
| test = test.reset_index(drop=True) | |
| logger.info(f"Preparation of [train] set consists of {len(train)} records!") | |
| logger.info(f"Preparation of [test] set consists of {len(test)} records!") | |
| os.makedirs(data_args.output_dir, exist_ok=True) | |
| train.to_csv(os.path.join(data_args.output_dir, "train.csv"), sep="\t", encoding="utf-8", index=False) | |
| test.to_csv(os.path.join(data_args.output_dir, "test.csv"), sep="\t", encoding="utf-8", index=False) | |
| logger.info(f"Data saved here {data_args.output_dir}") | |
| if __name__ == '__main__': | |
| main() |