# manascb1344/tensorforce architecture

> Architecture diagram of the GitHub repository manascb1344/tensorforce, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-04-30). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/manascb1344/tensorforce
- Repository: https://github.com/manascb1344/tensorforce

## Overview

##### Project Type and Purpose
- This is a full-stack, microservices-driven web application (“TensorForce”) for automated stock trading.
- It consists of:
- A React-based Single Page Application (SPA) in `WebD/`
- A Node.js/Express REST backend in `backend/`
- Multiple Python-based ML services in `ML/` (model training, inference, deployment)
- A MongoDB database for user and model metadata
- Integrations with external services (Alpaca trading API, news feeds)

##### Key Top-Level Directories & Roles
- `WebD/`
- React + Tailwind UI, Vite bundler, client-side routing (AppRoutes.jsx)
- Features: Dashboard, Heatmap, News, Shop, Sentiment, Charts
- `backend/`
- Node.js (Express) REST API: `index.js` exposes endpoints for userDetails, modelDetails
- Talks to MongoDB to store users, orders, marketplace listings
- `ML/`
- Subfolders for model development (`model1/`, `model2/`), news sentiment (`News/`), deployment scripts (`Deployment/`), Alpaca trading utilities
- `content/models/`
- Stores pre-built `.h5` model artifacts for direct load
- Config & infra: `.env` (API keys), `vercel.json` (frontend hosting), Render or similar for backend

##### High-Level System Components
A. Client Tier (WebD SPA)
– Browser-based React app
– UI modules: Login, Dashboard, Charts, Sentiment Bot
– Communicates over HTTPS with Backend Tier
B. Application Tier (Node.js Backend)
– REST API endpoints:
- Auth & Profile (`/login`, `/userDetails`)
- Model marketplace (`/modelDetails`, `/shop`)
- Trade orchestration
– Interacts with:
- MongoDB (User, Marketplace, Trade logs)
- ML Inference Services (Flask microservices)
- External Alpaca API for order placement
C. ML Tier (Python/Flask Microservices)
– Inference endpoints:
- Stock-prediction models (exposed via `Deployment/` scripts)
- Sentiment analysis service (`ML/News/SentimentBot.py`)
– Model registry: `.h5` files served from `content/models/` or cloud storage
– Training pipelines live in `ML/model1/`, `ML/model2/` (Jupyter notebooks, Keras/TensorFlow)
D. Data Tier
– MongoDB cluster (hosted)
– Model artifacts store (local `content/models/` or S3)
E. External Services
– Alpaca Trading REST API (order execution)
– Public News/API feeds for sentiment
– (Optional) Authentication provider (OAuth)

##### Relationships & Data Flows
1. User loads React SPA → Browser
2. SPA → Node.js Backend (HTTPS REST)
3. Backend → MongoDB (CRUD user, portfolio, model listings)
4. When user requests a prediction or auto-trade:
a. Backend forwards request to ML Inference Service (Flask) → returns prediction/sentiment
b. Backend uses prediction to call Alpaca API → executes trade
c. Backend writes trade result back to MongoDB → portfolio updated
5. Frontend polls or websockets → Backend → updates UI

##### Architectural Patterns & Design Principles
- Microservices: Separate ML inference from core business logic
- 3-Tier separation: Presentation / Application / Data
- Component-based UI (React) with Context Providers
- RESTful APIs for inter-service communication
- Model registry pattern: store and version `.h5` artifacts

