dabirsagar commited on
Commit
842c9c7
·
verified ·
1 Parent(s): 1322c25

Upload folder using huggingface_hub

Browse files
Files changed (3) hide show
  1. Dockerfile +16 -0
  2. app.py +91 -0
  3. requirements.txt +11 -0
Dockerfile ADDED
@@ -0,0 +1,16 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ FROM python:3.9-slim
2
+
3
+ # Set the working directory inside the container
4
+ WORKDIR /app
5
+
6
+ # Copy all files from the current directory to the container's working directory
7
+ COPY . .
8
+
9
+ # Install dependencies from the requirements file without using cache to reduce image size
10
+ RUN pip install --no-cache-dir --upgrade -r requirements.txt
11
+
12
+ # Define the command to start the application using Gunicorn with 4 worker processes
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:superkart_api`: Runs the Flask app (Flask app instance is named `superkart_api` inside app.py)
16
+ CMD ["gunicorn", "-w", "4", "-b", "0.0.0.0:7860", "app:app"]
app.py ADDED
@@ -0,0 +1,91 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ import os
3
+ import numpy as np
4
+ import pandas as pd
5
+ import joblib
6
+ from flask import Flask, request, jsonify
7
+ from flask_cors import CORS
8
+
9
+ # ------------------------------------------------------------
10
+ # Flask Application Setup
11
+ # ------------------------------------------------------------
12
+ def create_app(model_path: str):
13
+ """
14
+ Factory function to create and configure the Flask app.
15
+ """
16
+ app = Flask("superkart_api")
17
+ CORS(app)
18
+
19
+ # Load model once during startup
20
+ if not os.path.exists(model_path):
21
+ raise FileNotFoundError(f"Trained model not found at {model_path}")
22
+
23
+ model = joblib.load(model_path)
24
+
25
+ # --------------------------------------------------------
26
+ # Health check route
27
+ # --------------------------------------------------------
28
+ @app.route("/", methods=["GET"])
29
+ def health_check():
30
+ """
31
+ Returns a simple message to verify the API is running.
32
+ """
33
+ return jsonify({"status": "ok", "message": "SuperKart Sales Prediction API is active"})
34
+
35
+ # --------------------------------------------------------
36
+ # Prediction endpoint
37
+ # --------------------------------------------------------
38
+ @app.route("/api/v1/predict", methods=["POST"])
39
+ def predict():
40
+ """
41
+ Accepts JSON input, validates fields, runs the model, and returns predictions.
42
+ """
43
+ data = request.get_json()
44
+
45
+ if not data:
46
+ return jsonify({"error": "No input received"}), 400
47
+
48
+ expected_fields = [
49
+ "Product_Weight",
50
+ "Product_Sugar_Content",
51
+ "Product_Allocated_Area",
52
+ "Product_MRP",
53
+ "Store_Size",
54
+ "Store_Location_City_Type",
55
+ "Store_Type",
56
+ "Store_Age_Years",
57
+ "Product_Type_Category"
58
+ ]
59
+
60
+ missing = [f for f in expected_fields if f not in data]
61
+ if missing:
62
+ return jsonify({"error": f"Missing required fields: {missing}"}), 400
63
+
64
+ try:
65
+ # Preprocess input data into model format
66
+ processed = pd.DataFrame([{
67
+ "Product_Weight": float(data["Product_Weight"]),
68
+ "Product_Sugar_Content": data["Product_Sugar_Content"],
69
+ "Product_Allocated_Area_Log": np.log1p(float(data["Product_Allocated_Area"])),
70
+ "Product_MRP": float(data["Product_MRP"]),
71
+ "Store_Size": data["Store_Size"],
72
+ "Store_Location_City_Type": data["Store_Location_City_Type"],
73
+ "Store_Type": data["Store_Type"],
74
+ "Store_Age_Years": int(data["Store_Age_Years"]),
75
+ "Product_Type_Category": data["Product_Type_Category"]
76
+ }])
77
+
78
+ prediction = model.predict(processed)[0]
79
+
80
+ return jsonify({
81
+ "Predicted_Sales": round(float(prediction), 2),
82
+ "status": "success"
83
+ })
84
+
85
+ except Exception as exc:
86
+ return jsonify({"error": f"Prediction failed: {str(exc)}"}), 500
87
+
88
+ return app
89
+
90
+ MODEL_PATH = "/content/drive/My Drive/Colab Notebooks/superKart/superkart_product_sales_forecasting_model_v1_0.joblib"
91
+ app = create_app(saved_model_path)
requirements.txt ADDED
@@ -0,0 +1,11 @@
 
 
 
 
 
 
 
 
 
 
 
 
1
+ pandas==2.2.2
2
+ numpy==2.0.2
3
+ scikit-learn==1.6.1
4
+ flask-cors==3.0.10
5
+ joblib==1.4.2
6
+ Werkzeug==2.2.2
7
+ flask==2.2.2
8
+ gunicorn==20.1.0
9
+ requests==2.28.1
10
+ uvicorn[standard]
11
+ streamlit==1.43.2