Retheesh commited on
Commit
32e67e2
·
verified ·
1 Parent(s): 3bab238

Upload folder using huggingface_hub

Browse files
Files changed (4) hide show
  1. Dockerfile +23 -0
  2. app.py +113 -0
  3. requirements.txt +13 -0
  4. superkart_regression_model_v1.0.joblib +3 -0
Dockerfile ADDED
@@ -0,0 +1,23 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Use a lightweight Python image as the base image
2
+ FROM python:3.12-slim
3
+
4
+ # Set the working directory inside the container
5
+ WORKDIR /app
6
+
7
+ # Copy the requirements file into the container
8
+ COPY requirements.txt .
9
+
10
+ # Install the dependencies
11
+ RUN pip install --no-cache-dir -r requirements.txt
12
+
13
+ # Copy the application code into the container
14
+ COPY app.py .
15
+ COPY superkart_regression_model_v1.0.joblib .
16
+ COPY superkart_gbr_model_v1.0.json .
17
+
18
+
19
+ # Expose the port the Flask app will run on
20
+ EXPOSE 7860
21
+
22
+ # Command to run the Flask application using Gunicorn
23
+ CMD ["gunicorn", "--bind", "0.0.0.0:7860", "app:superkart_sales_predictor_api"]
app.py ADDED
@@ -0,0 +1,113 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import numpy as np
2
+ import joblib # For loading the serialized model
3
+ import pandas as pd # For data manipulation
4
+ from flask import Flask, request, jsonify # For creating the Flask API
5
+ import os # To check if the model file exists
6
+ import logging
7
+
8
+ # Configure logging
9
+ logging.basicConfig(level=logging.INFO)
10
+ logger = logging.getLogger(__name__)
11
+
12
+ logger.info("Starting SuperKart Sales Predictor API loading file...")
13
+ # Initialize the Flask application
14
+ superkart_sales_predictor_api = Flask("SuperKart Sales Predictor")
15
+
16
+ # Define the path to the trained machine learning model
17
+ model_path = "superkart_regression_model_v1.0.joblib"
18
+ model = None
19
+
20
+ def load_model():
21
+ """
22
+ This function loads the trained machine learning model.
23
+ It should be called when the Flask app starts to ensure the model is ready for predictions.
24
+ """
25
+ global model
26
+ if model is None:
27
+ try:
28
+ logger.info(f"Loading model from {model_path}...")
29
+ model = joblib.load(model_path)
30
+ logger.info("Model loaded successfully.")
31
+ except FileNotFoundError:
32
+ logger.info(f"Error: Model file not found at {model_path}")
33
+ except Exception as e:
34
+ logger.info(f"An error occurred while loading the model: {e}")
35
+
36
+
37
+ # Define a route for the home page (GET request)
38
+ @superkart_sales_predictor_api.route('/')
39
+ def home():
40
+ """
41
+ This function handles GET requests to the root URL ('/') of the API.
42
+ It returns a simple welcome message and model loading status.
43
+ """
44
+ global model
45
+ if model is None:
46
+ load_model()
47
+ # Check if the model is loaded successfully
48
+ if model:
49
+ return "Welcome to the SuperKart Sales Prediction API! Model loaded successfullyX."
50
+ else:
51
+ return "Welcome to the SuperKart Sales Prediction API! Model loading failedX."
52
+
53
+ # Define an endpoint for single sales prediction (POST request)
54
+ @superkart_sales_predictor_api.route('/predict_sales', methods=['POST'])
55
+ def predict_sales():
56
+ """
57
+ This function handles POST requests to the '/predict_sales' endpoint.
58
+ It expects a JSON payload containing product and store details and returns
59
+ the predicted sales as a JSON response.
60
+ """
61
+ global model
62
+ if model is None:
63
+ load_model()
64
+ if model is None:
65
+ return jsonify({'error': 'Model not loaded. Cannot make predictions.'}), 500
66
+
67
+ try:
68
+ # Get the JSON data from the request body
69
+ input_data = request.get_json()
70
+
71
+ # Convert the input data to a pandas DataFrame
72
+ # Ensure the column order matches the training data
73
+ input_df = pd.DataFrame([input_data])
74
+
75
+ # Preprocess the input data similar to how the training data was preprocessed
76
+ # This includes feature engineering, one-hot encoding, and scaling
77
+ # (Assuming the preprocessing steps from the notebook are applied here)
78
+
79
+ # Example of expected columns after preprocessing (adjust based on your actual preprocessing)
80
+ # This is a simplified example, you will need to replicate your exact preprocessing
81
+ # steps here, including any feature engineering and scaling.
82
+
83
+ # For demonstration, let's assume the input JSON keys directly map to the features
84
+ # used for training after one-hot encoding and scaling.
85
+ # You will need to replace this with your actual preprocessing logic.
86
+
87
+ # **IMPORTANT:** You need to add the exact preprocessing steps here that were used
88
+ # to train the model, including handling categorical variables (one-hot encoding)
89
+ # and scaling numerical features using the *same scaler* fitted on the training data.
90
+
91
+ # Placeholder for actual preprocessed input DataFrame
92
+ # Replace this with your actual preprocessing code
93
+ preprocessed_input = input_df # Replace with your actual preprocessed data
94
+
95
+ # Make prediction using the loaded model
96
+ # The model was trained on log-transformed sales, so the prediction will be log-transformed
97
+ predicted_sales_log = model.predict(preprocessed_input)[0]
98
+
99
+ # Inverse transform the prediction to get the actual sales value
100
+ predicted_sales = np.expm1(predicted_sales_log) # Use np.expm1 to reverse np.log1p
101
+
102
+ # Return the prediction as a JSON response
103
+ return jsonify({'predicted_sales': predicted_sales})
104
+
105
+ except Exception as e:
106
+ return jsonify({'error': str(e)}), 400
107
+
108
+ # To run the Flask app (for local testing)
109
+ if __name__ == '__main__':
110
+ # In a production environment, you would typically use a production-ready WSGI server
111
+ # such as Gunicorn or uWSGI.
112
+ logger.info("About to start the SuperKart Sales Predictor API...")
113
+ superkart_sales_predictor_api.run(debug=True, host='0.0.0.0', port=7860)
requirements.txt ADDED
@@ -0,0 +1,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ numpy==2.3.2
2
+ pandas==2.3.1
3
+ scikit-learn==1.6.1
4
+ matplotlib
5
+ seaborn==0.13.2
6
+ joblib==1.5.1
7
+ xgboost==3.0.4
8
+ requests==2.32.3
9
+ Werkzeug==2.2.2
10
+ flask==2.2.2
11
+ gunicorn==20.1.0
12
+ uvicorn[standard]
13
+ streamlit==1.43.2
superkart_regression_model_v1.0.joblib ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:82f53e00ea70bbecbf08c9b3dcccfe33c67736b73d5f6715b54d6ba9e5715f91
3
+ size 424984