| import streamlit as st |
| import requests |
| import json |
|
|
| |
| st.title("SuperKart Sales Prediction") |
|
|
| |
| with st.form("prediction_form"): |
| st.header("Enter Product and Store Details") |
|
|
| |
| product_weight = st.number_input( |
| "Product Weight (e.g., 9.57 to 16.54)", |
| min_value=0.0, |
| max_value=50.0, |
| value=13.5, |
| step=0.01 |
| ) |
| product_allocated_area = st.number_input( |
| "Product Allocated Area (ratio, e.g., 0.01 to 0.144)", |
| min_value=0.0, |
| max_value=1.0, |
| value=0.075, |
| step=0.001 |
| ) |
| product_mrp = st.number_input( |
| "Product MRP (e.g., 117.08 to 186.31)", |
| min_value=0.0, |
| max_value=500.0, |
| value=155.0, |
| step=0.01 |
| ) |
| store_establishment_year = st.number_input( |
| "Store Establishment Year (e.g., 1987 to 2009)", |
| min_value=1900, |
| max_value=2025, |
| value=2009, |
| step=1, |
| format="%d" |
| ) |
|
|
| |
| product_sugar_content = st.selectbox( |
| "Product Sugar Content", |
| ["Low Sugar", "Regular", "No Sugar"], |
| index=0 |
| ) |
| 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" |
| ], |
| index=1 |
| ) |
| store_size = st.selectbox( |
| "Store Size", |
| ["Low", "Medium", "High"], |
| index=1 |
| ) |
| store_location_city_type = st.selectbox( |
| "Store Location City Type", |
| ["Tier 1", "Tier 2", "Tier 3"], |
| index=1 |
| ) |
| store_type = st.selectbox( |
| "Store Type", |
| ["Departmental Store", "Supermarket Type 1", "Supermarket Type 2", "Food Mart"], |
| index=2 |
| ) |
|
|
| |
| submitted = st.form_submit_button("Predict Sales") |
|
|
| |
| if submitted: |
| |
| payload = { |
| "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_Establishment_Year": int(store_establishment_year), |
| "Store_Size": store_size, |
| "Store_Location_City_Type": store_location_city_type, |
| "Store_Type": store_type |
| } |
|
|
| |
| model_url = "https://sahilsingla-SuperKartPredictionBackend.hf.space/v1/sales" |
| try: |
| response = requests.post(model_url, json=payload) |
| response.raise_for_status() |
| result = response.json() |
| prediction = result.get("prediction", None) |
|
|
| if prediction is not None: |
| st.success(f"Predicted Sales: ${float(prediction):.2f}") |
| else: |
| st.error("No prediction returned from API") |
| except requests.exceptions.RequestException as e: |
| st.error(f"Error contacting the API: {e}") |
| except ValueError: |
| st.error("Invalid response format from API") |
|
|
| |
| st.subheader("Batch Prediction") |
|
|
| st.write("Upload a CSV file with columns: Product_Weight, Product_Sugar_Content, Product_Allocated_Area, Product_Type, Product_MRP, Store_Establishment_Year, Store_Size, Store_Location_City_Type, Store_Type") |
|
|
| |
| uploaded_file = st.file_uploader("Upload CSV file for batch prediction", type=["csv"]) |
|
|
| if uploaded_file is not None: |
| if st.button("Predict Batch"): |
| try: |
| |
| files = {"file": (uploaded_file.name, uploaded_file, "text/csv")} |
|
|
| |
| response = requests.post( |
| "https://sahilsingla-SuperKartPredictionBackend.hf.space/v1/salesbatch", |
| files=files |
| ) |
|
|
| |
| if response.status_code == 200: |
| predictions = response.json() |
| st.success("Batch predictions completed!") |
| st.write(predictions) |
| else: |
| st.error(f"Error: {response.status_code}") |
| st.error(response.text) |
|
|
| except Exception as e: |
| st.error("An unexpected error occurred during prediction.") |
| st.exception(e) |
|
|