aibtus commited on
Commit
d8aa3d3
·
verified ·
1 Parent(s): ab0c8e1

Upload 4 files

Browse files

Backed of Project 5 is set

Files changed (4) hide show
  1. Dockerfile +26 -0
  2. app.py +56 -0
  3. requirements.txt +6 -0
  4. tuned_xgb_sales_forecaster.pkl +3 -0
Dockerfile ADDED
@@ -0,0 +1,26 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Dockerfile
2
+
3
+ # Use a specific Python base image for stability and size optimization
4
+ FROM python:3.12-slim
5
+
6
+ # Set the working directory inside the container
7
+ WORKDIR /app
8
+
9
+ # Copy the requirements file and install dependencies
10
+ # Use --no-cache-dir to keep the image size small
11
+ COPY requirements.txt .
12
+ RUN pip install --no-cache-dir -r requirements.txt
13
+
14
+ # Copy the Flask application and the serialized model
15
+ COPY app.py .
16
+ COPY tuned_xgb_sales_forecaster.pkl .
17
+
18
+ # Expose the port the Flask app will run on
19
+ EXPOSE 5000
20
+
21
+ # Set the environment variable for Flask
22
+ ENV FLASK_APP=app.py
23
+
24
+ # Command to run the application when the container starts
25
+ # Use gunicorn or a similar WSGI server for production, but Flask's built-in server is fine for this context
26
+ CMD ["python", "app.py"]
app.py ADDED
@@ -0,0 +1,56 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # app.py
2
+
3
+ import joblib
4
+ import numpy as np
5
+ import pandas as pd
6
+ from flask import Flask, request, jsonify
7
+
8
+ # Define the model filename
9
+ MODEL_FILE = 'tuned_xgb_sales_forecaster.pkl'
10
+
11
+ # Define the 10 feature columns expected by the model pipeline
12
+ FEATURE_COLS = [
13
+ 'Product_Weight', 'Product_Sugar_Content', 'Product_Allocated_Area',
14
+ 'Product_Type', 'Product_MRP', 'Store_Size',
15
+ 'Store_Location_City_Type', 'Store_Type', 'Store_Age',
16
+ 'Product_Category_Simplified'
17
+ ]
18
+
19
+ # --- Load the Model Pipeline ---
20
+ try:
21
+ model_pipeline = joblib.load(MODEL_FILE)
22
+ print("Model loaded successfully.")
23
+ except Exception as e:
24
+ print(f"CRITICAL ERROR: Model not found: {e}. Check Dockerfile.")
25
+ model_pipeline = None
26
+
27
+ app = Flask(__name__)
28
+
29
+ @app.route('/predict', methods=['POST'])
30
+ def predict_sales():
31
+ if model_pipeline is None:
32
+ return jsonify({'error': 'Server setup error: Model not loaded.'}), 500
33
+
34
+ try:
35
+ data = request.get_json(force=True)
36
+ # Ensure input data matches the feature columns
37
+ input_df = pd.DataFrame(data, columns=FEATURE_COLS)
38
+
39
+ # Prediction on log scale
40
+ log_prediction = model_pipeline.predict(input_df)
41
+
42
+ # Inverse transformation: sales = exp(y) - 1
43
+ prediction_original_scale = np.expm1(log_prediction)
44
+
45
+ response = {
46
+ 'status': 'success',
47
+ 'predicted_sales_revenue': round(prediction_original_scale[0], 2)
48
+ }
49
+ return jsonify(response)
50
+
51
+ except Exception as e:
52
+ return jsonify({'error': f'Prediction logic failed: {str(e)}'}), 400
53
+
54
+ if __name__ == '__main__':
55
+ # This runs the server inside the Docker container
56
+ app.run(host='0.0.0.0', port=5000)
requirements.txt ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ Flask==3.0.3
2
+ pandas==2.2.2
3
+ numpy==1.26.4
4
+ scikit-learn==1.6.1
5
+ joblib==1.4.2
6
+ xgboost==2.1.4
tuned_xgb_sales_forecaster.pkl ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:64e94ad83c2cb59138fd0addc972f5484caad0c21455161edb6465fafe8f24ae
3
+ size 612275