hareeshkumarkb's picture
Upload app.py with huggingface_hub
291d1c1 verified
Raw
History Blame Contribute Delete
5.68 kB
import streamlit as st
import requests
import pandas as pd
import numpy as np
import json
# Define the URL of your deployed backend API
# Replace with the actual URL of your Hugging Face Space endpoint
# The endpoint will typically be your_space_url/predict
BACKEND_API_URL = "https://huggingface.co/spaces/hareeshkumarkn/hareesh539" # Corrected URL
st.title("SuperKart Sales Prediction")
st.write("Enter the details of the product and store to predict sales.")
# Create input fields for the features
# These should match the features your model expects
product_weight = st.number_input("Product Weight", value=10.0)
product_sugar_content = st.selectbox("Product Sugar Content", ['Low Sugar', 'Regular', 'No Sugar', 'Other'])
product_allocated_area = st.number_input("Product Allocated Area", value=0.05)
product_type = st.selectbox("Product Type", ['Frozen Foods', 'Dairy', 'Canned', 'Baking Goods', 'Health and Hygiene', 'Snack Foods', 'Household', 'Soft Drinks', 'Breakfast', 'Meat', 'Hard Drinks', 'Fruits and Vegetables', 'Breads', 'Starchy Foods', 'Others', 'Seafood'])
product_mrp = st.number_input("Product MRP", value=150.0)
store_establishment_year = st.number_input("Store Establishment Year", value=2000, format="%d")
store_size = st.selectbox("Store Size", ['Medium', 'High', 'Small'])
store_location_city_type = st.selectbox("Store Location City Type", ['Tier 1', 'Tier 2', 'Tier 3'])
store_type = st.selectbox("Store Type", ['Supermarket Type2', 'Departmental Store', 'Supermarket Type1', 'Food Mart'])
if st.button("Predict Sales"):
# Prepare the input data in the format expected by the backend API
# This will depend on how your backend processes the input
# Assuming your backend expects a list of lists corresponding to the processed features
# NOTE: This simplified example assumes the backend handles preprocessing.
# In a real scenario, you would need to send raw data and have the backend preprocess it
# or perform the exact same preprocessing steps here before sending.
# For demonstration, we are creating a dictionary matching the raw input structure
# and assuming the backend can handle this or you will adapt the backend.
# A robust solution would involve sending the raw data and letting the backend preprocess.
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_Establishment_Year': store_establishment_year,
'Store_Size': store_size,
'Store_Location_City_Type': store_location_city_type,
'Store_Type': store_type
}
# In the backend, we assumed input data is a list of lists for processed features.
# To make this work with the current backend code, we would need to:
# 1. Either send the raw data and modify the backend to preprocess it using the loaded preprocessor.
# 2. Or recreate the exact preprocessing steps here and send the processed data as a list of lists.
# Option 2 (recreating preprocessing here for simplicity in this frontend example - NOT RECOMMENDED FOR PRODUCTION without careful handling):
# This would be complex as it requires replicating the exact StandardScaler and OneHotEncoder logic
# and knowing the order of features after one-hot encoding.
# Option 1 (modifying backend to accept raw data and preprocess): This is the recommended approach.
# Since we cannot modify the backend from here, let's assume for this frontend example
# that we are sending the data in a way that the backend *can* process.
# A simple way to match the backend's assumed input format (list of lists)
# is to structure the raw data as a list containing a single list of values in the expected order.
# However, this *still* won't work directly with the backend's current assumption of *processed* features.
# Let's revert to assuming the backend expects raw data and you will update the backend accordingly.
# So, sending the raw input_data dictionary as a JSON object.
# You WILL need to update your backend's '/predict' endpoint to accept this raw data
# and apply the preprocessor (which you would also need to serialize and load in the backend).
# For now, sending the raw input data and acknowledging the backend needs modification.
try:
# Send the data to the backend API
# Sending as a list containing the dictionary to potentially handle batch predictions in the future
response = requests.post(BACKEND_API_URL, json=[input_data])
if response.status_code == 200:
predictions = response.json().get('predictions')
if predictions:
# Assuming the backend returns a list of predictions
predicted_sales = predictions[0] # Get the prediction for the single input
st.success(f"Predicted Product Store Sales Total: {predicted_sales:.2f}")
else:
st.error("Backend did not return predictions.")
else:
st.error(f"Error from backend: {response.status_code} - {response.text}")
st.write("Please ensure your backend API URL is correct and the API is running.")
except requests.exceptions.RequestException as e:
st.error(f"Error connecting to backend API: {e}")
st.write("Please ensure your backend API is running and accessible at the specified URL.")
st.markdown("---")
st.write("Note: Replace 'https://huggingface.co/spaces/hareeshkumarkn/hareesh539' with the actual URL of your deployed backend.")