How hongha1412/ai-trainer-server works
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.