VRS1503 commited on
Commit
cb17ba0
·
verified ·
1 Parent(s): 00390f0

Upload folder using huggingface_hub

Browse files
Files changed (2) hide show
  1. SuperKart_Sales_Prediction_Model.joblib +2 -2
  2. app.py +40 -37
SuperKart_Sales_Prediction_Model.joblib CHANGED
@@ -1,3 +1,3 @@
1
  version https://git-lfs.github.com/spec/v1
2
- oid sha256:703e82419781df3324bbcb71e8815ac468fc4cba261bb7a157e02ed502ea5fd1
3
- size 473604
 
1
  version https://git-lfs.github.com/spec/v1
2
+ oid sha256:9c298d589a2158eb513cb52191144518a2acab2cb0c04f1df14fca0f712fa4a1
3
+ size 4
app.py CHANGED
@@ -1,52 +1,55 @@
1
 
2
- import os
3
  import joblib
4
- from flask import Flask, request, jsonify
5
  import pandas as pd
6
  import numpy as np
7
- import warnings
8
- warnings.filterwarnings("ignore")
9
 
10
- # Define the path to the serialised model
11
- MODEL_PATH = "/content/Backend_files/SuperKart_Sales_Prediction_Model.joblib"
12
 
13
- # Load the trained model pipeline
14
- try:
15
- model_pipeline = joblib.load(MODEL_PATH)
16
- print(f"Model loaded successfully from {MODEL_PATH}")
17
- except Exception as e:
18
- model_pipeline = None
19
- print(f"Error loading model: {e}")
20
 
21
- # Initialize the Flask application
22
- app = Flask(__name__)
23
 
24
- # Define a route for the home page
25
- @app.route("/", methods=["GET"])
26
- def home():
27
- return "Welcome to the SuperKart Sales Prediction App!"
28
 
29
- # Define an endpoint for making predictions
30
- @app.route("/predict", methods=["POST"])
31
  def predict():
32
- if model_pipeline is None:
33
- return jsonify({"error": "Model not loaded"}), 500
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
34
 
35
- try:
36
- # Get JSON data from the request
37
- data = request.get_json()
38
- if not data:
39
- return jsonify({"error": "No data provided"}), 400
40
 
41
- # Extract features from the JSON data
42
- input_df = pd.DataFrame([data])
 
 
43
 
44
- prediction = model_pipeline.predict(input_df)
45
- return jsonify({"prediction": prediction.tolist()})
 
46
 
47
- except Exception as e:
48
- return jsonify({"error": f'Error during prediction: {e}'}), 500
49
 
50
- if __name__ == "__main__": # Correct indentation
51
- port = int(os.environ.get("PORT", 5000))
52
- app.run(host="0.0.0.0", port=5000, debug=True)
 
1
 
 
2
  import joblib
 
3
  import pandas as pd
4
  import numpy as np
5
+ from flask import Flask, request, jsonify
 
6
 
7
+ app = Flask(__name__)
 
8
 
9
+ # Define the path to the serialized model
10
+ MODEL_PATH = "/content/Backend_files/SuperKart_Sales_Prediction_Model.joblib"
 
 
 
 
 
11
 
12
+ #from joblib import load
 
13
 
14
+ model_pipeline = load("/content/Backend_files/SuperKart_Sales_Prediction_Model.joblib")
 
 
 
15
 
16
+ @app.route('/predict', methods=['POST'])
 
17
  def predict():
18
+ data = request.json
19
+ features = pd.DataFrame([data])
20
+
21
+ prediction = model_pipeline.predict(features)
22
+ return jsonify({'prediction': prediction.tolist()})
23
+
24
+
25
+ # Get JSON request data
26
+ data = request.json
27
+ sample_df = pd.DataFrame([data]) # Convert JSON to DataFrame
28
+
29
+ # Feature Engineering (Ensure required columns are present)
30
+ sample_df['Store_Age'] = 2025 - sample_df['Store_Establishment_Year']
31
+ sample_df['Is_Tier1'] = sample_df['Store_Location_City_Type'].apply(lambda x: 1 if x == "Tier 1" else 0)
32
+ sample_df['MRP_Category'] = pd.cut(sample_df['Product_MRP'], bins=[0, 100, 200, 300], labels=['Low', 'Medium', 'High'])
33
+
34
+ # Ensure categorical features match model expectations
35
+ categorical_cols = ['Product_Sugar_Content', 'Product_Type', 'Store_Size', 'Store_Location_City_Type', 'Store_Type']
36
+ for col in categorical_cols:
37
+ if col in sample_df.columns:
38
+ sample_df[col] = sample_df[col].astype('category')
39
 
40
+ # Ensure feature names match the trained model
41
+ required_features = model_pipeline.feature_names_in_
42
+ missing_features = set(required_features) - set(sample_df.columns)
 
 
43
 
44
+ if missing_features:
45
+ print(f"Warning: Missing features detected: {missing_features}")
46
+ for feature in missing_features:
47
+ sample_df[feature] = 0 # Assign default values for missing features
48
 
49
+ # Make a prediction
50
+ prediction = model_pipeline.predict(sample_df[required_features])[0]
51
+ return jsonify({'prediction': float(prediction)})
52
 
53
+ if __name__ == '__main__':
54
+ app.run(debug=True)
55