CoolFace
Modelpublic

nithin521/Meme_Caption_Generator

sourceHugging Faceupdated 23d agoView on Hugging Face
0likes858downloads
Model Card

๐Ÿ˜‚ Meme Caption Generator

A fine-tuned GPT-2 language model designed to generate short, humorous, and social-media-style meme captions from user-provided prompts.

The model learns common meme caption patterns such as:

  • โ€”When ...
  • โ€”Me when ...
  • โ€”POV: ...
  • โ€”How it feels when ...
  • โ€”That one friend who ...

It can generate multiple caption variations for a single prompt.


๐Ÿš€ Model Overview

PropertyDetails
Base ModelGPT-2
TaskMeme Caption Generation
LanguageEnglish
FrameworkPyTorch
Library๐Ÿค— Transformers
Model TypeAutoregressive Language Model
OutputShort Meme Captions
Fine-TuningSupervised Fine-Tuning
FormatSafetensors

โœจ Features

  • โ€”Generate meme captions from a text prompt
  • โ€”Generate multiple caption variations
  • โ€”Supports prompt-based and random caption generation
  • โ€”Adjustable text-generation parameters
  • โ€”Designed for short, humorous, social-media-style text

Example:

Prompt

text
When you finally get your salary

Possible generations

text
When you finally get your salary and your bills were waiting for you.

Me checking my bank account after getting paid.

POV: You finally get paid but somehow you're still broke.

๐Ÿง  Model Architecture

This model is based on GPT-2, an autoregressive Transformer language model.

During fine-tuning, the model was trained to learn the distribution and writing style of meme captions.

Each training example follows a structure similar to:

text
<|capbos|> meme caption <|capeos|>

where:

  • โ€”<|capbos|> marks the beginning of a caption
  • โ€”<|capeos|> marks the end of a caption
  • โ€”<|cappad|> is used as the padding token

This allows the model to learn where meme captions begin and end.


๐Ÿ“Š Training

The model was fine-tuned on a custom meme-caption dataset containing short English meme captions collected and processed for this project.

The dataset was cleaned and prepared before fine-tuning to improve training quality.

The preprocessing pipeline included:

  1. 1.Caption extraction
  2. 2.Text cleaning
  3. 3.Duplicate removal
  4. 4.Dataset preparation
  5. 5.Tokenization
  6. 6.Train/validation split
  7. 7.GPT-2 fine-tuning

The training process used:

  • โ€”PyTorch
  • โ€”Hugging Face Transformers
  • โ€”AdamW optimization
  • โ€”Learning-rate scheduling
  • โ€”Gradient accumulation
  • โ€”Validation monitoring
  • โ€”Early stopping

โš™๏ธ Generation Parameters

The model can be used with sampling-based generation.

Recommended starting configuration:

python
max_new_tokens = 25
temperature = 0.75
top_k = 40
top_p = 0.90
repetition_penalty = 1.1
no_repeat_ngram_size = 3
do_sample = True

For more creative outputs, temperature and sampling parameters can be increased.

For more predictable outputs, temperature can be decreased.


๐Ÿ’ป Usage

Using Transformers

python
from transformers import AutoTokenizer, AutoModelForCausalLM

model_id = "nithin521/Meme_Caption_Generator"

tokenizer = AutoTokenizer.from_pretrained(model_id)
model = AutoModelForCausalLM.from_pretrained(model_id)

model.eval()

# Get special tokens directly from tokenizer
BOS_TOKEN = tokenizer.bos_token
EOS_TOKEN = tokenizer.eos_token

tokenizer.pad_token = tokenizer.eos_token

prompt = "When you finally get your salary"

# Use tokenizer's BOS token
text = BOS_TOKEN + " " + prompt

inputs = tokenizer(
    text,
    return_tensors="pt"
)

outputs = model.generate(
    **inputs,
    max_new_tokens=25,
    temperature=0.75,
    top_k=40,
    top_p=0.90,
    do_sample=True,
    repetition_penalty=1.1,
    no_repeat_ngram_size=3,
    num_return_sequences=5,
    eos_token_id=tokenizer.eos_token_id,
    pad_token_id=tokenizer.pad_token_id
)

