harishsohani commited on
Commit
53fe5e4
·
verified ·
1 Parent(s): cebbb7b

Upload folder using huggingface_hub

Browse files
Files changed (2) hide show
  1. Dockerfile +1 -1
  2. 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:SuperKart_predictor_api"]
 
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
- SuperKart_predictor_api = Flask ("SuperKart Predictor")
8
 
9
  # Load the trained churn prediction model
10
- model = joblib.load ("SuperKart_prediction_model_v1_0.joblib")
11
 
12
  # Define a route for the home page
13
- @SuperKart_predictor_api.get ('/')
14
  def home ():
15
- return "Welcome to the Super Kart Prediction!"
16
 
17
  # Define an endpoint to predict churn for a single customer
18
- @SuperKart_predictor_api.post ('/v1/SuperKartSales')
19
- def predict_sales ():
20
  # Get JSON data from the request
21
- sales_data = request.get_json ()
22
 
23
  # Extract relevant customer features from the input data
24
- data_info = {
25
- 'Product_Weight' : sales_data ['Product_Weight'],
26
- 'Product_Sugar_Content' : sales_data ['TotalCharges'],
27
- 'Product_Allocated_Area' : sales_data ['Product_Allocated_Area'],
28
- 'Product_Type' : sales_data ['Product_Type'],
29
- 'Product_MRP' : sales_data ['Product_MRP'],
30
- 'Store_Id' : sales_data ['Store_Id'],
31
- 'Store_Establishment_Year' : sales_data ['Store_Establishment_Year'],
32
- 'Store_Size' : sales_data ['Store_Size'],
33
- 'Store_Location_City_Type' : sales_data ['Store_Location_City_Type'],
34
- 'Store_Type' : sales_data ['Store_Type']
 
35
  }
36
 
37
  # Convert the extracted data into a DataFrame
38
- input_data = pd.DataFrame ([data_info])
39
 
40
  # Make a churn prediction using the trained model
41
- predicted_sales = model.predict (input_data).tolist ()[0]
42
 
43
- # Return the prediction as a JSON response
44
- return jsonify ({'Sales Prediction': predicted_sales})
45
 
 
 
46
 
47
- # Define an endpoint to predict sales for a batch of data
48
- # here we assume the data to conatain same columns as per the data provided for this project
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
- # Handle Product_Id if present
58
- if "Product_Id" in input_data.columns:
59
- product_ids = input_data["Product_Id"].copy()
60
- X = input_data.drop(columns=["Product_Id"])
61
- else:
62
- product_ids = None
63
- X = input_data
64
-
65
- # Make predictions
66
- predictions = model.predict(X).tolist()
67
 
68
- # Prepare response
69
- if product_ids is not None:
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
- SuperKart_predictor_api.run (debug=True)
 
 
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)