Spaces:
Sleeping
Sleeping
| import streamlit as st | |
| import pandas as pd | |
| import requests | |
| import time | |
| # Set the title of the Streamlit app | |
| st.set_page_config(page_title="SuperKart Sales Forecast", layout="centered") | |
| st.title("SuperKart Sales Prediction") | |
| # Health check endpoint for Hugging Face | |
| if st.query_params.get("healthcheck") == "true": | |
| st.write("OK") | |
| st.stop() | |
| # Section for single prediction | |
| st.subheader("Single Product-Store Prediction") | |
| # Collect user input for product and store features | |
| product_weight = st.number_input("Product Weight", min_value=0.1, max_value=20.0, value=12.65, step=0.1) | |
| product_allocated_area = st.number_input("Product Allocated Area Ratio", min_value=0.0, max_value=1.0, value=0.07, step=0.01) | |
| product_mrp = st.number_input("Product MRP", min_value=10, max_value=200, value=147, step=1) | |
| product_sugar_content = st.selectbox("Product Sugar Content", ["Low Sugar", "Regular", "No Sugar"]) | |
| product_type = st.selectbox("Product Type", [ | |
| "Fruits and Vegetables", "Household", "Snack Foods", "Meat", | |
| "Hard Drinks", "Dairy", "Canned", "Soft Drinks", | |
| "Health and Hygiene", "Baking Goods", "Bread", "Breakfast", | |
| "Frozen Foods", "Seafood", "Starchy Foods", "Others" | |
| ]) | |
| store_size = st.selectbox("Store Size", ["High", "Medium", "Low"]) | |
| 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 Type 1", "Supermarket Type 2", "Food Mart"]) | |
| store_age = st.number_input("Store Age (years)", min_value=1, max_value=50, value=10, step=1) | |
| product_category_code = st.number_input("Product Category Code", min_value=1, max_value=100, value=10, step=1) | |
| # Convert user input into the format expected by the API | |
| input_data = { | |
| 'Product_Weight': float(product_weight), | |
| 'Product_Allocated_Area': float(product_allocated_area), | |
| 'Product_MRP': int(product_mrp), | |
| 'Product_Sugar_Content': product_sugar_content, | |
| 'Product_Type': product_type, | |
| 'Store_Size': store_size, | |
| 'Store_Location_City_Type': store_location_city_type, | |
| 'Store_Type': store_type, | |
| 'Store_Age': int(store_age), | |
| 'Product_Category_Code': int(product_category_code) | |
| } | |
| # Make prediction when the "Predict" button is clicked | |
| if st.button("Predict Sales"): | |
| try: | |
| # Replace with your actual backend URL | |
| backend_url = "https://simnid-superkartsalesbackend.hf.space/v1/predict" | |
| response = requests.post(backend_url, json=input_data) | |
| if response.status_code == 200: | |
| prediction = response.json()['Predicted Sales Total'] | |
| st.success(f"Predicted Sales Total: ${prediction:,.2f}") | |
| else: | |
| st.error(f"Error making prediction. Status code: {response.status_code}") | |
| st.write(response.text) | |
| except Exception as e: | |
| st.error(f"Error connecting to API: {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"): | |
| try: | |
| # Replace with your actual backend batch URL | |
| batch_backend_url = "https://simnid-superkartsalesbackend.hf.space/v1/predictbatch" | |
| response = requests.post(batch_backend_url, files={"file": uploaded_file}) | |
| if response.status_code == 200: | |
| predictions = response.json() | |
| st.success("Batch predictions completed!") | |
| # Display predictions in a nice format | |
| predictions_df = pd.DataFrame.from_dict(predictions, orient='index', columns=['Predicted Sales']) | |
| st.dataframe(predictions_df) | |
| # Add download button | |
| csv = predictions_df.to_csv(index=True) | |
| st.download_button( | |
| label="Download Predictions as CSV", | |
| data=csv, | |
| file_name="superkart_predictions.csv", | |
| mime="text/csv" | |
| ) | |
| else: | |
| st.error(f"Error making batch prediction. Status code: {response.status_code}") | |
| except Exception as e: | |
| st.error(f"Error connecting to API: {e}") | |
| # Add some information about the app | |
| st.sidebar.header("About") | |
| st.sidebar.info(""" | |
| This app predicts sales totals for SuperKart products using a machine learning model. | |
| - **Single Prediction**: Enter details for one product-store combination | |
| - **Batch Prediction**: Upload a CSV file with multiple records | |
| """) | |