##### Technologies & Libraries to Annotate
- Frontend: React, Vite, Tailwind CSS, React Router
- Backend: Node.js, Express, Mongoose (MongoDB driver)
- ML: Python, Flask, TensorFlow/Keras, PyTorch, Jupyter (for training)
- External: Alpaca trading API, News APIs

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Client Tier
    subgraph "Client Tier"
        direction TB
        WebD["WebD SPA\n(React, Vite, Tailwind, React Router)"]:::frontend
        AppRoutes["AppRoutes.jsx\n(Client Routing)"]:::frontend
        LoginMod["Login Module\n(Login.jsx, LoginButton.jsx,\nLogoutButton.jsx, Profile.jsx)"]:::frontend
        Dashboard["Dashboard Page\n(Dashboard.jsx)"]:::frontend
        Charts["Charts Components"]:::frontend
        SentimentUI["Sentiment Page\n(SentimentPage.jsx)"]:::frontend
        ShopUI["Shop Page\n(Shop.jsx)"]:::frontend
        QuickTrade["QuickTrade Component"]:::frontend
        AlpacaClient["AlpacaAPI Client\n(AlpacaAPI.js)"]:::frontend

        WebD --> AppRoutes
        AppRoutes --> LoginMod
        AppRoutes --> Dashboard
        Dashboard --> Charts
        Dashboard --> SentimentUI
        Dashboard --> ShopUI
        Dashboard --> QuickTrade
        QuickTrade --> AlpacaClient
    end

    %% Application Tier
    subgraph "Backend Microservices"
        direction TB
        NodeAPI["Node.js/Express API\n(index.js)"]:::backend
        UserAPI["User Endpoints\n(userDetails.js)"]:::backend
        ModelAPI["Model Marketplace Endpoints\n(modelDetails.js)"]:::backend

        NodeAPI --> UserAPI
        NodeAPI --> ModelAPI
    end

    %% ML Inference Tier
    subgraph "ML Inference Cluster"
        direction TB
        SentimentSvc["News Sentiment Service\n(Flask, app.py)"]:::mlservice
        SentimentLogic["SentimentBot.py\n(Sentiment Logic)"]:::mlservice
        AlpacaUtils["Alpaca Trading Utilities\n(ManasWorkingm1.py,\nUpdatedAlpacam1.py)"]:::mlservice
        DeploymentScripts["Deployment Scripts\n(alpaca.py, Givereq.py)"]:::mlservice
        INFYModel["Pre-built Model INFY.h5"]:::mlservice

        SentimentSvc --> SentimentLogic
        DeploymentScripts --> INFYModel
    end

    %% Training Pipelines
    subgraph "ML Training Pipelines"
        direction TB
        Model1["Model1 Experiments\n(ML/model1/)"]:::mlservice
        Model2["Model2 Notebooks\n(ML/model2/)"]:::mlservice
    end

    %% Data Tier
    subgraph "Data Tier"
        direction TB
        MongoDB["MongoDB Cluster"]:::database
        ModelRegistry["Model Registry\n(content/models/)"]:::database
    end

    %% External Services
    subgraph "External Services"
        direction TB
        AlpacaExt["Alpaca Trading API"]:::external
        NewsAPI["Public News API"]:::external
        OAuth["OAuth Provider (Optional)"]:::external
    end

    %% Connections
    WebD -->|"HTTPS REST"| NodeAPI
    NodeAPI -->|"CRUD user, portfolio"| MongoDB
    NodeAPI -->|"POST /sentiment"| SentimentSvc
    NodeAPI -->|"GET /predict"| DeploymentScripts
    NodeAPI -->|"POST /trade"| AlpacaExt
    NodeAPI -->|"Log trade"| MongoDB
    WebD -->|"WebSocket / Poll"| NodeAPI
    SentimentSvc --> NewsAPI
    DeploymentScripts --> ModelRegistry

    %% Click Events - Client
    click WebD "https://github.com/manascb1344/tensorforce/tree/main/WebD/"
    click AppRoutes "https://github.com/manascb1344/tensorforce/blob/main/WebD/src/AppRoutes.jsx"
    click LoginMod "https://github.com/manascb1344/tensorforce/blob/main/WebD/src/Login/Login.jsx"
    click LoginMod "https://github.com/manascb1344/tensorforce/blob/main/WebD/src/Login/LoginButton.jsx"
    click LoginMod "https://github.com/manascb1344/tensorforce/blob/main/WebD/src/Login/LogoutButton.jsx"
    click LoginMod "https://github.com/manascb1344/tensorforce/blob/main/WebD/src/Login/Profile.jsx"
    click Dashboard "https://github.com/manascb1344/tensorforce/blob/main/WebD/src/MainDashboard/pages/Dashboard.jsx"
    click Charts "https://github.com/manascb1344/tensorforce/tree/main/WebD/src/MainDashboard/components/Charts/"
    click SentimentUI "https://github.com/manascb1344/tensorforce/blob/main/WebD/src/MainDashboard/pages/SentimentPage.jsx"
    click ShopUI "https://github.com/manascb1344/tensorforce/blob/main/WebD/src/MainDashboard/pages/Shop.jsx"
    click QuickTrade "https://github.com/manascb1344/tensorforce/blob/main/WebD/src/MainDashboard/components/QuickTrade.jsx"
    click AlpacaClient "https://github.com/manascb1344/tensorforce/blob/main/WebD/src/MainDashboard/CompanyFinancials/Services/AlpacaAPI.js"

    %% Click Events - Backend
    click NodeAPI "https://github.com/manascb1344/tensorforce/blob/main/backend/index.js"
    click UserAPI "https://github.com/manascb1344/tensorforce/blob/main/backend/userDetails.js"
    click ModelAPI "https://github.com/manascb1344/tensorforce/blob/main/backend/modelDetails.js"

    %% Click Events - ML Inference
    click SentimentSvc "https://github.com/manascb1344/tensorforce/blob/main/ML/News/app.py"
    click SentimentLogic "https://github.com/manascb1344/tensorforce/blob/main/ML/News/SentimentBot.py"
    click AlpacaUtils "https://github.com/manascb1344/tensorforce/blob/main/ML/Alpaca/ManasWorkingm1.py"
    click AlpacaUtils "https://github.com/manascb1344/tensorforce/blob/main/ML/Alpaca/UpdatedAlpacam1.py"
    click DeploymentScripts "https://github.com/manascb1344/tensorforce/blob/main/ML/Deployment/alpaca.py"
    click DeploymentScripts "https://github.com/manascb1344/tensorforce/blob/main/ML/Deployment/Givereq.py"
    click INFYModel "https://github.com/manascb1344/tensorforce/blob/main/ML/Deployment/INFY.h5"

    %% Click Events - Training Pipelines
    click Model1 "https://github.com/manascb1344/tensorforce/tree/main/ML/model1/"
    click Model2 "https://github.com/manascb1344/tensorforce/tree/main/ML/model2/"

    %% Click Events - Model Registry
    click ModelRegistry "https://github.com/manascb1344/tensorforce/blob/main/content/models/INFY.h5"

    %% Styles
    classDef frontend fill:#b8e986,stroke:#333,stroke-width:1px
    classDef backend fill:#86b8e9,stroke:#333,stroke-width:1px
    classDef mlservice fill:#f2a25a,stroke:#333,stroke-width:1px
    classDef database fill:#c386e9,stroke:#333,stroke-width:1px
    classDef external fill:#d3d3d3,stroke:#333,stroke-width:1px
```
