Spaces:
Running
DataVision AI - Complete MLOps Architecture & Lifecycle
This document describes the complete lifecycle of DataVision AI, from source code to model training, deployment, and monitoring on HuggingFace Spaces.
1. System Architecture
The application is deployed as a single monolithic Docker container on HuggingFace Spaces for simplicity and cost-effectiveness.
graph TD
A[User] -->|HTTPS| B(HuggingFace Space :7860)
B -->|Serves Static Files| C[React Frontend]
B -->|API Routes| D[FastAPI Backend]
D <-->|PostgreSQL| E[(External Hosted DB)]
D <-->|Persistent Storage| F[(HF Space /app/storage)]
D <-->|LLM Queries| G(Groq / OpenAI API)
2. CI/CD Pipeline
We utilize GitHub Actions to provide a zero-touch deployment experience.
- Local Development: Developer works on code locally and tests using
docker-compose up. - Push to Main:
git push origin maintriggers the GitHub Actions pipeline. - CI Checks (
ci.yml):- Runs
rufffor Python linting and syntax checking. - Runs
npm run buildto ensure the React frontend compiles cleanly.
- Runs
- Deployment (
deploy.yml):- Pushes the source code (including the freshly built
frontend/dist) to the HuggingFace remote using theHF_TOKEN.
- Pushes the source code (including the freshly built
- HuggingFace Build:
- HF detects the push, spins up a Build server, and executes the
Dockerfile. - Python dependencies are installed.
- HF detects the push, spins up a Build server, and executes the
- Container Startup (
start.sh):- Space awakens.
alembic upgrade headexecutes to apply any new database schema migrations to the external PostgreSQL database.uvicornstarts the FastAPI server on port 7860.
3. Storage & Persistence Strategy
Because Docker containers on HuggingFace are ephemeral (they sleep after inactivity and get completely rebuilt on new commits), persistence must be explicitly managed.
Database (Structured Data)
- What: Users, channels, chat threads, report metadata, model metadata.
- Where: External PostgreSQL database (e.g., Neon).
- Why: Ephemeral SQLite would wipe user accounts every time the space sleeps.
File Storage (Unstructured/Large Data)
- What: Uploaded CSV/Excel files, trained ML models (.pkl, .joblib), generated PDF/HTML reports, FAISS vector indexes.
- Where:
/app/storageinside the container. - Why: HuggingFace Spaces provides 50GB of persistent storage mounted at this directory. Our
Dockerfileexplicitly sets permissions on this folder to ensure theappusercan write to it.
4. Machine Learning Lifecycle
DataVision features an autonomous AutoML pipeline.
- Ingestion: User uploads a CSV. It is parsed using pandas and saved to
/app/storage/uploads. - Preprocessing: The Agentic system analyzes the data, imputes missing values, and encodes categoricals.
- Training: LightGBM/CatBoost/XGBoost models are trained in memory.
- Persistence: The best performing model pipeline is serialized using
jobliband saved to/app/storage/models/{model_id}.pkl. Model metadata is saved to PostgreSQL. - Deployment: When a user queries the model, the backend uses
model_deployer.pyto deserialize the model into memory. - Inference: User provides new data; the in-memory engine predicts the outcome.
- Retirement: The user can delete the model via the UI, which soft-deletes it in Postgres and removes the
.pklfile from persistent storage.
5. Security Practices
- Non-root Execution: The
Dockerfilecreates a dedicatedappuser. The application does not run as root. - Secrets Management: No API keys are in the codebase. All keys (
GROQ_API_KEY,DATABASE_URL,JWT_SECRET) are securely injected via HuggingFace Space Secrets at runtime. - Rate Limiting: The
core/rate_limiter.pymodule protects expensive endpoints (like LLM generation and model training) from abuse. - Data Isolation: Files are stored in user-specific directories (
/app/storage/users/{user_id}/).
6. Rollback Procedure
If a deployment fails or introduces a critical bug:
- Revert the commit locally:
git revert HEAD - Push to main:
git push origin main - The CI/CD pipeline will automatically build and deploy the reverted, stable code.
If database migrations caused the issue, you may need to manually downgrade the database using Alembic before reverting the code, or restore the database from a backup via your Postgres provider (e.g. Neon's point-in-time recovery).