How sam-raiden/cortex-internaltool works
Purpose and Runtime
TamilSh.AI is a backend-only Python POC for collecting Tamil/Tanglish social content, extracting multimodal signals, embedding and clustering it, and exposing trend intelligence. It requires Python 3.9+, PostgreSQL via Docker, and Alembic-managed schema migrations. The API application entry point is app/main.py; collection can be invoked through app/collectors/__main__.py.
Persistence and Domain Model
PostgreSQL is the system of record for source governance, collection runs, raw content, processed signals, clusters, and trend-analysis entities. Database access and ORM schema are centralized in app/storage/database.py and app/models/schema.py. Schema evolution is controlled by migrations/env.py and the versioned migrations under migrations/versions/, including clustering and trend-intelligence additions.
Source Registry and Run Orchestration
Configured sources enter through JSON definitions such as config/sources/rss.json and config/sources/youtube.json. app/services/source_registry.py governs source registration/state, while app/services/collection_run.py tracks observable collection executions. app/services/pipeline.py coordinates pipeline stages; app/services/scheduler.py is the scheduling boundary for recurring work.
Multi-Platform Collection Boundary
Collectors isolate external-platform acquisition behind a common base in app/collectors/base.py. Instagram uses Playwright/browser-based public-profile access, authentication, parsing, and cycle execution in app/collectors/instagram/browser.py, app/collectors/instagram/collector.py, and app/collectors/instagram/run_cycle.py. RSS and YouTube have equivalent collector/cycle boundaries in app/collectors/rss/collector.py and app/collectors/youtube/run_cycle.py. Collected records are deduplicated by external identifiers before downstream processing.
Content Normalization and Signal Processing
Raw platform payloads are normalized into a consistent content representation by app/processing/normalize_raw_content.py and app/processing/normalizer.py. The processing stage assembles captions/text and derives usable signals through app/processing/processor.py, app/processing/text_assembler.py, and app/processing/process_signals.py. Signal eligibility and composition form an explicit quality boundary in app/processing/audit_signal_eligibility.py and app/processing/signal_composer.py.
Multimodal and AI Enrichment
Media-derived content is handled separately from collector adapters: extraction/validation in app/processing/media_extractor.py and app/processing/media_validation.py, OCR in app/processing/ocr.py, and speech transcription in app/processing/asr.py. Language detection and hashtag extraction enrich Tamil/Tanglish text in app/processing/language_detector.py and app/processing/hashtag_extractor.py. LLM enrichment is isolated behind app/services/llm_client.py and applied by app/processing/llm_enrichment.py, keeping the external model-provider boundary distinct from core processing.
Semantic Clustering and Trend Serving
Eligible text is transformed into multilingual sentence embeddings by app/processing/build_embeddings.py and app/processing/embeddings.py. HDBSCAN clustering is executed through app/clustering/run_clustering.py and app/clustering/hdbscan_runner.py; parameter evaluation is separated in app/clustering/evaluate_parameters.py. Trend interpretation is produced in app/processing/trend_intelligence.py and served through versioned API routes in app/api/v1.py, app/api/trends.py, and app/api/endpoints/collection_runs.py, with read-model assembly in app/services/trend_view.py.
Main entry points: database.py PostgreSQL · processor.py Signal processing · pipeline.py Pipeline coordinator · source_registry.py Source registry
Components 20
Orchestration
- Scheduler
app/services/scheduler.py - Pipeline coordinator
app/services/pipeline.py - Source registry
app/services/source_registry.py - Collection runs
app/services/collection_run.py
Collection
- Source definitions
config/sources/rss.json - Collector boundary
app/collectors/base.py - Platform collectors · Instagram uses browser-based public-profile collection.
app/collectors/instagram/collector.py
Processing & Enrichment
- Content normalization
app/processing/normalize_raw_content.py - Signal processing · Assembles text and applies eligibility and composition rules.
app/processing/processor.py - Multimodal enrichment
app/processing/media_extractor.py - LLM enrichment
app/services/llm_client.py
Clustering & Trends
- Multilingual embeddings
app/processing/build_embeddings.py - HDBSCAN clustering · Parameter evaluation is kept separate from execution.
app/clustering/run_clustering.py - Trend intelligence
app/processing/trend_intelligence.py
API & Persistence
- API application · Backend entry point.
app/main.py - Trend API routes
app/api/v1.py - Trend read model
app/services/trend_view.py - PostgreSQL · Stores sources, runs, content, signals, clusters, and trends.
app/storage/database.py - Domain schema
app/models/schema.py - Schema migrations
migrations/env.py
Connections 22
- API application → Trend API routes: mounts
- Trend API routes → Trend read model: serves trends
- Trend read model → PostgreSQL: reads
- Domain schema → PostgreSQL: maps
- Schema migrations → Domain schema: evolves
- Scheduler → Pipeline coordinator: triggers
- Pipeline coordinator → Source registry: selects sources
- Pipeline coordinator → Collection runs: tracks execution
- Source definitions → Source registry: registers
- Source registry → Collector boundary: configures
- Collector boundary → Platform collectors: implements
- Platform collectors → PostgreSQL: deduplicated raw content
- Pipeline coordinator → Content normalization: orchestrates
- PostgreSQL → Content normalization: raw records
- Content normalization → Signal processing: canonical content
- Multimodal enrichment → Signal processing: media signals
- LLM enrichment → Signal processing: enrichment
- Signal processing → PostgreSQL: processed signals
- Signal processing → Multilingual embeddings: eligible text
- Multilingual embeddings → HDBSCAN clustering: vectors
- HDBSCAN clustering → Trend intelligence: clusters
- Trend intelligence → PostgreSQL: trend entities