Spaces:
Build error
Build error
Upload folder using huggingface_hub
Browse files
app.py
CHANGED
|
@@ -1,87 +1,70 @@
|
|
| 1 |
-
|
| 2 |
-
import
|
| 3 |
-
import
|
| 4 |
-
|
| 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 |
-
# Return JSON response
|
| 51 |
-
return jsonify({'Predicted Store Sales': predicted_sales})
|
| 52 |
-
|
| 53 |
-
# Define an endpoint for batch prediction (POST request)
|
| 54 |
-
@store_sales_api.post('/v1/salebatch')
|
| 55 |
-
def predict_store_sales_batch():
|
| 56 |
-
"""
|
| 57 |
-
Handles batch predictions for multiple stores.
|
| 58 |
-
Expects a CSV file with store details.
|
| 59 |
-
"""
|
| 60 |
-
# Get uploaded CSV file
|
| 61 |
-
file = request.files['file']
|
| 62 |
-
|
| 63 |
-
# Read CSV into DataFrame
|
| 64 |
-
input_data = pd.read_csv(file)
|
| 65 |
-
|
| 66 |
-
# Make predictions
|
| 67 |
-
predicted_sales = model.predict(input_data).tolist()
|
| 68 |
-
|
| 69 |
-
# Convert to float and round
|
| 70 |
-
predicted_sales = [round(float(sale), 2) for sale in predicted_sales]
|
| 71 |
-
|
| 72 |
-
# Use Store IDs if available
|
| 73 |
-
if 'Store_ID' in input_data.columns:
|
| 74 |
-
store_ids = input_data['Store_ID'].tolist()
|
| 75 |
else:
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
#
|
| 85 |
-
if
|
| 86 |
-
|
| 87 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 Store Sales Prediction")
|
| 7 |
+
|
| 8 |
+
# Section for online prediction
|
| 9 |
+
st.subheader("Online Prediction")
|
| 10 |
+
|
| 11 |
+
# Collect user input for store/product features
|
| 12 |
+
product_weight = st.number_input("Product Weight", min_value=0.0, value=1.0)
|
| 13 |
+
product_allocated_area = st.number_input("Product Allocated Area", min_value=0.0, value=10.0)
|
| 14 |
+
product_mrp = st.number_input("Product MRP", min_value=0.0, value=50.0)
|
| 15 |
+
store_establishment_year = st.number_input("Store Establishment Year", min_value=1900, max_value=2025, value=2015)
|
| 16 |
+
product_sugar_content = st.selectbox("Product Sugar Content", ["Low", "Medium", "High"])
|
| 17 |
+
product_type = st.selectbox("Product Type", ["Dairy", "Beverages", "Snacks", "Others"])
|
| 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", ["Type 1", "Type 2", "Type 3", "Type 4"])
|
| 21 |
+
store_id = st.text_input("Store Id", "S001")
|
| 22 |
+
|
| 23 |
+
# Feature engineering for Store_Age
|
| 24 |
+
store_age = 2025 - store_establishment_year
|
| 25 |
+
|
| 26 |
+
# Convert user input into a DataFrame
|
| 27 |
+
input_data = pd.DataFrame([{
|
| 28 |
+
'Product_Weight': product_weight,
|
| 29 |
+
'Product_Allocated_Area': product_allocated_area,
|
| 30 |
+
'Product_MRP': product_mrp,
|
| 31 |
+
'Store_Age': store_age,
|
| 32 |
+
'Product_Sugar_Content': product_sugar_content,
|
| 33 |
+
'Product_Type': product_type,
|
| 34 |
+
'Store_Size': store_size,
|
| 35 |
+
'Store_Location_City_Type': store_location_city_type,
|
| 36 |
+
'Store_Type': store_type,
|
| 37 |
+
'Store_Id': store_id
|
| 38 |
+
}])
|
| 39 |
+
|
| 40 |
+
# Make prediction when the "Predict" button is clicked
|
| 41 |
+
if st.button("Predict"):
|
| 42 |
+
response = requests.post(
|
| 43 |
+
"https://Disha252001-SuperKart-Frontend.hf.space/v1/sale",
|
| 44 |
+
json=input_data.to_dict(orient='records')[0]
|
| 45 |
+
)
|
| 46 |
+
if response.status_code == 200:
|
| 47 |
+
prediction = response.json()['Predicted Store Sales']
|
| 48 |
+
st.success(f"Predicted Store Sales: {prediction}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
else:
|
| 50 |
+
st.error("Error making prediction.")
|
| 51 |
+
|
| 52 |
+
# Section for batch prediction
|
| 53 |
+
st.subheader("Batch Prediction")
|
| 54 |
+
|
| 55 |
+
# Allow users to upload a CSV file for batch prediction
|
| 56 |
+
uploaded_file = st.file_uploader("Upload CSV file for batch prediction", type=["csv"])
|
| 57 |
+
|
| 58 |
+
# Make batch prediction when the "Predict Batch" button is clicked
|
| 59 |
+
if uploaded_file is not None:
|
| 60 |
+
if st.button("Predict Batch"):
|
| 61 |
+
response = requests.post(
|
| 62 |
+
"https://Disha252001-SuperKart-Frontend.hf.space/v1/salebatch",
|
| 63 |
+
files={"file": uploaded_file}
|
| 64 |
+
)
|
| 65 |
+
if response.status_code == 200:
|
| 66 |
+
predictions = response.json()
|
| 67 |
+
st.success("Batch predictions completed!")
|
| 68 |
+
st.write(predictions) # Display the predictions
|
| 69 |
+
else:
|
| 70 |
+
st.error("Error making batch prediction.")
|