aakash200 commited on
Commit
bbd2db7
·
verified ·
1 Parent(s): b542883

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +58 -58
app.py CHANGED
@@ -1,58 +1,58 @@
1
- # Import necessary libraries
2
- import numpy as np
3
- import joblib # For loading the serialized model
4
- import pandas as pd # For data manipulation
5
- from flask import Flask, request, jsonify # For creating the Flask API
6
-
7
- # Import welcome message separately
8
- from backend_files.routes import welcome_message
9
-
10
- # Initialize the Flask application for Aakash
11
- superkart_sales_predictor_api = Flask("Superkart Sales Predictor - Aakash")
12
-
13
- # Load the trained machine learning model for Aakash
14
- model = joblib.load("backend_files/superkart_model_prediction_model_v1_0.joblib") # <- updated filename
15
-
16
- # Define a route for the home page (GET request)
17
- @superkart_sales_predictor_api.get('/')
18
- def home():
19
- return welcome_message()
20
-
21
- # Define an endpoint for single product prediction (POST request)
22
- @superkart_sales_predictor_api.post('/v1/superkart')
23
- def predict_sales_total():
24
- product_data = request.get_json()
25
-
26
- sample = {
27
- 'Product Type': product_data['Product Type'],
28
- 'Product ID': product_data['Product ID'],
29
- 'Product Weight': product_data['Product Weight'],
30
- 'Product Sugar Content': product_data['Product Sugar Content'],
31
- 'Product Allocated Area': product_data['Product Allocated Area'],
32
- 'Product MRP': product_data['Product MRP'],
33
- 'Store ID': product_data['Store ID'],
34
- 'Store Establishment Year': product_data['Store Establishment Year'],
35
- 'Store Size': product_data['Store Size'],
36
- 'Store Location': product_data['Store Location'],
37
- 'City Size': product_data['City Size'],
38
- 'Store Type': product_data['Store Type']
39
- }
40
-
41
- input_data = pd.DataFrame([sample])
42
- predicted_sales_total = round(float(model.predict(input_data)[0]), 2)
43
- return jsonify({'Predicted Product Store Sales Total': predicted_sales_total})
44
-
45
-
46
- # Define an endpoint for batch prediction (POST request)
47
- @superkart_sales_predictor_api.post('/v1/superkartbatch')
48
- def predict_sales_total_batch():
49
- file = request.files['file']
50
- input_data = pd.read_csv(file)
51
- predicted_sales = model.predict(input_data).tolist()
52
- product_ids = input_data['Product ID'].tolist()
53
- output_dict = dict(zip(product_ids, [round(float(s), 2) for s in predicted_sales]))
54
- return jsonify(output_dict)
55
-
56
- # Run the Flask application
57
- if __name__ == '__main__':
58
- superkart_sales_predictor_api.run(debug=True)
 
1
+ from flask import Flask, request, jsonify
2
+ import joblib
3
+ import pandas as pd
4
+
5
+ app = Flask(__name__)
6
+
7
+ # Load the model once
8
+ model = joblib.load("model.pkl")
9
+
10
+ # Day mapping (just an example)
11
+ day_mapping = {
12
+ "Monday": 0, "Tuesday": 1, "Wednesday": 2,
13
+ "Thursday": 3, "Friday": 4, "Saturday": 5, "Sunday": 6
14
+ }
15
+
16
+ @app.route("/predict", methods=["POST"])
17
+ def predict():
18
+ data = request.get_json()
19
+
20
+ # Prepare input for prediction
21
+ features = [[
22
+ data["store_id"],
23
+ data["item_id"],
24
+ data["item_price"],
25
+ data["promotion"],
26
+ data["holiday"],
27
+ day_mapping.get(data["day_of_week"], 0)
28
+ ]]
29
+
30
+ prediction = model.predict(features)[0]
31
+
32
+ return jsonify({"predicted_sales": round(prediction)})
33
+
34
+ @app.route("/predict_batch", methods=["POST"])
35
+ def predict_batch():
36
+ file = request.files.get("file")
37
+ if not file:
38
+ return jsonify({"error": "No file uploaded"}), 400
39
+
40
+ df = pd.read_csv(file)
41
+
42
+ # Assume same preprocessing
43
+ df["promotion"] = df["promotion"].map({"Yes": 1, "No": 0})
44
+ df["holiday"] = df["holiday"].map({"Yes": 1, "No": 0})
45
+ df["day_of_week"] = df["day_of_week"].map(day_mapping)
46
+
47
+ features = df[["store_id", "item_id", "item_price", "promotion", "holiday", "day_of_week"]]
48
+ predictions = model.predict(features)
49
+
50
+ df["predicted_sales"] = predictions.round().astype(int)
51
+ return df.to_dict(orient="records")
52
+
53
+ @app.route("/")
54
+ def root():
55
+ return "SuperKart Backend Running"
56
+
57
+ if __name__ == "__main__":
58
+ app.run(host="0.0.0.0", port=8000)