BujjiProjectPrep commited on
Commit
7acc16a
·
verified ·
1 Parent(s): af10125

Upload folder using huggingface_hub

Browse files
Files changed (4) hide show
  1. Dockerfile +16 -0
  2. app.py +98 -0
  3. requirements.txt +10 -0
  4. sales_forecast_model_v1_0.joblib +3 -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: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:rental_price_predictor_api"]
app.py ADDED
@@ -0,0 +1,98 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Import necessary libraries
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
+
6
+ # Initialize the Flask application
7
+ superkart_sales_api = Flask("SuperKart Sales Forecast API")
8
+
9
+ # Load the trained machine learning pipeline (preprocessor + model)
10
+ # Make sure this file is present next to app.py in your backend folder
11
+ model = joblib.load("sales_forecast_model_v1_0.joblib")
12
+
13
+ # Expected feature names (order doesn't matter for DataFrame, kept for clarity)
14
+ EXPECTED_FEATURES = [
15
+ "Product_Weight",
16
+ "Product_Allocated_Area",
17
+ "Product_MRP",
18
+ "Store_Age",
19
+ "Product_Sugar_Content",
20
+ "Product_Type",
21
+ "Store_Size",
22
+ "Store_Location_City_Type",
23
+ "Store_Type",
24
+ ]
25
+
26
+ # Define a route for the home page (GET request)
27
+ @superkart_sales_api.get("/")
28
+ def home():
29
+ """
30
+ Handles GET requests to the root URL ('/').
31
+ Returns a simple welcome message and the expected schema.
32
+ """
33
+ return "Welcome to the SuperKart Sales Forecast API!"
34
+
35
+ # jsonify({
36
+ # "message": ,
37
+ # "expected_payload": {
38
+ # "Product_Weight": "float",
39
+ # "Product_Allocated_Area": "float (0-1)",
40
+ # "Product_MRP": "float",
41
+ # "Store_Age": "int",
42
+ # "Product_Sugar_Content": "str (e.g., 'Regular', 'Low Sugar', 'No Sugar')",
43
+ # "Product_Type": "str (e.g., 'Snack Foods', 'Dairy', ...)",
44
+ # "Store_Size": "str (e.g., 'Small', 'Medium', 'High')",
45
+ #"Store_Location_City_Type": "str (e.g., 'Tier 1', 'Tier 2', 'Tier 3')",
46
+ #"Store_Type": "str (e.g., 'Supermarket Type 2', 'Departmental Store', ...)"
47
+ #}
48
+ #})
49
+
50
+ # Define an endpoint for single sales prediction (POST request)
51
+ @superkart_sales_api.post("/v1/sales")
52
+ def predict_sales():
53
+ """
54
+ Handles POST requests to the '/v1/sales' endpoint.
55
+ Expects a JSON payload with SuperKart product & store features and
56
+ returns the predicted Product_Store_Sales_Total as JSON.
57
+
58
+ Example payload:
59
+ {
60
+ "Product_Weight": 12.5,
61
+ "Product_Allocated_Area": 0.06,
62
+ "Product_MRP": 150,
63
+ "Store_Age": 16,
64
+ "Product_Sugar_Content": "Regular",
65
+ "Product_Type": "Snack Foods",
66
+ "Store_Size": "Medium",
67
+ "Store_Location_City_Type": "Tier 2",
68
+ "Store_Type": "Supermarket Type 2"
69
+ }
70
+ """
71
+ try:
72
+ payload = request.get_json()
73
+
74
+ # Basic validation: ensure all required features are present
75
+ missing = [f for f in EXPECTED_FEATURES if f not in payload]
76
+ if missing:
77
+ return jsonify({
78
+ "error": "Missing required feature(s).",
79
+ "missing": missing,
80
+ "expected_features": EXPECTED_FEATURES
81
+ }), 400
82
+
83
+ # Build a single-row DataFrame in the expected feature order
84
+ sample = {f: payload[f] for f in EXPECTED_FEATURES}
85
+ input_df = pd.DataFrame([sample])
86
+
87
+ # Predict sales (model outputs actual sales; no log transform)
88
+ pred = model.predict(input_df)[0]
89
+ pred = round(float(pred), 2) # ensure JSON-serializable and nicely rounded
90
+
91
+ return jsonify({"Predicted Product_Store_Sales_Total": pred})
92
+
93
+ except Exception as e:
94
+ return jsonify({"error": str(e)}), 500
95
+
96
+ # Run the Flask application in debug mode if this script is executed directly
97
+ if __name__ == "__main__":
98
+ superkart_sales_api.run(debug=True)
requirements.txt ADDED
@@ -0,0 +1,10 @@
 
 
 
 
 
 
 
 
 
 
 
1
+ pandas==2.2.2
2
+ numpy==2.0.2
3
+ scikit-learn==1.6.1
4
+ joblib==1.4.2
5
+ Werkzeug==2.2.2
6
+ flask==2.2.2
7
+ gunicorn==20.1.0
8
+ requests==2.28.1
9
+ uvicorn[standard]
10
+ streamlit==1.43.2
sales_forecast_model_v1_0.joblib ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:0dc4282180e8e3994e569450d1c09b35ce2825e6116917d752c350f91295fd0a
3
+ size 63809427