Spaces:
Paused
Paused
| import streamlit as st | |
| import requests | |
| import json | |
| # Define the URL of your Flask backend | |
| # If running locally, it might be http://127.0.0.1:5000 | |
| # If deployed, use the public URL of your deployed Flask app | |
| # For Colab with ngrok, you would get the public ngrok URL | |
| # For Hugging Face Spaces with Flask, the URL would be different | |
| # IMPORTANT: Replace with your actual backend URL when deployed | |
| BACKEND_URL = 'YOUR_FLASK_BACKEND_URL_HERE' # <-- **UPDATE THIS URL** | |
| st.title('SuperKart Sales Forecasting') | |
| st.write(""" | |
| This application predicts the sales of a product in a SuperKart store based on its characteristics and store information. | |
| Enter the details below and click 'Predict Sales'. | |
| """) | |
| # Create input fields for the user to enter data | |
| st.header('Enter Product and Store Details:') | |
| # Example input fields (adjust based on your actual features expected by the Flask backend) | |
| # Ensure the keys used here match the keys expected by your Flask app's /predict endpoint | |
| # Numerical inputs | |
| product_weight = st.number_input('Product Weight', value=12.0) # Add appropriate min/max/default values | |
| product_allocated_area = st.number_input('Product Allocated Area', value=0.05) # Add appropriate min/max/default values | |
| product_mrp = st.number_input('Product MRP', value=150.0) # Add appropriate min/max/default values | |
| store_establishment_year = st.number_input('Store Establishment Year', value=2000, format="%d") # Add appropriate min/max/default values | |
| # Categorical inputs (use the expected categories from your original data) | |
| product_sugar_content = st.selectbox('Product Sugar Content', ['Low Sugar', 'Regular', 'No Sugar']) # Use actual categories | |
| product_type = st.selectbox('Product Type', ['Dairy', 'Soft Drinks', 'Meat', 'Fruits and Vegetables', 'Baking Goods', 'Health and Hygiene', 'Frozen Foods', 'Breads', 'Household', 'Snack Foods', 'Canned', 'Starchy Foods', 'Breakfast', 'Seafood', 'Others', 'Hard Drinks']) # Use actual categories | |
| store_id = st.selectbox('Store ID', ['OUT001', 'OUT002', 'OUT003', 'OUT004']) # Use actual categories | |
| store_size = st.selectbox('Store Size', ['Small', 'Medium', 'High']) # Use actual categories | |
| store_location_city_type = st.selectbox('Store Location City Type', ['Tier 1', 'Tier 2', 'Tier 3']) # Use actual categories | |
| store_type = st.selectbox('Store Type', ['Departmental Store', 'Supermarket Type1', 'Supermarket Type2', 'Food Mart']) # Use actual categories | |
| # Create a dictionary with the input data | |
| 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_Establishment_Year': store_establishment_year, | |
| 'Store_Size': store_size, | |
| 'Store_Location_City_Type': store_location_city_type, | |
| 'Store_Type': store_type | |
| } | |
| # Make a prediction when the user clicks a button | |
| if st.button('Predict Sales'): | |
| try: | |
| # Send the input data to the Flask backend for prediction | |
| response = requests.post(f'{BACKEND_URL}/predict', json=input_data) | |
| if response.status_code == 200: | |
| prediction_result = response.json() | |
| predicted_sales = prediction_result.get('prediction') | |
| st.subheader('Predicted Sales:') | |
| st.write(f'The predicted sales for this product in the specified store is: **{predicted_sales:.2f}**') | |
| else: | |
| st.error(f"Error from backend: {response.status_code} - {response.text}") | |
| except requests.exceptions.ConnectionError: | |
| st.error(f"Connection Error: Could not connect to the backend at {BACKEND_URL}. Please ensure the backend is running and the URL is correct.") | |
| except Exception as e: | |
| st.error(f"An error occurred: {e}") | |
| st.markdown("---") | |
| st.markdown("Note: This is a basic frontend. You need to update the `BACKEND_URL` with the actual URL of your deployed Flask backend.") | |