Spaces:
Sleeping
Sleeping
Upload folder using huggingface_hub
Browse files- Dockerfile +1 -1
- SuperKart_prediction_model_v1_0.joblib +3 -0
- app.py +45 -37
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:SuperKart_predictor_api"]
|
SuperKart_prediction_model_v1_0.joblib
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:945e3bbcca81bd8231d865137efc1a3f6a77f9f98b9ec285bd0400488c2a3610
|
| 3 |
+
size 63874755
|
app.py
CHANGED
|
@@ -1,53 +1,52 @@
|
|
|
|
|
| 1 |
import joblib
|
| 2 |
import pandas as pd
|
| 3 |
from flask import Flask, request, jsonify
|
| 4 |
|
| 5 |
# Initialize Flask app with a name
|
| 6 |
-
|
| 7 |
|
| 8 |
# Load the trained churn prediction model
|
| 9 |
-
model = joblib.load ("
|
| 10 |
|
| 11 |
# Define a route for the home page
|
| 12 |
-
@
|
| 13 |
def home ():
|
| 14 |
-
return "Welcome to the
|
| 15 |
|
| 16 |
# Define an endpoint to predict churn for a single customer
|
| 17 |
-
@
|
| 18 |
-
def
|
| 19 |
# Get JSON data from the request
|
| 20 |
-
|
| 21 |
|
| 22 |
# Extract relevant customer features from the input data
|
| 23 |
-
|
| 24 |
-
'
|
| 25 |
-
'
|
| 26 |
-
'
|
| 27 |
-
'
|
| 28 |
-
'
|
| 29 |
-
'
|
| 30 |
-
'
|
| 31 |
-
'
|
| 32 |
-
'
|
| 33 |
-
'
|
| 34 |
-
'PaymentMethod' : customer_data ['PaymentMethod']
|
| 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 |
-
# 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':
|
| 48 |
|
| 49 |
-
|
| 50 |
-
|
|
|
|
|
|
|
| 51 |
def predict_churn_batch ():
|
| 52 |
# Get the uploaded CSV file from the request
|
| 53 |
file = request.files ['file']
|
|
@@ -55,18 +54,27 @@ def predict_churn_batch ():
|
|
| 55 |
# Read the file into a DataFrame
|
| 56 |
input_data = pd.read_csv (file)
|
| 57 |
|
| 58 |
-
#
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
|
| 65 |
-
|
| 66 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
|
| 68 |
-
return output_dict
|
| 69 |
|
| 70 |
# Run the Flask app in debug mode
|
| 71 |
if __name__ == '__main__':
|
| 72 |
-
|
|
|
|
| 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 |
# 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)
|