Spaces:
Sleeping
Sleeping
| from datetime import datetime | |
| import streamlit as st | |
| import pandas as pd | |
| import requests | |
| # Set the title of the Streamlit app | |
| st.title("SuperKart Sales Forecast System") | |
| # Section for online prediction | |
| st.subheader("Online Prediction") | |
| # Collect user input for Product and Store features | |
| Product_Id = st.text_input("Product_Id") | |
| Product_Weight = st.number_input("Product_Weight", min_value=1.0,max_value=100.0, value=1.0) | |
| Product_Sugar_Content = st.selectbox("Product_Sugar_Content", ["No Sugar", "Low Sugar", "Regular"]) | |
| Product_Allocated_Area = st.number_input("Product_Allocated_Area", min_value=0.0,max_value=1.0,value=.5) | |
| Product_Type = st.selectbox("Product_Type", ["Fruits and Vegetables", "Snack Foods", "Frozen Foods","Dairy", "Household", "Baking Goods","Canned", "Health and Hygiene", "Meat","Soft Drinks", "Breads", "Hard Drinks", "Others", "Starchy Foods","Breakfast", "Seafood"]) | |
| Product_MRP = st.number_input("Product_MRP", min_value=0.0) | |
| #Store_Id = st.selectbox("Store_Id", ["OUT001", "OUT002", "OUT003","OUT004"]) | |
| Store_Establishment_Year = st.number_input("Store_Establishment_Year", min_value=1987,max_value=2025,value=2009) | |
| 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", ["Supermarket Type1", "Supermarket Type2", "Departmental Store","Food Mart"]) | |
| # Convert user input into a DataFrame | |
| input_data = pd.DataFrame([{ | |
| '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 | |
| }]) | |
| # Extract the Product_Code and Store_Age before feeding to the model | |
| input_data["Product_Code"] = input_data["Product_Id"].str[:2] | |
| input_data.drop("Product_Id", axis=1, inplace=True) | |
| current_year = datetime.now().year | |
| input_data["Store_Age"] = current_year - input_data["Store_Establishment_Year"] | |
| input_data.drop("Store_Establishment_Year", axis=1, inplace=True) | |
| # Make prediction when the "Predict" button is clicked | |
| if st.button("Forecast"): | |
| try: | |
| response = requests.post("https://UncloudMe-SK_Sales_Backend.hf.space/salespredict", | |
| json=input_data.to_dict(orient='records')[0], | |
| timeout=30 # add timeout for safety | |
| ) | |
| if response.status_code == 200: | |
| prediction = response.json().get('Predicted Sale', None) | |
| if prediction is not None: | |
| st.success(f"Predicted Sale: {prediction}") | |
| else: | |
| st.error("No prediction found in response.") | |
| else: | |
| # show backend error text if available | |
| st.error(f"Error {response.status_code}: {response.text}") | |
| except requests.exceptions.RequestException as e: | |
| # catch all connection, timeout, DNS, etc. errors | |
| st.error(f"Connection error: {str(e)}") | |
| #if st.button("Forecast"): | |
| # response = requests.post("https://UncloudMe-SK_Sales_Backend.hf.space/salespredict", json=input_data.to_dict(orient='records')[0]) # Send data to Flask API | |
| # if response.status_code == 200: | |
| # prediction = response.json()['Predicted Sales'] | |
| # st.success(f"Predicted Sales: {prediction}") | |
| # else: | |
| # st.error("Error making prediction.") | |
| # 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"): | |
| response = requests.post("https://UncloudMe-SK_Sales_Backend.hf.space/salespredictbatch", files={"file": uploaded_file}) # Send file to Flask API | |
| if response.status_code == 200: | |
| predictions = response.json() | |
| st.success("Batch predictions completed!") | |
| st.write(predictions) # Display the predictions | |
| else: | |
| st.error("Error making batch prediction.") | |