Lokiiparihar commited on
Commit
5ba44a7
·
verified ·
1 Parent(s): e3ab10b

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +62 -0
app.py ADDED
@@ -0,0 +1,62 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import pandas as pd
3
+ import requests # Import requests for API calls
4
+
5
+ # --- Configuration for Backend API --- #
6
+ # This URL should point to your deployed backend Hugging Face Space
7
+ BACKEND_API_URL = "https://vyasmax9-superkartsalesbackend.hf.space" # Replace with your actual backend space URL
8
+ PREDICT_ENDPOINT = f"{BACKEND_API_URL}/v1/sales"
9
+
10
+ # Streamlit UI for Sales Prediction
11
+ st.title("SuperKart Sales Prediction App")
12
+ st.write("This tool predicts the sales revenue for a specific product in a SuperKart store.")
13
+
14
+ st.subheader("Enter the product and store details:")
15
+
16
+ # Collect user input for SuperKart sales prediction
17
+ product_id = st.selectbox("Product ID Prefix", ['FD', 'NC', 'DR'])
18
+ product_weight = st.number_input("Product Weight", min_value=0.0, value=12.0)
19
+ product_sugar_content = st.selectbox("Product Sugar Content", ['Low Sugar', 'Regular', 'No Sugar'])
20
+ product_allocated_area = st.number_input("Product Allocated Area", min_value=0.0, value=0.05)
21
+ product_type = st.selectbox("Product Type", ['Perishables', 'Non Perishables'])
22
+ product_mrp = st.number_input("Product MRP", min_value=0.0, value=150.0)
23
+ store_id = st.selectbox("Store ID", ['OUT004', 'OUT003', 'OUT001', 'OUT002'])
24
+ store_size = st.selectbox("Store Size", ['Medium', 'High', 'Small'])
25
+ store_location_city_type = st.selectbox("Store Location City Type", ['Tier 2', 'Tier 1', 'Tier 3'])
26
+ store_current_age = st.number_input("Store Current Age (Years)", min_value=0, value=15)
27
+
28
+ # Convert user input into a dictionary for JSON payload
29
+ input_data = {
30
+ 'Product_Id': product_id,
31
+ 'Product_Weight': product_weight,
32
+ 'Product_Sugar_Content': product_sugar_content,
33
+ 'Product_Allocated_Area': product_allocated_area,
34
+ 'Product_Type': product_type,
35
+ 'Product_MRP': product_mrp,
36
+ 'Store_Id': store_id,
37
+ 'Store_Size': store_size,
38
+ 'Store_Location_City_Type': store_location_city_type,
39
+ 'Store_Current_Age': store_current_age
40
+ }
41
+
42
+ # Predict button
43
+ if st.button("Predict Sales"):
44
+ try:
45
+ # Make a POST request to the backend API
46
+ response = requests.post(PREDICT_ENDPOINT, json=input_data)
47
+ response.raise_for_status() # Raise an exception for HTTP errors (4xx or 5xx)
48
+
49
+ predicted_sales = response.json().get('Predicted Sales')
50
+ if predicted_sales is not None:
51
+ st.write(f"The predicted sales revenue is ${predicted_sales:.2f}.")
52
+ else:
53
+ st.error("Prediction failed: Unexpected response from backend.")
54
+ st.json(response.json()) # Display full response for debugging
55
+
56
+ except requests.exceptions.ConnectionError:
57
+ st.error("Could not connect to the backend API. Please ensure the backend Space is running and accessible.")
58
+ except requests.exceptions.RequestException as e:
59
+ st.error(f"An error occurred during the API request: {e}")
60
+ st.text(response.text) # Display raw response text for debugging
61
+ except Exception as e:
62
+ st.error(f"An unexpected error occurred: {e}")