Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,72 +1,58 @@
|
|
| 1 |
-
|
| 2 |
-
import
|
| 3 |
-
import
|
| 4 |
-
import
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
"Store_Location_City_Type": meta['city_type'],
|
| 60 |
-
"Store_Type": meta['store_type'],
|
| 61 |
-
}])
|
| 62 |
-
|
| 63 |
-
# Prediction
|
| 64 |
-
if st.button("Predict"):
|
| 65 |
-
if not product_id_valid:
|
| 66 |
-
st.error("Please fix the Product ID before proceeding.")
|
| 67 |
-
else:
|
| 68 |
-
try:
|
| 69 |
-
prediction = model.predict(input_data)[0]
|
| 70 |
-
st.success(f"Predicted Product Sales: {prediction:.2f}")
|
| 71 |
-
except Exception as e:
|
| 72 |
-
st.error(f"Prediction failed: {e}")
|
|
|
|
| 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)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|