Upload 4 files
Browse files- Dockerfile +26 -0
- app.py +80 -0
- best_sales_forecasting_model.joblib +3 -0
- requirements.txt +6 -0
Dockerfile
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Use an official Python runtime as a parent image
|
| 2 |
+
FROM python:3.9-slim-buster
|
| 3 |
+
|
| 4 |
+
# Set the working directory in the container
|
| 5 |
+
WORKDIR /app
|
| 6 |
+
|
| 7 |
+
# Copy the requirements file into the container at /app
|
| 8 |
+
COPY requirements.txt .
|
| 9 |
+
|
| 10 |
+
# Install any needed packages specified in requirements.txt
|
| 11 |
+
RUN pip install --no-cache-dir -r requirements.txt
|
| 12 |
+
|
| 13 |
+
# Copy the Flask application and the trained model into the container at /app
|
| 14 |
+
COPY app.py .
|
| 15 |
+
COPY best_sales_forecasting_model.joblib .
|
| 16 |
+
|
| 17 |
+
# Expose the port that the Flask app will run on
|
| 18 |
+
EXPOSE 5000
|
| 19 |
+
|
| 20 |
+
# Define environment variables for Flask
|
| 21 |
+
ENV FLASK_APP=app.py
|
| 22 |
+
ENV FLASK_RUN_HOST=0.0.0.0
|
| 23 |
+
ENV FLASK_RUN_PORT=5000
|
| 24 |
+
|
| 25 |
+
# Run the Flask application
|
| 26 |
+
CMD ["flask", "run"]
|
app.py
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from flask import Flask, request, jsonify
|
| 2 |
+
import joblib
|
| 3 |
+
import pandas as pd
|
| 4 |
+
import numpy as np
|
| 5 |
+
|
| 6 |
+
# Import necessary sklearn components for the pipeline to be reconstructed
|
| 7 |
+
# These were part of the pipeline during training and are needed to load the model
|
| 8 |
+
from sklearn.pipeline import Pipeline
|
| 9 |
+
from sklearn.compose import ColumnTransformer
|
| 10 |
+
from sklearn.preprocessing import OneHotEncoder
|
| 11 |
+
from sklearn.ensemble import RandomForestRegressor # Or whichever regressor was used
|
| 12 |
+
|
| 13 |
+
app = Flask(__name__)
|
| 14 |
+
|
| 15 |
+
# Define the path to your serialized model
|
| 16 |
+
MODEL_PATH = 'best_sales_forecasting_model.joblib'
|
| 17 |
+
|
| 18 |
+
# Load the model when the application starts
|
| 19 |
+
try:
|
| 20 |
+
model = joblib.load(MODEL_PATH)
|
| 21 |
+
print(f"Model loaded successfully from {MODEL_PATH}")
|
| 22 |
+
except Exception as e:
|
| 23 |
+
print(f"Error loading model: {e}")
|
| 24 |
+
model = None # Set model to None if loading fails
|
| 25 |
+
|
| 26 |
+
@app.route('/predict', methods=['POST'])
|
| 27 |
+
def predict():
|
| 28 |
+
if model is None:
|
| 29 |
+
return jsonify({'error': 'Model not loaded.'}), 500
|
| 30 |
+
|
| 31 |
+
try:
|
| 32 |
+
# Get JSON data from the request
|
| 33 |
+
data = request.get_json(force=True)
|
| 34 |
+
|
| 35 |
+
# Convert incoming data to a Pandas DataFrame
|
| 36 |
+
# Ensure the column order and names match the training data's original features
|
| 37 |
+
# before preprocessing.
|
| 38 |
+
# X_train_raw (from your notebook) had these columns originally:
|
| 39 |
+
# ['Product_Id', 'Product_Weight', 'Product_Sugar_Content', 'Product_Allocated_Area',
|
| 40 |
+
# 'Product_Type', 'Product_MRP', 'Store_Id', 'Store_Establishment_Year',
|
| 41 |
+
# 'Store_Size', 'Store_Location_City_Type', 'Store_Type', 'Store_Age']
|
| 42 |
+
# Ensure the input data has these columns.
|
| 43 |
+
|
| 44 |
+
# Example of expected input data structure:
|
| 45 |
+
# {
|
| 46 |
+
# "Product_Id": ["FD6114"],
|
| 47 |
+
# "Product_Weight": [12.66],
|
| 48 |
+
# "Product_Sugar_Content": ["Low Sugar"],
|
| 49 |
+
# "Product_Allocated_Area": [0.027],
|
| 50 |
+
# "Product_Type": ["Frozen Foods"],
|
| 51 |
+
# "Product_MRP": [117.08],
|
| 52 |
+
# "Store_Id": ["OUT004"],
|
| 53 |
+
# "Store_Establishment_Year": [2009],
|
| 54 |
+
# "Store_Size": ["Medium"],
|
| 55 |
+
# "Store_Location_City_Type": ["Tier 2"],
|
| 56 |
+
# "Store_Type": ["Supermarket Type2"],
|
| 57 |
+
# "Store_Age": [17] # Or calculate in app if Store_Establishment_Year is provided
|
| 58 |
+
# }
|
| 59 |
+
|
| 60 |
+
input_df = pd.DataFrame(data)
|
| 61 |
+
|
| 62 |
+
# Make prediction using the loaded model pipeline
|
| 63 |
+
prediction = model.predict(input_df)
|
| 64 |
+
|
| 65 |
+
# Return the prediction as JSON
|
| 66 |
+
return jsonify({'predicted_sales': prediction.tolist()})
|
| 67 |
+
|
| 68 |
+
except Exception as e:
|
| 69 |
+
return jsonify({'error': str(e)}), 400
|
| 70 |
+
|
| 71 |
+
@app.route('/', methods=['GET'])
|
| 72 |
+
def home():
|
| 73 |
+
return "Welcome to the SuperKart Sales Forecasting API! Send POST requests to /predict with your data."
|
| 74 |
+
|
| 75 |
+
if __name__ == '__main__':
|
| 76 |
+
# In a Colab environment, you might want to use ngrok to expose your local server.
|
| 77 |
+
# For local testing, you can run:
|
| 78 |
+
# app.run(debug=True)
|
| 79 |
+
# For deployment, remove debug=True and configure a production-ready WSGI server.
|
| 80 |
+
app.run(host='0.0.0.0', port=5000)
|
best_sales_forecasting_model.joblib
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:0a1c15b842d2f51860705e60695f09787c082bb685c6b797bf5cb085e279b3f9
|
| 3 |
+
size 37543978
|
requirements.txt
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
flask
|
| 2 |
+
pandas
|
| 3 |
+
scikit-learn
|
| 4 |
+
joblib
|
| 5 |
+
numpy
|
| 6 |
+
pyngrok
|