Spaces:
Runtime error
Runtime error
| import streamlit as st | |
| import pandas as pd | |
| import requests | |
| # Set the title of the Streamlit app | |
| st.title("SuperKart Sales Predictor") | |
| # Section for online prediction | |
| st.subheader("Online Prediction") | |
| # Collect business input for features | |
| Product_Weight = st.number_input("Product Weight", min_value=0.0, max_value=100.0, step=0.1) | |
| Product_Sugar_Content = st.selectbox("Product Sugar Content", ["Low Sugar", "No Sugar", "Regular"]) | |
| Product_Type = st.selectbox("Product Type", ["Perishable", "Non Perishable"]) | |
| Product_Allocated_Area = st.number_input("Product Allocated Area", min_value=0.000, max_value=0.300, step=0.1) | |
| Product_MRP = st.number_input("Product MRP", min_value=00.00, max_value=1000.00, step=0.1) | |
| Store_Size = st.selectbox("Store Size", ["Small", "Medium", "High"]) | |
| Store_Location_City_Type = st.selectbox("Store Location City Type", ["Tier 1", "Tier 2", "Tier 3"]) | |
| Store_Type = st.selectbox("Store Type", ["Departmental Store", "Food Mart", "Supermarket Type1", "Supermarket Type2"]) | |
| Store_Current_Age = st.number_input("Store Current Age", min_value=0, max_value=100, step=1) | |
| # Convert user input into a DataFrame | |
| business_df = pd.DataFrame({ | |
| 'Product_Weight': [Product_Weight], | |
| 'Product_Sugar_Content': [Product_Sugar_Content], | |
| 'Product_Type': [Product_Type], | |
| 'Product_Allocated_Area': [Product_Allocated_Area], | |
| 'Product_MRP': [Product_MRP], | |
| 'Store_Size': [Store_Size], | |
| 'Store_Location_City_Type': [Store_Location_City_Type], | |
| 'Store_Type': [Store_Type], | |
| 'Store_Current_Age': [Store_Current_Age] # Changed key name | |
| }) | |
| # Make prediction when the "Predict" button is clicked | |
| if st.button("Predict"): | |
| backend_url = "https://vrs1503-superkart-backend.hf.space/v1/predict" # Ensure correct URL | |
| try: | |
| response = requests.post(backend_url, json=business_df.to_dict(orient="records")[0]) | |
| response.raise_for_status() # Raise an exception for bad status codes | |
| data = response.json() | |
| if 'prediction' in data: | |
| prediction = data['prediction'][0] # Access the first element of the list | |
| st.success(f"Predicted Sales (in dollars): {prediction}") | |
| else: | |
| st.error(f"Error: 'prediction' key not found in response. Response: {data}") | |
| except requests.exceptions.RequestException as e: | |
| st.error(f"Error making prediction: {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 a CSV file", type=["csv"]) | |
| # Make predictions when the "Predict" button is clicked | |
| if uploaded_file is not None: | |
| if st.button("Predict Batch"): # Changed button name to avoid duplication | |
| backend_url = "https://vrs1503-superkart-backend.hf.space/v1/batch_predict" # Ensure correct URL | |
| try: | |
| response = requests.post(backend_url, files={"file": uploaded_file}) | |
| response.raise_for_status() | |
| predictions = response.json() | |
| st.success("Batch predictions completed!") | |
| st.write(predictions) # Display the predictions | |
| except requests.exceptions.RequestException as e: | |
| st.error(f"Error making batch prediction: {e}") | |