md-fe / app.py
bhumitps's picture
Upload ./app.py with huggingface_hub
8999842 verified
import streamlit as st
import requests
import pandas as pd
import json
from datetime import datetime
st.title("SuperKart Sales Forecasting")
st.write("Enter the details of the product and store to get a sales forecast.")
# Input fields for the user to provide data
product_id = st.text_input("Product ID")
product_weight = st.number_input("Product Weight", value=10.0, format="%.2f")
product_sugar_content = st.selectbox("Product Sugar Content", ['Low Sugar', 'Regular', 'No Sugar', 'reg'])
product_allocated_area = st.number_input("Product Allocated Area", value=0.1, format="%.3f")
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", value=150.0, format="%.2f")
store_id = st.selectbox("Store ID", ['OUT004', 'OUT003', 'OUT001', 'OUT002'])
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 2', 'Tier 1', 'Tier 3'])
store_type = st.selectbox("Store Type", ['Supermarket Type2', 'Departmental Store', 'Supermarket Type1', 'Food Mart'])
# Create a dictionary with the input data
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
}
# Add feature engineering for Product_Category and Store_Age
current_year = datetime.now().year
input_data['Store_Age'] = current_year - input_data['Store_Establishment_Year']
input_data['Product_Category'] = input_data['Product_Id'][:2]
# Button to trigger prediction
if st.button("Predict Sales"):
# Hugging Face proxy URL for Flask backend
# Replace with the actual URL of your deployed backend API on Hugging Face Spaces
backend_url = "https://bhumitps-md-be.hf.space/predict"
try:
# Send a POST request to the backend API
response = requests.post(backend_url, json=[input_data]) # Send as a list of one data point
# Check if the request was successful
if response.status_code == 200:
predictions = response.json().get('predictions')
if predictions:
st.success(f"Predicted Sales: {predictions[0]:.2f}")
else:
st.error("Error: Could not retrieve predictions from the backend.")
else:
st.error(f"Error: Received status code {response.status_code} from the backend.")
st.error(f"Response: {response.text}")
except requests.exceptions.RequestException as e:
st.error(f"Error connecting to the backend API: {e}")