Spaces:
Sleeping
Sleeping
Upload README.md
Browse files
README.md
ADDED
|
@@ -0,0 +1,105 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
title: Text Vectorization Lab
|
| 3 |
+
emoji: ๐งฎ
|
| 4 |
+
colorFrom: yellow
|
| 5 |
+
colorTo: indigo
|
| 6 |
+
sdk: docker
|
| 7 |
+
app_port: 7860
|
| 8 |
+
pinned: false
|
| 9 |
+
---
|
| 10 |
+
|
| 11 |
+
# Text Vectorization Lab
|
| 12 |
+
|
| 13 |
+
A step-by-step, interactive simulation of every technique covered in the
|
| 14 |
+
*Text Vectorization in NLP* deck and notebook:
|
| 15 |
+
|
| 16 |
+
- One-Hot Encoding
|
| 17 |
+
- Count Vectorizer
|
| 18 |
+
- Bag-of-Words
|
| 19 |
+
- N-grams
|
| 20 |
+
- TF-IDF Vectorizer
|
| 21 |
+
- Word Embeddings (Word2Vec โ Skip-gram & CBOW โ and FastText)
|
| 22 |
+
|
| 23 |
+
It's a single small web app: a **Flask backend** (`app.py`) that actually
|
| 24 |
+
runs the `scikit-learn` / `numpy` / `gensim` code from the reference
|
| 25 |
+
notebook on whatever text you type in, and a **vanilla HTML/CSS/JS
|
| 26 |
+
frontend** that calls that backend and reveals the result stage by stage โ
|
| 27 |
+
tokenize โ vocabulary โ vectors โ along an animated pipeline tape. There is
|
| 28 |
+
no precomputed/fake data: every matrix, score, and embedding on the page is
|
| 29 |
+
computed live by the Python backend for the exact text you entered.
|
| 30 |
+
|
| 31 |
+
## 1. Setup
|
| 32 |
+
|
| 33 |
+
Requires Python 3.9+.
|
| 34 |
+
|
| 35 |
+
```bash
|
| 36 |
+
cd text-vectorization-lab
|
| 37 |
+
python -m venv venv
|
| 38 |
+
source venv/bin/activate # Windows: venv\Scripts\activate
|
| 39 |
+
pip install -r requirements.txt
|
| 40 |
+
```
|
| 41 |
+
|
| 42 |
+
## 2. Run
|
| 43 |
+
|
| 44 |
+
```bash
|
| 45 |
+
python app.py
|
| 46 |
+
```
|
| 47 |
+
|
| 48 |
+
Then open **http://localhost:5000** in your browser.
|
| 49 |
+
|
| 50 |
+
The dev server runs with `debug=True`, so editing `app.py` or the
|
| 51 |
+
templates/static files will auto-reload.
|
| 52 |
+
|
| 53 |
+
## 3. How it's wired
|
| 54 |
+
|
| 55 |
+
```
|
| 56 |
+
text-vectorization-lab/
|
| 57 |
+
โโโ app.py # Flask app + all vectorization logic (the "backend")
|
| 58 |
+
โโโ requirements.txt
|
| 59 |
+
โโโ templates/
|
| 60 |
+
โ โโโ index.html # Single-page app shell, one <section> per technique
|
| 61 |
+
โโโ static/
|
| 62 |
+
โโโ css/style.css # Design system (dark "lab" theme, pipeline tape, matrices)
|
| 63 |
+
โโโ js/main.js # Fetches /api/* and animates the step-by-step reveal
|
| 64 |
+
```
|
| 65 |
+
|
| 66 |
+
Each technique has its own REST endpoint:
|
| 67 |
+
|
| 68 |
+
| Endpoint | Mirrors notebook section |
|
| 69 |
+
|---|---|
|
| 70 |
+
| `POST /api/onehot` | One-Hot Encoding (manual NumPy + `OneHotEncoder` cross-check) |
|
| 71 |
+
| `POST /api/count-vectorizer` | `CountVectorizer` (vocabulary, matrix, stop words, `max_features`, `.transform()` on new text) |
|
| 72 |
+
| `POST /api/bow` | Hand-rolled Bag-of-Words counter + binary BoW + cosine similarity |
|
| 73 |
+
| `POST /api/ngrams` | Manual n-gram generator + `CountVectorizer(ngram_range=...)` + char-level n-grams |
|
| 74 |
+
| `POST /api/tfidf` | Manual TF/IDF computation (sklearn-style smoothing) + `TfidfVectorizer` + top words per doc |
|
| 75 |
+
| `POST /api/embeddings` | `gensim.models.Word2Vec` (Skip-gram & CBOW), similarity, most-similar, PCA-to-2D, and `FastText` for out-of-vocabulary words |
|
| 76 |
+
|
| 77 |
+
The frontend never computes vectors itself โ it only sends your raw text
|
| 78 |
+
to these endpoints and renders whatever comes back, so the numbers you see
|
| 79 |
+
are always whatever scikit-learn/gensim actually produce.
|
| 80 |
+
|
| 81 |
+
## 4. Using it
|
| 82 |
+
|
| 83 |
+
Every panel has a textarea pre-filled with the same example corpus used in
|
| 84 |
+
the notebook, plus a **Run** button. Edit the text, click run, and watch
|
| 85 |
+
the pipeline tape light up stage by stage as the backend tokenizes,
|
| 86 |
+
builds the vocabulary, and fills in the matrix or vectors. **Reset to
|
| 87 |
+
example** restores the original notebook text for that panel.
|
| 88 |
+
|
| 89 |
+
The Word Embeddings panel trains a real Word2Vec + FastText model on your
|
| 90 |
+
sentences on every run, so it takes a couple of seconds โ that's genuine
|
| 91 |
+
training time, not a fake delay.
|
| 92 |
+
|
| 93 |
+
## 5. Notes & limits
|
| 94 |
+
|
| 95 |
+
- All endpoints lower-case and strip punctuation with a simple regex
|
| 96 |
+
tokenizer (`re.findall(r"[A-Za-z0-9']+", text.lower())`), matching the
|
| 97 |
+
`.split()`-based tokenization used in the notebook closely enough for
|
| 98 |
+
teaching purposes. `CountVectorizer`/`TfidfVectorizer` use scikit-learn's
|
| 99 |
+
own tokenizer internally, as in the notebook.
|
| 100 |
+
- Word2Vec/FastText need a handful of sentences with repeated/shared words
|
| 101 |
+
to produce meaningful similarities โ very short or fully disjoint corpora
|
| 102 |
+
will train but the similarity scores won't mean much.
|
| 103 |
+
- This is a teaching tool, not a production service: there's no auth, rate
|
| 104 |
+
limiting, or persistence, and `debug=True` should be turned off if you
|
| 105 |
+
ever deploy it anywhere other than your own machine.
|