CoolFace
Apppublic

Mavrxai/tribev2

sourceHugging Facemitupdated 5mo agoView on Hugging Face
0likes
App README

META-TRIBE-v2-api

A REST API wrapping Meta's TRIBE v2 brain encoding model. Given any text content, it predicts which regions of the human brain would activate — returning scores for language processing, visual imagery, attention capture, emotional engagement, and a composite viral potential score.

Live Space: https://atharv-1447-meta-tribe-v2-api.hf.space


What TRIBE v2 Does

TRIBE v2 (Transformers for In-silico Brain Encoding v2) is Meta's multimodal foundation model that predicts fMRI brain responses to naturalistic stimuli. It combines:

  • —LLaMA 3.2-3B — text encoding
  • —V-JEPA2 — video encoding
  • —Wav2Vec-BERT — audio encoding

Output: predicted fMRI BOLD signal on the fsaverage5 cortical surface (~20,484 vertices).

This Space exposes a text-based API. Image and video are supported in the Mavrx backend via a preprocessing pipeline (Claude Vision for images, Whisper + keyframe for video) before scoring.


Auth Requirements

Every request requires a HuggingFace Read token passed in the request body or Authorization header. The token must belong to an account that has accepted the license for:

No token is stored in this Space. Users supply their own.


API Reference

GET /health

Returns model status.

json
{ "status": "ok", "model_loaded": true, "model_loading": false, "error": null }

POST /warmup

Triggers model load. Required on first use after cold start (~90s to load).

json
{ "hf_token": "hf_..." }

POST /predict/text

json
{
  "text": "Your content here (5–5000 chars)",
  "hf_token": "hf_..."
}

Response:

json
{
  "status": "ok",
  "modality": "text",
  "scores": {
    "language_processing": 52.0,
    "visual_imagery": 50.8,
    "attention_capture": 59.4,
    "emotional_valence": 52.1,
    "overall_brain_engagement": 50.1,
    "viral_potential": 52.8,
    "activation_timeline": [0.14, 0.16, 0.19, ...],
    "n_timesteps": 27,
    "n_vertices": 20484,
    "dominant_hemisphere": "left",
    "word_count": 57,
    "overall_mean_activation": 0.193791
  }
}

POST /predict/image

Multipart form — image file + token.

bash
curl -X POST https://atharv-1447-meta-tribe-v2-api.hf.space/predict/image \
  -F "file=@image.jpg" \
  -F "hf_token=hf_..."

POST /predict/video

Multipart form — video file + token.

bash
curl -X POST https://atharv-1447-meta-tribe-v2-api.hf.space/predict/video \
  -F "file=@video.mp4" \
  -F "hf_token=hf_..."

POST /predict/base64

Send file as base64 JSON.

json
{
  "data": "<base64 string>",
  "type": "image",
  "ext": ".jpg",
  "hf_token": "hf_..."
}

Auth via header (alternative to body field)

Authorization: Bearer hf_...

Score Reference

FieldBrain RegionWhat it Measures
language_processingBroca's area / Wernicke's areaHow hard the brain decodes the language
visual_imageryVisual cortex (V1–V4)How vividly the text creates mental images
attention_captureParietal / frontal attention networkHow much the content demands focused attention
emotional_valencevmPFC (ventromedial prefrontal cortex)Emotional intensity — reward, fear, sentiment
overall_brain_engagementDefault Mode Network (DMN)Deep reflection, memory, cognitive engagement
viral_potentialCompositeDMN 35% + emotion 30% + attention 20% + language 10% + visual 5%
activation_timelineAll regionsMean activation per word-chunk over time
dominant_hemisphereLeft / RightWhich hemisphere is more active

All scores are on a 0–100 scale. 50 = average human response. Differences of 5–10 points are meaningful. Scores rarely reach extremes (0 or 100).