for output in outputs:

    # Remove the input prompt
    generated_tokens = output[
        inputs["input_ids"].shape[1]:
    ]

    caption = tokenizer.decode(
        generated_tokens,
        skip_special_tokens=False
    )

    # Stop at tokenizer's EOS token
    if EOS_TOKEN:
        caption = caption.split(EOS_TOKEN)[0]

    # Remove tokenizer special tokens
    caption = caption.replace(BOS_TOKEN or "", "")
    caption = caption.replace(tokenizer.pad_token or "", "")

    caption = caption.strip()

    if caption:
        print(caption)

๐ŸŽ›๏ธ Generation Controls

The model works particularly well when generation parameters are adjusted according to the desired output.

ParameterEffect
temperatureControls randomness
top_kLimits token selection to the top K candidates
top_pControls nucleus sampling
max_new_tokensControls maximum caption length
repetition_penaltyReduces repetitive text
no_repeat_ngram_sizePrevents repeated phrases

Recommended values:

text
Temperature:       0.75
Top-K:             40
Top-P:             0.90
Max New Tokens:    25

๐ŸŒ Interactive Demo

The model is deployed as an interactive Gradio application on Hugging Face Spaces.

Demo:

https://huggingface.co/spaces/nithin521/Meme-caption-generation

The application allows users to:

  • โ€”Enter a meme prompt
  • โ€”Generate multiple captions
  • โ€”Adjust generation parameters
  • โ€”Experiment with different levels of randomness

๐ŸŽฏ Intended Use

This model is intended for:

  • โ€”Meme caption generation
  • โ€”Social-media content experimentation
  • โ€”NLP demonstrations
  • โ€”Generative AI projects
  • โ€”Educational purposes
  • โ€”Exploring fine-tuned language models

โš ๏ธ Limitations

The model may occasionally generate:

  • โ€”Grammatically incorrect sentences
  • โ€”Repetitive captions
  • โ€”Incomplete captions
  • โ€”Semantically unrelated text
  • โ€”Offensive or inappropriate language
  • โ€”Memorized patterns from the training dataset
  • โ€”Captions that require human editing

Generated captions should therefore be reviewed before being used publicly.

The model is designed for creative generation rather than factual or reliable text generation.


๐Ÿ”’ Safety & Responsible Use

The model generates text based on patterns learned from its training data and does not have an independent understanding of the generated content.

Users should review generated content before publishing it.

The model should not be used to generate targeted harassment, hateful content, misinformation, or other harmful material.


๐Ÿ“š Technologies Used

  • โ€”Python
  • โ€”PyTorch
  • โ€”Hugging Face Transformers
  • โ€”Hugging Face Hub
  • โ€”GPT-2
  • โ€”Gradio
  • โ€”Safetensors

๐Ÿ—๏ธ Project Pipeline

The complete project includes a data-processing and caption-generation pipeline:

text
Meme Images
     โ†“
Text Detection
     โ†“
YOLO-based Caption Region Detection
     โ†“
Caption Cropping
     โ†“
OCR / Text Extraction
     โ†“
Dataset Cleaning
     โ†“
Duplicate Removal
     โ†“
GPT-2 Fine-tuning
     โ†“
Meme Caption Generation
     โ†“
Gradio Web Application

The YOLO-based text detector was developed to identify meme text regions and improve the quality of captions extracted from meme images during dataset preparation.


๐Ÿ“ˆ Future Improvements

Potential improvements include:

  • โ€”Increasing the size and diversity of the training dataset
  • โ€”Improving dataset quality and caption filtering
  • โ€”Adding automated generation-quality ranking
  • โ€”Removing noisy or incomplete generations
  • โ€”Fine-tuning larger language models
  • โ€”Improving semantic control over generated captions
  • โ€”Adding multilingual meme caption generation
  • โ€”Improving safety filtering
  • โ€”Adding image-to-caption generation

๐Ÿ‘จโ€๐Ÿ’ป Project

This model was developed as part of an end-to-end AI Meme Caption Generator project combining computer vision, natural language processing, and generative AI.

Author

Nithin Kumar

Hugging Face: https://huggingface.co/nithin521