12ongo commited on
Commit
6f29323
·
verified ·
1 Parent(s): 162b6bd

Upload folder using huggingface_hub

Browse files
Files changed (2) hide show
  1. app.py +48 -61
  2. requirements.txt +0 -8
app.py CHANGED
@@ -1,61 +1,48 @@
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("sales_predictor.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 product prediction (POST request)
23
- @sales_predictor_api.post('/v1/predict')
24
- def predict_sales():
25
- """
26
- This function handles POST requests to the '/v1/predict' endpoint.
27
- It expects a JSON payload containing product and store details and returns
28
- the predicted sales as a JSON response.
29
- """
30
- # Get the JSON data from the request body
31
- product_store_data = request.get_json()
32
-
33
- # Extract relevant features from the JSON data
34
- sample = {
35
- 'Product_Weight': product_store_data['Product_Weight'],
36
- 'Product_Sugar_Content': product_store_data['Product_Sugar_Content'],
37
- 'Product_Allocated_Area': product_store_data['Product_Allocated_Area'],
38
- 'Product_Type': product_store_data['Product_Type'],
39
- 'Product_MRP': product_store_data['Product_MRP'],
40
- 'Store_Establishment_Year_Before_2005': product_store_data['Store_Establishment_Year_Before_2005'],
41
- 'Store_Size': product_store_data['Store_Size'],
42
- 'Store_Location_City_Type': product_store_data['Store_Location_City_Type'],
43
- 'Store_Type': product_store_data['Store_Type']
44
- }
45
-
46
- # Convert the extracted data into a Pandas DataFrame
47
- input_data = pd.DataFrame([sample])
48
-
49
- # Make prediction
50
- predicted_sales = model.predict(input_data)[0]
51
-
52
- # Convert predicted_sales to Python float
53
- predicted_sales = round(float(predicted_sales), 2)
54
-
55
-
56
- # Return the predicted sales
57
- return jsonify({'Predicted Sales Total (in dollars)': predicted_sales})
58
-
59
- # Run the Flask application in debug mode if this script is executed directly
60
- if __name__ == '__main__':
61
- sales_predictor_api.run(debug=True)
 
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("Predict Single Product Sales")
10
+
11
+ # Collect user input for product and store features
12
+ product_weight = st.number_input("Product Weight", min_value=0.0, value=10.0)
13
+ product_sugar_content = st.selectbox("Product Sugar Content", ["Low Sugar", "Regular", "No Sugar", "reg"]) # Include 'reg' based on EDA
14
+ product_allocated_area = st.number_input("Product Allocated Area", min_value=0.0, value=0.05)
15
+ product_type = st.selectbox("Product Type", ['Frozen Foods', 'Dairy', 'Canned', 'Baking Goods', 'Health and Hygiene', 'Snack Foods', 'Meat', 'Household', 'Hard Drinks', 'Fruits and Vegetables', 'Breads', 'Soft Drinks', 'Breakfast', 'Others', 'Starchy Foods', 'Seafood']) # Based on EDA
16
+ product_mrp = st.number_input("Product MRP", min_value=0.0, value=100.0)
17
+ store_establishment_year_before_2005 = st.selectbox("Store Established Before 2005?", ["True", "False"])
18
+ store_size = st.selectbox("Store Size", ["Small", "Medium", "High"])
19
+ store_location_city_type = st.selectbox("Store Location City Type", ["Tier 1", "Tier 2", "Tier 3"])
20
+ store_type = st.selectbox("Store Type", ['Supermarket Type2', 'Departmental Store', 'Supermarket Type1', 'Food Mart']) # Based on EDA
21
+
22
+
23
+ # Convert user input into a dictionary
24
+ input_data = {
25
+ 'Product_Weight': product_weight,
26
+ 'Product_Sugar_Content': product_sugar_content,
27
+ 'Product_Allocated_Area': product_allocated_area,
28
+ 'Product_Type': product_type,
29
+ 'Product_MRP': product_mrp,
30
+ 'Store_Establishment_Year_Before_2005': store_establishment_year_before_2005 == "True", # Convert string to boolean
31
+ 'Store_Size': store_size,
32
+ 'Store_Location_City_Type': store_location_city_type,
33
+ 'Store_Type': store_type
34
+ }
35
+
36
+ # Make prediction when the "Predict" button is clicked
37
+ if st.button("Predict Sales"):
38
+ # Replace <username>-<repo_id> with your actual Hugging Face username and backend space repo ID
39
+ backend_url = "https://<username>-<repo_id>.hf.space/v1/predict" # Update endpoint
40
+ response = requests.post(backend_url, json=input_data) # Send data as JSON
41
+ if response.status_code == 200:
42
+ prediction = response.json()['Predicted Sales Total (in dollars)']
43
+ st.success(f"Predicted Sales Total (in dollars): {prediction}")
44
+ else:
45
+ st.error(f"Error making prediction. Status code: {response.status_code}")
46
+ st.error(f"Response: {response.text}")
47
+
48
+ # Removed batch prediction section as it's commented out in the backend Flask app
 
 
 
 
 
 
 
 
 
 
 
 
 
requirements.txt CHANGED
@@ -1,11 +1,3 @@
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
 
1
  pandas==2.2.2
 
 
 
 
 
 
 
2
  requests==2.28.1
 
3
  streamlit==1.43.2