Skip to content

Migrate Flask Setup to FastAPI #83

@naman9271

Description

@naman9271

Summary

This issue proposes migrating the current Flask implementation to FastAPI to enhance performance, scalability, and maintainability for the TensorMap application.

Why is this change necessary?

The TensorMap project is focused on Machine Learning (ML) and Deep Learning (DL) applications, which demand high-performance APIs, better concurrency handling, and improved scalability. Flask, while widely used, has some limitations that FastAPI addresses effectively:


Migration plan

Phase 1: Setup FastAPI & Initial Configuration

Tasks

  • Create a new FastAPI project (tensormap-server-fastapi).
  • Install dependencies: fastapi uvicorn pydantic SQLAlchemy alembic python-dotenv
  • Set up main.py as the new entry point (equivalent to app.py in Flask).
  • Configure CORS middleware for frontend communication.
  • Migrate environment configurations from Flask to FastAPI (setup/settings.py).

Files to Migrate

  • app.pymain.py
  • setup/settings.py
  • setup/test_settings.py

Can Be Copied Directly:

  • .gitignore

Phase 2: Migrate Core API Endpoints

Tasks

  • Convert Flask @app.route to FastAPI @app.get, @app.post, etc.
  • Replace Flask’s request.json handling with Pydantic models for validation.
  • Refactor API responses to return FastAPI’s JSONResponse.
  • Update urls.py for each module to use FastAPI’s APIRouter.

Files to Migrate

Data Processing Endpoints

  • endpoints/DataProcess/views.py
  • endpoints/DataProcess/models.py
  • endpoints/DataProcess/services.py
  • endpoints/DataProcess/validators.py
  • endpoints/DataProcess/urls.py

Data Upload Endpoints

  • endpoints/DataUpload/views.py
  • endpoints/DataUpload/models.py
  • endpoints/DataUpload/services.py
  • endpoints/DataUpload/validators.py
  • endpoints/DataUpload/urls.py

Deep Learning Endpoints

  • endpoints/DeepLearning/modelRun.py
  • endpoints/DeepLearning/models.py
  • endpoints/DeepLearning/services.py
  • endpoints/DeepLearning/validators.py
  • endpoints/DeepLearning/urls.py

Can Be Copied Directly:

  • constants/errors.py

Phase 3: Migrate Database & Migrations

Tasks

  • Replace Flask-SQLAlchemy with SQLAlchemy + FastAPI Dependency Injection.
  • Update Alembic configurations for FastAPI.
  • Ensure database models and session handling work correctly.
  • Migrate Alembic migrations.

Files to Migrate

  • migrations/env.py
  • migrations/alembic.ini
  • migrations/script.py.mako
  • migrations/versions/*.py
  • setup/settings.py (for database settings)

Can Be Copied Directly:

  • migrations/README

Phase 4: Migrate Testing & Deployment

Tasks

  • Refactor tests/unit/ to use FastAPI’s TestClient.
  • Update Dockerfile for FastAPI and change Gunicorn to Uvicorn:
  • Update .env.example for any required environment variables.
  • Test Docker compatibility with FastAPI.
  • Ensure all API endpoints work correctly and validate responses.

Files to Migrate

  • tests/unit/test_DataProcess.py
  • tests/unit/test_DataUpload.py
  • tests/unit/conftest.py
  • tests/unit/__init__.py
  • .flake8
  • .gitignore
  • .env.example
  • Dockerfile

Can Be Copied Directly:

  • .flake8
  • .gitignore
  • .env.example

Phase 5: Final Integration & Documentation

Tasks

  • Update README.md with FastAPI usage instructions.
  • Document all API endpoints with FastAPI’s interactive Swagger UI.
  • Final refactoring and performance optimizations.
  • Complete final testing before merging PRs.

Files to Migrate

  • README.md
  • shared/request/response.py
  • shared/services/code/generation.py
  • shared/services/model/generation.py
  • shared/services/model/config.py

Can Be Copied Directly:

  • shared/request/
  • shared/services/

Expected Outcome

  • All API endpoints fully migrated to FastAPI.
  • Improved performance and async support.
  • Cleaner and modularized backend architecture.
  • Comprehensive test coverage with pytest and httpx.

  • Yes, I am willing to open a PR for this issue

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions