File size: 4,060 Bytes
9616ff9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
108
109
110
111
112

import streamlit as st
import requests
import json
import pandas as pd

# --- Page Configuration ---
st.set_page_config(page_title='SuperKart Sales Revenue Forecaster', layout='wide')
st.title('SuperKart Sales Revenue Forecaster')

# --- Backend API URL ---
# Make sure to update this with your deployed Flask API URL
# During local development, it might be 'http://localhost:5000/forecast_revenue'
# For Hugging Face Space, it will be the URL of your deployed backend space, e.g., 'https://<your-space-id>.hf.space/forecast_revenue'
BACKEND_URL = 'https://sagarathf-superkart.hf.space/v1/forecastrevenue' # API URL for POST

st.markdown("""
    This application predicts the total sales revenue for a product in a given store.
    Please fill in the details below to get a sales forecast.
""")

# --- Input Widgets for Features ---

st.subheader('Product Details')
col1, col2, col3 = st.columns(3)

with col1:
    product_weight = st.number_input('Product Weight (kg)', min_value=0.1, max_value=50.0, value=10.0, step=0.1)
    product_sugar_content = st.selectbox(
        'Product Sugar Content',
        ['Low Sugar', 'Regular', 'No Sugar', 'Others']
    )

with col2:
    product_allocated_area = st.number_input('Product Allocated Area Ratio', min_value=0.001, max_value=0.5, value=0.05, step=0.001, format="%.3f")
    product_type = st.selectbox(
        'Product Type',
        ['Dairy', 'Soft Drinks', 'Meat', 'Fruits and Vegetables', 'Household', 
         'Baking Goods', 'Snack Foods', 'Frozen Foods', 'Breakfast', 
         'Health and Hygiene', 'Hard Drinks', 'Canned', 'Breads', 
         'Starchy Foods', 'Others', 'Seafood']
    )

with col3:
    product_mrp = st.number_input('Product MRP (Max. Retail Price)', min_value=10.0, max_value=500.0, value=150.0, step=1.0)

st.subheader('Store Details')
col4, col5, col6 = st.columns(3)

with col4:
    store_id = st.selectbox(
        'Store ID',
        ['OUT003', 'OUT002', 'OUT001', 'OUT004']
    )
    store_establishment_year = st.number_input('Store Establishment Year', min_value=1950, max_value=2024, value=2000, step=1)

with col5:
    store_size = st.selectbox(
        'Store Size',
        ['Medium', 'High', 'Small']
    )
    store_location_city_type = st.selectbox(
        'Store Location City Type',
        ['Tier 1', 'Tier 2', 'Tier 3']
    )

with col6:
    store_type = st.selectbox(
        'Store Type',
        ['Departmental Store', 'Supermarket Type1', 'Food Mart', 'Supermarket Type2']
    )


# --- Prediction Button and Logic ---
if st.button('Predict Sales Revenue'):
    # Collect input data into a dictionary
    input_data = {
        "Product_Weight": product_weight,
        "Product_Sugar_Content": product_sugar_content,
        "Product_Allocated_Area": product_allocated_area,
        "Product_Type": product_type,
        "Product_MRP": product_mrp,
        "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
    }

    # Display collected data (for debugging purposes)
    st.json(input_data)

    try:
        # Send POST request to the backend API
        response = requests.post(BACKEND_URL, json=input_data)

        # Check if the request was successful
        if response.status_code == 200:
            prediction_result = response.json()
            predicted_sales = prediction_result.get('predicted_sales')
            if predicted_sales is not None:
                st.success(f"Predicted Sales Revenue: &#8377;{predicted_sales:,.2f}")
            else:
                st.error("Prediction result not found in the API response.")
        else:
            st.error(f"Error from backend API: {response.status_code} - {response.text}")

    except requests.exceptions.ConnectionError:
        st.error("Could not connect to the backend API. Please ensure the API is running and the URL is correct.")
    except Exception as e:
        st.error(f"An unexpected error occurred: {e}")