Quantum9999 commited on
Commit
a3de9bb
·
verified ·
1 Parent(s): a4554e7

Upload folder using huggingface_hub

Browse files
Files changed (3) hide show
  1. Dockerfile +16 -0
  2. app.py +87 -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: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:sales_predictor_api"]
app.py ADDED
@@ -0,0 +1,87 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # backend_files/app.py
2
+
3
+ import numpy as np
4
+ import joblib
5
+ import pandas as pd
6
+ from flask import Flask, request, jsonify
7
+
8
+ # Initialize Flask app
9
+ sales_predictor_api = Flask("Retail Sales Prediction API")
10
+
11
+ # Load your trained model (change filename to your saved model file)
12
+ model = joblib.load("SuperKart_sales_rf_tuned_prediction_model_p1_0.joblib")
13
+
14
+ # Define a route for the home page (GET request)
15
+ @sales_predictor_api.get('/')
16
+ def home():
17
+ """
18
+ This function handles GET requests to the root URL ('/') of the API.
19
+ It returns a simple welcome message.
20
+ """
21
+ return "Welcome to the Retail Sales Prediction API!"
22
+
23
+
24
+ # Single prediction endpoint (POST)
25
+ @sales_predictor_api.route('/v1/sales', methods=['POST'])
26
+ def predict_sales():
27
+ """
28
+ Expects JSON with feature key-value pairs.
29
+ Returns predicted sales value.
30
+ """
31
+ data = request.get_json()
32
+
33
+ # Extract features for prediction (replace keys with your exact feature names)
34
+ features = {
35
+ 'Store_Establishment_Year': data['Store_Establishment_Year'],
36
+ 'Product_MRP': data['Product_MRP'],
37
+ 'Product_Weight': data['Product_Weight'],
38
+ 'Store_Id': data['Store_Id'],
39
+ 'Product_Type': data['Product_Type'],
40
+ 'Product_Sugar_Content': data['Product_Sugar_Content'],
41
+ 'Store_Location_City_Type': data['Store_Location_City_Type'],
42
+ 'Store_Size': data['Store_Size'],
43
+ 'Product_Allocated_Area': data['Product_Allocated_Area'],
44
+ 'Product_id': data['Product_id'],
45
+ 'Store_Type': data['Store_Type'],
46
+ # Add or remove features per your model input
47
+ }
48
+
49
+ # Convert to DataFrame for model input
50
+ input_df = pd.DataFrame([features])
51
+
52
+ # Predict sales
53
+ predicted_sales = model.predict(input_df)[0]
54
+
55
+ # Convert to float and round for JSON serialization
56
+ predicted_sales = round(float(predicted_sales), 2)
57
+
58
+ return jsonify({'Predicted_Sales': predicted_sales})
59
+
60
+
61
+ # Batch prediction endpoint (POST)
62
+ @sales_predictor_api.route('/v1/salesbatch', methods=['POST'])
63
+ def predict_sales_batch():
64
+ """
65
+ Expects uploaded CSV file with all required features and an 'id' column.
66
+ Returns a JSON dict of {id: predicted_sales} for all entries.
67
+ """
68
+ try:
69
+ file = request.files['file']
70
+ input_df = pd.read_csv(file)
71
+
72
+ # Predict sales for batch
73
+ preds = model.predict(input_df).tolist()
74
+ preds_rounded = [round(float(p), 2) for p in preds]
75
+
76
+ # Map property/product ID to prediction
77
+ ids = input_df['id'].tolist() # Ensure 'id' column exists in your batch data
78
+ results = dict(zip(ids, preds_rounded))
79
+
80
+ return jsonify(results)
81
+
82
+ except Exception as e:
83
+ return jsonify({"error": str(e)}), 400
84
+
85
+ # Run the Flask application in debug mode if this script is executed directly
86
+ if __name__ == "__main__":
87
+ sales_predictor_api.run(debug=True)
requirements.txt ADDED
@@ -0,0 +1,11 @@
 
 
 
 
 
 
 
 
 
 
 
 
1
+ pandas==2.2.2
2
+ numpy==2.0.2
3
+ scikit-learn==1.6.1
4
+ xgboost==2.1.4
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