harikbab02 commited on
Commit
b44786d
·
verified ·
1 Parent(s): 39b3cf9

Upload folder using huggingface_hub

Browse files
Files changed (3) hide show
  1. Dockerfile +21 -0
  2. app.py +103 -0
  3. requirements.txt +11 -0
Dockerfile ADDED
@@ -0,0 +1,21 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ FROM python:3.9-slim
2
+
3
+ # Set the working directory
4
+ WORKDIR /app
5
+
6
+ # Copy requirements and install dependencies
7
+ COPY requirements.txt /app/requirements.txt
8
+ RUN pip install --no-cache-dir --upgrade -r /app/requirements.txt
9
+
10
+ # Copy source files into the container
11
+ COPY app.py /app/
12
+ COPY gradboost_RSCV.joblib /app/
13
+ COPY RndmFrstReg_RSCV.joblib /app/
14
+ COPY pipeline.joblib /app/
15
+ COPY feature_names.joblib /app/
16
+
17
+ # Expose the port used by the Hugging Face Docker Space
18
+ EXPOSE 7860
19
+
20
+ # Command to run the Flask app with gunicorn
21
+ CMD ["gunicorn", "--bind", "0.0.0.0:7860", "app:app"]
app.py ADDED
@@ -0,0 +1,103 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ # Importing necessary libraries
3
+ from flask import Flask, request, jsonify # Flask framework and JSON utilities
4
+ from flask_cors import CORS # To handle Cross-Origin Resource Sharing (CORS)
5
+ import joblib # For loading saved model/pipeline objects
6
+ import numpy as np
7
+ import pandas as pd
8
+ import logging # For logging API activity
9
+
10
+ # Set logging level and format
11
+ logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s')
12
+
13
+ # Initialize the Flask app
14
+ app = Flask(__name__)
15
+
16
+ # Enable CORS for the '/predict' endpoint
17
+ CORS(app, resources={r"/predict": {"origins": "*"}})
18
+
19
+ # Load the models, pipeline, and feature list
20
+ try:
21
+ model_gradboost = joblib.load('gradboost_RSCV.joblib') # Tuned Gradient Boosting model
22
+ model_rndmfrst = joblib.load('RndmFrstReg_RSCV.joblib') # Tuned Random Forest model
23
+ pipeline = joblib.load('pipeline.joblib') # Preprocessing pipeline
24
+ feature_names = joblib.load('feature_names.joblib') # Ordered feature list (optional, if used)
25
+
26
+ except Exception as ex:
27
+ logging.error(f'Exception in loading joblib file: {ex}')
28
+
29
+ # List of required input features for prediction
30
+ required_features = [
31
+ 'Product_Weight',
32
+ 'Product_Sugar_Content',
33
+ 'Product_Allocated_Area',
34
+ 'Product_Type',
35
+ 'Product_MRP',
36
+ 'Store_Size',
37
+ 'Store_Location_City_Type',
38
+ 'Store_Type',
39
+ 'Age_Of_Store'
40
+ ]
41
+
42
+ # Default endpoint for status check
43
+ @app.get('/')
44
+ def home():
45
+ logging.debug("Accessed endpoint of Home page")
46
+ return "Welcome to Superkart Prediction system"
47
+
48
+ # Prediction endpoint to handle POST requests
49
+ @app.route('/predict', methods=['POST'])
50
+ def predict():
51
+ try:
52
+ # Read JSON payload
53
+ data = request.get_json()
54
+ logging.debug(f"Input received: {data}")
55
+
56
+ # If no data sent
57
+ if not data:
58
+ return jsonify({'Error': 'No data provided for prediction'})
59
+
60
+ # Check for missing features
61
+ if not all(feature in data for feature in required_features):
62
+ feature_missing = [feature for feature in required_features if feature not in data]
63
+ logging.error(f"Exception feature missing: {feature_missing}")
64
+ return jsonify({'Exception': f"Feature missing {feature_missing}"})
65
+
66
+ # Convert input JSON to DataFrame
67
+ feature_for_prediction = pd.DataFrame([{
68
+ 'Product_Weight': float(data['Product_Weight']),
69
+ 'Product_Sugar_Content': data['Product_Sugar_Content'],
70
+ 'Product_Allocated_Area': float(data['Product_Allocated_Area']),
71
+ 'Product_Type': data['Product_Type'],
72
+ 'Product_MRP': float(data['Product_MRP']),
73
+ 'Store_Size': data['Store_Size'],
74
+ 'Store_Location_City_Type': data['Store_Location_City_Type'],
75
+ 'Store_Type': data['Store_Type'],
76
+ 'Age_Of_Store': float(data['Age_Of_Store'])
77
+ }], columns=required_features)
78
+
79
+ # Transform input using the preprocessing pipeline
80
+ features_scaled = pipeline.transform(feature_for_prediction)
81
+ logging.debug(f"Features scaled: {features_scaled}")
82
+
83
+ # Predict using both models
84
+ prediction_gradboost = model_gradboost.predict(features_scaled)
85
+ prediction_randfrst = model_rndmfrst.predict(features_scaled)
86
+
87
+ # Log predictions
88
+ logging.debug(f"Prediction gradboost: {prediction_gradboost}")
89
+ logging.debug(f"Prediction randmfrst: {prediction_randfrst}")
90
+
91
+ # Return predictions as JSON
92
+ return jsonify({
93
+ 'gradientBoosting': float(prediction_gradboost[0]),
94
+ 'randomForest': float(prediction_randfrst[0])
95
+ })
96
+
97
+ except Exception as ex:
98
+ logging.error(f'Exception: {ex}')
99
+ return jsonify({'Exception': str(ex)})
100
+
101
+ # Run the Flask app on port 7860 (as required by Hugging Face Docker Spaces)
102
+ if __name__ == '__main__':
103
+ app.run(host='0.0.0.0', port=7860, debug=False)
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
+ flask==2.3.3
7
+ gunicorn==20.1.0
8
+ requests==2.28.1
9
+ uvicorn[standard]
10
+ streamlit==1.43.2
11
+ flask-cors==4.0.1