Spaces:
Sleeping
Sleeping
Upload app.py with huggingface_hub
Browse files123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475import streamlit as stimport requestsimport pandas as pd # Used for displaying data
# --- Configuration --- #BACKEND_URL = "https://roopast-sales-prediction-backend.hf.space/predict" # Replace with your deployed backend URL
# --- Streamlit UI --- #st.set_page_config(page_title="SuperKart Sales Predictor", layout="wide")st.title("🛒 SuperKart Sales Predictor")st.write("Enter product and store details to predict sales revenue.")
# --- Input Form --- #with st.form("sales_prediction_form"): st.header("Product Details") product_id = st.text_input("Product ID", "FD6114", help="e.g., FD6114, NC1180") product_weight = st.number_input("Product Weight (kg)", min_value=4.0, max_value=22.0, value=12.66, step=0.01) product_mrp = st.number_input("Product MRP (₹)", min_value=30.0, max_value=270.0, value=117.08, step=0.01) product_allocated_area = st.number_input("Product Allocated Area Ratio", min_value=0.0, max_value=0.2, value=0.027, step=0.001) product_sugar_content = st.selectbox("Product Sugar Content", ['Low Sugar', 'Regular', 'No Sugar'], index=0) product_type = st.selectbox("Product Type", [ 'Baking Goods', 'Breads', 'Breakfast', 'Canned', 'Dairy', 'Frozen Foods', 'Fruits and Vegetables', 'Hard Drinks', 'Health and Hygiene', 'Household', 'Meat', 'Others', 'Seafood', 'Snack Foods', 'Soft Drinks', 'Starchy Foods' ], index=6) # Fruits and Vegetables as default
st.header("Store Details") store_id = st.text_input("Store ID", "OUT004", help="e.g., OUT004, OUT003") store_establishment_year = st.number_input("Store Establishment Year", min_value=1985, max_value=2009, value=2009, step=1) store_size = st.selectbox("Store Size", ['High', 'Medium', 'Small'], index=1) store_location_city_type = st.selectbox("Store Location City Type", ['Tier 1', 'Tier 2', 'Tier 3'], index=1) store_type = st.selectbox("Store Type", ['Departmental Store', 'Food Mart', 'Supermarket Type1', 'Supermarket Type2'], index=3)
submitted = st.form_submit_button("Predict Sales")
# --- Prediction Logic --- #if submitted: # Create input data dictionary matching the backend's expected raw input format input_data = { "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": store_establishment_year, "Store_Size": store_size, "Store_Location_City_Type": store_location_city_type, "Store_Type": store_type }
st.subheader("Input Data Provided:") st.json(input_data)
try: response = requests.post(BACKEND_URL, json=input_data) response.raise_for_status() # Raise an exception for HTTP errors (4xx or 5xx) prediction = response.json()
if "predicted_sales" in prediction: st.success(f"Predicted Product Sales Total: ₹ {prediction['predicted_sales']:.2f}") else: st.error(f"Prediction failed: {prediction.get('error', 'Unknown error from backend')}")
except requests.exceptions.ConnectionError: st.error(f"Could not connect to the backend URL: {BACKEND_URL}. Please ensure the backend is running and accessible.") except requests.exceptions.RequestException as e: st.error(f"An error occurred during the API request: {e}. Check backend logs for more details.") except Exception as e: st.error(f"An unexpected error occurred: {e}")
st.markdown("---")st.info("Note: This UI connects to a Flask backend deployed on Hugging Face Spaces. Make sure the backend is running.")
app.pyAdded+75