Spaces:
Runtime error
Runtime error
| from datetime import datetime | |
| import pandas as pd | |
| import requests | |
| import streamlit as st | |
| USER_ID = "rakesh1715" | |
| REPO_ID = "gl-backend" | |
| st.title("SuperKart Sales Forecasting") | |
| # Batch Prediction | |
| st.subheader("Online Prediction") | |
| # Input fields for product data | |
| Product_ID = st.text_input("Product ID", value="FD6114") | |
| Product_Type_Category = st.selectbox("Product Type", ["Perishable", "Non Perishable"]) | |
| Product_Weight = st.number_input("Product Weight", min_value=0, value=650) | |
| Product_Sugar_Content = st.selectbox("Sugar Content in Product", ["Regular", "No Sugar", "Low Sugar"]) | |
| Product_Allocated_Area = st.number_input("Allocated Area for Product", min_value=0, value=30) | |
| Product_MRP = st.number_input("Product Price", min_value=0, value=12) | |
| Store_Id = st.selectbox("Store ID", ["OUT001", "OUT002", "OUT003", "OUT004"]) | |
| Store_Establishment_Year = st.number_input("Store Establishment Year", min_value=1970, max_value=datetime.now().year, | |
| step=1, format="%d") | |
| Store_Location_City_Type = st.selectbox("Store City Type", ["Tier 1", "Tier 2", "Tier 3"]) | |
| Store_Type = st.selectbox("Store Type", ["Departmental Store", "Food Mart", "Supermarket Type1", "Supermarket Type2"]) | |
| Store_Size = st.selectbox("Store Size", ["High", "Medium", "Small"]) | |
| product_data = { | |
| 'Product_Weight': Product_Weight, | |
| 'Product_Sugar_Content': Product_Sugar_Content, | |
| 'Product_Allocated_Area': Product_Allocated_Area, | |
| 'Product_MRP': Product_MRP, | |
| 'Store_Size': Store_Size, | |
| 'Store_Id': Store_Id, | |
| 'Store_Location_City_Type': Store_Location_City_Type, | |
| 'Store_Type': Store_Type, | |
| 'Store_Age': datetime.now().year - Store_Establishment_Year, | |
| 'Product_Type_Categories': Product_Type_Category, | |
| } | |
| if st.button("Predict", type='primary'): | |
| response = requests.post(f"https://{USER_ID}-{REPO_ID}.hf.space/v1/predict", json=product_data) | |
| if response.status_code == 200: | |
| result = response.json() | |
| sales_prediction = result["Sales"] # Extract only the value | |
| st.write( | |
| f"Based on the information provided, the product with ID {Product_ID} is likely to have sales of {float(sales_prediction):,.2f}") | |
| else: | |
| st.error("Error in API request") | |
| # Batch Prediction | |
| st.subheader("Batch Prediction") | |
| # Create a sample dataframe (customize columns to match your model’s inputs) | |
| sample_data = { | |
| "Product_ID": ["FD6114", "FD6115"], | |
| "Product_Type_Categories": ["Perishable", "Non Perishable"], | |
| "Product_Weight": [0.7, 10], | |
| "Product_Sugar_Content": ["Regular", "No Sugar"], | |
| "Product_Allocated_Area": [0.19, 20], | |
| "Product_MRP": [100.23, 19.001], | |
| "Store_Id": ["OUT003", "OUT002"], | |
| "Store_Establishment_Year": [2010, 2020], | |
| "Store_Location_City_Type": ["Tier 1", "Tier 2"], | |
| "Store_Type": ["Departmental Store", "Food Mart"], | |
| "Store_Size": ["High", "Medium"], | |
| } | |
| sample_df = pd.DataFrame(sample_data) | |
| # Convert to CSV and create a download button | |
| st.download_button( | |
| label="📥 Download Sample CSV", | |
| data=sample_df.to_csv(index=False).encode("utf-8"), | |
| file_name="sample_input.csv", | |
| mime="text/csv" | |
| ) | |
| file = st.file_uploader("Upload CSV file", type=["csv"]) | |
| if file is not None: | |
| if st.button("Predict for Batch", type='primary'): | |
| response = requests.post(f"https://{USER_ID}-{REPO_ID}.hf.space/v1/bulk/predict", files={"file": file}) | |
| if response.status_code == 200: | |
| result = response.json() | |
| if result: | |
| df = pd.DataFrame(list(result.items()), columns=["Product Id", "Predicted Sales"]) | |
| df["Predicted Sales"] = df["Predicted Sales"].round(2) | |
| st.header("Batch Prediction Results") | |
| st.dataframe(df, use_container_width=True) | |
| else: | |
| st.error("Error in API request") | |