File size: 3,147 Bytes
5ba44a7
5fa6625
 
 
6861993
5fa6625
 
 
5ba44a7
 
5fa6625
5ba44a7
5fa6625
 
 
 
 
 
 
 
 
 
 
 
3870490
65e2502
5fa6625
 
 
 
5ba44a7
5fa6625
 
 
 
 
 
 
 
65e2502
5fa6625
 
5ba44a7
 
5fa6625
5ba44a7
5fa6625
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d4470ae
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

import streamlit as st
import pandas as pd
import requests # Import requests for API calls

# --- Configuration for Backend API --- #
# This URL should point to your deployed backend Hugging Face Space
BACKEND_API_URL = "https://lokiiparihar-superkart-api-t.hf.space" # Replace with your actual backend space URL
PREDICT_ENDPOINT = f"{BACKEND_API_URL}/v1/sales"

# Streamlit UI for Sales Prediction
st.title("SuperKart Sales Prediction App")
st.write("This tool predicts the sales revenue for a specific product in a SuperKart store.")

st.subheader("Enter the product and store details:")

# Collect user input for SuperKart sales prediction
product_id = st.selectbox("Product ID Prefix", ['FD', 'NC', 'DR'])
product_weight = st.number_input("Product Weight", min_value=0.0, value=12.0)
product_sugar_content = st.selectbox("Product Sugar Content", ['Low Sugar', 'Regular', 'No Sugar'])
product_allocated_area = st.number_input("Product Allocated Area", min_value=0.0, value=0.05)
product_type = st.selectbox("Product Type", ['Perishables', 'Non Perishables'])
product_mrp = st.number_input("Product MRP", min_value=0.0, value=150.0)
store_id = st.selectbox("Store ID", ['OUT004', 'OUT003', 'OUT001', 'OUT002'])
store_type = st.selectbox("Store Type", ["Grocery Store","Supermarket Type1","Supermarket Type2","Supermarket Type3"])
store_size = st.selectbox("Store Size", ['Medium', 'High', 'Small'])
store_location_city_type = st.selectbox("Store Location City Type", ['Tier 2', 'Tier 1', 'Tier 3'])
store_current_age = st.number_input("Store Current Age (Years)", min_value=0, value=15)

# Convert user input into a dictionary for JSON payload
input_data = {
    'Product_Id': product_id,
    '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_Type': store_type,
    'Store_Size': store_size,
    'Store_Location_City_Type': store_location_city_type,
    'Store_Current_Age': store_current_age
}

# Predict button
if st.button("Predict Sales"):
    try:
        # Make a POST request to the backend API
        response = requests.post(PREDICT_ENDPOINT, json=input_data)
        response.raise_for_status() # Raise an exception for HTTP errors (4xx or 5xx)

        predicted_sales = response.json().get('Predicted Sales')
        if predicted_sales is not None:
            st.write(f"The predicted sales revenue is ${predicted_sales:.2f}.")
        else:
            st.error("Prediction failed: Unexpected response from backend.")
            st.json(response.json()) # Display full response for debugging

    except requests.exceptions.ConnectionError:
        st.error("Could not connect to the backend API. Please ensure the backend Space is running and accessible.")
    except requests.exceptions.RequestException as e:
        st.error(f"An error occurred during the API request: {e}")
        st.text(response.text) # Display raw response text for debugging
    except Exception as e:
        st.error(f"An unexpected error occurred: {e}")