File size: 4,459 Bytes
2d043d7
61a977e
2d043d7
61a977e
2d043d7
 
61a977e
2d043d7
 
61a977e
2d043d7
 
 
 
 
 
 
 
 
 
 
 
61a977e
2d043d7
 
 
61a977e
2d043d7
 
61a977e
2d043d7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
61a977e
 
2d043d7
 
 
 
 
61a977e
2d043d7
 
 
 
 
 
 
 
 
 
61a977e
 
2d043d7
 
61a977e
2d043d7
 
61a977e
2d043d7
 
 
 
 
 
61a977e
2d043d7
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
import pandas as pd
import requests

# Set the title of the Streamlit app
st.title("SuperKart Product Store Sales Prediction")

# Section for online prediction
st.subheader("Predict Single Product Sales")

# Collect user input for product and store features
product_id = st.text_input("Product ID")
product_weight = st.number_input("Product Weight", min_value=0.0, value=10.0)
product_sugar_content = st.selectbox("Product Sugar Content", ["Low Sugar", "Regular", "No Sugar", "low sugar"])
product_allocated_area = st.number_input("Product Allocated Area", min_value=0.0, value=0.05)
product_type = st.selectbox("Product Type", ['Frozen Foods', 'Dairy', 'Canned', 'Baking Goods', 'Health and Hygiene', 'Snack Foods', 'Meat', 'Soft Drinks', 'Breads', 'Hard Drinks', 'Others', 'Starchy Foods', 'Breakfast', 'Seafood', 'Fruits and Vegetables', 'Household'])
product_mrp = st.number_input("Product MRP", min_value=0.0, value=100.0)
store_id = st.selectbox("Store ID", ['OUT004', 'OUT003', 'OUT001', 'OUT002'])
store_establishment_year = st.number_input("Store Establishment Year", min_value=1900, value=2000)
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_type = st.selectbox("Store Type", ['Supermarket Type2', 'Departmental Store', 'Supermarket Type1', 'Food Mart'])

# Calculate engineered features (ensure these match the backend)
current_year = pd.to_datetime('now').year
store_age = current_year - store_establishment_year

perishables = ['Fruits and Vegetables', 'Dairy', 'Meat', 'Seafood', 'Breads', 'Breakfast']
product_category_type = 'Perishables' if product_type in perishables else 'Non Perishables'

# Create a dictionary with the input data
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_Establishment_Year': store_establishment_year,
    'Store_Size': store_size,
    'Store_Location_City_Type': store_location_city_type,
    'Store_Type': store_type,
    'Product_Category_from_ID': product_id[:2] if product_id else '', # Assuming product_id is not empty
    'Store_Age': store_age,
    'Product_Category_Type': product_category_type
}


# Make prediction when the "Predict" button is clicked
if st.button("Predict"):
    # Replace with the actual URL of your deployed backend Hugging Face Space
    backend_url = "https://Garg06-superkart.hf.space" # Example URL format
    predict_url = f"{backend_url}/v1/product"

    try:
        response = requests.post(predict_url, json=input_data)
        if response.status_code == 200:
            prediction = response.json()['Predicted_Product_Store_Sales_Total']
            st.success(f"Predicted Product Store Sales Total: {prediction:.2f}")
        else:
            st.error(f"Error making prediction. Status code: {response.status_code}")
            st.error(f"Response: {response.text}")
    except requests.exceptions.RequestException as e:
        st.error(f"Error connecting to the backend: {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"):
        # Replace with the actual URL of your deployed backend Hugging Face Space
        backend_url = "https://Garg06-superkart.hf.space" # Example URL format
        predict_batch_url = f"{backend_url}/v1/productbatch"

        try:
            response = requests.post(predict_batch_url, files={"file": uploaded_file})  # Send file to Flask API
            if response.status_code == 200:
                predictions = response.json()
                st.success("Batch predictions completed!")
                st.write(predictions)  # Display the predictions
            else:
                st.error(f"Error making batch prediction. Status code: {response.status_code}")
                st.error(f"Response: {response.text}")
        except requests.exceptions.RequestException as e:
            st.error(f"Error connecting to the backend: {e}")