Na-Rajan commited on
Commit
2d85bfe
·
verified ·
1 Parent(s): 1abe904

Add SuperKart Flask backend deployment files

Browse files
Files changed (4) hide show
  1. Dockerfile +20 -0
  2. app.py +59 -0
  3. requirements.txt +7 -0
  4. superkart_model.joblib +3 -0
Dockerfile ADDED
@@ -0,0 +1,20 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Lightweight Python base image
2
+ FROM python:3.10-slim
3
+
4
+ # Set the working directory inside the container
5
+ WORKDIR /app
6
+
7
+ # Copy dependency file first (leverages Docker layer caching)
8
+ COPY requirements.txt .
9
+
10
+ # Install Python dependencies
11
+ RUN pip install --no-cache-dir -r requirements.txt
12
+
13
+ # Copy the rest of the backend files (app.py, superkart_model.joblib)
14
+ COPY . .
15
+
16
+ # Expose the port Flask/gunicorn will run on
17
+ EXPOSE 7860
18
+
19
+ # Start the Flask API using gunicorn (production-ready WSGI server)
20
+ CMD ["gunicorn", "--bind", "0.0.0.0:7860", "app:superkart_api"]
app.py ADDED
@@ -0,0 +1,59 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Flask backend that serves the trained SuperKart sales-forecasting model pipeline
2
+
3
+ from flask import Flask, request, jsonify
4
+ import pandas as pd
5
+ import joblib
6
+
7
+ # Initialize the Flask application
8
+ superkart_api = Flask(__name__)
9
+
10
+ # Load the trained pipeline (preprocessing + model) once at startup
11
+ model = joblib.load("superkart_model.joblib")
12
+
13
+ # The exact feature columns (and order) the model pipeline expects
14
+ FEATURE_COLUMNS = [
15
+ "Product_Weight", "Product_Sugar_Content", "Product_Allocated_Area",
16
+ "Product_MRP", "Store_Size", "Store_Location_City_Type", "Store_Type",
17
+ "Product_Id_char", "Store_Age_Years", "Product_Type_Category",
18
+ ]
19
+
20
+
21
+ @superkart_api.get("/")
22
+ def home():
23
+ """Simple health-check route."""
24
+ return {"message": "SuperKart Sales Forecasting API is up and running."}
25
+
26
+
27
+ @superkart_api.post("/v1/predict")
28
+ def predict():
29
+ """Online inference: predicts sales for a single record sent as JSON."""
30
+ data = request.get_json()
31
+
32
+ # Build a single-row DataFrame from the incoming JSON payload, in the
33
+ # exact column order the pipeline was trained on
34
+ input_df = pd.DataFrame([data], columns=FEATURE_COLUMNS)
35
+
36
+ prediction = model.predict(input_df)[0]
37
+
38
+ return jsonify({"predicted_Product_Store_Sales_Total": round(float(prediction), 2)})
39
+
40
+
41
+ @superkart_api.post("/v1/predictbatch")
42
+ def predict_batch():
43
+ """Batch inference: predicts sales for every row in an uploaded CSV file."""
44
+ file = request.files["file"]
45
+
46
+ # Read the uploaded CSV into a DataFrame and align its columns
47
+ input_df = pd.read_csv(file)
48
+ input_df = input_df[FEATURE_COLUMNS]
49
+
50
+ predictions = model.predict(input_df)
51
+
52
+ # Return predictions keyed by row index, as a JSON object
53
+ result = {str(idx): round(float(pred), 2) for idx, pred in enumerate(predictions)}
54
+ return jsonify(result)
55
+
56
+
57
+ if __name__ == "__main__":
58
+ # Run the Flask app on all interfaces, port 7860 (matches Codespace forwarding)
59
+ superkart_api.run(host="0.0.0.0", port=7860)
requirements.txt ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ flask==3.0.3
2
+ pandas==2.2.2
3
+ numpy==2.0.2
4
+ scikit-learn==1.6.1
5
+ xgboost==2.1.4
6
+ joblib==1.4.2
7
+ gunicorn==22.0.0
superkart_model.joblib ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:d38e63576bd6118f1949eacb289d86ee437f4bb28893682e339a3ba2eef2f60c
3
+ size 16231882