Update app.py
Browse files
app.py
CHANGED
|
@@ -1,298 +1,3 @@
|
|
| 1 |
-
"""Interactive Gradio app for the experimental wine-production forecast.
|
| 2 |
-
|
| 3 |
-
Run from the repository root with:
|
| 4 |
-
python src/app.py
|
| 5 |
-
|
| 6 |
-
The app is intentionally a visual prototype. It uses the same forecasting logic
|
| 7 |
-
as ``src/future_forecast.py`` and does not make causal claims about vineyard
|
| 8 |
-
area changes.
|
| 9 |
-
"""
|
| 10 |
-
|
| 11 |
-
from __future__ import annotations
|
| 12 |
-
|
| 13 |
-
import pandas as pd
|
| 14 |
-
import gradio as gr
|
| 15 |
-
|
| 16 |
-
try:
|
| 17 |
-
from future_forecast import evaluate_forecasters, forecast_2026, load_history
|
| 18 |
-
except ModuleNotFoundError:
|
| 19 |
-
from src.future_forecast import evaluate_forecasters, forecast_2026, load_history
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
def number(value: float | int, decimals: int = 0) -> str:
|
| 23 |
-
"""Format numbers for cards and tables."""
|
| 24 |
-
if pd.isna(value):
|
| 25 |
-
return ""
|
| 26 |
-
return f"{value:,.{decimals}f}"
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
def format_forecast_table(data: pd.DataFrame) -> pd.DataFrame:
|
| 30 |
-
"""Return a human-readable forecast table for display in Gradio."""
|
| 31 |
-
view = data.copy()
|
| 32 |
-
numeric_columns = [
|
| 33 |
-
"vineyard_area_ha",
|
| 34 |
-
"predicted_production_hl",
|
| 35 |
-
"lower_90_hl",
|
| 36 |
-
"upper_90_hl",
|
| 37 |
-
]
|
| 38 |
-
for column in numeric_columns:
|
| 39 |
-
if column in view.columns:
|
| 40 |
-
view[column] = view[column].map(lambda value: number(value))
|
| 41 |
-
|
| 42 |
-
return view.rename(
|
| 43 |
-
columns={
|
| 44 |
-
"region": "Region",
|
| 45 |
-
"year_start": "Year",
|
| 46 |
-
"vineyard_area_ha": "Area (ha)",
|
| 47 |
-
"selected_model": "Selected model",
|
| 48 |
-
"predicted_production_hl": "Predicted production (hl)",
|
| 49 |
-
"lower_90_hl": "Lower empirical 90% (hl)",
|
| 50 |
-
"upper_90_hl": "Upper empirical 90% (hl)",
|
| 51 |
-
}
|
| 52 |
-
)
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
def format_metrics_table(data: pd.DataFrame) -> pd.DataFrame:
|
| 56 |
-
"""Round and rename metric columns for a cleaner dashboard table."""
|
| 57 |
-
view = data[["model", "MAE", "RMSE", "R2"]].copy()
|
| 58 |
-
view["MAE"] = view["MAE"].map(lambda value: number(value))
|
| 59 |
-
view["RMSE"] = view["RMSE"].map(lambda value: number(value))
|
| 60 |
-
view["R2"] = view["R2"].map(lambda value: f"{value:.3f}")
|
| 61 |
-
return view.rename(
|
| 62 |
-
columns={
|
| 63 |
-
"model": "Model",
|
| 64 |
-
"MAE": "MAE (hl)",
|
| 65 |
-
"RMSE": "RMSE (hl)",
|
| 66 |
-
"R2": "R²",
|
| 67 |
-
}
|
| 68 |
-
)
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
history = load_history()
|
| 72 |
-
evaluation = evaluate_forecasters()
|
| 73 |
-
base_forecast = forecast_2026(evaluation=evaluation)
|
| 74 |
-
|
| 75 |
-
latest_area = (
|
| 76 |
-
history.sort_values(["region", "year_start"])
|
| 77 |
-
.groupby("region", as_index=False)
|
| 78 |
-
.tail(1)[["region", "vineyard_area_ha"]]
|
| 79 |
-
)
|
| 80 |
-
|
| 81 |
-
region_choices = sorted(base_forecast["region"].astype(str).unique().tolist())
|
| 82 |
-
default_region = region_choices[0]
|
| 83 |
-
area_map = dict(zip(latest_area["region"].astype(str), latest_area["vineyard_area_ha"]))
|
| 84 |
-
|
| 85 |
-
validation_metrics = (
|
| 86 |
-
evaluation.metrics.query("split == 'rolling_validation_2018_2022'")
|
| 87 |
-
.sort_values("MAE")
|
| 88 |
-
.reset_index(drop=True)
|
| 89 |
-
)
|
| 90 |
-
test_metrics = (
|
| 91 |
-
evaluation.metrics.query("split == 'test_2023_2025'")
|
| 92 |
-
.sort_values("MAE")
|
| 93 |
-
.reset_index(drop=True)
|
| 94 |
-
)
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
def update_area(region: str):
|
| 98 |
-
"""Reset the area input to the latest observed area for the chosen region."""
|
| 99 |
-
return gr.update(value=round(float(area_map.get(region, 0.0)), 2))
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
def simulate(region: str, area_ha: float):
|
| 103 |
-
"""Forecast one region under the selected vineyard-area scenario."""
|
| 104 |
-
if not region:
|
| 105 |
-
raise gr.Error("Please choose a region.")
|
| 106 |
-
if area_ha is None or area_ha <= 0:
|
| 107 |
-
raise gr.Error("Area (ha) must be greater than 0.")
|
| 108 |
-
|
| 109 |
-
result = forecast_2026(
|
| 110 |
-
evaluation=evaluation,
|
| 111 |
-
area_overrides={region: float(area_ha)},
|
| 112 |
-
)
|
| 113 |
-
selected = result[result["region"] == region].copy().reset_index(drop=True)
|
| 114 |
-
if selected.empty:
|
| 115 |
-
raise gr.Error(f"No forecast returned for region: {region}")
|
| 116 |
-
|
| 117 |
-
row = selected.iloc[0]
|
| 118 |
-
summary = f"""
|
| 119 |
-
<div class="forecast-card">
|
| 120 |
-
<div class="forecast-eyebrow">2026/27 forecast</div>
|
| 121 |
-
<div class="forecast-title">{row['region']}</div>
|
| 122 |
-
<div class="forecast-grid">
|
| 123 |
-
<div class="big-stat">
|
| 124 |
-
<span>Predicted production</span>
|
| 125 |
-
<strong>{number(row['predicted_production_hl'])} hl</strong>
|
| 126 |
-
</div>
|
| 127 |
-
<div class="big-stat">
|
| 128 |
-
<span>Empirical 90% interval</span>
|
| 129 |
-
<strong>{number(row['lower_90_hl'])} – {number(row['upper_90_hl'])} hl</strong>
|
| 130 |
-
</div>
|
| 131 |
-
<div class="big-stat">
|
| 132 |
-
<span>Vineyard-area scenario</span>
|
| 133 |
-
<strong>{number(row['vineyard_area_ha'], 2)} ha</strong>
|
| 134 |
-
</div>
|
| 135 |
-
<div class="big-stat">
|
| 136 |
-
<span>Selected model</span>
|
| 137 |
-
<strong>{row['selected_model']}</strong>
|
| 138 |
-
</div>
|
| 139 |
-
</div>
|
| 140 |
-
<p class="card-note">
|
| 141 |
-
Scenario output only: changing vineyard area does not prove causality.
|
| 142 |
-
</p>
|
| 143 |
-
</div>
|
| 144 |
-
"""
|
| 145 |
-
return summary, format_forecast_table(selected)
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
custom_css = """
|
| 149 |
-
:root {
|
| 150 |
-
--wine: #6f1232;
|
| 151 |
-
--wine-dark: #3d071a;
|
| 152 |
-
--rose: #fff4f6;
|
| 153 |
-
--rose-2: #fbe7eb;
|
| 154 |
-
--gold: #c29348;
|
| 155 |
-
--ink: #24151a;
|
| 156 |
-
--muted: #76676b;
|
| 157 |
-
}
|
| 158 |
-
|
| 159 |
-
.gradio-container {
|
| 160 |
-
max-width: 1180px !important;
|
| 161 |
-
margin: 0 auto !important;
|
| 162 |
-
background:
|
| 163 |
-
radial-gradient(circle at 10% 0%, rgba(194, 147, 72, 0.18), transparent 26%),
|
| 164 |
-
radial-gradient(circle at 95% 8%, rgba(111, 18, 50, 0.16), transparent 24%),
|
| 165 |
-
linear-gradient(180deg, #fffafa 0%, #fbf5f1 100%) !important;
|
| 166 |
-
}
|
| 167 |
-
|
| 168 |
-
.hero {
|
| 169 |
-
border-radius: 28px;
|
| 170 |
-
padding: 34px 38px;
|
| 171 |
-
margin: 8px 0 22px;
|
| 172 |
-
color: white;
|
| 173 |
-
background:
|
| 174 |
-
linear-gradient(135deg, rgba(61, 7, 26, 0.98), rgba(111, 18, 50, 0.94)),
|
| 175 |
-
radial-gradient(circle at 85% 20%, rgba(194, 147, 72, 0.45), transparent 28%);
|
| 176 |
-
box-shadow: 0 22px 55px rgba(61, 7, 26, 0.22);
|
| 177 |
-
}
|
| 178 |
-
|
| 179 |
-
.hero-kicker {
|
| 180 |
-
display: inline-block;
|
| 181 |
-
padding: 6px 12px;
|
| 182 |
-
border: 1px solid rgba(255, 255, 255, 0.28);
|
| 183 |
-
border-radius: 999px;
|
| 184 |
-
color: #ffe6b8;
|
| 185 |
-
font-size: 0.84rem;
|
| 186 |
-
letter-spacing: 0.08em;
|
| 187 |
-
text-transform: uppercase;
|
| 188 |
-
}
|
| 189 |
-
|
| 190 |
-
.hero h1 {
|
| 191 |
-
margin: 14px 0 8px;
|
| 192 |
-
font-size: clamp(2rem, 4vw, 3.5rem);
|
| 193 |
-
line-height: 1.02;
|
| 194 |
-
}
|
| 195 |
-
|
| 196 |
-
.hero p {
|
| 197 |
-
max-width: 780px;
|
| 198 |
-
color: rgba(255, 255, 255, 0.86);
|
| 199 |
-
font-size: 1.04rem;
|
| 200 |
-
}
|
| 201 |
-
|
| 202 |
-
.metric-row {
|
| 203 |
-
display: grid;
|
| 204 |
-
grid-template-columns: repeat(3, minmax(0, 1fr));
|
| 205 |
-
gap: 14px;
|
| 206 |
-
margin: 0 0 20px;
|
| 207 |
-
}
|
| 208 |
-
|
| 209 |
-
.metric-card {
|
| 210 |
-
border: 1px solid rgba(111, 18, 50, 0.10);
|
| 211 |
-
border-radius: 20px;
|
| 212 |
-
padding: 18px 20px;
|
| 213 |
-
background: rgba(255, 255, 255, 0.78);
|
| 214 |
-
box-shadow: 0 12px 34px rgba(61, 7, 26, 0.08);
|
| 215 |
-
}
|
| 216 |
-
|
| 217 |
-
.metric-card span {
|
| 218 |
-
display: block;
|
| 219 |
-
color: var(--muted);
|
| 220 |
-
font-size: 0.83rem;
|
| 221 |
-
text-transform: uppercase;
|
| 222 |
-
letter-spacing: 0.06em;
|
| 223 |
-
}
|
| 224 |
-
|
| 225 |
-
.metric-card strong {
|
| 226 |
-
display: block;
|
| 227 |
-
margin-top: 6px;
|
| 228 |
-
color: var(--wine-dark);
|
| 229 |
-
font-size: 1.25rem;
|
| 230 |
-
}
|
| 231 |
-
|
| 232 |
-
.interpretation {
|
| 233 |
-
border-left: 5px solid var(--gold);
|
| 234 |
-
border-radius: 18px;
|
| 235 |
-
padding: 16px 18px;
|
| 236 |
-
margin-bottom: 22px;
|
| 237 |
-
background: rgba(255, 250, 239, 0.86);
|
| 238 |
-
color: var(--ink);
|
| 239 |
-
}
|
| 240 |
-
|
| 241 |
-
.panel-card {
|
| 242 |
-
border: 1px solid rgba(111, 18, 50, 0.11) !important;
|
| 243 |
-
border-radius: 24px !important;
|
| 244 |
-
padding: 18px !important;
|
| 245 |
-
background: rgba(255, 255, 255, 0.84) !important;
|
| 246 |
-
box-shadow: 0 16px 38px rgba(61, 7, 26, 0.07) !important;
|
| 247 |
-
}
|
| 248 |
-
|
| 249 |
-
.section-title {
|
| 250 |
-
color: var(--wine-dark);
|
| 251 |
-
margin-bottom: 6px;
|
| 252 |
-
}
|
| 253 |
-
|
| 254 |
-
.forecast-card {
|
| 255 |
-
border: 1px solid rgba(111, 18, 50, 0.14);
|
| 256 |
-
border-radius: 26px;
|
| 257 |
-
padding: 26px;
|
| 258 |
-
background:
|
| 259 |
-
linear-gradient(180deg, rgba(255, 255, 255, 0.96), rgba(255, 244, 246, 0.98));
|
| 260 |
-
box-shadow: 0 18px 46px rgba(61, 7, 26, 0.12);
|
| 261 |
-
}
|
| 262 |
-
|
| 263 |
-
.forecast-eyebrow {
|
| 264 |
-
color: var(--gold);
|
| 265 |
-
font-weight: 700;
|
| 266 |
-
text-transform: uppercase;
|
| 267 |
-
letter-spacing: 0.08em;
|
| 268 |
-
font-size: 0.78rem;
|
| 269 |
-
}
|
| 270 |
-
|
| 271 |
-
.forecast-title {
|
| 272 |
-
margin-top: 4px;
|
| 273 |
-
color: var(--wine-dark);
|
| 274 |
-
font-size: 2rem;
|
| 275 |
-
font-weight: 800;
|
| 276 |
-
}
|
| 277 |
-
|
| 278 |
-
.forecast-grid {
|
| 279 |
-
display: grid;
|
| 280 |
-
grid-template-columns: repeat(2, minmax(0, 1fr));
|
| 281 |
-
gap: 14px;
|
| 282 |
-
margin-top: 20px;
|
| 283 |
-
}
|
| 284 |
-
|
| 285 |
-
.big-stat {
|
| 286 |
-
border-radius: 18px;
|
| 287 |
-
padding: 15px 16px;
|
| 288 |
-
background: white;
|
| 289 |
-
border: 1px solid rgba(111, 18, 50, 0.08);
|
| 290 |
-
}
|
| 291 |
-
|
| 292 |
-
.big-stat span {
|
| 293 |
-
display: block;
|
| 294 |
-
color: var(--muted);
|
| 295 |
-
font-size: 0.82rem;
|
| 296 |
}
|
| 297 |
|
| 298 |
.big-stat strong {
|
|
@@ -325,20 +30,25 @@ button.primary {
|
|
| 325 |
theme = gr.themes.Soft(primary_hue="rose", secondary_hue="orange", neutral_hue="stone")
|
| 326 |
|
| 327 |
with gr.Blocks(
|
| 328 |
-
title="Wine Production
|
| 329 |
theme=theme,
|
| 330 |
css=custom_css,
|
| 331 |
) as demo:
|
| 332 |
gr.HTML(
|
| 333 |
"""
|
| 334 |
<section class="hero">
|
| 335 |
-
<span class="hero-kicker">
|
| 336 |
-
<h1>Wine Production
|
| 337 |
<p>
|
| 338 |
Forecast 2026/27 wine production for Portuguese viticultural
|
| 339 |
-
regions using lagged production, vineyard
|
| 340 |
rolling-origin model validation.
|
| 341 |
</p>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 342 |
</section>
|
| 343 |
"""
|
| 344 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
}
|
| 2 |
|
| 3 |
.big-stat strong {
|
|
|
|
| 30 |
theme = gr.themes.Soft(primary_hue="rose", secondary_hue="orange", neutral_hue="stone")
|
| 31 |
|
| 32 |
with gr.Blocks(
|
| 33 |
+
title="Wine Production Dashboard",
|
| 34 |
theme=theme,
|
| 35 |
css=custom_css,
|
| 36 |
) as demo:
|
| 37 |
gr.HTML(
|
| 38 |
"""
|
| 39 |
<section class="hero">
|
| 40 |
+
<span class="hero-kicker">Professional dashboard ? 2026/27 forecast</span>
|
| 41 |
+
<h1>Wine Production Dashboard</h1>
|
| 42 |
<p>
|
| 43 |
Forecast 2026/27 wine production for Portuguese viticultural
|
| 44 |
+
regions using lagged production, vineyard-area scenarios, and
|
| 45 |
rolling-origin model validation.
|
| 46 |
</p>
|
| 47 |
+
<div style="margin-top: 22px; display:flex; gap:12px; flex-wrap:wrap;">
|
| 48 |
+
<span style="background:rgba(255,255,255,.16); border:1px solid rgba(255,255,255,.22); padding:8px 12px; border-radius:999px;">?? IVV official data</span>
|
| 49 |
+
<span style="background:rgba(255,255,255,.16); border:1px solid rgba(255,255,255,.22); padding:8px 12px; border-radius:999px;">?? One-step-ahead model</span>
|
| 50 |
+
<span style="background:rgba(255,255,255,.16); border:1px solid rgba(255,255,255,.22); padding:8px 12px; border-radius:999px;">?? Scenario simulator</span>
|
| 51 |
+
</div>
|
| 52 |
</section>
|
| 53 |
"""
|
| 54 |
)
|