# hongha1412/ai-trainer-server architecture

> Architecture diagram of the GitHub repository hongha1412/ai-trainer-server, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-05-12). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/hongha1412/ai-trainer-server
- Repository: https://github.com/hongha1412/ai-trainer-server

## Overview

##### Project Type and Purpose
- This is a modular monolithic Python REST API (Flask-based) for managing AI model training workflows.
- It provides endpoints for authentication, dataset upload/preprocessing, model registration, and orchestrating various training paradigms (supervised, self-supervised, etc.), with Hugging Face integration.

##### Main Architectural Layers & Components
a. Presentation/API Layer
- Flask app entrypoint (main.py / app.py)
- API modules under `api/`:
– auth.py (JWT + API-key auth)
– datasets.py
– models.py
– training.py
– docs.py (serves Swagger UI)
- Static assets: `static/swagger-ui` for interactive documentation
b. Service Layer (Business Logic)
- services/*.py – dataset_service, model_service, training_service, plus individual paradigm modules (supervised, self-supervised, etc.).
c. Data Layer
- Relational database (SQLite for dev, Postgres for prod) via SQLAlchemy models in models.py
- File-based storage:
– Datasets directory
– Model cache directory
d. Utilities
- utils/*.py – validation, exceptions, logging, OpenAPI schema generation
e. External Systems
- Hugging Face model hub (REST API)
- (Optional) Message broker / background workers (for diagramming future extensions)

##### Relationships & Data Flow
1. Client (e.g., Postman, browser-Swagger UI) → sends HTTP request → Flask API
2. Auth module validates JWT/API-key → passes control to endpoint
3. Endpoint calls appropriate Service component
4. Service component:
– Reads/writes metadata in the database
– Reads/writes files on disk (datasets, cache)
– Calls external Hugging Face REST API (model download/push)
5. Training jobs:
– Dispatched by training_service, invoke specific paradigm modules
– (In future, could enqueue to background worker)
6. Responses flow back up to API → client

##### Architectural Patterns & Principles
- Layered (Presentation → Service → Data)
- Separation of Concerns: API modules only handle routing & HTTP specifics; services encapsulate business logic.
- Modular design: each ML paradigm in its own service module.
- External integration encapsulation: Hugging Face calls behind service interfaces.
- Configurable via config.py / environment variables.

##### Final Checklist
- Clear, distinct labels for each component and subcomponent.
- Legends or annotations for environment variables (e.g., HF_API_KEY).
- Call out important frameworks: Flask, SQLAlchemy, Transformers/Torch, Swagger UI.

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% Client
    Client["Client (Postman / Browser / Swagger UI)"]:::external

    %% API Layer
    subgraph "API Layer"
        direction TB
        EntrypointMain["main.py"]:::api
        EntrypointApp["app.py"]:::api
        AuthAPI["auth.py"]:::api
        DatasetsAPI["datasets.py"]:::api
        ModelsAPI["models.py"]:::api
        TrainingAPI["training.py"]:::api
        DocsAPI["docs.py (Swagger UI)"]:::api
        StaticUI["static/swagger-ui"]:::api
    end

    %% Service Layer
    subgraph "Service Layer"
        direction TB
        DatasetService["dataset_service.py"]:::service
        ModelService["model_service.py"]:::service
        TrainingService["training_service.py"]:::service
        subgraph "Paradigm Modules"
            direction TB
            Supervised["transfer_learning.py"]:::service
            SelfSupervised["self_supervised_learning.py"]:::service
            SemiSupervised["semi_supervised_learning.py"]:::service
            Reinforcement["reinforcement_learning.py"]:::service
        end
    end

    %% Utilities
    subgraph "Utilities"
        direction TB
        ExceptionsUtil["exceptions.py"]:::util
        LoggingUtil["logging_config.py"]:::util
        OpenAPIUtil["openapi.py"]:::util
        ValidationUtil["validators.py"]:::util
    end

    %% Data Layer
    subgraph "Data Layer"
        direction TB
        DB["database.db (SQLite/Postgres)"]:::data
        SQLAModels["models.py (SQLAlchemy)"]:::data
        DatasetStorage["Datasets Directory"]:::data
        CacheStorage["Model Cache Directory"]:::data
    end

    %% External Systems
    subgraph "External"
        direction TB
        HFHub["Hugging Face Model Hub"]:::external
    end

    %% Connections
    Client -->|"HTTP REST"| EntrypointMain
    Client -->|"HTTP REST"| EntrypointApp

    EntrypointMain --> AuthAPI
    EntrypointMain --> DatasetsAPI
    EntrypointMain --> ModelsAPI
    EntrypointMain --> TrainingAPI
    EntrypointMain --> DocsAPI

    AuthAPI -->|"calls"| DatasetService
    DatasetsAPI --> DatasetService
    ModelsAPI --> ModelService
    TrainingAPI --> TrainingService
    DocsAPI --> StaticUI

    DatasetService --> SQLAModels
    ModelService --> SQLAModels
    TrainingService --> SQLAModels

    DatasetService --> DatasetStorage
    ModelService --> CacheStorage
    TrainingService --> DatasetStorage
    TrainingService --> CacheStorage

    TrainingService --> Supervised
    TrainingService --> SelfSupervised
    TrainingService --> SemiSupervised
    TrainingService --> Reinforcement

    Supervised --> HFHub
    SelfSupervised --> HFHub
    SemiSupervised --> HFHub
    Reinforcement --> HFHub

    EntrypointMain --> ExceptionsUtil
    EntrypointMain --> LoggingUtil
    EntrypointMain --> OpenAPIUtil
    EntrypointMain --> ValidationUtil
    DatasetService --> ExceptionsUtil
    ModelService --> ExceptionsUtil
    TrainingService --> ExceptionsUtil

    %% Click Events
    click EntrypointMain "https://github.com/hongha1412/ai-trainer-server/blob/main/main.py"
    click EntrypointApp "https://github.com/hongha1412/ai-trainer-server/blob/main/app.py"
    click AuthAPI "https://github.com/hongha1412/ai-trainer-server/blob/main/api/auth.py"
    click DatasetsAPI "https://github.com/hongha1412/ai-trainer-server/blob/main/api/datasets.py"
    click ModelsAPI "https://github.com/hongha1412/ai-trainer-server/blob/main/api/models.py"
    click TrainingAPI "https://github.com/hongha1412/ai-trainer-server/blob/main/api/training.py"
    click DocsAPI "https://github.com/hongha1412/ai-trainer-server/blob/main/api/docs.py"
    click StaticUI "https://github.com/hongha1412/ai-trainer-server/tree/main/static/swagger-ui"
    click DatasetService "https://github.com/hongha1412/ai-trainer-server/blob/main/services/dataset_service.py"
    click ModelService "https://github.com/hongha1412/ai-trainer-server/blob/main/services/model_service.py"
    click TrainingService "https://github.com/hongha1412/ai-trainer-server/blob/main/services/training_service.py"
    click Supervised "https://github.com/hongha1412/ai-trainer-server/blob/main/services/transfer_learning.py"
    click SelfSupervised "https://github.com/hongha1412/ai-trainer-server/blob/main/services/self_supervised_learning.py"
    click SemiSupervised "https://github.com/hongha1412/ai-trainer-server/blob/main/services/semi_supervised_learning.py"
    click Reinforcement "https://github.com/hongha1412/ai-trainer-server/blob/main/services/reinforcement_learning.py"
    click ExceptionsUtil "https://github.com/hongha1412/ai-trainer-server/blob/main/utils/exceptions.py"
    click LoggingUtil "https://github.com/hongha1412/ai-trainer-server/blob/main/utils/logging_config.py"
    click OpenAPIUtil "https://github.com/hongha1412/ai-trainer-server/blob/main/utils/openapi.py"
    click ValidationUtil "https://github.com/hongha1412/ai-trainer-server/blob/main/utils/validators.py"
    click SQLAModels "https://github.com/hongha1412/ai-trainer-server/blob/main/models.py"

    %% Styles
    classDef api fill:#D0E8FF,stroke:#0288D1,color:#000
    classDef service fill:#E0F8E0,stroke:#2E7D32,color:#000
    classDef data fill:#FFF4E5,stroke:#F57C00,color:#000
    classDef external fill:#E0E0E0,stroke:#757575,color:#000
    classDef util fill:#FFF9C4,stroke:#FBC02D,color:#000
```