Known Behaviours

  • —Aggressive/salesy copy scores lower than natural storytelling — the brain pattern-matches it as noise
  • —Visual descriptive language produces the highest brain engagement
  • —Generic aspirational copy ("be the best version of yourself") scores below average — treated as a template
  • —Left hemisphere dominant = narrative/emotional language. Right hemisphere dominant = data, urgency, non-English
  • —Cold starts take ~90s — the LLaMA 3.2 tokenizer downloads on first predict call. Subsequent requests are faster

Build History & Issues Resolved

This Space went through multiple iterations. Documented here for future reference.

FixIssueSolution
1libgl1-mesa-glx not available in Debian trixieReplaced with libgl1
2uvx not found (tribev2 calls WhisperX via uvx)Installed uv via pip (puts uvx in /usr/local/bin)
3WhisperX crashes with float16 compute type on CPUPatched ExtractWordsFromAudio._get_transcript_from_audio to use int8 + device=cpu
4LLaMA 3.2-3B gated repo 401 errorPer-request hf_token design — users supply their own approved token
5torch.Tensor.to("cuda") crash — no GPUTried patching torch.Tensor.to (failed — C extension, not patchable from Python)
6Same CUDA crash via audio/video extractorsPatched torch.nn.Module.to and transformers.BatchEncoding.to (Python classes — patchable) + CUDA_VISIBLE_DEVICES="" in Dockerfile
7All neuralset extractors still using device="cuda" from configPatched model_post_init on every pydantic extractor class in neuralset with a device field
8AutoVideoProcessor not in transformers 4.42/4.47Unpinned transformers — use latest. Image/video natively blocked on CPU anyway (V-JEPA2 needs GPU)
9huggingface_hub==0.23.4 conflicts with transformers>=4.46Loosened to huggingface_hub>=0.24.0

Current status: Text endpoint fully working. Image and video endpoints exist but V-JEPA2 requires GPU — handled via Mavrx backend preprocessing (Claude Vision + Whisper → text → score).


CPU Patches Applied in app.py

tribev2 was designed for GPU. Running on CPU requires several patches:

python
# 1. WhisperX: replace float16 with int8 in subprocess command
subprocess.run = _patched_run  # intercepts uvx whisperx calls

# 2. torch.nn.Module.to: redirect cuda → cpu
torch.nn.Module.to = _cpu_module_to

# 3. transformers BatchEncoding.to: redirect cuda → cpu
BatchEncoding.to = _cpu_batch_to

# 4. All neuralset pydantic extractor classes: force device="cpu" after init
# Applied to: base, audio, video, text extractors
cls.model_post_init = _make_cpu_post(original)

# 5. HuggingFaceText._load_model: force device="cpu" before weight loading
HuggingFaceText._load_model = _cpu_load

# 6. ExtractWordsFromAudio._get_transcript_from_audio: full CPU-compatible override
# Uses --compute_type int8 --device cpu --batch_size 4

# 7. Environment
os.environ["CUDA_VISIBLE_DEVICES"] = ""  # also set in Dockerfile ENV
torch.cuda.is_available = lambda: False

Local Development

bash
# Clone
git clone https://huggingface.co/spaces/atharv-1447/META-TRIBE-v2-api
cd META-TRIBE-v2-api

# Build
docker build -t tribe-v2-api .

# Run (requires HF token with LLaMA access)
docker run -p 7860:7860 -e HF_TOKEN=hf_... tribe-v2-api

# Test
curl http://localhost:7860/health
curl -X POST http://localhost:7860/warmup -d '{"hf_token":"hf_..."}' -H "Content-Type: application/json"
curl -X POST http://localhost:7860/predict/text \
  -H "Content-Type: application/json" \
  -d '{"text": "Your content here", "hf_token": "hf_..."}'

Mavrx Integration

This Space is called from the Mavrx backend via backend/core/tribe_client.py.

Endpoints exposed to Mavrx frontend:

  • —POST /api/v1/brain/analyze/text — direct text scoring
  • —POST /api/v1/brain/analyze/media — image or video (preprocessed via Claude Vision / Whisper before scoring)

Config: Set TRIBE_HF_TOKEN=hf_... in backend/.env.


License

Model weights: CC-BY-NC-4.0 — non-commercial use only. API wrapper code: MIT.