Spaces:
Sleeping
Sleeping
| import streamlit as st | |
| import pandas as pd | |
| import numpy as np | |
| import requests | |
| # Streamlit UI for Price Prediction | |
| st.title("SuperKart Sales Predictor") | |
| st.write("This tool predicts the sales based on various store parameters.") | |
| st.subheader("Enter the store details(Single Predication):") | |
| # Collect user input | |
| product_weight = st.number_input("Product Weight (in kg)", min_value=1.0, max_value=30.0) | |
| product_sugar = st.selectbox("Sugar Content", ["Low Sugar", "Regular", "No Sugar"]) | |
| product_area = st.slider("Allocated Area (sq m)", min_value=0.0, max_value=1.0, step=0.01) | |
| product_type = st.selectbox("Product Type", ["Fruits and Vegetables", "Snack Foods", "Frozen Foods", "Dairy", "Household","Baking Goods", "Canned", "Health and Hygiene", "Meat", "Breads","Hard Drinks", "Soft Drinks", "Seafood", "Starchy Foods", "Others"]) | |
| product_mrp = st.number_input("Product MRP", min_value=10.0, max_value=300.0) | |
| store_year = st.number_input("Store Establishment Year", min_value=1980, max_value=2025) | |
| store_size = st.selectbox("Store Size", ["Small", "Medium", "High"]) | |
| store_city = st.selectbox("City Type", ["Tier 1", "Tier 2", "Tier 3"]) | |
| store_type = st.selectbox("Store Type", ["Supermarket Type1", "Supermarket Type2", "Food Mart", "Departmental Store"]) | |
| # Prepare input | |
| if st.button("Predict Sales"): | |
| input_df = { | |
| "Product_Weight": product_weight, | |
| "Product_Sugar_Content": product_sugar, | |
| "Product_Allocated_Area": product_area, | |
| "Product_Type": product_type, | |
| "Product_MRP": product_mrp, | |
| "Store_Establishment_Year": 2025 - store_year, # we have modified this to get the store age | |
| "Store_Size": store_size, | |
| "Store_Location_City_Type": store_city, | |
| "Store_Type": store_type | |
| } | |
| response = requests.post("https://harasar-SuperKartBackend.hf.space/v1/customer", json=input_df) # enter user name and space name before running the cell | |
| if response.status_code == 200: | |
| result = response.json() | |
| churn_prediction = result["predicted_sales"] # Extract only the value | |
| st.write(f"Based on the information provided, the sproject sales is likely to {churn_prediction}.") | |
| else: | |
| st.error("Error in API request") | |
| #Batch Prediction | |
| uploaded_file = st.file_uploader("Upload CSV file", type=["csv"]) | |
| if st.button("Predict for Batch"): | |
| if uploaded_file is not None: | |
| try: | |
| # Convert uploaded file to a DataFrame | |
| df = pd.read_csv(uploaded_file) | |
| # Convert DataFrame to CSV bytes like your working script | |
| csv_bytes = df.to_csv(index=False).encode('utf-8') | |
| # Send POST request with raw bytes | |
| response = requests.post( | |
| "https://harasar-SuperKartBackend.hf.space/v1/customerbatch", | |
| files={"file": ("SuperKart.csv", csv_bytes, "text/csv")} | |
| ) | |
| if response.status_code == 200: | |
| st.success("Batch prediction successful!") | |
| st.write(response.json()) | |
| else: | |
| st.error(f"Error {response.status_code}: {response.text}") | |
| except Exception as e: | |
| st.error(f"Upload failed: {e}") | |
| else: | |
| st.warning("Please upload a CSV file first.") | |