-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path1_Price_Prediction.py
131 lines (112 loc) Β· 3.71 KB
/
1_Price_Prediction.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import pandas as pd
import streamlit as st
from src.core.errors import ModelNotFoundError
from src.ml.price_predictor import PricePredictor
from src.property import _utils as prop_utils
from src.property.entity import ALL_PROPERTY
from src.property.form_options import form_options
from src.property.property_type import PropertyType
from src.typing import DatasetType, PropertyAlias
from src.typing import stop as _stop
from src.utils import st_pages
st.set_page_config("Price Prediction", "ποΈ", "centered", "expanded")
st_msg = st.container()
st.sidebar.selectbox(
"Choose the model for prediction",
options=["main", "user"],
format_func=lambda x: x.capitalize(),
help="This is used to select the model for prediction.",
key="DatasetType",
)
dataset_type: DatasetType = st.session_state["DatasetType"]
prop_type: PropertyAlias = st.sidebar.radio(
"Select Property Type",
options=list(ALL_PROPERTY.keys()),
format_func=st_pages.decorate_options,
key="PROPERTY_TYPE",
label_visibility="collapsed",
) # type: ignore
st.subheader(
st_pages.colorizer(st_pages.decorate_options(prop_type), "green"), divider="green"
)
selected_property = ALL_PROPERTY[prop_type]
price_predictor = PricePredictor(selected_property, dataset_type)
# Button to train model of the selected property
if not prop_utils.get_model_path(
selected_property.prop_type, dataset_type, "price_predictor"
).exists():
st.sidebar.button(
"π Train Model π",
use_container_width=True,
on_click=price_predictor.train,
type="primary",
)
else:
with st.spinner("Re-Training in progress..."):
_ = st.sidebar.button(
"π Re-Train Model π",
use_container_width=True,
)
if _:
price_predictor.train()
st_msg.info("Model Re-trained successfully.", icon="π
")
st.selectbox(
"Select City",
options=["Select ..."] + form_options.CITY(dataset_type, prop_type),
key="CITY",
)
if st.session_state["CITY"] != "Select ...":
st.selectbox(
"Select Locality",
options=form_options.LOCALITY_NAME(
st.session_state["CITY"], dataset_type, prop_type
),
key="LOCALITY_NAME",
)
else:
_stop()
def get_df_from_session_state(prop: PropertyType):
return pd.DataFrame.from_dict(
{
k: v
for k in prop.schema.ALL_COLS
for i, v in st.session_state.items()
if k == i
},
orient="index",
).T
# Show streamlit form according to selected prop_type
with st.form("predictor_form"):
try:
selected_property.st_form()
if st.form_submit_button():
st.toast("Form Submitted!", icon="π")
df = get_df_from_session_state(selected_property)
else:
_stop()
except ValueError as e:
st.toast("Got an Error!", icon="π΅βπ«")
st_msg.error(e, icon="π₯")
_stop()
# --- --- --- --- --- --- --- --- --- --- --- --- --- --- #
# Price Prediction
# --- --- --- --- --- --- --- --- --- --- --- --- --- --- #
try:
with st.spinner("Prediction in progress..."):
pred_price = price_predictor.predict(df)
except ModelNotFoundError as e:
st.toast("Error Occurred!!", icon="π΅βπ«")
st_msg.error(e, icon="π€")
# Button to train model of the selected property
st_msg.button(
"π Train Model π", # Applied `**` to provide unique key
use_container_width=True,
on_click=price_predictor.train,
type="secondary",
)
st.toast("Are you in a hurry?", icon="π
")
_stop()
st.subheader(
st_pages.colorizer(f"Prediction is {st_pages.format_price(pred_price)}"),
divider="rainbow",
)