st.set_page_config(page_title="SuperKart Sales Forecasting App", layout="wide") st.title("SuperKart Sales Forecasting App") st.write("Enter the product and store details to get a sales forecast.") # Collect user input product_weight = st.number_input("Product Weight", min_value=1.0, max_value=25.0, value=10.0, step=0.1) product_sugar_content = st.selectbox("Product Sugar Content", ['Low Sugar', 'Regular', 'No Sugar']) product_allocated_area = st.number_input("Product Allocated Area", min_value=0.0, max_value=0.5, value=0.1, step=0.01) product_mrp = st.number_input("Product MRP", min_value=10.0, max_value=300.0, value=150.0, step=1.0) 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']) store_establishment_year = st.selectbox("Store Establishment Year", list(range(1985, 2023))) store_size = st.selectbox("Store Size", ['High', 'Medium', 'Low']) store_location_city_type = st.selectbox("Store Location City Type", ['Tier 1', 'Tier 2', 'Tier 3']) store_type = st.selectbox("Store Type", ['Departmental Store', 'Supermarket Type 1', 'Supermarket Type 2', 'Food Mart']) # Construct the input data dictionary # The keys here must exactly match the column names expected by your backend model input_data = { 'Product_Id': ["dummy"], # Placeholder, if not used by model during training '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': ["dummy"], # Placeholder, if not used by model during training 'Store_Establishment_Year': [store_establishment_year], 'Store_Size': [store_size], 'Store_Location_City_Type': [store_location_city_type], 'Store_Type': [store_type], } # --- Predict Button --- if st.button("Predict Sales"): # Define the URL of your deployed Flask API # You'll get this URL from your Hugging Face Backend Space settings after deployment. # It will be in the format: https://-.hf.space # You then append '/predict' to call your endpoint. api_url = "https://huggingface.co/spaces/pawanmall/superkart-frontend/predict" # <--- **IMPORTANT: Replace this** if "YOUR_HUGGING_FACE_BACKEND_SPACE_URL" in api_url: st.error("Please replace 'https://huggingface.co/spaces/pawanmall/superkart-frontend' with your actual backend Space URL.") else: try: # Send a POST request to the Flask API # Sending input_data as a list for robustness in the backend response = requests.post(api_url, json=input_data) response.raise_for_status() # Raise an exception for bad status codes (4xx or 5xx) # Get predictions from the response predictions = response.json().get("predictions") if predictions is not None and len(predictions) > 0: st.success(f"Predicted Sales Total: {predictions[0]:,.2f}") else: st.error("API did not return valid predictions.") except requests.exceptions.RequestException as e: st.error(f"Error calling prediction API: {e}") except json.JSONDecodeError: st.error("Error decoding JSON response from API. Check backend logs for details.") except Exception as e: st.error(f"An unexpected error occurred: {e}")