Spaces:
Build error
Build error
| import re | |
| import streamlit as st | |
| import pandas as pd | |
| import joblib | |
| # Load trained model | |
| MODEL_PATH = "superkart_sales_prediction_model_v1_0.joblib" | |
| model = joblib.load(MODEL_PATH) | |
| valid_store_ids = ["OUT001", "OUT002", "OUT003", "OUT004"] | |
| store_meta = { | |
| "OUT001": {"year": 1987, "size": "High", "city_type": "Tier 2", "store_type": "Supermarket Type1"}, | |
| "OUT002": {"year": 1998, "size": "Small", "city_type": "Tier 3", "store_type": "Food Mart"}, | |
| "OUT003": {"year": 1999, "size": "Medium", "city_type": "Tier 1", "store_type": "Departmental Store"}, | |
| "OUT004": {"year": 2009, "size": "Medium", "city_type": "Tier 2", "store_type": "Supermarket Type2"}, | |
| } | |
| st.title("SmartKart: Product Sales Prediction") | |
| st.subheader("Online Prediction") | |
| # Product ID validation | |
| product_id = st.text_input("Product ID (2 uppercase letters + 4 digits, e.g., FD6114)", max_chars=6) | |
| product_id_valid = bool(re.fullmatch(r"[A-Z]{2}\d{4}", product_id)) | |
| if product_id and not product_id_valid: | |
| st.error("Invalid Product ID format; it must be 2 uppercase letters followed by 4 digits.") | |
| elif product_id_valid: | |
| st.success("Product ID format is valid.") | |
| # Store selection | |
| store_id = st.selectbox("Store ID", options=valid_store_ids) | |
| meta = store_meta[store_id] | |
| st.text(f"Store Establishment Year: {meta['year']}") | |
| st.text(f"Store Size: {meta['size']}") | |
| st.text(f"Store Location City Type: {meta['city_type']}") | |
| st.text(f"Store Type: {meta['store_type']}") | |
| # Product features | |
| product_weight = st.number_input("Product Weight", min_value=0.0, value=12.0, format="%.2f") | |
| product_sugar_content = st.selectbox("Product Sugar Content", ["Low Sugar", "Regular", "No Sugar"]) | |
| product_allocated_area = st.slider("Product Allocated Area Ratio", min_value=0.0, max_value=1.0, step=0.001, value=0.05) | |
| product_type = st.selectbox("Product Type", [ | |
| "Meat", "Snack Foods", "Hard Drinks", "Dairy", "Canned", "Soft Drinks", | |
| "Health and Hygiene", "Baking Goods", "Bread", "Breakfast", "Frozen Foods", | |
| "Fruits and Vegetables", "Household", "Seafood", "Starchy Foods", "Others" | |
| ]) | |
| product_mrp = st.number_input("Product MRP", min_value=0.0, value=150.0, format="%.2f") | |
| # Prepare 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": meta['year'], | |
| "Store_Size": meta['size'], | |
| "Store_Location_City_Type": meta['city_type'], | |
| "Store_Type": meta['store_type'], | |
| }]) | |
| # Prediction | |
| if st.button("Predict"): | |
| if not product_id_valid: | |
| st.error("Please fix the Product ID before proceeding.") | |
| else: | |
| try: | |
| prediction = model.predict(input_data)[0] | |
| st.success(f"Predicted Product Sales: {prediction:.2f}") | |
| except Exception as e: | |
| st.error(f"Prediction failed: {e}") |