Spaces:
Sleeping
Sleeping
| import streamlit as st | |
| import pandas as pd | |
| import joblib | |
| import requests | |
| # Streamlit UI for Product Sales Prediction | |
| st.title("Product Sales Prediction") | |
| st.subheader("Please fill details below for sales prediction") | |
| # Collect user input | |
| ProductWeight = st.number_input("Product Weight (weight of the product)", min_value=4.0, max_value=22.0, value=10.0) | |
| ProductSugarContent = st.selectbox("Product Sugar Content(sugar content of the product)", ["Low Sugar", "Regular", "No sugar"]) | |
| ProductAllocatedArea = st.number_input("Product Allocated Area (ratio of the allocated display area of each product to the total display area )", min_value=0.00, max_value=0.29, value=0.06) | |
| ProductType = st.selectbox("Product Type", ['Baking Goods', 'Breads', 'Breakfast', 'Canned', | |
| 'Dairy', 'Frozen Food', 'Fruits and Vegetables', | |
| 'Hard Drinks', 'Health and Hygiene', 'Household', | |
| 'Meat', 'Others', 'Seafood', 'Snack foods', | |
| 'Soft Drinks', 'Starchy Foods']) | |
| ProductMRP = st.number_input("Product MRP(maximum retail price of each product)", min_value=31.0, max_value=266.0, value=120.0) | |
| StoreID = st.selectbox("Store Id",['OUT001', 'OUT002', 'OUT003', 'OUT004'] ) | |
| StoreEstablishmentYear = st.number_input("Store Establishment Year (the year in which the store was established)", min_value=1987, max_value=2009, value=2000) | |
| StoreSize= st.selectbox("Store Size", ['Small','Medium', 'High']) | |
| StoreLocationCityType = st.selectbox("Store Location City Type", ['Tier 1', 'Tier 2', 'Tier 3']) | |
| StoreType = st.selectbox("Store Type",['Departmental Store', 'Food Mart', 'Supermarket Type 1', 'Supermarket Type 2']) | |
| # Convert categorical inputs to match model training | |
| input_data = pd.DataFrame([{ | |
| 'Product_Weight': ProductWeight, | |
| 'Product_Sugar_Content': ProductSugarContent, | |
| 'Product_Allocated_Area': ProductAllocatedArea, | |
| 'Product_Type': ProductType, | |
| 'Product_MRP': ProductMRP, | |
| 'Store_Id': StoreID, | |
| 'Store_Establishment_Year': StoreEstablishmentYear, | |
| 'Store_Size': StoreSize, | |
| 'Store_Location_City_Type': StoreLocationCityType, | |
| 'Store_Type': StoreType | |
| }]) | |
| if st.button("Predict", type='primary'): | |
| headers = {'Content-Type': 'application/json'} | |
| response = requests.post("https://csankaran3-backend.hf.space/v1/product", json=input_data.to_dict(orient='records')[0],headers=headers) # enter user name and space name before running the cell | |
| if response.status_code == 200: | |
| result = response.json() | |
| predicted_sales = result['Predicted Sales'] # Extract only the value | |
| st.success(f"Sales predictions completed!.. The predicted product sale is {predicted_sales}.") | |
| else: | |
| st.error("Error in API request") | |
| # 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 sales 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'): | |
| response = requests.post("https://csankaran3-backend.hf.space/v1/productbatch", 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.") | |