File size: 4,732 Bytes
9922101
0fb1b94
 
 
70d1f02
0fb1b94
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
70d1f02
0fb1b94
 
 
 
 
 
 
70d1f02
0fb1b94
 
70d1f02
0fb1b94
70d1f02
0fb1b94
 
70d1f02
0fb1b94
 
70d1f02
0fb1b94
 
 
70d1f02
0fb1b94
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
70d1f02
0fb1b94
70d1f02
0fb1b94
 
 
 
 
 
 
 
 
1
2
3
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import streamlit as st
import pandas as pd
import requests
import time

# Set the title of the Streamlit app
st.set_page_config(page_title="SuperKart Sales Forecast", layout="centered")
st.title("SuperKart Sales Prediction")

# Health check endpoint for Hugging Face
if st.query_params.get("healthcheck") == "true":
    st.write("OK")
    st.stop()

# Section for single prediction
st.subheader("Single Product-Store Prediction")

# Collect user input for product and store features
product_weight = st.number_input("Product Weight", min_value=0.1, max_value=20.0, value=12.65, step=0.1)
product_allocated_area = st.number_input("Product Allocated Area Ratio", min_value=0.0, max_value=1.0, value=0.07, step=0.01)
product_mrp = st.number_input("Product MRP", min_value=10, max_value=200, value=147, step=1)
product_sugar_content = st.selectbox("Product Sugar Content", ["Low Sugar", "Regular", "No Sugar"])
product_type = st.selectbox("Product Type", [
    "Fruits and Vegetables", "Household", "Snack Foods", "Meat", 
    "Hard Drinks", "Dairy", "Canned", "Soft Drinks", 
    "Health and Hygiene", "Baking Goods", "Bread", "Breakfast",
    "Frozen Foods", "Seafood", "Starchy Foods", "Others"
])
store_size = st.selectbox("Store Size", ["High", "Medium", "Low"])
store_location_city_type = st.selectbox("Store Location City Type", ["Tier 1", "Tier 2", "Tier 3"])
store_type = st.selectbox("Store Type", ["Departmental Store", "Supermarket Type 1", "Supermarket Type 2", "Food Mart"])
store_age = st.number_input("Store Age (years)", min_value=1, max_value=50, value=10, step=1)
product_category_code = st.number_input("Product Category Code", min_value=1, max_value=100, value=10, step=1)

# Convert user input into the format expected by the API
input_data = {
    'Product_Weight': float(product_weight),
    'Product_Allocated_Area': float(product_allocated_area),
    'Product_MRP': int(product_mrp),
    'Product_Sugar_Content': product_sugar_content,
    'Product_Type': product_type,
    'Store_Size': store_size,
    'Store_Location_City_Type': store_location_city_type,
    'Store_Type': store_type,
    'Store_Age': int(store_age),
    'Product_Category_Code': int(product_category_code)
}

# Make prediction when the "Predict" button is clicked
if st.button("Predict Sales"):
    try:
        # Replace with your actual backend URL
        backend_url = "https://simnid-superkartsalesbackend.hf.space/v1/predict"
        response = requests.post(backend_url, json=input_data)
        
        if response.status_code == 200:
            prediction = response.json()['Predicted Sales Total']
            st.success(f"Predicted Sales Total: ${prediction:,.2f}")
        else:
            st.error(f"Error making prediction. Status code: {response.status_code}")
            st.write(response.text)
    except Exception as e:
        st.error(f"Error connecting to API: {e}")

# Section for batch prediction
st.subheader("Batch Prediction")

# Allow users to upload a CSV file for batch prediction
uploaded_file = st.file_uploader("Upload CSV file for batch prediction", type=["csv"])

# Make batch prediction when the "Predict Batch" button is clicked
if uploaded_file is not None:
    if st.button("Predict Batch"):
        try:
            # Replace with your actual backend batch URL
            batch_backend_url = "https://simnid-superkartsalesbackend.hf.space/v1/predictbatch"
            response = requests.post(batch_backend_url, files={"file": uploaded_file})
            
            if response.status_code == 200:
                predictions = response.json()
                st.success("Batch predictions completed!")
                
                # Display predictions in a nice format
                predictions_df = pd.DataFrame.from_dict(predictions, orient='index', columns=['Predicted Sales'])
                st.dataframe(predictions_df)
                
                # Add download button
                csv = predictions_df.to_csv(index=True)
                st.download_button(
                    label="Download Predictions as CSV",
                    data=csv,
                    file_name="superkart_predictions.csv",
                    mime="text/csv"
                )
            else:
                st.error(f"Error making batch prediction. Status code: {response.status_code}")
        except Exception as e:
            st.error(f"Error connecting to API: {e}")

# Add some information about the app
st.sidebar.header("About")
st.sidebar.info("""
This app predicts sales totals for SuperKart products using a machine learning model.
- **Single Prediction**: Enter details for one product-store combination
- **Batch Prediction**: Upload a CSV file with multiple records
""")