LalithaShiva commited on
Commit
7a46ce7
·
verified ·
1 Parent(s): 26a990b

Upload folder using huggingface_hub

Browse files
Files changed (2) hide show
  1. app.py +67 -45
  2. requirements.txt +9 -0
app.py CHANGED
@@ -1,46 +1,68 @@
1
- import streamlit as st
2
- import pandas as pd
3
- import requests
4
-
5
- # Set the title of the Streamlit app
6
- st.title("SuperKart sales Prediction")
7
-
8
- # Section for online prediction
9
- st.subheader("Online Prediction")
10
-
11
- # Collect user input for Sales features
12
- # Collect user input
13
- product_Weight=st.number_input("Product_Weight", min_value=1, value=30.00)
14
- product_Sugar_Content=st.selectbox("Product Sugar Content", ["Low Sugar", "No Sugar", "Regular"])
15
- product_Allocated_Area=st.number_input("Product Allocate Area", min_value=0.001, value=0.09)
16
- product_Type=st.selectbox("Product Type", ["meat", "snack foods", "hard drinks", "dairy", "canned", "soft drinks", "health and hygiene", "baking goods", "bread", "breakfast", "frozen foods", "fruits and vegetables", "household", "seafood", "starchy foods", "others"])
17
- product_MRP=st.number_input("Product MRP", min_value=1, value=30.00)
18
- store_Id=st.selectbox("Store ID", ["OUT001", "OUT002", "OUT003", "OUT004"])
19
- store_Establishment_Year=st.number_input("Store Establishment year", min_value=1987, value=2009)
20
- store_Size=st.selectbox("Store Size", ["High", "Medium", "Small"])
21
- store_Location_City_Type=st.selectbox("Store location City", ["Tier1", "Tier2", "Tier3"])
22
- store_Type=st.selectbox("Store Type", ["Departmental Store", "Supermarket Type 1", "Supermarket Type 2", "Food Mart"])
23
-
24
- # Convert user input into a DataFrame
25
- input_data = pd.DataFrame([{
26
- 'product_Weight': product_Weight,
27
- 'product_Sugar_Content': product_Sugar_Content,
28
- 'product_Allocated_Area': product_Allocated_Area,
29
- 'product_Type': product_Type,
30
- 'product_MRP': product_MRP,
31
- 'store_Id': store_Id,
32
- 'store_Establishment_Year': store_Establishment_Year,
33
- 'store_Size': store_Size,
34
- 'store_Location_City_Type': store_Location_City_Type,
35
- 'store_Type': store_Type
36
- }])
37
-
38
- # Make prediction when the "Predict" button is clicked
39
- if st.button("Predict"):
40
- response = requests.post("https://LalithaShiva/skproject.hf.space/v1/sksales", json=input_data.to_dict(orient='records')[0]) # Send data to Flask API
41
- if response.status_code == 200:
42
- prediction = response.json()['Predicted Price (in dollars)']
43
- st.success(f"Predicted Rental Price (in dollars): {prediction}")
44
- else:
45
- st.error("Error making prediction.")
46
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ superkart_sales_predictor_api = Flask("Superkart sales Predictor")
9
+
10
+ # Load the trained machine learning model
11
+ model = joblib.load("superkart_price_prediction_model_v1_0.joblib")
12
+
13
+ # Define a route for the home page (GET request)
14
+ @superkart_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 sales prediction (POST request)
23
+ @superkart_sales_predictor_api.post('/v1/sksales')
24
+ def predict_sksales_price():
25
+ """
26
+ This function handles POST requests to the '/v1/sksales' endpoint.
27
+ It expects a JSON payload containing property details and returns
28
+ the predicted rental price as a JSON response.
29
+ """
30
+ # Get the JSON data from the request body
31
+ sksales_data = request.get_json()
32
+
33
+ # Extract relevant features from the JSON data
34
+ sample = {
35
+ 'product_Weight': product_Weight,
36
+ 'product_Sugar_Content': product_Sugar_Content,
37
+ 'product_Allocated_Area': product_Allocated_Area,
38
+ 'product_Type': product_Type,
39
+ 'product_MRP': product_MRP,
40
+ 'store_Id': store_Id,
41
+ 'store_Establishment_Year': store_Establishment_Year,
42
+ 'store_Size': store_Size,
43
+ 'store_Location_City_Type': store_Location_City_Type,
44
+ 'store_Type': 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_price = model.predict(input_data)[0]
52
+
53
+ # Calculate actual price
54
+ predicted_price = np.exp(predicted_sales_price)
55
+
56
+ # Convert predicted_price to Python float
57
+ predicted_price = round(float(predicted_price), 2)
58
+ # The conversion above is needed as we convert the model prediction (log price) to actual price using np.exp, which returns predictions as NumPy float32 values.
59
+ # When we send this value directly within a JSON response, Flask's jsonify function encounters a datatype error
60
+
61
+ # Return the actual price
62
+ return jsonify({'Predicted sales price (in dollars)': predicted_price})
63
+
64
+
65
+
66
+ # Run the Flask application in debug mode if this script is executed directly
67
+ if __name__ == '__main__':
68
+ superkart_sales_predictor_api.run(debug=True)
requirements.txt CHANGED
@@ -1,2 +1,11 @@
1
  pandas==2.2.2
 
 
 
 
 
 
 
2
  requests==2.28.1
 
 
 
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
+ Werkzeug==2.2.2
7
+ flask==2.2.2
8
+ gunicorn==20.1.0
9
  requests==2.28.1
10
+ uvicorn[standard]
11
+ streamlit==1.43.2