This repository hosts an Employee Attrition Prediction model, developed using FastAPI and deployed on Hugging Face. The model predicts whether an employee is likely to stay or leave the company based on various input features. It uses a K-Nearest Neighbors (KNN) algorithm for prediction.
-
API Endpoint:
/predict
- Takes employee information as input and returns a prediction about whether the employee will leave or stay with the company.
-
Machine Learning Model: K-Nearest Neighbors (KNN)
-
Input Features:
- Gender (string)
- Age (int)
- Department (string)
- Job Title (string)
- Years at Company (int)
- Satisfaction Level (float)
- Average Monthly Hours (int)
- Promotion in Last 5 Years (boolean)
- Salary (int)
You can access the hosted API here.
To set up this project locally, you need to clone the repository to your local machine.
git clone https://github.com/yourusername/employee-attrition-prediction-api.git
cd employee-attrition-prediction-api
Before running the application, install the necessary dependencies using the requirements.txt
file.
pip install -r requirements.txt
To run the FastAPI
server locally, you can use Uvicorn
, which is a high-performance ASGI server.
uvicorn app:main --reload --host 0.0.0.0 --port 8000
The API server will be available at http://127.0.0.1:8000.
This endpoint predicts whether an employee will leave the company based on their profile information.
{
"Gender": "Female",
"Age": 30,
"Department": "Sales",
"Job_Title": "Analyst",
"Years_at_Company": 3,
"Satisfaction_Level": 80.5,
"Average_Monthly_Hours": 160,
"Promotion_Last_5Years": "No",
"Salary": 50000
}
{
"prediction": "The employee is likely to stay in the company."
}
🧪 Using Swagger UI (FASTAPI) :
The Swagger UI documentation automatically generated by FastAPI. Use this to explore and test all available API endpoints interactively./docs (GET)
To test the API
server locally using swaggerUI, you can use Run
, the following Url on Server :
http://127.0.0.1:8000/docs
🧪 Using Postman or CURL:
- Example
CURL
Request:
curl -X 'POST' \
'http://127.0.0.1:8000/predict' \
-H 'Content-Type: application/json' \
-d '{
"Gender": "Female",
"Age": 30,
"Department": "Sales",
"Job_Title": "Analyst",
"Years_at_Company": 3,
"Satisfaction_Level": 80.5,
"Average_Monthly_Hours": 160,
"Promotion_Last_5Years": "No",
"Salary": 50000
}'
- Expected Response:
{
"prediction": "The employee is likely to stay in the company."
}
What is FastAPI?
FastAPI is a modern, fast (high-performance) web framework for building APIs with Python. It is based on standard Python type hints and provides:
-
Automatic validation
-
Automatic documentation (Swagger UI)
-
Interactive API testing
Key Features of FastAPI:
-
Fast
: Quick to code, fast to run, and easy to get started. -
Automatic Documentation
: Swagger UI and ReDoc are auto-generated. -
Validation
: Pydantic models validate input data and provide detailed error messages. -
Asynchronous Support
: Allows sync and async endpoints for flexible request handling.
Request Methods:
-
POST:
Used to send data to the server (e.g., /predict). -
GET:
Used to retrieve data from the server (e.g., /docs).
Steps to Deploy:
-
Create a Hugging Face Account:
-
Sign up at Hugging Face.
-
Link your Hugging Face account with GitHub to host the project.
-
Create a Space:
-
Navigate to the
Spaces
section of your Hugging Face profile. -
Create a new space and choose the
Docker
template. -
Push Your Code to Hugging Face:
-
Initialize a Git repository in the project folder (if not already done).
-
Push the code to Hugging Face using the following commands:
git init
git remote add origin https://huggingface.co/spaces/yourusername/yourrepo
git add .
git commit -m "Initial commit"
git push origin main
Verify Deployment:
-
Hugging Face will automatically build and deploy your API.
-
Visit the Space's URL to access the hosted API.
Feel free to fork the repository and submit pull requests. When contributing, please adhere to these guidelines:
-
Code Style
: Follow the PEP 8 style guide. -
Testing
: Ensure all new features are covered with tests. -
Documentation
: Update the documentation if you add new features or make changes.
- For any questions or inquiries, feel free to reach out via open an issue in the repository.
FastAPI
: For providing an easy-to-use framework for building APIs.
Hugging Face
: For enabling simple deployment of machine learning models.
Scikit-learn
: For providing machine learning algorithms like KNN.
Uvicorn
: For serving the FastAPI application with high performance.