File size: 8,873 Bytes
853e005 a447407 853e005 a447407 853e005 a447407 853e005 a447407 853e005 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 | ---
license: mit
library_name: lightgbm
language:
- multilingual
- en
tags:
- tabular-classification
- social-media
- virality
- x
- twitter
- lightgbm
- calibration
pipeline_tag: tabular-classification
---
<p align="left"><a href="https://www.brainfish.ai"><img src="https://huggingface.co/brainfish/x-virality-model/resolve/main/brainfish-logo.svg" alt="Brainfish" height="28"></a></p>
# X Virality Model (v2.1, with v2.2 opt-in)
*by [Brainfish](https://www.brainfish.ai)*
Predicts, **before a post is published**, how an X (Twitter) draft is likely to perform:
- **P(viral)**: calibrated probability that the post is unusually strong *for its own author* (definition below).
- **Predicted impressions**: a median estimate and a split-conformal **80% interval**.
Inputs are pre-posting only: the text, follower count, media/reply/thread/quote flags and posting time (plus optional profile numbers). No engagement data is accepted at scoring time.
**Demo:** [brainfish/x-virality-demo](https://huggingface.co/spaces/brainfish/x-virality-demo) · Built by [@ajainvivek](https://x.com/ajainvivek) at [Brainfish](https://www.brainfish.ai)
| File | What it is |
|---|---|
| `model_v2b/virality_v2b.joblib` | **v2.1 bundle (default)**: LightGBM `lgbm_reg_yrel` scorer, Platt calibrator, text-embedding PCA, thresholds, LightGBM impressions regressor + conformal width. Trained 2026-11-29 on 5,348 posts. |
| `model_v2c/virality_v2c.joblib` | **v2.2 bundle (opt-in)**: same pipeline and features retrained 2026-12-01 on 13,337 posts. |
| `model_v2b/metrics.json`, `model_v2c/metrics.json` | Full evaluation outputs (aggregate numbers only). |
| `*/reliability_by_bucket.png` | Reliability diagrams by follower bucket. |
| `predict_v2.py`, `features.py`, `features_v2.py`, `calibration.py`, `events_join.py`, `trends_join.py`, `embed.py` | Inference code (unchanged from the training repo). |
| `nrc_emotion_lexicon.json` | NRC Word-Emotion Association Lexicon (Mohammad & Turney), used for emotion-rate features. See *Third-party data*. |
| `events/events.jsonl` | Public events calendar (Wikipedia "Current events" portal items + public holidays, 2024-11-01 to 2026-12-28). |
| `reference_percentiles.json` | 101 quantiles of each model's predicted P(viral) on the held-out test set (used for the demo's percentile). |
## Viral definition
`y = log(1 + likes + reposts + replies + quotes)`. The author's baseline is the mean `y` of their **earlier** posts only (no look-ahead), shrunk toward the follower-bucket mean. A post is **viral** if `y − baseline` is in the **top 5%** among posts from the same follower bucket (`<1k`, `1k–10k`, `10k–100k`, `100k–1M`, `1M+`) on the **same posting day** (sparse historical days fall back to week/month cohorts). In plain words: *unusually strong for this account, compared with similar-size accounts that day*. About 5–6% of posts are viral in every bucket, so a 200-follower account and a 10M-follower account are judged on the same footing.
## Features
- **Text (hand-crafted):** length, words, lines, caps ratio, `!`/`?` counts, question/hook openings, calls to action, list format, numbers, emojis, hashtags, mentions, links, starts-with-@.
- **Sentiment and emotion:** VADER (compound/pos/neg/neu/abs) and NRC emotion rates (anger, anticipation, disgust, fear, joy, sadness, surprise, trust, total).
- **Text embedding:** [`sentence-transformers/paraphrase-multilingual-MiniLM-L12-v2`](https://huggingface.co/sentence-transformers/paraphrase-multilingual-MiniLM-L12-v2) (384-d, ONNX via `fastembed`, [`Qdrant/paraphrase-multilingual-MiniLM-L12-v2-onnx-Q`](https://huggingface.co/Qdrant/paraphrase-multilingual-MiniLM-L12-v2-onnx-Q)), reduced to 32 PCA components.
- **Post:** photo/video/media, reply to others, thread (self-reply), quote, posting hour (UTC, sin/cos), weekend.
- **Author (lite profile):** log followers, verified/type, log following, log listed, follow ratio, lists per 1k followers.
- **Events calendar:** news-day intensity, keyword match against events on day ±1 (specificity, category), public holidays (US/GB/IN/AU).
- **Impressions regressor only:** additionally image-embedding PCA and live X-trend match features (neutral when no trend snapshot is available).
## Evaluation (held-out test set)
Both models scored on **one shared held-out set neither trained on**: 2,428 posts, 118 viral (base rate 4.86%) = posts after 2026-11-27 16:10 UTC from seen authors (1,229) + all posts of 700 held-out authors (1,199). Source: `model_v2c/metrics.json` → `test.test_all`.
| Metric | **v2.1 (default)** | v2.2 |
|---|---|---|
| ROC-AUC | **0.773** | 0.765 |
| PR-AUC | **0.162** | 0.148 |
| Pairwise within-author accuracy | 0.630 | **0.662** |
| Spearman (score vs `y − baseline`) | 0.510 | **0.528** |
| Brier | **0.0438** | 0.0441 |
| ECE | 0.0142 | **0.0136** |
| Precision @ top 5% | **0.207** | 0.198 |
| Precision @ top 1% | 0.208 | **0.292** |
| F1 threshold (precision / recall on test) | 0.0924 (0.145 / 0.390) | 0.0801 (0.120 / 0.492) |
| Conformal 80%-recall screen (precision / recall) | 0.031 (0.086 / 0.881) | 0.0377 (0.087 / 0.856) |
| *Natural posts only (no reposted originals, n=2,314):* ROC-AUC / PR-AUC | 0.751 / 0.093 | 0.744 / 0.083 |
90% author-cluster bootstrap CI of v2.2 − v2.1: AUC [−0.026, +0.007], pairwise [+0.008, +0.056], Spearman [+0.003, +0.034]. So v2.2 is reliably better at **ranking one author's drafts against each other**, while v2.1 is slightly (not significantly) better at **viral detection**; v2.1 stays the default.
**Impressions regressor** (test, n=2,402, log10 scale): v2.1 Spearman 0.733, MAE 0.576, 80% interval coverage 76.3%; v2.2 Spearman 0.725, MAE 0.583, coverage 79.6%.
**By follower bucket (ROC-AUC, v2.1 / v2.2):** <1k 0.798 / 0.809 · 1k–10k 0.797 / 0.809 · 10k–100k 0.857 / 0.803 · 100k–1M 0.778 / 0.783 · **1M+ 0.490 / 0.596** (n=231, 12 viral).
The v2.1 bundle's own training-time evaluation (frozen 1,296-post test set, 60 viral) is in `model_v2b/metrics.json`: ROC-AUC 0.808, PR-AUC 0.175, pairwise 0.658, ECE 0.014.
## Usage
```bash
pip install -r requirements.txt
python predict_v2.py "We just open sourced a 5.4 KB bot-detection model" --followers 1200 --video --posted-at 2026-12-02T22:00:00Z
```
```python
import predict_v2
r = predict_v2.predict("draft text", version="v2.1", followers=1200, has_video=True, posted_at="2026-12-02T22:00:00Z")
r["p_viral"], r["impressions_pred"], r["impressions_80"]
```
The first call downloads the ONNX text-embedding model (~235 MB) into `emb_cache/models/` (or `$XVM_EMB_CACHE`). Bundles are `joblib` pickles: load them only from this repo, with the pinned `scikit-learn==1.9.1` / `lightgbm==4.7.0`.
## Training data
About 17.8k public X posts collected via the X API (keyword search, author timelines, reposted originals) plus a full-archive historical backfill (Dec 2024 – Sep 2026). v2.1 trained on 5,348 posts (incl. 2,242 backfill); v2.2 on 13,337 (9,710 of the 11,558 train rows are backfill). **No raw post data, text, labels, author ids or other personal data are published here**; only model artifacts, aggregate metrics and inference code.
## Limitations
- **Mostly backfill training data:** about three-quarters of v2.2's training posts are 2024–26 backfill while the test posts are live November 2026 posts; that shift is the likely reason tripling the data barely helped.
- **Noisy for >1M-follower accounts:** ROC-AUC 0.49 (v2.1) / 0.60 (v2.2) in that bucket, and probabilities are over-predicted there (v2.2 mean predicted 8.4% vs 5.2% observed).
- **No live early-velocity scoring:** this is a pre-posting estimate only; "about to blow up" scoring from early engagement is not included.
- Calibrated to this collected sample, not to an unfiltered X feed. It cannot see idea quality, who will repost, or X's ranking system.
- Trend features need hourly X trend snapshots (`trends/trends.jsonl`, not distributed); without them they default to "no snapshot", which only affects the impressions estimate. Event features are neutral outside the calendar range (after 2026-12-28).
- Image embeddings are not used by the virality scorer.
## License
Code and model weights: **MIT** (see `LICENSE`). The Brainfish name and logo (`brainfish-logo.svg`) are Brainfish trademarks and are not covered by the MIT licence.
### Third-party data
- `nrc_emotion_lexicon.json`: NRC Word-Emotion Association Lexicon, © Saif M. Mohammad and Peter Turney, National Research Council Canada. Free for research and non-commercial use; commercial use requires a licence from NRC. It is **not** covered by the MIT licence.
- Text embedding model: Apache-2.0 (downloaded from its own repo, not redistributed here).
- `events/events.jsonl`: derived from Wikipedia's Current events portal (CC BY-SA 4.0) and public-holiday data (`python-holidays`, Nager.Date).
|