A lightweight FastAPI application for serving machine learning predictions via HTTP.
-
cd FastAPI-ML
-
python3 -m venv fastmlsource fastml/bin/activate
-
pip install numpy==1.23.5 scikit-learn==1.1.3 pandas fastapi uvicorn
-
pip install -r requirements.txt
-
- Let users install all dependencies in one go:
pip freeze > requirements.txt
- Let users install all dependencies in one go:
-
python -m uvicorn mlapi:app --reload- Ping GET:
http://127.0.0.1:8000/ping- Checks if the API is live.- Response :
{ "message": "API is live!" }
- Response :
- POST /:
http://127.0.0.1:8000- Submit data for prediction.- Request Body (JSON):
{ "YearsAtCompany": 5, "EmployeeSatisfaction": 1, "Position": "Manager", "Salary": 2.0 }
- Response :
{ "prediction": 1 }
- Request Body (JSON):
- Ping GET:
- Example request (using curl or Postman):
curl -X POST "http://127.0.0.1:8000/" -H "Content-Type: application/json" -d '{ "YearsAtCompany": 3, "EmployeeSatisfaction": 4.2, "Position": "Manager", "Salary": 2 }'
- ModuleNotFoundError: Ensure your
virtualenvis activated before running anything. - Binary Incompatibility Errors (
numpy.dtype size changed):- Likely due to version mismatch between model training and current environment.
- Recreate environment using exact versions.
mlapi.py– FastAPI app with prediction endpoint.rfmodel.pkl– Pre-trained ML model.
- Make sure
rfmodel.pklwas trained using the same scikit-learn and numpy versions. - If you face issues like numpy dtype size changed, recreate the virtualenv with matching versions.
- Clone the repository:
git clone https://github.com/amitesh786/FastAPI-ML
Author Amitesh Singh – GitHub
- Feel free to contribute or suggest improvements!
