-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
166 lines (132 loc) Β· 6.18 KB
/
app.py
File metadata and controls
166 lines (132 loc) Β· 6.18 KB
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import streamlit as st
import pandas as pd
import requests
import plotly.express as px
import boto3, os
from pathlib import Path
API_URL = os.environ.get("API_URL", "http://127.0.0.1:8000/predict")
S3_BUCKET = os.getenv("S3_BUCKET", "model-regression-data")
REGION = os.getenv("AWS_REGION", "eu-west-2")
s3 = boto3.client("s3", region_name=REGION)
def load_from_s3(key, local_path):
"""Download from S3 if not already cached locally."""
local_path = Path(local_path)
if not local_path.exists():
os.makedirs(local_path.parent, exist_ok=True)
st.info(f"π₯ Downloading {key} from S3β¦")
s3.download_file(S3_BUCKET, key, str(local_path))
return str(local_path)
HOLDOUT_ENGINEERED_PATH = load_from_s3(
"processed/feature_engineered_holdout.csv",
"data/processed/feature_engineered_holdout.csv"
)
HOLDOUT_META_PATH = load_from_s3(
"processed/cleaning_holdout.csv",
"data/processed/cleaning_holdout.csv"
)
@st.cache_data
def load_data():
fe = pd.read_csv(HOLDOUT_ENGINEERED_PATH)
meta = pd.read_csv(HOLDOUT_META_PATH, parse_dates=["date"])[["date", "city_full"]]
if len(fe) != len(meta):
st.warning("β οΈ Engineered and meta holdout lengths differ. Aligning by index.")
min_len = min(len(fe), len(meta))
fe = fe.iloc[:min_len].copy()
meta = meta.iloc[:min_len].copy()
disp = pd.DataFrame(index=fe.index)
disp["date"] = meta["date"]
disp["region"] = meta["city_full"]
disp["year"] = disp["date"].dt.year
disp["month"] = disp["date"].dt.month
disp["actual_price"] = fe["price"]
return fe, disp
fe_df, disp_df = load_data()
st.title("π Housing Price Prediction β Holdout Explorer")
years = sorted(disp_df["year"].unique())
months = list(range(1, 13))
regions = ["All"] + sorted(disp_df["region"].dropna().unique())
col1, col2, col3 = st.columns(3)
with col1:
year = st.selectbox("Select Year", years, index=0)
with col2:
month = st.selectbox("Select Month", months, index=0)
with col3:
region = st.selectbox("Select Region", regions, index=0)
if st.button("Show Predictions π"):
mask = (disp_df["year"] == year) & (disp_df["month"] == month)
if region != "All":
mask &= (disp_df["region"] == region)
idx = disp_df.index[mask]
if len(idx) == 0:
st.warning("No data found for these filters.")
else:
st.write(f"π
Running predictions for **{year}-{month:02d}** | Region: **{region}**")
payload = fe_df.loc[idx].replace([float("inf"), float("-inf")], 0).fillna(0).to_dict(orient="records")
try:
resp = requests.post(API_URL, json=payload, timeout=60)
resp.raise_for_status()
out = resp.json()
preds = out.get("predictions", [])
actuals = out.get("actuals", None)
view = disp_df.loc[idx, ["date", "region", "actual_price"]].copy()
view = view.sort_values("date")
view["prediction"] = pd.Series(preds, index=view.index).astype(float)
if actuals is not None and len(actuals) == len(view):
view["actual_price"] = pd.Series(actuals, index=view.index).astype(float)
mae = (view["prediction"] - view["actual_price"]).abs().mean()
rmse = ((view["prediction"] - view["actual_price"]) ** 2).mean() ** 0.5
avg_pct_error = ((view["prediction"] - view["actual_price"]).abs() / view["actual_price"]).mean() * 100
st.subheader("Predictions vs Actuals")
st.dataframe(
view[["date", "region", "actual_price", "prediction"]].reset_index(drop=True),
use_container_width=True
)
c1, c2, c3 = st.columns(3)
with c1:
st.metric("MAE", f"{mae:,.0f}")
with c2:
st.metric("RMSE", f"{rmse:,.0f}")
with c3:
st.metric("Avg % Error", f"{avg_pct_error:.2f}%")
# Yearly trend: fetch predictions for all months in the selected year
if region == "All":
yearly_data = disp_df[disp_df["year"] == year].copy()
idx_all = yearly_data.index
payload_all = fe_df.loc[idx_all].replace([float("inf"), float("-inf")], 0).fillna(0).to_dict(orient="records")
resp_all = requests.post(API_URL, json=payload_all, timeout=60)
resp_all.raise_for_status()
preds_all = resp_all.json().get("predictions", [])
yearly_data["prediction"] = pd.Series(preds_all, index=yearly_data.index).astype(float)
else:
yearly_data = disp_df[(disp_df["year"] == year) & (disp_df["region"] == region)].copy()
idx_region = yearly_data.index
payload_region = fe_df.loc[idx_region].replace([float("inf"), float("-inf")], 0).fillna(0).to_dict(orient="records")
resp_region = requests.post(API_URL, json=payload_region, timeout=60)
resp_region.raise_for_status()
preds_region = resp_region.json().get("predictions", [])
yearly_data["prediction"] = pd.Series(preds_region, index=yearly_data.index).astype(float)
monthly_avg = yearly_data.groupby("month")[["actual_price", "prediction"]].mean().reset_index()
monthly_avg["highlight"] = monthly_avg["month"].apply(lambda m: "Selected" if m == month else "Other")
fig = px.line(
monthly_avg,
x="month",
y=["actual_price", "prediction"],
markers=True,
labels={"value": "Price", "month": "Month"},
title=f"Yearly Trend β {year}{'' if region=='All' else f' β {region}'}"
)
highlight_month = month
fig.add_vrect(
x0=highlight_month - 0.5,
x1=highlight_month + 0.5,
fillcolor="red",
opacity=0.1,
layer="below",
line_width=0,
)
st.plotly_chart(fig, use_container_width=True)
except Exception as e:
st.error(f"API call failed: {e}")
st.exception(e)
else:
st.info("Choose filters and click **Show Predictions** to compute.")