import streamlit as st import requests import pandas as pd # Used for displaying data import json # For parsing JSON input # --- Configuration --- # BACKEND_SINGLE_PREDICT_URL = "https://RoopaST-Sales-Prediction-Backend-1.hf.space/predict" BACKEND_BATCH_PREDICT_URL = "https://RoopaST-Sales-Prediction-Backend-1.hf.space/predict_batch" # --- Streamlit UI --- # st.set_page_config(page_title="SuperKart Sales Predictor", layout="wide") st.title("🛒 SuperKart Sales Predictor") st.write("Enter product and store details to predict sales revenue.") # --- Single Prediction Form --- st.header("Single Prediction") with st.form("single_sales_prediction_form"): st.subheader("Product Details") product_id = st.text_input("Product ID", "FD6114", help="e.g., FD6114, NC1180") product_weight = st.number_input("Product Weight (kg)", min_value=4.0, max_value=22.0, value=12.66, step=0.01) product_mrp = st.number_input("Product MRP (₹)", min_value=30.0, max_value=270.0, value=117.08, step=0.01) product_allocated_area = st.number_input("Product Allocated Area Ratio", min_value=0.0, max_value=0.2, value=0.027, step=0.001) product_sugar_content = st.selectbox("Product Sugar Content", ['Low Sugar', 'Regular', 'No Sugar'], index=0, key='single_sugar') product_type = st.selectbox("Product Type", [ 'Baking Goods', 'Breads', 'Breakfast', 'Canned', 'Dairy', 'Frozen Foods', 'Fruits and Vegetables', 'Hard Drinks', 'Health and Hygiene', 'Household', 'Meat', 'Others', 'Seafood', 'Snack Foods', 'Soft Drinks', 'Starchy Foods' ], index=6, key='single_type') # Fruits and Vegetables as default st.subheader("Store Details") store_id = st.text_input("Store ID", "OUT004", help="e.g., OUT004, OUT003", key='single_store_id') store_establishment_year = st.number_input("Store Establishment Year", min_value=1985, max_value=2009, value=2009, step=1, key='single_est_year') store_size = st.selectbox("Store Size", ['High', 'Medium', 'Small'], index=1, key='single_store_size') store_location_city_type = st.selectbox("Store Location City Type", ['Tier 1', 'Tier 2', 'Tier 3'], index=1, key='single_city_type') store_type = st.selectbox("Store Type", ['Departmental Store', 'Food Mart', 'Supermarket Type1', 'Supermarket Type2'], index=3, key='single_store_type') submitted_single = st.form_submit_button("Predict Single Sale") # --- Single Prediction Logic --- if submitted_single: input_data_single = { "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 } st.subheader("Input Data Provided for Single Prediction:") st.json(input_data_single) try: response = requests.post(BACKEND_SINGLE_PREDICT_URL, json=input_data_single) st.write(f"Backend Response Status Code: {response.status_code}") response.raise_for_status() prediction = response.json() if "predicted_sales" in prediction: st.success(f"Predicted Product Sales Total: {prediction['predicted_sales']:.2f}") else: st.error(f"Prediction failed: {prediction.get('error', 'Unknown error from backend')}") except requests.exceptions.ConnectionError: st.error(f"Could not connect to the backend URL: {BACKEND_SINGLE_PREDICT_URL}. Please ensure the backend is running and accessible.") except requests.exceptions.RequestException as e: st.error(f"An error occurred during the API request: {e}. Check backend logs for more details.") except Exception as e: st.error(f"An unexpected error occurred: {e}") st.markdown("--- --- --- --- --- ") # --- Batch Prediction Form --- st.header("Batch Prediction") st.info("Upload a CSV file containing product and store details for multiple predictions.") batch_file = st.file_uploader("Upload CSV file", type=["csv"]) submitted_batch = st.button("Predict Batch Sales from CSV") # --- Batch Prediction Logic --- if submitted_batch and batch_file is not None: try: # Read the CSV file into a pandas DataFrame batch_df = pd.read_csv(batch_file) st.subheader("Batch Input Data Provided (from CSV):") st.dataframe(batch_df) # Convert DataFrame to a list of dictionaries (records) for JSON payload batch_data_parsed = batch_df.to_dict(orient='records') response = requests.post(BACKEND_BATCH_PREDICT_URL, json=batch_data_parsed) st.write(f"Backend Response Status Code: {response.status_code}") response.raise_for_status() batch_prediction_results = response.json() if "predicted_sales" in batch_prediction_results: st.success("Batch Predictions:") predictions_df = pd.DataFrame({"Predicted Sales": batch_prediction_results['predicted_sales']}) st.dataframe(predictions_df) else: st.error(f"Batch prediction failed: {batch_prediction_results.get('error', 'Unknown error from backend')}") except pd.errors.EmptyDataError: st.error("The uploaded CSV file is empty. Please upload a file with data.") except json.JSONDecodeError: st.error("Invalid JSON response from backend. Check backend logs.") except requests.exceptions.ConnectionError: st.error(f"Could not connect to the backend URL: {BACKEND_BATCH_PREDICT_URL}. Please ensure the backend is running and accessible.") except requests.exceptions.RequestException as e: st.error(f"An error occurred during the API request: {e}. Check backend logs for more details.") except Exception as e: st.error(f"An unexpected error occurred: {e}") elif submitted_batch and batch_file is None: st.warning("Please upload a CSV file before attempting batch prediction.") st.markdown("--- --- --- --- --- ") st.info("Note: This UI connects to a Flask backend deployed on Hugging Face Spaces. Make sure the backend is running and correctly deployed for both single and batch prediction endpoints.")