How trendzaphq/trendzap-intelligence works
Purpose and Entry Point
TrendZap Intelligence is a stateless Python 3.11+ signal service for TrendZap’s social-virality prediction markets. It exposes prediction, forecasting, anomaly-detection, trend-discovery, and LLM explanation APIs used to price and help resolve markets.
The runtime entry point is the FastAPI application in src/api/main.py, launched as uvicorn src.api.main:app. The core package is src/trendzap_intelligence/.
API Boundary and Request Flow
src/api/main.py owns HTTP schemas, /health, and /api/v1/* routes. Requests enter through one of two paths:
• Deterministic/model paths: virality, engagement, anomaly, and trends invoke local model classes.
• AI paths: post/trend/anomaly requests invoke asynchronous Groq-backed analysis.
This is the boundary consumed by the TrendZap app, oracle, and risk systems; the service does not settle on-chain markets itself.
Predictive Signal Models
The model layer in src/trendzap_intelligence/models/ supplies the market-facing signals:
• virality_predictor.py: probability and threshold-relative OVER/UNDER decision. It has an LSTM/attention architecture using text embeddings and engineered social features, with a heuristic fallback pending production training.
• engagement_forecaster.py: final engagement estimate and confidence bounds. It currently uses heuristic growth-curve behavior, with an XGBoost path designed for later trained inference.
• anomaly_detector.py: artificial-engagement detection. Rule filters are active; Isolation Forest is available for trained use.
• trend_detector.py: TF-IDF plus DBSCAN clustering, ranked by growth velocity; operates without supervised training.
AI Explanation Layer
src/trendzap_intelligence/ai_analyzer.py translates raw post, trend, and anomaly inputs into human-readable analysis: drivers, audience or opportunity assessment, optimization guidance, longevity, risks, and anomaly explanations.
It uses an async OpenAI-compatible client configured for Groq/Llama 3.3 70B. This isolates slow external inference from FastAPI’s event loop while keeping deterministic model outputs separate from generative interpretation.
Configuration and External Dependencies
src/trendzap_intelligence/config.py centralizes settings and sync/async AI-client creation. Environment configuration includes the required Groq API key and optional Redis URL, represented by .env.example.
Material dependencies/services:
• FastAPI/Uvicorn API runtime.
• Groq LLM API for AI endpoints.
• Optional Redis for LLM response caching.
• ML/NLP stack supporting LSTM embeddings, XGBoost, Isolation Forest, TF-IDF, and DBSCAN.
Cache and Failure Boundary
AI endpoint payloads are hashed into Redis keys and cached for five minutes in src/api/main.py. Redis is deliberately non-critical: cache connection or retrieval failures bypass caching rather than fail client requests. This keeps the service available when optional infrastructure is absent; Groq availability remains the primary dependency for AI analysis.
Deployment and Platform Integration
Dockerfile packages the service; railway.toml defines Railway deployment behavior, including health checks and restart policy. Production binds to Railway’s injected port, with local fallback to port 8000.
Upstream social engagement metrics from trendzap-oracle feed anomaly assessment before resolution. Resulting bot-risk signals inform trendzap-risk; the frontend consumes analysis APIs. Final market settlement occurs externally through TrendZap’s Avalanche contracts, using validated oracle data rather than this repository directly.
Main entry points: main.py FastAPI app · anomaly_detector.py Anomaly detector · ai_analyzer.py AI analyzer · Dockerfile Docker image
Components 16
API Service
- FastAPI app · Health and versioned market-signal endpoints.
src/api/main.py - AI response cache · Five-minute cache; failures bypass caching.
src/api/main.py
Signal Models
- Virality predictor · Probability and OVER/UNDER signal with heuristic fallback.
src/trendzap_intelligence/models/virality_predictor.py - Engagement forecaster · Engagement estimate and confidence bounds.
src/trendzap_intelligence/models/engagement_forecaster.py - Anomaly detector · Rule-based artificial-engagement detection with Isolation Forest path.
src/trendzap_intelligence/models/anomaly_detector.py - Trend detector · TF-IDF and DBSCAN trends ranked by growth velocity.
src/trendzap_intelligence/models/trend_detector.py
AI Analysis
- Settings & AI clients · Builds sync and async Groq-compatible clients.
src/trendzap_intelligence/config.py - AI analyzer · Produces post, trend, and anomaly explanations.
src/trendzap_intelligence/ai_analyzer.py
Platform & Dependencies
- Groq LLM API · Llama 3.3 70B inference for generative endpoints.
- Redis · Non-critical remote cache backend.
- TrendZap frontend · Consumes analysis APIs.
- trendzap-oracle · Supplies social engagement data for assessment.
- trendzap-risk · Uses bot-risk signals.
- Avalanche contracts · Markets settle externally using validated oracle data.
- Docker image
Dockerfile - Railway deployment · Provides port, health checks, and restart policy.
railway.toml
Connections 15
- TrendZap frontend → FastAPI app: analysis requests
- FastAPI app → Virality predictor: virality requests
- FastAPI app → Engagement forecaster: engagement requests
- FastAPI app → Anomaly detector: anomaly requests
- FastAPI app → Trend detector: trend requests
- FastAPI app → AI response cache: read/write AI payloads
- AI response cache → Redis: optional backend
- FastAPI app → AI analyzer: AI endpoints
- Settings & AI clients → AI analyzer: async client configuration
- AI analyzer → Groq LLM API: async inference
- trendzap-oracle → Anomaly detector: engagement metrics
- Anomaly detector → trendzap-risk: bot-risk signal
- trendzap-oracle → Avalanche contracts: validated resolution data
- Docker image → FastAPI app: packages service
- Railway deployment → Docker image: deploys