TokenLab / README.md
YanaGabelev's picture
Update README.md
8e6e79c verified
|
Raw History Blame Contribute Delete
3.73 kB

A newer version of the Gradio SDK is available: 6.32.0

Upgrade
metadata
title: TokenLab
emoji: 🐠
colorFrom: purple
colorTo: blue
sdk: gradio
sdk_version: 5.34.2
app_file: app.py
pinned: false
license: mit

🧠 Interactive Text Tokenization App

An interactive Gradio app for exploring how different language models tokenize and process natural language inputs. The app demonstrates Tokenization, Stemming, and Lemmatization using modern NLP tools.


πŸš€ Features

  • Input a sentence and choose a tokenizer (gpt2 or bert-base-uncased)

  • Display:

    • Tokens from the selected model
    • Decoded tokens (token IDs and readable tokens)
    • Stemmed words using PorterStemmer
    • Lemmatized words using spaCy (if available) or fallback to WordNetLemmatizer
  • Comparison table:

    Original Stemmed Lemmatized
    running runn run
    better better good
  • Comparison of Tokenizers:

    • BERT (WordPiece)
    • GPT2 (BPE)
    • tiktoken (OpenAI's cl100k_base) β€” if installed
  • Markdown explanations and linguistic insights


🧰 Technologies Used

  • Gradio – for the user interface
  • Transformers (Hugging Face) – AutoTokenizer for GPT2 and BERT
  • nltk – for stemming and fallback lemmatization
  • spaCy – primary lemmatizer (if available)
  • tiktoken – OpenAI tokenizer, used if installed

πŸ” How to Use

  1. Enter a sentence (e.g., I was running faster than him)
  2. Choose a tokenizer (e.g., gpt2 or bert)
  3. Click Analyze
  4. View:
    • Token comparisons (tokens, decoded, token IDs)
    • Stemmed & Lemmatized results
    • Table comparison
    • Linguistic explanation

πŸ“Š What is Tokenization?

Tokenization breaks text into smaller units called tokens β€” words, subwords, or characters. Different models tokenize differently:

  • GPT2 uses byte-pair encoding (BPE)
  • BERT uses WordPiece encoding
  • tiktoken simulates tokenization in OpenAI models like ChatGPT

🌿 What is Stemming?

Stemming chops off suffixes to reduce a word to its base form.

  • Example: running, runner, runs β†’ run
  • Can produce non-words: running β†’ runn
  • Tool used: nltk.PorterStemmer

πŸƒ What is Lemmatization?

Lemmatization maps a word to its dictionary base form using linguistic rules.

  • Example: running β†’ run, better β†’ good
  • More accurate than stemming
  • Tools used:
    • spaCy (if available)
    • Fallback: nltk.WordNetLemmatizer

πŸ“Œ When to Use Each?

Technique Use Case Pros Cons
Tokenization Model preprocessing Fast, essential Model-dependent results
Stemming Lightweight, fast normalization Simple, quick Less accurate
Lemmatization Semantic-level analysis Linguistic, precise Slower, depends on tools

πŸ§ͺ Example Insight

Do all models treat running the same?

No:

  • BERT might split it into ["run", "##ning"]
  • GPT2 may tokenize it differently (based on byte-pairs)
  • spaCy lemmatizes running β†’ run, WordNet does the same (but context-dependent)
  • Stemming gives: runn

🧠 Built as part of a Natural Language Processing assignment


πŸ“‚ Repository Files

  • app.py: Main Gradio application
  • README.md: This file
  • requirements.txt: Project dependencies

πŸ“¦ Installation (for local use)

pip install -r requirements.txt
python app.py