Instructions to use Maryam033/gpt2-text-generation with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use Maryam033/gpt2-text-generation with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("text-generation", model="Maryam033/gpt2-text-generation")# pip install -U transformers accelerate # Load model directly from transformers import AutoTokenizer, AutoModelForCausalLM tokenizer = AutoTokenizer.from_pretrained("Maryam033/gpt2-text-generation") model = AutoModelForCausalLM.from_pretrained("Maryam033/gpt2-text-generation", device_map="auto") - Notebooks
- Google Colab
- Kaggle
- Local Apps Settings
- vLLM
How to use Maryam033/gpt2-text-generation with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "Maryam033/gpt2-text-generation" # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:8000/v1/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "Maryam033/gpt2-text-generation", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }'Use Docker
docker model run hf.co/Maryam033/gpt2-text-generation
- SGLang
How to use Maryam033/gpt2-text-generation 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 "Maryam033/gpt2-text-generation" \ --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": "Maryam033/gpt2-text-generation", "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 "Maryam033/gpt2-text-generation" \ --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": "Maryam033/gpt2-text-generation", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }' - Docker Model Runner
How to use Maryam033/gpt2-text-generation with Docker Model Runner:
docker model run hf.co/Maryam033/gpt2-text-generation
Download PartB_README.md from Maryam033/gpt2-text-generation: direct link, hf CLI and curl.
- Browser
- Download file 4.86 kB
-
https://huggingface.co/Maryam033/gpt2-text-generation/resolve/main/PartB_README.md
- Command line
-
hf download hf://Maryam033/gpt2-text-generation/PartB_README.md
-
curl -L -o PartB_README.md https://huggingface.co/Maryam033/gpt2-text-generation/resolve/main/PartB_README.md
Part B: GPT-2 Decoder-Only Transformer - Improved News Text Generation
This project uses a pre-trained GPT-2 model (openai-community/gpt2), a decoder-only Transformer, to continue the beginning of an English sentence. It improves on a basic WikiText-2 version in three ways: a cleaner dataset, better decoding settings, and optional fine-tuning with a measured result.
GPT-2 predicts the next token from the previous tokens, again and again (autoregressive generation), so it can write a continuation of any prompt.
What is improved
| Problem in the basic version | Improvement here |
|---|---|
WikiText-2 raw text has tokens such as @-@ and spaces before punctuation, so prompts look broken |
AG News: clean English news, further cleaned (HTML codes, agency prefixes such as "Reuters - ") |
| Plain sampling often repeats phrases | Better decoding: repetition penalty and no repeated 3-grams |
| No way to measure quality | Perplexity before and after fine-tuning, and a repetition score (distinct-3) |
| Output is generic | Optional fine-tuning on 12,000 news articles |
Dataset
AG News (categories: World, Sports, Business, Sci/Tech). Original corpus: http://www.di.unipi.it/~gulli/AG_corpus_of_news_articles.html
| File | Rows | Use |
|---|---|---|
data/ag_news_generation_prompts.csv |
6,865 | Test articles. The first 10 words of each article are the prompt. Also used to measure perplexity. |
data/ag_news_finetune_train.csv |
12,000 | Training articles (3,000 per category) for the optional fine-tuning. Never overlaps with the test articles. |
Cleaning: HTML codes and entities converted, backslashes removed, agency and dateline prefixes removed, extra spaces fixed, articles under 20 words and duplicates removed.
If the CSV files are not uploaded to Colab, the notebook downloads the original AG News files from GitHub and builds both files automatically.
How to run (Google Colab)
- Open colab.research.google.com and choose File -> Upload notebook. Upload
Part_B_GPT2_Improved_NewsGeneration.ipynb. - Choose Runtime -> Change runtime type -> T4 GPU.
- (Optional) Drag the two CSV files from
data/into the Colab Files panel. - Run the cells in order with Shift + Enter.
- Set
FINE_TUNE = Falsein Section 6 if you only want the pre-trained model.
Notebook sections
| Section | What it does |
|---|---|
| 1-2 | Install libraries, set the seed (42), check the GPU |
| 3 | Load or build the AG News CSV files |
| 4 | Load pre-trained GPT-2 |
| 5 | Baseline perplexity, then plain sampling vs improved decoding |
| 6 | Optional fine-tuning (2 epochs, about 5 to 10 minutes on a T4) |
| 7 | Perplexity before and after |
| 8 | Generate text with the final model |
| 9 | Try your own sentence |
| 10 | Save the results as CSV files |
| 11 | Upload the model to Hugging Face |
Settings
Decoding
| Setting | Plain sampling | Improved |
|---|---|---|
| Temperature | 1.0 | 0.8 |
| Top-k | off | 50 |
| Top-p | off | 0.92 |
| Repetition penalty | off | 1.2 |
| No repeated n-gram size | off | 3 |
Fine-tuning: 2 epochs, learning rate 5e-5, batch size 16, max length 128 tokens, weight decay 0.01, seed 42.
Results
Fill these in from your notebook output.
Perplexity on 300 held-out test articles (lower is better, Section 7)
| Model | Perplexity |
|---|---|
| Pre-trained GPT-2 | ... |
| Fine-tuned GPT-2 | ... |
Repetition score over 30 prompts (distinct-3, higher means less repetition, Section 5)
| Decoding | distinct-3 |
|---|---|
| Plain sampling | ... |
| Improved decoding | ... |
Example
| Prompt | Generated text |
|---|---|
| ... | ... |
Project files
.
βββ Part_B_GPT2_Improved_NewsGeneration.ipynb
βββ requirements.txt
βββ README.md
βββ data/
βββ ag_news_generation_prompts.csv
βββ ag_news_finetune_train.csv
Upload to Hugging Face
Run Section 11 of the notebook (it needs a Hugging Face Write token). If you fine-tuned the model, say so in its model card. If you set FINE_TUNE = False, the uploaded model is an unchanged copy of the pre-trained GPT-2.
Limitations
- Generated text can be factually wrong. It is not real news.
- Output changes on every run because of random sampling.
- GPT-2 can repeat itself and can reflect biases from its training data.
- English only. Works best with news-style prompts.
- Fine-tuning adapts the model to news style. It does not make it more truthful.
Citation
Zhang, X., Zhao, J., LeCun, Y. (2015). Character-level Convolutional Networks for Text Classification. NIPS 2015. (AG News)
GPT-2: Radford et al. (2019), OpenAI. Language Models are Unsupervised Multitask Learners.
Author
Maryam Zubair Ahmed