Lokiiparihar commited on
Commit
224ee4e
·
verified ·
1 Parent(s): e877f04

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 re
 
 
2
  import streamlit as st
3
+ import pandas as pd
4
+ import joblib
5
+
6
+ # Load trained model
7
+ MODEL_PATH = "superkart_sales_prediction_model_v1_0.joblib"
8
+ model = joblib.load(MODEL_PATH)
9
+
10
+ valid_store_ids = ["OUT001", "OUT002", "OUT003", "OUT004"]
11
+ store_meta = {
12
+ "OUT001": {"year": 1987, "size": "High", "city_type": "Tier 2", "store_type": "Supermarket Type1"},
13
+ "OUT002": {"year": 1998, "size": "Small", "city_type": "Tier 3", "store_type": "Food Mart"},
14
+ "OUT003": {"year": 1999, "size": "Medium", "city_type": "Tier 1", "store_type": "Departmental Store"},
15
+ "OUT004": {"year": 2009, "size": "Medium", "city_type": "Tier 2", "store_type": "Supermarket Type2"},
16
+ }
17
+
18
+ st.title("SmartKart: Product Sales Prediction")
19
+ st.subheader("Online Prediction")
20
+
21
+ # Product ID validation
22
+ product_id = st.text_input("Product ID (2 uppercase letters + 4 digits, e.g., FD6114)", max_chars=6)
23
+ product_id_valid = bool(re.fullmatch(r"[A-Z]{2}\d{4}", product_id))
24
+ if product_id and not product_id_valid:
25
+ st.error("Invalid Product ID format; it must be 2 uppercase letters followed by 4 digits.")
26
+ elif product_id_valid:
27
+ st.success("Product ID format is valid.")
28
+
29
+ # Store selection
30
+ store_id = st.selectbox("Store ID", options=valid_store_ids)
31
+ meta = store_meta[store_id]
32
+ st.text(f"Store Establishment Year: {meta['year']}")
33
+ st.text(f"Store Size: {meta['size']}")
34
+ st.text(f"Store Location City Type: {meta['city_type']}")
35
+ st.text(f"Store Type: {meta['store_type']}")
36
+
37
+ # Product features
38
+ product_weight = st.number_input("Product Weight", min_value=0.0, value=12.0, format="%.2f")
39
+ product_sugar_content = st.selectbox("Product Sugar Content", ["Low Sugar", "Regular", "No Sugar"])
40
+ product_allocated_area = st.slider("Product Allocated Area Ratio", min_value=0.0, max_value=1.0, step=0.001, value=0.05)
41
+ product_type = st.selectbox("Product Type", [
42
+ "Meat", "Snack Foods", "Hard Drinks", "Dairy", "Canned", "Soft Drinks",
43
+ "Health and Hygiene", "Baking Goods", "Bread", "Breakfast", "Frozen Foods",
44
+ "Fruits and Vegetables", "Household", "Seafood", "Starchy Foods", "Others"
45
+ ])
46
+ product_mrp = st.number_input("Product MRP", min_value=0.0, value=150.0, format="%.2f")
47
+
48
+ # Prepare dataframe
49
+ input_data = pd.DataFrame([{
50
+ "Product_Id": product_id,
51
+ "Product_Weight": product_weight,
52
+ "Product_Sugar_Content": product_sugar_content,
53
+ "Product_Allocated_Area": product_allocated_area,
54
+ "Product_Type": product_type,
55
+ "Product_MRP": product_mrp,
56
+ "Store_Id": store_id,
57
+ "Store_Establishment_Year": meta['year'],
58
+ "Store_Size": meta['size'],
59
+ "Store_Location_City_Type": meta['city_type'],
60
+ "Store_Type": meta['store_type'],
61
+ }])
62
 
63
+ # Prediction
64
+ if st.button("Predict"):
65
+ if not product_id_valid:
66
+ st.error("Please fix the Product ID before proceeding.")
67
+ else:
68
+ try:
69
+ prediction = model.predict(input_data)[0]
70
+ st.success(f"Predicted Product Sales: {prediction:.2f}")
71
+ except Exception as e:
72
+ st.error(f"Prediction failed: {e}")