Spaces:
Sleeping
Sleeping
| 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.") | |