Spaces:
Sleeping
Sleeping
| import streamlit as st | |
| import pandas as pd | |
| import numpy as np | |
| from sklearn.linear_model import LinearRegression | |
| from sklearn.model_selection import train_test_split | |
| from sklearn.preprocessing import LabelEncoder | |
| # Streamlit UI | |
| st.set_page_config(page_title="BigMart Sales Predictor", page_icon="🛒", layout="centered") | |
| st.title("🛒 BigMart Sales Prediction using Real Dataset") | |
| st.markdown("Fill in the product details to get a sales prediction.") | |
| # Load and preprocess dataset | |
| def load_data(): | |
| data = pd.read_csv("Train.csv") # 👈 Make sure Train.csv is in the same directory | |
| # Handle missing values | |
| data.fillna(data.mean(numeric_only=True), inplace=True) | |
| data.fillna("Unknown", inplace=True) | |
| # Encode categorical columns | |
| label_enc = LabelEncoder() | |
| for col in ['Item_Fat_Content', 'Item_Type', 'Outlet_Identifier', 'Outlet_Size', 'Outlet_Location_Type', 'Outlet_Type']: | |
| data[col] = label_enc.fit_transform(data[col]) | |
| return data | |
| df = load_data() | |
| # Select features and target | |
| features = ['Item_Weight', 'Item_Visibility', 'Item_MRP'] | |
| target = 'Item_Outlet_Sales' | |
| X = df[features] | |
| y = df[target] | |
| # Train model | |
| model = LinearRegression() | |
| model.fit(X, y) | |
| # Input UI | |
| product_name = st.text_input("📦 Product Name") | |
| item_weight = st.number_input("⚖️ Item Weight (kg)", min_value=0.0, step=0.1) | |
| item_visibility = st.slider("👀 Item Visibility", 0.0, 1.0, 0.05) | |
| item_mrp = st.number_input("💰 Item MRP", min_value=0.0, step=1.0) | |
| # Prediction | |
| if st.button("Predict Sales"): | |
| if not product_name: | |
| st.warning("Please enter a product name.") | |
| else: | |
| user_input = np.array([[item_weight, item_visibility, item_mrp]]) | |
| predicted_sales = model.predict(user_input)[0] | |
| st.success(f"📈 Predicted Sales for '{product_name}': ₹{predicted_sales:,.2f}") | |
| # Optional: Download Prediction | |
| result_df = pd.DataFrame({ | |
| "Product Name": [product_name], | |
| "Item Weight": [item_weight], | |
| "Item Visibility": [item_visibility], | |
| "Item MRP": [item_mrp], | |
| "Predicted Sales": [predicted_sales] | |
| }) | |
| st.download_button("📥 Download Result as CSV", result_df.to_csv(index=False), file_name="prediction.csv", mime="text/csv") | |
| # Sidebar Info | |
| st.sidebar.title("📌 About") | |
| st.sidebar.markdown(""" | |
| This app uses a **real BigMart dataset** from Kaggle and a **Linear Regression model** to predict sales. | |
| You can customize features or switch to advanced ML models later! | |
| """) | |