# kinggiaan/crypto-predictor architecture

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

- Interactive diagram: https://gitdiagram.com/kinggiaan/crypto-predictor
- Repository: https://github.com/kinggiaan/crypto-predictor

## Overview

##### Main Components
- Frontend: Built with React and styled using Tailwind CSS, it contains components (e.g., Navbar, PredictionPanel, PriceChart) and pages (Dashboard, History, Learn). It interacts with the backend via API calls.
- Backend: Based on Flask, the backend exposes API endpoints (e.g., POST /predict, GET /history, POST /explain) and includes utility functions, model training logic, and database initialization.
- Machine Learning Models: Integrated within the backend—models like Random Forest and SVM, which are trained (using scripts like train_models.py) and then serve predictions.
- Database: SQLite serves as the storage for historical prediction data and related information.
- External Data Source: The Binance API is used as the data source for cryptocurrency prices.
- Additional Directories: Files such as .env and configuration files for managing environments, as well as deployment-related artifacts (e.g., package.json in both frontend and backend).

##### Establish Relationships and Data Flow
- The frontend sends HTTP requests to the Flask-based backend for predictions, historical data, and model explanations.
- The backend processes these requests; it accesses the machine learning models to compute predictions and interacts with the SQLite database to retrieve and store historical data.
- Data is also fetched from the Binance API to get updated crypto prices.
- Once processed, responses are sent back to the frontend, which then updates its interactive charts and displays to the user.

##### Architectural Patterns and Key Technologies
- Separation of Concerns: Clear division between the presentation layer (frontend) and the business logic/data handling layer (backend).
- API-Driven Design: The backend functions as an API server accepting POST/GET requests, simplifying interaction with separate components.
- MVC-like Pattern on Frontend: Though not strictly MVC, the frontend separates components (views) and pages to organize user interactions.
- Integration of AI/ML: The machine learning models (Random Forest and SVM) are central to the logic for making predictions.
- Data Flow: The use of a central database (SQLite) to persist historical prediction data, along with external API calls, signifies a real-time data processing pipeline.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Frontend Layer
    subgraph "Frontend Layer (React + Tailwind CSS)"
        FE["Frontend"]:::frontend
        DASHBOARD["Dashboard (Page)"]:::frontend
        HISTORY["History (Page)"]:::frontend
        LEARN["Learn (Page)"]:::frontend
        NAVBAR["Navbar (Component)"]:::frontend
        PREDICTIONPANEL["PredictionPanel (Component)"]:::frontend
        PRICECHART["PriceChart (Component)"]:::frontend
        SIDEBAR["Sidebar (Component)"]:::frontend
        EXPLANATIONBOX["ExplanationBox (Component)"]:::frontend
    end

    %% Backend Layer
    subgraph "Backend Layer (Flask)"
        BACKEND_API["Backend API"]:::backend
        UTILITY_FUNCS["Utility Functions"]:::backend
        DB_INIT["Database Initialization"]:::backend
        TRAIN_MODELS["Train Models"]:::backend
        ENV_CONFIG["Environment Config"]:::backend
    end

    %% Machine Learning Models
    subgraph "Machine Learning Models (Scikit-learn)"
        RF_MODEL["Random Forest Model"]:::ml
        SVM_MODEL["SVM Model"]:::ml
    end

    %% Database and External Data Source
    SQLITE_DB["SQLite Database"]:::database
    BINANCE_API["Binance API"]:::external

    %% Internal Frontend Structure
    FE -->|"contains"| DASHBOARD
    FE -->|"contains"| HISTORY
    FE -->|"contains"| LEARN
    FE -->|"contains"| NAVBAR
    FE -->|"contains"| PREDICTIONPANEL
    FE -->|"contains"| PRICECHART
    FE -->|"contains"| SIDEBAR
    FE -->|"contains"| EXPLANATIONBOX

    %% Frontend to Backend Communication
    FE -->|"HTTP_requests"| BACKEND_API

    %% Backend internal components
    BACKEND_API -->|"uses"| UTILITY_FUNCS
    BACKEND_API -->|"initializes"| DB_INIT
    BACKEND_API -->|"reads_config"| ENV_CONFIG

    %% Backend and Data Flow Connections
    BACKEND_API -->|"fetches_prices"| BINANCE_API
    BACKEND_API -->|"stores/retrieves"| SQLITE_DB
    BACKEND_API -->|"calls_predict"| RF_MODEL
    BACKEND_API -->|"calls_predict"| SVM_MODEL

    %% Training models connection
    TRAIN_MODELS -->|"trains"| RF_MODEL
    TRAIN_MODELS -->|"trains"| SVM_MODEL

    %% Styles
    classDef frontend fill:#cce5ff,stroke:#004085,stroke-width:2px;
    classDef backend fill:#d4edda,stroke:#155724,stroke-width:2px;
    classDef ml fill:#fff3cd,stroke:#856404,stroke-width:2px;
    classDef database fill:#f8d7da,stroke:#721c24,stroke-width:2px;
    classDef external fill:#e2e3e5,stroke:#6c757d,stroke-width:2px;

click FE "https://github.com/kinggiaan/crypto-predictor/tree/master/frontend/"
click DASHBOARD "https://github.com/kinggiaan/crypto-predictor/blob/master/frontend/src/pages/Dashboard.js"
click HISTORY "https://github.com/kinggiaan/crypto-predictor/blob/master/frontend/src/pages/History.js"
click LEARN "https://github.com/kinggiaan/crypto-predictor/blob/master/frontend/src/pages/Learn.js"
click NAVBAR "https://github.com/kinggiaan/crypto-predictor/blob/master/frontend/src/components/Navbar.js"
click PREDICTIONPANEL "https://github.com/kinggiaan/crypto-predictor/blob/master/frontend/src/components/PredictionPanel.jsx"
click PRICECHART "https://github.com/kinggiaan/crypto-predictor/blob/master/frontend/src/components/PriceChart.jsx"
click SIDEBAR "https://github.com/kinggiaan/crypto-predictor/blob/master/frontend/src/components/Sidebar.jsx"
click EXPLANATIONBOX "https://github.com/kinggiaan/crypto-predictor/blob/master/frontend/src/components/ExplanationBox.jsx"
click BACKEND_API "https://github.com/kinggiaan/crypto-predictor/blob/master/backend/app.py"
click DB_INIT "https://github.com/kinggiaan/crypto-predictor/blob/master/backend/init_db.py"
click TRAIN_MODELS "https://github.com/kinggiaan/crypto-predictor/blob/master/backend/train_models.py"
click UTILITY_FUNCS "https://github.com/kinggiaan/crypto-predictor/blob/master/backend/utils.py"
click ENV_CONFIG "https://github.com/kinggiaan/crypto-predictor/blob/master/.env"
```
