Spaces:
Sleeping
Sleeping
| import streamlit as st | |
| import requests | |
| import json | |
| import pandas as pd | |
| import os # Import os to access environment variables if needed later | |
| # Define the URL of your deployed backend API on Hugging Face Spaces | |
| # The URL format is typically https://<your-username>-<your-space-name>.hf.space | |
| # Replace with your actual Space URL | |
| # Assuming the backend space name is 'SuperKartBackendSC' | |
| # You need to manually replace <your-username> with your actual Hugging Face username | |
| HF_USERNAME = os.getenv("HF_USERNAME", "sumachakkingal") # Get username from environment variable or use placeholder | |
| BACKEND_SPACE_NAME = "SuperKartBackendSC" | |
| BACKEND_API_URL = f"https://{HF_USERNAME}-{BACKEND_SPACE_NAME}.hf.space/predict" | |
| st.title("SuperKart Sales Prediction Frontend") | |
| st.write("Enter the product and store details to get a sales prediction.") | |
| # Create input fields for the features | |
| # Ensure the keys match the column names expected by your Flask app | |
| product_weight = st.number_input("Product Weight", min_value=0.0) | |
| 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.00) | |
| product_type = st.selectbox("Product Type", ['Frozen Foods', 'Dairy', 'Canned', 'Baking Goods', 'Health and Hygiene', 'Snack Foods', 'Meat', 'Household', 'Hard Drinks', 'Fruits and Vegetables', 'Breads', 'Soft Drinks', 'Breakfast', 'Others', 'Starchy Foods', 'Seafood']) | |
| product_mrp = st.number_input("Product MRP", min_value=0.0) | |
| store_id = st.selectbox("Store ID", ['OUT004', 'OUT001', 'OUT003', 'OUT002']) | |
| store_size = st.selectbox("Store Size", ['Medium', 'High', 'Small']) | |
| store_location_city_type = st.selectbox("Store Location City Type", ['Tier 2', 'Tier 1', 'Tier 3']) | |
| store_type = st.selectbox("Store Type", ['Supermarket Type2', 'Departmental Store', 'Supermarket Type1', 'Food Mart']) | |
| store_establishment_year = st.number_input("Store Establishment Year", min_value=1985, max_value=2024) | |
| # Calculate Store_Age (assuming current year is 2024) | |
| current_year = 2024 | |
| store_age = current_year - store_establishment_year | |
| # Create a dictionary with the input data | |
| # The keys must match the column names used for training and expected by the Flask app | |
| input_data = { | |
| "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_Size": store_size, | |
| "Store_Location_City_Type": store_location_city_type, | |
| "Store_Type": store_type, | |
| "Store_Age": store_age # Include Store_Age as it's used by the model | |
| } | |
| # Create a button to trigger the prediction | |
| if st.button("Predict Sales"): | |
| try: | |
| # Send the input data to the backend API | |
| response = requests.post(BACKEND_API_URL, json=input_data) | |
| # Check if the request was successful | |
| if response.status_code == 200: | |
| prediction_result = response.json() | |
| predicted_sales = prediction_result.get("prediction") | |
| if predicted_sales is not None: | |
| st.success(f"Predicted Product Store Sales Total: {predicted_sales:.2f}") | |
| else: | |
| st.error("Could not retrieve a valid prediction from the backend.") | |
| #st.success(f"Predicted Product Store Sales Total: {predicted_sales:.2f}") | |
| else: | |
| st.error(f"Error: Could not get prediction from the backend. Status code: {response.status_code}") | |
| st.error(f"Response: {response.text}") | |
| except requests.exceptions.RequestException as e: | |
| st.error(f"Error connecting to the backend API: {e}") | |
| except Exception as e: | |
| st.error(f"An unexpected error occurred: {e}") | |