How thomas131104/regressionapi works
Project type and purpose
• This is a backend‐only RESTful API (“Regression API”) built with FastAPI that exposes multiple endpoints for running and evaluating regression models (Linear, Ridge, Lasso, ElasticNet, SVM, Decision Tree, Random Forest, KNN, etc.).
• It persists request results to MongoDB, automatically manages DB connections, and includes automated tests via pytest.
File structure
• Top level:
• main.py – entry point launching the FastAPI app (Hypercorn/Uvicorn)
• app/ – contains application code
• run/ – OS‐specific startup scripts
• render.yaml, requirements.txt, pytest.ini – config and dependencies
• test/ – pytest unit and integration tests
• app/ substructure:
• app/app.py – builds the FastAPI instance
• app/lifespan.py – startup/shutdown events for DB lifecycle
• app/middleware.py – any request/response middleware
• app/router/ – REST route handlers grouped by feature: best_model, option, history, stack_model, utils
• app/schemas/ – Pydantic models defining request/response payloads
• app/utils/machine_learning/ – core ML logic separated by phase: preprocessing, training, classification, running specific model flows
• app/database/ – MongoDB connection and CRUD operations
Extract insights from README
• REST endpoints under /regression/option, /regression/best-model-without-prediction, /regression/{...}/history
• Core features: model training, prediction, scoring (RMSE, MAE, R²), history persistence (with an auto-deletion policy), environment‐based DB config
• Tech stack: Python, FastAPI, Pydantic, MongoDB, pytest, Hypercorn/Uvicorn
a. Components
1. External Client(s) (Postman, frontend, other services)
2. HTTP Server (Hypercorn/Uvicorn process running main.py → app.app)
3. FastAPI Application
• Routers (controllers)
• Middleware
• Lifespan handlers
4. Business Logic Layer (service) in app/utils/machine_learning
5. Data Schemas (Pydantic models in app/schemas)
6. Persistence Layer (MongoDB driver in app/database/database.py)
7. MongoDB Database
8. Test Suite (pytest) – separate but helpful to show CI or local testing
9. Configuration/Env (.env → URL for MongoDB)
b. Relationships & data flows
1. Client → HTTP request → HTTP Server
2. Server passes through Middleware → Router
3. Router validates and parses JSON payload into Pydantic schema
4. Router invokes ML service methods (preprocessing, training, predicting)
5. ML services return results to Router
6. Router invokes Persistence layer to insert or query history in MongoDB
7. Persistence layer issues commands to MongoDB and returns stored documents
8. Router constructs response schema and returns JSON → Client
c. Architectural patterns & principles
• Layered architecture (Controller → Service → Repository)
• Separation of concerns (routers vs. schemas vs. business logic vs. database access)
• Environment‐driven configuration and automatic resource management via lifespan
• Testability via dependency injection (FastAPI’s dependency system)
d. Key technologies & libraries
• FastAPI (web framework)
• Pydantic (data validation / serialization)
• Hypercorn / Uvicorn (ASGI server)
• pymongo / motor (MongoDB driver)
• scikit-learn (model implementations, metrics)
• pytest (unit/integration testing)
Additional notes
• Initialization (lifespan) flow separately if desired: connect to MongoDB at startup, disconnect at shutdown