How janani-n14/twincart-ai works
Purpose and entry points
TwinCart AI is a hyperlocal commerce decisioning platform: it models regions and customer segments as digital twins, uses agent workflows to produce campaign recommendations, and evaluates proposed spend through simulation. The backend starts at backend/app/main.py; the Streamlit client starts at frontend/streamlit_app.py and calls the API through frontend/utils/api_client.py.
API and application boundary
FastAPI route modules expose the platform’s principal capabilities: campaign generation, seller intelligence, simulations, twin operations, and ML training. Request/response contracts are Pydantic domain models under backend/app/models/campaign.py, backend/app/models/simulation.py, and related model files. Route handlers delegate rather than embedding the core decision logic: backend/app/routers/campaigns.py, backend/app/routers/simulation.py, and backend/app/routers/twins.py.
Digital-twin domain layer
Regional and segment twins are the domain representations used to contextualize recommendations by geography and customer behavior. backend/app/twins/regional_twin.py incorporates regional factors; backend/app/twins/segment_twin.py captures customer-segment behavior. Seed reference data, including regional and festival context, is held locally in backend/app/data/regions.json and backend/app/data/festivals.json.
Agentic campaign workflow
The agent layer contains specialized analytical and creative stages: trend detection, demand forecasting, weather/festival interpretation, catalog-gap analysis, campaign creation, budget optimization, explainability, and asset generation. Key nodes reside in backend/app/agents/trend_detection.py, backend/app/agents/campaign_generator.py, and backend/app/agents/budget_optimizer.py. LangGraph connects those stages through shared workflow state in backend/app/graph/state.py and execution topology in backend/app/graph/workflow.py; orchestration glue is centralized in backend/app/services/orchestrator.py.
ML and generated assets
Forecasting has both an agent-facing capability and a standalone ML implementation, separating orchestration from model/data handling. The ML boundary is backend/app/ml/demand_forecasting.py and backend/app/ml/dataset_loader.py. Creative output generation is represented by backend/app/ml/image_generation.py, with presentation-oriented agent nodes in backend/app/agents/banner_studio.py and backend/app/agents/poster_generator.py.
Simulation and decision loop
Before recommendations are committed, proposed campaigns can be evaluated as what-if scenarios by backend/app/simulation/engine.py. Simulation endpoints in backend/app/routers/simulation.py form the externally visible boundary. The expected loop is: twin/context inputs → graph-driven analysis and recommendation → campaign/budget output → simulation → explainable decision support.
Runtime and external services
The backend is Python/FastAPI with Pydantic v2; the frontend is Python/Streamlit. Agent execution uses LangGraph and LangChain, while LLM inference is delegated to Groq through the LLM integration in backend/app/core/llm.py; credentials are configured via backend/.env.example and application settings in backend/app/config.py. Shared cross-cutting facilities include caching, logging, and exception handling under backend/app/core/cache.py, backend/app/core/logging.py, and backend/app/core/exceptions.py.
Main entry points: campaigns.py Capability routes · workflow.py LangGraph workflow · orchestrator.py Campaign orchestrator · campaign_generator.py Campaign creation
Components 18
Client
- Streamlit client
frontend/streamlit_app.py - API client
frontend/utils/api_client.py
Backend API
- FastAPI application
backend/app/main.py - Capability routes · Campaign, simulation, and twin endpoints expose core capabilities.
backend/app/routers/campaigns.py - Domain contracts
backend/app/models/campaign.py
Twin Context
- Regional twin
backend/app/twins/regional_twin.py - Segment twin
backend/app/twins/segment_twin.py - Regional reference data · Regional and festival context is held locally.
backend/app/data/regions.json
Campaign Workflow
- Campaign orchestrator
backend/app/services/orchestrator.py - LangGraph workflow
backend/app/graph/workflow.py - Workflow state
backend/app/graph/state.py - Trend analysis
backend/app/agents/trend_detection.py - Campaign creation
backend/app/agents/campaign_generator.py - Budget optimization
backend/app/agents/budget_optimizer.py
Decisioning
- Demand forecasting · Standalone forecasting separates model and data handling from orchestration.
backend/app/ml/demand_forecasting.py - Creative generation
backend/app/ml/image_generation.py - What-if simulation · Evaluates proposed campaigns before commitment.
backend/app/simulation/engine.py
Other components
- Groq LLM
backend/app/core/llm.py
Connections 20
- Streamlit client → API client: uses
- API client → FastAPI application: API calls
- FastAPI application → Capability routes: mounts
- Capability routes → Domain contracts: validates
- Capability routes → Campaign orchestrator: delegates campaign requests
- Capability routes → What-if simulation: runs scenarios
- Capability routes → Regional twin: twin operations
- Regional reference data → Regional twin: context
- Regional twin → Campaign orchestrator: regional inputs
- Segment twin → Campaign orchestrator: segment inputs
- Campaign orchestrator → LangGraph workflow: executes
- LangGraph workflow → Workflow state: shares
- LangGraph workflow → Trend analysis: analysis stage
- LangGraph workflow → Campaign creation: creation stage
- LangGraph workflow → Budget optimization: optimization stage
- Trend analysis → Demand forecasting: demand signal
- Campaign creation → Creative generation: creative assets
- Budget optimization → What-if simulation: proposed spend
- Campaign creation → Groq LLM: LLM inference
- What-if simulation → Campaign orchestrator: decision feedback