harasar commited on
Commit
a23f54b
·
verified ·
1 Parent(s): b4c318a

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +70 -38
src/streamlit_app.py CHANGED
@@ -1,40 +1,72 @@
1
- import altair as alt
2
- import numpy as np
3
- import pandas as pd
4
  import streamlit as st
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
 
6
- """
7
- # Welcome to Streamlit!
8
-
9
- Edit `/streamlit_app.py` to customize this app to your heart's desire :heart:.
10
- If you have any questions, checkout our [documentation](https://docs.streamlit.io) and [community
11
- forums](https://discuss.streamlit.io).
12
-
13
- In the meantime, below is an example of what you can do with just a few lines of code:
14
- """
15
-
16
- num_points = st.slider("Number of points in spiral", 1, 10000, 1100)
17
- num_turns = st.slider("Number of turns in spiral", 1, 300, 31)
18
-
19
- indices = np.linspace(0, 1, num_points)
20
- theta = 2 * np.pi * num_turns * indices
21
- radius = indices
22
-
23
- x = radius * np.cos(theta)
24
- y = radius * np.sin(theta)
25
-
26
- df = pd.DataFrame({
27
- "x": x,
28
- "y": y,
29
- "idx": indices,
30
- "rand": np.random.randn(num_points),
31
- })
32
-
33
- st.altair_chart(alt.Chart(df, height=700, width=700)
34
- .mark_point(filled=True)
35
- .encode(
36
- x=alt.X("x", axis=None),
37
- y=alt.Y("y", axis=None),
38
- color=alt.Color("idx", legend=None, scale=alt.Scale()),
39
- size=alt.Size("rand", legend=None, scale=alt.Scale(range=[1, 150])),
40
- ))
 
 
 
 
1
  import streamlit as st
2
+ import pandas as pd
3
+ import numpy as np
4
+ import requests
5
+
6
+
7
+ # Streamlit UI for Price Prediction
8
+ st.title("SuperKart Sales Predictor")
9
+ st.write("This tool predicts the sales based on various store parameters.")
10
+
11
+ st.subheader("Enter the store details(Single Predication):")
12
+
13
+ # Collect user input
14
+ product_weight = st.number_input("Product Weight (in kg)", min_value=1.0, max_value=30.0)
15
+ product_sugar = st.selectbox("Sugar Content", ["Low Sugar", "Regular", "No Sugar"])
16
+ product_area = st.slider("Allocated Area (sq m)", min_value=0.0, max_value=1.0, step=0.01)
17
+ product_type = st.selectbox("Product Type", ["Fruits and Vegetables", "Snack Foods", "Frozen Foods", "Dairy", "Household","Baking Goods", "Canned", "Health and Hygiene", "Meat", "Breads","Hard Drinks", "Soft Drinks", "Seafood", "Starchy Foods", "Others"])
18
+ product_mrp = st.number_input("Product MRP", min_value=10.0, max_value=300.0)
19
+ store_year = st.number_input("Store Establishment Year", min_value=1980, max_value=2025)
20
+ store_size = st.selectbox("Store Size", ["Small", "Medium", "High"])
21
+ store_city = st.selectbox("City Type", ["Tier 1", "Tier 2", "Tier 3"])
22
+ store_type = st.selectbox("Store Type", ["Supermarket Type1", "Supermarket Type2", "Food Mart", "Departmental Store"])
23
+
24
+ # Prepare input
25
+ if st.button("Predict Sales"):
26
+ input_df = {
27
+ "Product_Weight": product_weight,
28
+ "Product_Sugar_Content": product_sugar,
29
+ "Product_Allocated_Area": product_area,
30
+ "Product_Type": product_type,
31
+ "Product_MRP": product_mrp,
32
+ "Store_Establishment_Year": 2025 - store_year, # we have modified this to get the store age
33
+ "Store_Size": store_size,
34
+ "Store_Location_City_Type": store_city,
35
+ "Store_Type": store_type
36
+ }
37
+ response = requests.post("https://harasar-SuperKartBackend.hf.space/v1/customer", json=input_df) # enter user name and space name before running the cell
38
+ if response.status_code == 200:
39
+ result = response.json()
40
+ churn_prediction = result["predicted_sales"] # Extract only the value
41
+ st.write(f"Based on the information provided, the sproject sales is likely to {churn_prediction}.")
42
+ else:
43
+ st.error("Error in API request")
44
+
45
+ #Batch Prediction
46
+ uploaded_file = st.file_uploader("Upload CSV file", type=["csv"])
47
+
48
+ if st.button("Predict for Batch"):
49
+ if uploaded_file is not None:
50
+ try:
51
+ # Convert uploaded file to a DataFrame
52
+ df = pd.read_csv(uploaded_file)
53
+
54
+ # Convert DataFrame to CSV bytes like your working script
55
+ csv_bytes = df.to_csv(index=False).encode('utf-8')
56
+
57
+ # Send POST request with raw bytes
58
+ response = requests.post(
59
+ "https://harasar-SuperKartBackend.hf.space/v1/customerbatch",
60
+ files={"file": ("SuperKart.csv", csv_bytes, "text/csv")}
61
+ )
62
+
63
+ if response.status_code == 200:
64
+ st.success("Batch prediction successful!")
65
+ st.write(response.json())
66
+ else:
67
+ st.error(f"Error {response.status_code}: {response.text}")
68
 
69
+ except Exception as e:
70
+ st.error(f"Upload failed: {e}")
71
+ else:
72
+ st.warning("Please upload a CSV file first.")