Spaces:
No application file
No application file
Upload folder using huggingface_hub
Browse files- Dockerfile +9 -10
- app.py +71 -60
- customer_prediction_model_v1_0.joblib +2 -2
- requirements.txt +5 -1
Dockerfile
CHANGED
|
@@ -1,17 +1,16 @@
|
|
| 1 |
-
# Use a minimal base image with Python 3.9 installed
|
| 2 |
FROM python:3.9-slim
|
| 3 |
|
| 4 |
-
# Set the working directory inside the container
|
| 5 |
WORKDIR /app
|
| 6 |
|
| 7 |
-
# Copy all files from the current directory
|
| 8 |
COPY . .
|
| 9 |
|
| 10 |
-
# Install
|
| 11 |
-
RUN
|
| 12 |
|
| 13 |
-
# Define the command to
|
| 14 |
-
|
| 15 |
-
#
|
| 16 |
-
|
| 17 |
-
|
|
|
|
|
|
|
| 1 |
FROM python:3.9-slim
|
| 2 |
|
| 3 |
+
# Set the working directory inside the container
|
| 4 |
WORKDIR /app
|
| 5 |
|
| 6 |
+
# Copy all files from the current directory to the container's working directory
|
| 7 |
COPY . .
|
| 8 |
|
| 9 |
+
# Install dependencies from the requirements file without using cache to reduce image size
|
| 10 |
+
RUN pip install --no-cache-dir --upgrade -r requirements.txt
|
| 11 |
|
| 12 |
+
# Define the command to start the application using Gunicorn with 4 worker processes
|
| 13 |
+
# - `-w 4`: Uses 4 worker processes for handling requests
|
| 14 |
+
# - `-b 0.0.0.0:7860`: Binds the server to port 7860 on all network interfaces
|
| 15 |
+
# - `app:app`: Runs the Flask app (assuming `app.py` contains the Flask instance named `app`)
|
| 16 |
+
CMD ["gunicorn", "-w", "4", "-b", "0.0.0.0:7860", "app:cust_predictor_api"]
|
app.py
CHANGED
|
@@ -1,61 +1,72 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
#
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
#
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
}
|
| 51 |
-
|
| 52 |
-
#
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
#
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Import necessary libraries
|
| 2 |
+
import numpy as np
|
| 3 |
+
import joblib # For loading the serialized model
|
| 4 |
+
import pandas as pd # For data manipulation
|
| 5 |
+
from flask import Flask, request, jsonify # For creating the Flask API
|
| 6 |
|
| 7 |
+
# Initialize the Flask application
|
| 8 |
+
cust_predictor_api = Flask("ExtraaLearn Customer Predictor")
|
| 9 |
+
|
| 10 |
+
# Load the trained machine learning model
|
| 11 |
+
try:
|
| 12 |
+
model = joblib.load("customer_prediction_model_v1_0.joblib")
|
| 13 |
+
except Exception as e:
|
| 14 |
+
# If the model fails to load, print the error and continue, but the API will fail gracefully
|
| 15 |
+
print(f"ERROR: Failed to load model: {e}")
|
| 16 |
+
model = None # Set to None so the prediction route can check it
|
| 17 |
+
|
| 18 |
+
# Define a route for the home page (GET request)
|
| 19 |
+
@cust_predictor_api.get('/')
|
| 20 |
+
def home():
|
| 21 |
+
"""
|
| 22 |
+
This function handles GET requests to the root URL ('/') of the API.
|
| 23 |
+
It returns a simple welcome message.
|
| 24 |
+
"""
|
| 25 |
+
return "Welcome to the ExtraaLearn Customer Prediction API!"
|
| 26 |
+
|
| 27 |
+
classification_threshold = 0.45
|
| 28 |
+
|
| 29 |
+
# Define an endpoint for customer prediction (POST request)
|
| 30 |
+
@cust_predictor_api.post('/v1/cust_lead')
|
| 31 |
+
def predict_cust_lead():
|
| 32 |
+
"""
|
| 33 |
+
This function handles POST requests to the '/v1/cust_lead' endpoint.
|
| 34 |
+
It expects a JSON payload containing customer details and returns
|
| 35 |
+
the predicted customer probability as a JSON response.
|
| 36 |
+
"""
|
| 37 |
+
# Get the JSON data from the request body
|
| 38 |
+
cust_data = request.get_json()
|
| 39 |
+
|
| 40 |
+
# Extract relevant features from the JSON data
|
| 41 |
+
sample = {
|
| 42 |
+
'age' : cust_data['age'],
|
| 43 |
+
'current_occupation' : cust_data['current_occupation'],
|
| 44 |
+
'first_interaction' : cust_data['first_interaction'],
|
| 45 |
+
'profile_completed' : cust_data['profile_completed'],
|
| 46 |
+
'website_visits' : cust_data['website_visits'],
|
| 47 |
+
'time_spent_on_website' : cust_data['time_spent_on_website'],
|
| 48 |
+
'page_views_per_visit' : cust_data['page_views_per_visit'],
|
| 49 |
+
'last_activity' : cust_data['last_activity'],
|
| 50 |
+
'print_media_type1' : cust_data['print_media_type1'],
|
| 51 |
+
'print_media_type2' : cust_data['print_media_type2'],
|
| 52 |
+
'digital_media' : cust_data['digital_media'],
|
| 53 |
+
'educational_channels' : cust_data['educational_channels'],
|
| 54 |
+
'referral' : cust_data['referral']
|
| 55 |
+
}
|
| 56 |
+
|
| 57 |
+
# Convert the extracted data into a Pandas DataFrame
|
| 58 |
+
input_data = pd.DataFrame([sample])
|
| 59 |
+
|
| 60 |
+
# Make prediction
|
| 61 |
+
predicted_cust = model.predict_proba(input_data)[0][1]
|
| 62 |
+
|
| 63 |
+
# convert continuous prob as 0/1
|
| 64 |
+
predicted_cust = (predicted_cust >= classification_threshold).astype(int)
|
| 65 |
+
|
| 66 |
+
# Return the actual prediction status
|
| 67 |
+
return jsonify({'Predicted customer status': predicted_cust})
|
| 68 |
+
|
| 69 |
+
|
| 70 |
+
# Run the Flask application in debug mode if this script is executed directly
|
| 71 |
+
if __name__ == '__main__':
|
| 72 |
+
cust_predictor_api.run(debug=True)
|
customer_prediction_model_v1_0.joblib
CHANGED
|
@@ -1,3 +1,3 @@
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
-
oid sha256:
|
| 3 |
-
size
|
|
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:234d512ea8a1e062ee74102b1379852e2eddbaa697b07dd1bb79baf8351723d8
|
| 3 |
+
size 323770
|
requirements.txt
CHANGED
|
@@ -1,7 +1,11 @@
|
|
| 1 |
-
|
| 2 |
pandas==2.2.2
|
| 3 |
numpy==2.0.2
|
| 4 |
scikit-learn==1.6.1
|
| 5 |
xgboost==2.1.4
|
| 6 |
joblib==1.4.2
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
streamlit==1.43.2
|
|
|
|
|
|
|
| 1 |
pandas==2.2.2
|
| 2 |
numpy==2.0.2
|
| 3 |
scikit-learn==1.6.1
|
| 4 |
xgboost==2.1.4
|
| 5 |
joblib==1.4.2
|
| 6 |
+
Werkzeug==2.2.2
|
| 7 |
+
flask==2.2.2
|
| 8 |
+
gunicorn==20.1.0
|
| 9 |
+
requests==2.28.1
|
| 10 |
+
uvicorn[standard]
|
| 11 |
streamlit==1.43.2
|