File size: 4,336 Bytes
f8b6687
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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

import streamlit as st
import requests
import pandas as pd

# Title and description
st.set_page_config(page_title="SuperKart Store Sales Forecast", layout="centered")
st.title('πŸ“ˆ SuperKart Store Sales Forecast')
st.write('Enter product and store details to predict the sales total or upload a CSV for batch forecasting.')

# --- Online Prediction ---
st.header('πŸ›’ Product and Store Details (Single Forecast)')

col1, col2 = st.columns(2)

with col1:
    product_weight = st.number_input('Product Weight (kg)', min_value=0.0, format="%.2f")
    product_mrp = st.number_input('Product MRP (β‚Ή)', min_value=0.0, format="%.2f")
    product_sugar_content = st.selectbox('Product Sugar Content', ['Low Sugar', 'Regular', 'No Sugar'])
    product_allocated_area = st.number_input('Product Allocated Area (0.0 - 1.0)', min_value=0.0, max_value=1.0, format="%.4f")
    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'
    ])

with col2:
    store_id = st.selectbox('Store ID', ['OUT001', 'OUT002', 'OUT003', 'OUT004'])
    store_establishment_year = st.number_input('Store Establishment Year', min_value=1980, max_value=2025, step=1)
    store_size = st.selectbox('Store Size', ['High', 'Medium', 'Small'])
    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 Type1', 'Supermarket Type2', 'Food Mart'])

# Predict Button
if st.button('πŸ“Š Predict Sales Forecast'):
    input_data = {
        'Product_Weight': product_weight,
        'Product_MRP': product_mrp,
        'Product_Sugar_Content': product_sugar_content,
        'Product_Allocated_Area': product_allocated_area,
        'Product_Type': product_type,
        'Store_Id': store_id,
        'Store_Establishment_Year': store_establishment_year,
        'Store_Size': store_size,
        'Store_Location_City_Type': store_location_city_type,
        'Store_Type': store_type
    }

    api_url = 'https://Yash0204-API-SuperKart-Backend.hf.space/v1/sales'

    try:
        response = requests.post(api_url, json=input_data)

        if response.status_code == 200:
            prediction_result = response.json()
            predicted_sales = prediction_result.get('predicted_product_store_sales_total')

            if predicted_sales is not None:
                st.success(f'βœ… Predicted Product Store Sales Total: β‚Ή{predicted_sales:.2f}')
            else:
                st.error('❌ Prediction not found in the response.')

        elif response.status_code == 400:
            st.error(f'❌ API Error: Invalid input data. Details: {response.json().get("error", "Unknown error")}')
        else:
            st.error(f'❌ API Error: Status Code {response.status_code}. Details: {response.text}')

    except requests.exceptions.RequestException as e:
        st.error(f'❌ Connection Error: {e}')
    except Exception as e:
        st.error(f'❌ Unexpected Error: {e}')

st.markdown("---")

# --- Batch Forecast ---
st.header("πŸ“ Batch Forecast using CSV Upload")
uploaded_file = st.file_uploader("Upload a CSV file containing product/store data", type=["csv"])

if uploaded_file is not None:
    if st.button("πŸ“₯ Predict Batch Sales Forecast"):
        api_batch_url = "https://Yash0204-API-SuperKart-Backend.hf.space/v1/salesbatch"

        try:
            response = requests.post(api_batch_url, files={"file": uploaded_file})

            if response.status_code == 200:
                result = response.json()
                df_result = pd.DataFrame(result)
                st.success("βœ… Batch predictions completed.")
                st.dataframe(df_result)
            else:
                st.error(f'❌ Batch Prediction Error: {response.status_code} - {response.text}')

        except requests.exceptions.RequestException as e:
            st.error(f'❌ Connection Error: {e}')
        except Exception as e:
            st.error(f'❌ Unexpected Error: {e}')

st.info("ℹ️ Please ensure your backend API supports `/v1/sales` and `/v1/salesbatch` endpoints.")