Spaces:
Sleeping
Sleeping
Upload folder using huggingface_hub
Browse files- Dockerfile +1 -1
- app.py +36 -44
Dockerfile
CHANGED
|
@@ -13,4 +13,4 @@ RUN pip install --no-cache-dir --upgrade -r requirements.txt
|
|
| 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:
|
|
|
|
| 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_churn_predictor_api"]
|
app.py
CHANGED
|
@@ -1,52 +1,53 @@
|
|
| 1 |
-
|
| 2 |
import joblib
|
| 3 |
import pandas as pd
|
| 4 |
from flask import Flask, request, jsonify
|
| 5 |
|
| 6 |
# Initialize Flask app with a name
|
| 7 |
-
|
| 8 |
|
| 9 |
# Load the trained churn prediction model
|
| 10 |
-
model = joblib.load ("
|
| 11 |
|
| 12 |
# Define a route for the home page
|
| 13 |
-
@
|
| 14 |
def home ():
|
| 15 |
-
return "Welcome to the
|
| 16 |
|
| 17 |
# Define an endpoint to predict churn for a single customer
|
| 18 |
-
@
|
| 19 |
-
def
|
| 20 |
# Get JSON data from the request
|
| 21 |
-
|
| 22 |
|
| 23 |
# Extract relevant customer features from the input data
|
| 24 |
-
|
| 25 |
-
'
|
| 26 |
-
'
|
| 27 |
-
'
|
| 28 |
-
'
|
| 29 |
-
'
|
| 30 |
-
'
|
| 31 |
-
'
|
| 32 |
-
'
|
| 33 |
-
'
|
| 34 |
-
'
|
|
|
|
| 35 |
}
|
| 36 |
|
| 37 |
# Convert the extracted data into a DataFrame
|
| 38 |
-
input_data = pd.DataFrame ([
|
| 39 |
|
| 40 |
# Make a churn prediction using the trained model
|
| 41 |
-
|
| 42 |
|
| 43 |
-
#
|
| 44 |
-
|
| 45 |
|
|
|
|
|
|
|
| 46 |
|
| 47 |
-
# Define an endpoint to predict
|
| 48 |
-
|
| 49 |
-
@SuperKart_predictor_api.post ('/v1/SuperKartBatchSales')
|
| 50 |
def predict_churn_batch ():
|
| 51 |
# Get the uploaded CSV file from the request
|
| 52 |
file = request.files ['file']
|
|
@@ -54,27 +55,18 @@ def predict_churn_batch ():
|
|
| 54 |
# Read the file into a DataFrame
|
| 55 |
input_data = pd.read_csv (file)
|
| 56 |
|
| 57 |
-
#
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
X = input_data
|
| 64 |
-
|
| 65 |
-
# Make predictions
|
| 66 |
-
predictions = model.predict(X).tolist()
|
| 67 |
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
# Return mapping of Product_Id to predictions
|
| 71 |
-
output_dict = dict(zip(product_ids.tolist(), predictions))
|
| 72 |
-
else:
|
| 73 |
-
# If no Product_Id, just return index → predictions
|
| 74 |
-
output_dict = dict(zip(input_data.index.tolist(), predictions))
|
| 75 |
|
| 76 |
return output_dict
|
| 77 |
|
| 78 |
# Run the Flask app in debug mode
|
| 79 |
if __name__ == '__main__':
|
| 80 |
-
|
|
|
|
|
|
|
| 1 |
import joblib
|
| 2 |
import pandas as pd
|
| 3 |
from flask import Flask, request, jsonify
|
| 4 |
|
| 5 |
# Initialize Flask app with a name
|
| 6 |
+
cust_churn_predictor_api = Flask ("Customer Churn Predictor Week1")
|
| 7 |
|
| 8 |
# Load the trained churn prediction model
|
| 9 |
+
model = joblib.load ("churn_prediction_model_v2_0.joblib")
|
| 10 |
|
| 11 |
# Define a route for the home page
|
| 12 |
+
@cust_churn_predictor_api.get ('/')
|
| 13 |
def home ():
|
| 14 |
+
return "Welcome to the Customer Churn Prediction Week1 API!"
|
| 15 |
|
| 16 |
# Define an endpoint to predict churn for a single customer
|
| 17 |
+
@cust_churn_predictor_api.post ('/v1/customer')
|
| 18 |
+
def predict_churn ():
|
| 19 |
# Get JSON data from the request
|
| 20 |
+
customer_data = request.get_json ()
|
| 21 |
|
| 22 |
# Extract relevant customer features from the input data
|
| 23 |
+
sample = {
|
| 24 |
+
'customerID' : customer_data ['customerID'],
|
| 25 |
+
'SeniorCitizen' : customer_data ['SeniorCitizen'],
|
| 26 |
+
'tenure' : customer_data ['tenure'],
|
| 27 |
+
'MonthlyCharges' : customer_data ['MonthlyCharges'],
|
| 28 |
+
'TotalCharges' : customer_data ['TotalCharges'],
|
| 29 |
+
'Partner' : customer_data ['Partner'],
|
| 30 |
+
'Dependents' : customer_data ['Dependents'],
|
| 31 |
+
'PhoneService' : customer_data ['PhoneService'],
|
| 32 |
+
'InternetService' : customer_data ['InternetService'],
|
| 33 |
+
'Contract' : customer_data ['Contract'],
|
| 34 |
+
'PaymentMethod' : customer_data ['PaymentMethod']
|
| 35 |
}
|
| 36 |
|
| 37 |
# Convert the extracted data into a DataFrame
|
| 38 |
+
input_data = pd.DataFrame ([sample])
|
| 39 |
|
| 40 |
# Make a churn prediction using the trained model
|
| 41 |
+
prediction = model.predict (input_data).tolist ()[0]
|
| 42 |
|
| 43 |
+
# Map prediction result to a human-readable label
|
| 44 |
+
prediction_label = "churn" if prediction == 1 else "not churn"
|
| 45 |
|
| 46 |
+
# Return the prediction as a JSON response
|
| 47 |
+
return jsonify ({'Prediction': prediction_label})
|
| 48 |
|
| 49 |
+
# Define an endpoint to predict churn for a batch of customers
|
| 50 |
+
@cust_churn_predictor_api.post ('/v1/customerbatch')
|
|
|
|
| 51 |
def predict_churn_batch ():
|
| 52 |
# Get the uploaded CSV file from the request
|
| 53 |
file = request.files ['file']
|
|
|
|
| 55 |
# Read the file into a DataFrame
|
| 56 |
input_data = pd.read_csv (file)
|
| 57 |
|
| 58 |
+
# Make predictions for the batch data and convert raw predictions into a readable format
|
| 59 |
+
predictions = [
|
| 60 |
+
'Churn' if x == 1
|
| 61 |
+
else "Not Churn"
|
| 62 |
+
for x in model.predict (input_data.drop ("customerID",axis=1)).tolist ()
|
| 63 |
+
]
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
|
| 65 |
+
cust_id_list = input_data.customerID.values.tolist ()
|
| 66 |
+
output_dict = dict(zip (cust_id_list, predictions))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
|
| 68 |
return output_dict
|
| 69 |
|
| 70 |
# Run the Flask app in debug mode
|
| 71 |
if __name__ == '__main__':
|
| 72 |
+
cust_churn_predictor_api.run (debug=True)
|