Lokiiparihar commited on
Commit
c65bdaa
·
verified ·
1 Parent(s): 2769f74

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +58 -0
app.py ADDED
@@ -0,0 +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
+ # Initialize the Flask application
8
+ sales_predictor_api = Flask("Superkart Sales Predictor")
9
+
10
+ # Load the trained machine learning model
11
+ model = joblib.load("superkart_pred_model_v1_0.joblib")
12
+
13
+ # Define a route for the home page (GET request)
14
+ @sales_predictor_api.get('/')
15
+ def home():
16
+ """
17
+ This function handles GET requests to the root URL ('/') of the API.
18
+ It returns a simple welcome message.
19
+ """
20
+ return "Welcome to the Superkart Sales Prediction API!"
21
+
22
+ # Define an endpoint for single property prediction (POST request)
23
+ @sales_predictor_api.post('/v1/sales')
24
+ def predict_sales():
25
+ """
26
+ This function handles POST requests to the '/v1/sales' endpoint.
27
+ It expects a JSON payload containing property details and returns
28
+ the predicted sales as a JSON response.
29
+ """
30
+ # Get the JSON data from the request body
31
+ property_data = request.get_json()
32
+
33
+
34
+ # Extract relevant features from the JSON data
35
+ sample = {
36
+ 'Product_Weight': property_data['Product_Weight'],
37
+ 'Product_Sugar_Content': property_data['Product_Sugar_Content'],
38
+ 'Product_Allocated_Area': property_data['Product_Allocated_Area'],
39
+ 'Product_Type': property_data['Product_Type'],
40
+ 'Product_MRP': property_data['Product_MRP'],
41
+ 'Store_Id': property_data['Store_Id'],
42
+ 'Store_Establishment_Year': property_data['Store_Establishment_Year'],
43
+ 'Store_Size': property_data['Store_Size'],
44
+ 'Store_Type': property_data['Store_Type']
45
+ }
46
+
47
+ # Convert the extracted data into a Pandas DataFrame
48
+ input_data = pd.DataFrame([sample])
49
+
50
+ # Make prediction (get log_price)
51
+ predicted_sales = model.predict(input_data)[0]
52
+
53
+ # Return the actual price
54
+ return jsonify({'Predicted Sales (in dollars)': predicted_sales})
55
+
56
+ # Run the Flask application in debug mode if this script is executed directly
57
+ if __name__ == '__main__':
58
+ sales_predictor_api.run(debug=True)