Spaces:
Sleeping
Sleeping
| import streamlit as st | |
| import pandas as pd | |
| import requests | |
| # Set the title of the Streamlit app | |
| st.title("SuperKart Product Store Sales Prediction") | |
| # Section for online prediction | |
| st.subheader("Predict Single Product Sales") | |
| # Collect user input for product and store features (original features only) | |
| product_id = st.text_input("Product ID") | |
| product_weight = st.number_input("Product Weight", min_value=0.0, value=10.0) | |
| product_sugar_content = st.selectbox("Product Sugar Content", ["Low Sugar", "Regular", "No Sugar", "low sugar"]) | |
| product_allocated_area = st.number_input("Product Allocated Area", min_value=0.0, value=0.05) | |
| product_type = st.selectbox("Product Type", ['Frozen Foods', 'Dairy', 'Canned', 'Baking Goods', 'Health and Hygiene', 'Snack Foods', 'Meat', 'Soft Drinks', 'Breads', 'Hard Drinks', 'Others', 'Starchy Foods', 'Breakfast', 'Seafood', 'Fruits and Vegetables', 'Household']) | |
| product_mrp = st.number_input("Product MRP", min_value=0.0, value=100.0) | |
| store_id = st.selectbox("Store ID", ['OUT004', 'OUT003', 'OUT001', 'OUT002']) | |
| store_establishment_year = st.number_input("Store Establishment Year", min_value=1900, value=2000) | |
| store_size = st.selectbox("Store Size", ["Medium", "High", "Small"]) | |
| store_location_city_type = st.selectbox("Store Location City Type", ["Tier 2", "Tier 1", "Tier 3"]) | |
| store_type = st.selectbox("Store Type", ['Supermarket Type2', 'Departmental Store', 'Supermarket Type1', 'Food Mart']) | |
| # Create a dictionary with the input data for single prediction (original features only) | |
| input_data = { | |
| '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 | |
| } | |
| # Make prediction when the "Predict" button is clicked (single prediction) | |
| if st.button("Predict"): | |
| # Replace with the actual URL of your deployed backend Hugging Face Space | |
| backend_url = "https://garg06-superkartsalesbackend.hf.space" # Example URL format | |
| predict_url = f"{backend_url}/v1/product" | |
| try: | |
| response = requests.post(predict_url, json=input_data) | |
| if response.status_code == 200: | |
| prediction = response.json()['Predicted_Product_Store_Sales_Total'] | |
| st.success(f"Predicted Product Store Sales Total: {prediction:.2f}") | |
| else: | |
| st.error(f"Error making prediction. Status code: {response.status_code}") | |
| st.error(f"Response: {response.text}") | |
| except requests.exceptions.RequestException as e: | |
| st.error(f"Error connecting to the backend: {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", type='primary'): | |
| # Replace with the actual URL of your deployed backend Hugging Face Space | |
| backend_url = "https://garg06-superkartsalesbackend.hf.space" # Example URL format | |
| predict_batch_url = f"{backend_url}/v1/productbatch" | |
| try: | |
| # Send the DataFrame to the Flask API for batch prediction | |
| # The backend will calculate the engineered features | |
| # We'll send the file directly as multipart/form-data | |
| response = requests.post(predict_batch_url, files={"file": uploaded_file}) | |
| if response.status_code == 200: | |
| predictions = response.json() | |
| st.success("Batch predictions completed!") | |
| st.write(predictions) # Display the predictions (including engineered features and predictions from backend) | |
| else: | |
| st.error(f"Error making batch prediction. Status code: {response.status_code}") | |
| st.error(f"Response: {response.text}") | |
| except requests.exceptions.RequestException as e: | |
| st.error(f"Error connecting to the backend: {e}") | |