rajaprabu27 commited on
Commit
8160659
·
verified ·
1 Parent(s): ddd5aba

Upload folder using huggingface_hub

Browse files
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:shipping_return_predictor_api"]
app.py ADDED
@@ -0,0 +1,70 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ from flask import Flask, request, jsonify
3
+ import pandas as pd
4
+ import numpy as np
5
+ import xgboost as xgb
6
+ import joblib
7
+
8
+ # Initialize Flask app
9
+ app = Flask(__name__)
10
+
11
+ # Load trained model and encoders
12
+ model = joblib.load("shipment_return_predictor_model.pkl")
13
+ target_encoder = joblib.load("target_encoder.pkl")
14
+ feature_encoders = joblib.load("feature_encoders.pkl") # Dictionary of encoders for categorical features
15
+ feature_columns = joblib.load("feature_columns.pkl") # List of feature columns used in training
16
+
17
+ @app.route('/predict', methods=['POST'])
18
+ def predict():
19
+ # Parse input JSON
20
+ input_data = request.get_json()
21
+ input_df = pd.DataFrame([input_data])
22
+
23
+ # Apply encoding to categorical features
24
+ for col, encoder in feature_encoders.items():
25
+ if col in input_df.columns:
26
+ input_df[col] = encoder.transform(input_df[col])
27
+
28
+ # Ensure all feature columns are present
29
+ for col in feature_columns:
30
+ if col not in input_df.columns:
31
+ input_df[col] = 0
32
+
33
+ input_df = input_df[feature_columns]
34
+
35
+ # Predict probabilities
36
+ probs = model.predict_proba(input_df)
37
+ preds = np.argmax(probs, axis=1)
38
+ pred_probs = probs[np.arange(len(preds)), preds]
39
+
40
+ # Get feature contributions
41
+ booster = model.get_booster()
42
+ dmatrix = xgb.DMatrix(input_df, feature_names=input_df.columns.tolist())
43
+ contribs = booster.predict(dmatrix, pred_contribs=True)
44
+
45
+ # Extract top contributing features
46
+ top_features = []
47
+ for i, pred_class in enumerate(preds):
48
+ contrib_vector = contribs[i]
49
+ class_contribs = contrib_vector[pred_class]
50
+ feature_contribs = dict(zip(input_df.columns.tolist(), class_contribs))
51
+ sorted_features = sorted(feature_contribs.items(), key=lambda x: abs(x[1]), reverse=True)
52
+ top_features.append([f[0] for f in sorted_features[:3]])
53
+
54
+ # Map predicted class to original label
55
+ predicted_labels = target_encoder.inverse_transform(preds)
56
+
57
+ # Prepare response
58
+ response = {
59
+ "Predicted Label": predicted_labels[0],
60
+ "Probability": float(pred_probs[0]),
61
+ "Top Contributing Features": top_features[0]
62
+ }
63
+
64
+ return jsonify(response)
65
+
66
+ # Run the app
67
+ if __name__ == '__main__':
68
+ app.run(debug=True)
69
+
70
+
feature_columns.pkl ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:db79374159549b801f6450389ef966ab230c7181e26a8d31328511d4edfa3e10
3
+ size 171
feature_encoders.pkl ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:02e6dca274d959896c9ee81ce7bd7f591ac78cc67d04a60dc4578bbaf56dfcdd
3
+ size 639803
requirements.txt ADDED
@@ -0,0 +1,12 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ flask==2.2.2
2
+ joblib==1.4.2
3
+ shap==0.44.1
4
+ xgboost==2.1.4
5
+ pandas==2.2.2
6
+ numpy==2.0.2
7
+ scikit-learn==1.6.1
8
+ Werkzeug==2.2.2
9
+ gunicorn==20.1.0
10
+ requests==2.28.1
11
+ uvicorn[standard]
12
+ streamlit==1.43.2
shipment_return_predictor_model.pkl ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:123ddda2b51c8c78b57d86276fcf2b5459c59f9e6b130164a51f7d3507a6ea83
3
+ size 2193702
target_encoder.pkl ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:32de92fcab8b4762ab0b873eba004c4741c64759a59de490c6f81221c4440e9b
3
+ size 375