Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,43 +1,16 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import requests
|
| 3 |
import pandas as pd
|
| 4 |
-
from pathlib import Path
|
| 5 |
-
import random
|
| 6 |
|
| 7 |
API_BASE = "https://ewa-demand-forecast-api.onrender.com"
|
| 8 |
PREDICT_URL = f"{API_BASE}/predict"
|
| 9 |
|
| 10 |
-
CUSTOMERS_FILE = Path("customers.csv")
|
| 11 |
-
|
| 12 |
CONFIDENCE_MAP = {
|
| 13 |
"Standard": 0.90,
|
| 14 |
"Conservative (recommended)": 0.95,
|
| 15 |
"Wide range": 0.80,
|
| 16 |
}
|
| 17 |
|
| 18 |
-
TREND_MAP = {
|
| 19 |
-
"Down": -0.01,
|
| 20 |
-
"Stable": 0.00,
|
| 21 |
-
"Up": 0.01,
|
| 22 |
-
}
|
| 23 |
-
|
| 24 |
-
VOLATILITY_MAP = {
|
| 25 |
-
"Low": 0.03,
|
| 26 |
-
"Medium": 0.08,
|
| 27 |
-
"High": 0.15,
|
| 28 |
-
}
|
| 29 |
-
|
| 30 |
-
def load_customers() -> pd.DataFrame:
|
| 31 |
-
if not CUSTOMERS_FILE.exists():
|
| 32 |
-
return pd.DataFrame(columns=["customer_id", "label", "history"])
|
| 33 |
-
df = pd.read_csv(CUSTOMERS_FILE)
|
| 34 |
-
# Allow older format without label
|
| 35 |
-
if "label" not in df.columns:
|
| 36 |
-
df["label"] = df["customer_id"].astype(str)
|
| 37 |
-
if "history" not in df.columns:
|
| 38 |
-
df["history"] = ""
|
| 39 |
-
return df
|
| 40 |
-
|
| 41 |
def parse_numbers(text: str) -> list[float]:
|
| 42 |
if not text or not text.strip():
|
| 43 |
return []
|
|
@@ -51,58 +24,39 @@ def parse_numbers(text: str) -> list[float]:
|
|
| 51 |
pass
|
| 52 |
return nums
|
| 53 |
|
| 54 |
-
def
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
return (customer_id, history_text)
|
| 67 |
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
"""
|
| 72 |
-
trend = TREND_MAP[trend_choice]
|
| 73 |
-
vol = VOLATILITY_MAP[volatility_choice]
|
| 74 |
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
for _ in range(int(lookback_days)):
|
| 78 |
-
noise = random.uniform(-vol, vol)
|
| 79 |
-
v = max(0.0, v * (1.0 + trend + noise))
|
| 80 |
-
values.append(v)
|
| 81 |
-
# Ensure minimum length
|
| 82 |
-
if len(values) < 2:
|
| 83 |
-
values = values + values
|
| 84 |
-
return values
|
| 85 |
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
history = []
|
| 92 |
|
| 93 |
-
|
| 94 |
-
customer_id, history_text = get_history_by_label(selected_customer_label)
|
| 95 |
-
history = parse_numbers(history_text)
|
| 96 |
-
if len(history) < 2:
|
| 97 |
-
return (
|
| 98 |
-
"No history found for this Customer ID. Please switch to 'Quick estimate (no typing)'.",
|
| 99 |
-
None,
|
| 100 |
-
)
|
| 101 |
-
else:
|
| 102 |
-
history = build_synthetic_history(typical_level, trend_choice, volatility_choice, lookback_days)
|
| 103 |
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
|
|
|
|
|
|
|
|
|
| 108 |
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import requests
|
| 3 |
import pandas as pd
|
|
|
|
|
|
|
| 4 |
|
| 5 |
API_BASE = "https://ewa-demand-forecast-api.onrender.com"
|
| 6 |
PREDICT_URL = f"{API_BASE}/predict"
|
| 7 |
|
|
|
|
|
|
|
| 8 |
CONFIDENCE_MAP = {
|
| 9 |
"Standard": 0.90,
|
| 10 |
"Conservative (recommended)": 0.95,
|
| 11 |
"Wide range": 0.80,
|
| 12 |
}
|
| 13 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
def parse_numbers(text: str) -> list[float]:
|
| 15 |
if not text or not text.strip():
|
| 16 |
return []
|
|
|
|
| 24 |
pass
|
| 25 |
return nums
|
| 26 |
|
| 27 |
+
def forecast(recent_values_text: str, forecast_days: int, uncertainty_choice: str):
|
| 28 |
+
history = parse_numbers(recent_values_text)
|
| 29 |
+
if len(history) < 2:
|
| 30 |
+
return "Please enter at least 2 numbers. Example: 10, 11, 9, 12", None
|
| 31 |
+
|
| 32 |
+
payload = {
|
| 33 |
+
"history": history,
|
| 34 |
+
"horizon": int(forecast_days),
|
| 35 |
+
"confidence_level": float(CONFIDENCE_MAP[uncertainty_choice]),
|
| 36 |
+
"alpha": None,
|
| 37 |
+
"beta": None,
|
| 38 |
+
}
|
|
|
|
| 39 |
|
| 40 |
+
r = requests.post(PREDICT_URL, json=payload, timeout=60)
|
| 41 |
+
r.raise_for_status()
|
| 42 |
+
data = r.json()
|
|
|
|
|
|
|
|
|
|
| 43 |
|
| 44 |
+
forecast_vals = data.get("forecast", [])
|
| 45 |
+
intervals = data.get("confidence_intervals", []) or []
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
|
| 47 |
+
rows = []
|
| 48 |
+
for i, y in enumerate(forecast_vals):
|
| 49 |
+
lo = intervals[i].get("lower") if i < len(intervals) and isinstance(intervals[i], dict) else None
|
| 50 |
+
hi = intervals[i].get("upper") if i < len(intervals) and isinstance(intervals[i], dict) else None
|
| 51 |
+
rows.append({"Day": i + 1, "Expected": y, "Low": lo, "High": hi})
|
|
|
|
| 52 |
|
| 53 |
+
return "Forecast ready.", pd.DataFrame(rows)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
|
| 55 |
+
with gr.Blocks(title="Payactiv EWA Demand Forecast") as app:
|
| 56 |
+
gr.Markdown("# Payactiv EWA Demand Forecast")
|
| 57 |
+
|
| 58 |
+
with gr.Row():
|
| 59 |
+
with gr.Column():
|
| 60 |
+
recent_values = gr.Textbox(
|
| 61 |
+
label
|
| 62 |
|