llauravalente commited on
Commit
d11778a
·
verified ·
1 Parent(s): a0eebef

Upload 6 files

Browse files
Files changed (7) hide show
  1. .gitattributes +1 -0
  2. Dockerfile +49 -0
  3. app.py +622 -0
  4. background_bottom.png +0 -0
  5. background_mid.png +0 -0
  6. background_top.png +3 -0
  7. style.css +326 -0
.gitattributes CHANGED
@@ -33,3 +33,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
 
 
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
36
+ background_top.png filter=lfs diff=lfs merge=lfs -text
Dockerfile ADDED
@@ -0,0 +1,49 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ FROM python:3.10-slim
2
+
3
+ ENV DEBIAN_FRONTEND=noninteractive
4
+ ENV PYTHONDONTWRITEBYTECODE=1
5
+ ENV PYTHONUNBUFFERED=1
6
+
7
+ ENV GRADIO_SERVER_NAME=0.0.0.0
8
+ ENV GRADIO_SERVER_PORT=7860
9
+
10
+ # System deps: R + compilers + common R pkg build deps
11
+ RUN apt-get update && apt-get install -y --no-install-recommends \
12
+ r-base \
13
+ r-base-dev \
14
+ build-essential \
15
+ curl \
16
+ git \
17
+ libcurl4-openssl-dev \
18
+ libssl-dev \
19
+ libxml2-dev \
20
+ && rm -rf /var/lib/apt/lists/*
21
+
22
+ # Install required R packages
23
+ RUN R -e "install.packages(c('forecast','ggplot2','jsonlite','readr','dplyr','tidyr','stringr','lubridate','broom'), repos='https://cloud.r-project.org')"
24
+
25
+ WORKDIR /app
26
+ COPY . /app
27
+
28
+ # Python deps (from requirements.txt)
29
+ RUN pip install --no-cache-dir -r requirements.txt
30
+
31
+ # Notebook execution deps
32
+ RUN pip install --no-cache-dir notebook ipykernel papermill
33
+
34
+ # Pre-install packages that the notebooks install via !pip install
35
+ # so papermill doesn't waste time or fail on them at runtime:
36
+ # datacreation.ipynb: beautifulsoup4 pandas matplotlib seaborn numpy textblob
37
+ # pythonanalysis.ipynb: pandas matplotlib seaborn numpy textblob faker transformers vaderSentiment
38
+ # Most are already in requirements.txt; add the extras:
39
+ RUN pip install --no-cache-dir textblob faker transformers
40
+
41
+ RUN python -m ipykernel install --user --name python3 --display-name "Python 3"
42
+
43
+ # R deps for notebook execution via papermill (IRkernel)
44
+ RUN R -e "install.packages('IRkernel', repos='https://cloud.r-project.org/')"
45
+ RUN R -e "IRkernel::installspec(user = FALSE)"
46
+
47
+ EXPOSE 7860
48
+
49
+ CMD ["python", "app.py"]
app.py ADDED
@@ -0,0 +1,622 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import re
3
+ import json
4
+ import time
5
+ import traceback
6
+ from pathlib import Path
7
+ from typing import Dict, Any, List, Optional, Tuple
8
+
9
+ import pandas as pd
10
+ import gradio as gr
11
+ import papermill as pm
12
+
13
+ # Optional LLM (HuggingFace Inference API)
14
+ try:
15
+ from huggingface_hub import InferenceClient
16
+ except Exception:
17
+ InferenceClient = None
18
+
19
+ # =========================================================
20
+ # CONFIG
21
+ # =========================================================
22
+
23
+ BASE_DIR = Path(__file__).resolve().parent
24
+
25
+ NB1 = os.environ.get("NB1", "datacreation.ipynb").strip()
26
+ NB2 = os.environ.get("NB2", "pythonanalysis.ipynb").strip()
27
+ NB3 = os.environ.get("NB3", "ranalysis.ipynb").strip()
28
+
29
+ RUNS_DIR = BASE_DIR / "runs"
30
+ ART_DIR = BASE_DIR / "artifacts"
31
+ PY_FIG_DIR = ART_DIR / "py" / "figures"
32
+ PY_TAB_DIR = ART_DIR / "py" / "tables"
33
+ R_FIG_DIR = ART_DIR / "r" / "figures"
34
+ R_TAB_DIR = ART_DIR / "r" / "tables"
35
+
36
+ PAPERMILL_TIMEOUT = int(os.environ.get("PAPERMILL_TIMEOUT", "1800"))
37
+ MAX_PREVIEW_ROWS = int(os.environ.get("MAX_FILE_PREVIEW_ROWS", "50"))
38
+ MAX_LOG_CHARS = int(os.environ.get("MAX_LOG_CHARS", "8000"))
39
+
40
+ HF_API_KEY = os.environ.get("HF_API_KEY", "").strip()
41
+ MODEL_NAME = os.environ.get("MODEL_NAME", "deepseek-ai/DeepSeek-R1").strip()
42
+ HF_PROVIDER = os.environ.get("HF_PROVIDER", "novita").strip()
43
+
44
+ LLM_ENABLED = bool(HF_API_KEY) and InferenceClient is not None
45
+ llm_client = (
46
+ InferenceClient(provider=HF_PROVIDER, api_key=HF_API_KEY)
47
+ if LLM_ENABLED
48
+ else None
49
+ )
50
+
51
+ # =========================================================
52
+ # HELPERS
53
+ # =========================================================
54
+
55
+ def ensure_dirs():
56
+ for p in [RUNS_DIR, ART_DIR, PY_FIG_DIR, PY_TAB_DIR, R_FIG_DIR, R_TAB_DIR]:
57
+ p.mkdir(parents=True, exist_ok=True)
58
+
59
+ def stamp():
60
+ return time.strftime("%Y%m%d-%H%M%S")
61
+
62
+ def tail(text: str, n: int = MAX_LOG_CHARS) -> str:
63
+ return (text or "")[-n:]
64
+
65
+ def _ls(dir_path: Path, exts: Tuple[str, ...]) -> List[str]:
66
+ if not dir_path.is_dir():
67
+ return []
68
+ return sorted(p.name for p in dir_path.iterdir() if p.is_file() and p.suffix.lower() in exts)
69
+
70
+ def _read_csv(path: Path) -> pd.DataFrame:
71
+ return pd.read_csv(path, nrows=MAX_PREVIEW_ROWS)
72
+
73
+ def _read_json(path: Path):
74
+ with path.open(encoding="utf-8") as f:
75
+ return json.load(f)
76
+
77
+ def artifacts_index() -> Dict[str, Any]:
78
+ return {
79
+ "python": {
80
+ "figures": _ls(PY_FIG_DIR, (".png", ".jpg", ".jpeg")),
81
+ "tables": _ls(PY_TAB_DIR, (".csv", ".json")),
82
+ },
83
+ "r": {
84
+ "figures": _ls(R_FIG_DIR, (".png", ".jpg", ".jpeg")),
85
+ "tables": _ls(R_TAB_DIR, (".csv", ".json")),
86
+ },
87
+ }
88
+
89
+ # =========================================================
90
+ # PIPELINE RUNNERS
91
+ # =========================================================
92
+
93
+ def run_notebook(nb_name: str) -> str:
94
+ ensure_dirs()
95
+ nb_in = BASE_DIR / nb_name
96
+ if not nb_in.exists():
97
+ return f"ERROR: {nb_name} not found."
98
+ nb_out = RUNS_DIR / f"run_{stamp()}_{nb_name}"
99
+ pm.execute_notebook(
100
+ input_path=str(nb_in),
101
+ output_path=str(nb_out),
102
+ cwd=str(BASE_DIR),
103
+ log_output=True,
104
+ progress_bar=False,
105
+ request_save_on_cell_execute=True,
106
+ execution_timeout=PAPERMILL_TIMEOUT,
107
+ )
108
+ return f"Executed {nb_name}"
109
+
110
+
111
+ def run_datacreation() -> str:
112
+ try:
113
+ log = run_notebook(NB1)
114
+ csvs = [f.name for f in BASE_DIR.glob("*.csv")]
115
+ return f"OK {log}\n\nCSVs now in /app:\n" + "\n".join(f" - {c}" for c in sorted(csvs))
116
+ except Exception as e:
117
+ return f"FAILED {e}\n\n{traceback.format_exc()[-2000:]}"
118
+
119
+
120
+ def run_pythonanalysis() -> str:
121
+ try:
122
+ log = run_notebook(NB2)
123
+ idx = artifacts_index()
124
+ figs = idx["python"]["figures"]
125
+ tabs = idx["python"]["tables"]
126
+ return (
127
+ f"OK {log}\n\n"
128
+ f"Figures: {', '.join(figs) or '(none)'}\n"
129
+ f"Tables: {', '.join(tabs) or '(none)'}"
130
+ )
131
+ except Exception as e:
132
+ return f"FAILED {e}\n\n{traceback.format_exc()[-2000:]}"
133
+
134
+
135
+ def run_r() -> str:
136
+ try:
137
+ log = run_notebook(NB3)
138
+ idx = artifacts_index()
139
+ figs = idx["r"]["figures"]
140
+ tabs = idx["r"]["tables"]
141
+ return (
142
+ f"OK {log}\n\n"
143
+ f"Figures: {', '.join(figs) or '(none)'}\n"
144
+ f"Tables: {', '.join(tabs) or '(none)'}"
145
+ )
146
+ except Exception as e:
147
+ return f"FAILED {e}\n\n{traceback.format_exc()[-2000:]}"
148
+
149
+
150
+ def run_full_pipeline() -> str:
151
+ logs = []
152
+ logs.append("=" * 50)
153
+ logs.append("STEP 1/3: Data Creation (web scraping + synthetic data)")
154
+ logs.append("=" * 50)
155
+ logs.append(run_datacreation())
156
+ logs.append("")
157
+ logs.append("=" * 50)
158
+ logs.append("STEP 2/3: Python Analysis (sentiment, ARIMA, dashboard)")
159
+ logs.append("=" * 50)
160
+ logs.append(run_pythonanalysis())
161
+ logs.append("")
162
+ logs.append("=" * 50)
163
+ logs.append("STEP 3/3: R Analysis (ETS/ARIMA forecasting)")
164
+ logs.append("=" * 50)
165
+ logs.append(run_r())
166
+ return "\n".join(logs)
167
+
168
+
169
+ # =========================================================
170
+ # GALLERY LOADERS
171
+ # =========================================================
172
+
173
+ def _load_all_figures() -> List[Tuple[str, str]]:
174
+ """Return list of (filepath, caption) for Gallery."""
175
+ items = []
176
+ for p in sorted(PY_FIG_DIR.glob("*.png")):
177
+ items.append((str(p), f"Python | {p.stem.replace('_', ' ').title()}"))
178
+ for p in sorted(R_FIG_DIR.glob("*.png")):
179
+ items.append((str(p), f"R | {p.stem.replace('_', ' ').title()}"))
180
+ return items
181
+
182
+
183
+ def _load_table_safe(path: Path) -> pd.DataFrame:
184
+ try:
185
+ if path.suffix == ".json":
186
+ obj = _read_json(path)
187
+ if isinstance(obj, dict):
188
+ return pd.DataFrame([obj])
189
+ return pd.DataFrame(obj)
190
+ return _read_csv(path)
191
+ except Exception as e:
192
+ return pd.DataFrame([{"error": str(e)}])
193
+
194
+
195
+ def refresh_gallery():
196
+ """Called when user clicks Refresh on Gallery tab."""
197
+ figures = _load_all_figures()
198
+ idx = artifacts_index()
199
+
200
+ # Build table choices
201
+ table_choices = []
202
+ for scope in ("python", "r"):
203
+ for name in idx[scope]["tables"]:
204
+ table_choices.append(f"{scope}/{name}")
205
+
206
+ # Default: show first table if available
207
+ default_df = pd.DataFrame()
208
+ if table_choices:
209
+ parts = table_choices[0].split("/", 1)
210
+ base = PY_TAB_DIR if parts[0] == "python" else R_TAB_DIR
211
+ default_df = _load_table_safe(base / parts[1])
212
+
213
+ return (
214
+ figures if figures else [],
215
+ gr.update(choices=table_choices, value=table_choices[0] if table_choices else None),
216
+ default_df,
217
+ )
218
+
219
+
220
+ def on_table_select(choice: str):
221
+ if not choice or "/" not in choice:
222
+ return pd.DataFrame([{"hint": "Select a table above."}])
223
+ scope, name = choice.split("/", 1)
224
+ base = {"python": PY_TAB_DIR, "r": R_TAB_DIR}.get(scope)
225
+ if not base:
226
+ return pd.DataFrame([{"error": f"Unknown scope: {scope}"}])
227
+ path = base / name
228
+ if not path.exists():
229
+ return pd.DataFrame([{"error": f"File not found: {path}"}])
230
+ return _load_table_safe(path)
231
+
232
+
233
+ # =========================================================
234
+ # KPI LOADER
235
+ # =========================================================
236
+
237
+ def load_kpis() -> Dict[str, Any]:
238
+ for candidate in [PY_TAB_DIR / "kpis.json", PY_FIG_DIR / "kpis.json"]:
239
+ if candidate.exists():
240
+ try:
241
+ return _read_json(candidate)
242
+ except Exception:
243
+ pass
244
+ return {}
245
+
246
+
247
+ # =========================================================
248
+ # AI DASHBOARD (Tab 3) -- LLM picks what to display
249
+ # =========================================================
250
+
251
+ DASHBOARD_SYSTEM = """You are an AI dashboard assistant for a book-sales analytics app.
252
+ The user asks questions or requests about their data. You have access to pre-computed
253
+ artifacts from Python and R analysis pipelines.
254
+
255
+ AVAILABLE ARTIFACTS (only reference ones that exist):
256
+ {artifacts_json}
257
+
258
+ KPI SUMMARY: {kpis_json}
259
+
260
+ YOUR JOB:
261
+ 1. Answer the user's question conversationally using the KPIs and your knowledge of the artifacts.
262
+ 2. At the END of your response, output a JSON block (fenced with ```json ... ```) that tells
263
+ the dashboard which artifact to display. The JSON must have this shape:
264
+ {{"show": "figure"|"table"|"none", "scope": "python"|"r", "filename": "..."}}
265
+
266
+ - Use "show": "figure" to display a chart image.
267
+ - Use "show": "table" to display a CSV/JSON table.
268
+ - Use "show": "none" if no artifact is relevant.
269
+
270
+ RULES:
271
+ - If the user asks about sales trends or forecasting by title, show sales_trends or arima figures.
272
+ - If the user asks about sentiment, show sentiment figure or sentiment_counts table.
273
+ - If the user asks about R regression, the R notebook focuses on forecasting, show accuracy_table.csv.
274
+ - If the user asks about forecast accuracy or model comparison, show accuracy_table.csv or forecast_compare.png.
275
+ - If the user asks about top sellers, show top_titles_by_units_sold.csv.
276
+ - If the user asks a general data question, pick the most relevant artifact.
277
+ - Keep your answer concise (2-4 sentences), then the JSON block.
278
+ """
279
+
280
+ JSON_BLOCK_RE = re.compile(r"```json\s*(\{.*?\})\s*```", re.DOTALL)
281
+ FALLBACK_JSON_RE = re.compile(r"\{[^{}]*\"show\"[^{}]*\}", re.DOTALL)
282
+
283
+
284
+ def _parse_display_directive(text: str) -> Dict[str, str]:
285
+ m = JSON_BLOCK_RE.search(text)
286
+ if m:
287
+ try:
288
+ return json.loads(m.group(1))
289
+ except json.JSONDecodeError:
290
+ pass
291
+ m = FALLBACK_JSON_RE.search(text)
292
+ if m:
293
+ try:
294
+ return json.loads(m.group(0))
295
+ except json.JSONDecodeError:
296
+ pass
297
+ return {"show": "none"}
298
+
299
+
300
+ def _clean_response(text: str) -> str:
301
+ """Strip the JSON directive block from the displayed response."""
302
+ return JSON_BLOCK_RE.sub("", text).strip()
303
+
304
+
305
+ def ai_chat(user_msg: str, history: list):
306
+ """Chat function for the AI Dashboard tab."""
307
+ if not user_msg or not user_msg.strip():
308
+ return history, "", None, None
309
+
310
+ idx = artifacts_index()
311
+ kpis = load_kpis()
312
+
313
+ if not LLM_ENABLED:
314
+ reply, directive = _keyword_fallback(user_msg, idx, kpis)
315
+ else:
316
+ system = DASHBOARD_SYSTEM.format(
317
+ artifacts_json=json.dumps(idx, indent=2),
318
+ kpis_json=json.dumps(kpis, indent=2) if kpis else "(no KPIs yet, run the pipeline first)",
319
+ )
320
+ msgs = [{"role": "system", "content": system}]
321
+ for entry in (history or [])[-6:]:
322
+ msgs.append(entry)
323
+ msgs.append({"role": "user", "content": user_msg})
324
+
325
+ try:
326
+ r = llm_client.chat_completion(
327
+ model=MODEL_NAME,
328
+ messages=msgs,
329
+ temperature=0.3,
330
+ max_tokens=600,
331
+ stream=False,
332
+ )
333
+ raw = (
334
+ r["choices"][0]["message"]["content"]
335
+ if isinstance(r, dict)
336
+ else r.choices[0].message.content
337
+ )
338
+ directive = _parse_display_directive(raw)
339
+ reply = _clean_response(raw)
340
+ except Exception as e:
341
+ reply = f"LLM error: {e}. Falling back to keyword matching."
342
+ reply_fb, directive = _keyword_fallback(user_msg, idx, kpis)
343
+ reply += "\n\n" + reply_fb
344
+
345
+ # Resolve artifact paths
346
+ fig_out = None
347
+ tab_out = None
348
+ show = directive.get("show", "none")
349
+ scope = directive.get("scope", "")
350
+ fname = directive.get("filename", "")
351
+
352
+ if show == "figure" and scope and fname:
353
+ base = {"python": PY_FIG_DIR, "r": R_FIG_DIR}.get(scope)
354
+ if base and (base / fname).exists():
355
+ fig_out = str(base / fname)
356
+ else:
357
+ reply += f"\n\n*(Could not find figure: {scope}/{fname})*"
358
+
359
+ if show == "table" and scope and fname:
360
+ base = {"python": PY_TAB_DIR, "r": R_TAB_DIR}.get(scope)
361
+ if base and (base / fname).exists():
362
+ tab_out = _load_table_safe(base / fname)
363
+ else:
364
+ reply += f"\n\n*(Could not find table: {scope}/{fname})*"
365
+
366
+ new_history = (history or []) + [
367
+ {"role": "user", "content": user_msg},
368
+ {"role": "assistant", "content": reply},
369
+ ]
370
+
371
+ return new_history, "", fig_out, tab_out
372
+
373
+
374
+ def _keyword_fallback(msg: str, idx: Dict, kpis: Dict) -> Tuple[str, Dict]:
375
+ """Simple keyword matcher when LLM is unavailable."""
376
+ msg_lower = msg.lower()
377
+
378
+ if not any(idx[s]["figures"] or idx[s]["tables"] for s in ("python", "r")):
379
+ return (
380
+ "No artifacts found yet. Please run the pipeline first (Tab 1), "
381
+ "then come back here to explore the results.",
382
+ {"show": "none"},
383
+ )
384
+
385
+ kpi_text = ""
386
+ if kpis:
387
+ total = kpis.get("total_units_sold", 0)
388
+ kpi_text = (
389
+ f"Quick summary: **{kpis.get('n_titles', '?')}** book titles across "
390
+ f"**{kpis.get('n_months', '?')}** months, with **{total:,.0f}** total units sold."
391
+ )
392
+
393
+ if any(w in msg_lower for w in ["trend", "sales trend", "monthly sale"]):
394
+ return (
395
+ f"Here are the sales trends for sampled titles. {kpi_text}",
396
+ {"show": "figure", "scope": "python", "filename": "sales_trends_sampled_titles.png"},
397
+ )
398
+
399
+ if any(w in msg_lower for w in ["sentiment", "review", "positive", "negative"]):
400
+ return (
401
+ f"Here is the sentiment distribution across sampled book titles. {kpi_text}",
402
+ {"show": "figure", "scope": "python", "filename": "sentiment_distribution_sampled_titles.png"},
403
+ )
404
+
405
+ if any(w in msg_lower for w in ["arima", "forecast", "predict"]):
406
+ if "compar" in msg_lower or "ets" in msg_lower or "accuracy" in msg_lower:
407
+ if "forecast_compare.png" in idx.get("r", {}).get("figures", []):
408
+ return (
409
+ "Here is the ARIMA+Fourier vs ETS forecast comparison from the R analysis.",
410
+ {"show": "figure", "scope": "r", "filename": "forecast_compare.png"},
411
+ )
412
+ return (
413
+ f"Here are the ARIMA forecasts for sampled titles from the Python analysis. {kpi_text}",
414
+ {"show": "figure", "scope": "python", "filename": "arima_forecasts_sampled_titles.png"},
415
+ )
416
+
417
+ if any(w in msg_lower for w in ["regression", "lm", "coefficient", "price effect", "rating effect"]):
418
+ return (
419
+ "The R notebook focuses on forecasting rather than regression. "
420
+ "Here is the forecast accuracy comparison instead.",
421
+ {"show": "table", "scope": "r", "filename": "accuracy_table.csv"},
422
+ )
423
+
424
+ if any(w in msg_lower for w in ["top", "best sell", "popular", "rank"]):
425
+ return (
426
+ f"Here are the top-selling titles by units sold. {kpi_text}",
427
+ {"show": "table", "scope": "python", "filename": "top_titles_by_units_sold.csv"},
428
+ )
429
+
430
+ if any(w in msg_lower for w in ["accuracy", "benchmark", "rmse", "mape"]):
431
+ return (
432
+ "Here is the forecast accuracy comparison (ARIMA+Fourier vs ETS) from the R analysis.",
433
+ {"show": "table", "scope": "r", "filename": "accuracy_table.csv"},
434
+ )
435
+
436
+ if any(w in msg_lower for w in ["r analysis", "r output", "r result"]):
437
+ if "forecast_compare.png" in idx.get("r", {}).get("figures", []):
438
+ return (
439
+ "Here is the main R output: forecast model comparison plot.",
440
+ {"show": "figure", "scope": "r", "filename": "forecast_compare.png"},
441
+ )
442
+
443
+ if any(w in msg_lower for w in ["dashboard", "overview", "summary", "kpi"]):
444
+ return (
445
+ f"Dashboard overview: {kpi_text}\n\nAsk me about sales trends, sentiment, forecasts, "
446
+ "forecast accuracy, or top sellers to see specific visualizations.",
447
+ {"show": "table", "scope": "python", "filename": "df_dashboard.csv"},
448
+ )
449
+
450
+ # Default
451
+ return (
452
+ f"I can show you various analyses. {kpi_text}\n\n"
453
+ "Try asking about: **sales trends**, **sentiment**, **ARIMA forecasts**, "
454
+ "**forecast accuracy**, **top sellers**, or **dashboard overview**.",
455
+ {"show": "none"},
456
+ )
457
+
458
+
459
+ # =========================================================
460
+ # UI
461
+ # =========================================================
462
+
463
+ ensure_dirs()
464
+
465
+ def load_css() -> str:
466
+ css_path = BASE_DIR / "style.css"
467
+ return css_path.read_text(encoding="utf-8") if css_path.exists() else ""
468
+
469
+
470
+ with gr.Blocks(title="RX12 Workshop App") as demo:
471
+
472
+ gr.Markdown(
473
+ "# RX12 - Intro to Python and R - Workshop App\n"
474
+ "*The app to integrate the three notebooks in to get a functioning blueprint of the group project's final product*",
475
+ elem_id="escp_title",
476
+ )
477
+
478
+ # ===========================================================
479
+ # TAB 1 -- Pipeline Runner
480
+ # ===========================================================
481
+ with gr.Tab("Pipeline Runner"):
482
+ gr.Markdown(
483
+ )
484
+
485
+ with gr.Row():
486
+ with gr.Column(scale=1):
487
+ btn_nb1 = gr.Button(
488
+ "Step 1: Data Creation",
489
+ variant="secondary",
490
+ )
491
+ gr.Markdown(
492
+ )
493
+ with gr.Column(scale=1):
494
+ btn_nb2 = gr.Button(
495
+ "Step 2a: Python Analysis",
496
+ variant="secondary",
497
+ )
498
+ gr.Markdown(
499
+ )
500
+ with gr.Column(scale=1):
501
+ btn_r = gr.Button(
502
+ "Step 2b: R Analysis",
503
+ variant="secondary",
504
+ )
505
+ gr.Markdown(
506
+ )
507
+
508
+ with gr.Row():
509
+ btn_all = gr.Button(
510
+ "Run All 3 Steps",
511
+ variant="primary",
512
+ )
513
+
514
+ run_log = gr.Textbox(
515
+ label="Execution Log",
516
+ lines=18,
517
+ max_lines=30,
518
+ interactive=False,
519
+ )
520
+
521
+ btn_nb1.click(run_datacreation, outputs=[run_log])
522
+ btn_nb2.click(run_pythonanalysis, outputs=[run_log])
523
+ btn_r.click(run_r, outputs=[run_log])
524
+ btn_all.click(run_full_pipeline, outputs=[run_log])
525
+
526
+ # ===========================================================
527
+ # TAB 2 -- Results Gallery
528
+ # ===========================================================
529
+ with gr.Tab("Results Gallery"):
530
+ gr.Markdown(
531
+ "### All generated artifacts\n\n"
532
+ "After running the pipeline, click **Refresh** to load all figures and tables. "
533
+ "Figures are shown in the gallery; select a table from the dropdown to inspect it."
534
+ )
535
+
536
+ refresh_btn = gr.Button("Refresh Gallery", variant="primary")
537
+
538
+ gr.Markdown("#### Figures")
539
+ gallery = gr.Gallery(
540
+ label="All Figures (Python + R)",
541
+ columns=2,
542
+ height=480,
543
+ object_fit="contain",
544
+ )
545
+
546
+ gr.Markdown("#### Tables")
547
+ table_dropdown = gr.Dropdown(
548
+ label="Select a table to view",
549
+ choices=[],
550
+ interactive=True,
551
+ )
552
+ table_display = gr.Dataframe(
553
+ label="Table Preview",
554
+ interactive=False,
555
+ )
556
+
557
+ refresh_btn.click(
558
+ refresh_gallery,
559
+ outputs=[gallery, table_dropdown, table_display],
560
+ )
561
+ table_dropdown.change(
562
+ on_table_select,
563
+ inputs=[table_dropdown],
564
+ outputs=[table_display],
565
+ )
566
+
567
+ # ===========================================================
568
+ # TAB 3 -- AI Dashboard
569
+ # ===========================================================
570
+ with gr.Tab('"AI" Dashboard'):
571
+ gr.Markdown(
572
+ "### Ask questions, get visualisations\n\n"
573
+ "Describe what you want to see and the AI will pick the right chart or table. "
574
+ + (
575
+ "*LLM is active.*"
576
+ if LLM_ENABLED
577
+ else "*No API key detected \u2014 using keyword matching. "
578
+ "Set `HF_API_KEY` in Space secrets for full LLM support.*"
579
+ )
580
+ )
581
+
582
+ with gr.Row(equal_height=True):
583
+ with gr.Column(scale=1):
584
+ chatbot = gr.Chatbot(
585
+ label="Conversation",
586
+ height=380,
587
+ )
588
+ user_input = gr.Textbox(
589
+ label="Ask about your data",
590
+ placeholder="e.g. Show me sales trends / What drives revenue? / Compare forecast models",
591
+ lines=1,
592
+ )
593
+ gr.Examples(
594
+ examples=[
595
+ "Show me the sales trends",
596
+ "What does the sentiment look like?",
597
+ "Which titles sell the most?",
598
+ "Show the forecast accuracy comparison",
599
+ "Compare the ARIMA and ETS forecasts",
600
+ "Give me a dashboard overview",
601
+ ],
602
+ inputs=user_input,
603
+ )
604
+
605
+ with gr.Column(scale=1):
606
+ ai_figure = gr.Image(
607
+ label="Visualisation",
608
+ height=350,
609
+ )
610
+ ai_table = gr.Dataframe(
611
+ label="Data Table",
612
+ interactive=False,
613
+ )
614
+
615
+ user_input.submit(
616
+ ai_chat,
617
+ inputs=[user_input, chatbot],
618
+ outputs=[chatbot, user_input, ai_figure, ai_table],
619
+ )
620
+
621
+
622
+ demo.launch(css=load_css(), allowed_paths=[str(BASE_DIR)])
background_bottom.png ADDED
background_mid.png ADDED
background_top.png ADDED

Git LFS Details

  • SHA256: 27e963d20dbb7ae88368fb527d475c85ef0de3df63d8f0d7d5e2af7403a5b365
  • Pointer size: 131 Bytes
  • Size of remote file: 726 kB
style.css ADDED
@@ -0,0 +1,326 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /* --- Page background: Top header + mid repeating --- */
2
+ html, body {
3
+ background-color: rgb(40,9,109) !important;
4
+ background-image:
5
+ url('https://huggingface.co/spaces/ESCP/RX12WorkshopTemplate/resolve/main/background_top.png'),
6
+ url('https://huggingface.co/spaces/ESCP/RX12WorkshopTemplate/resolve/main/background_mid.png') !important;
7
+ background-position:
8
+ top center,
9
+ top center !important;
10
+ background-repeat:
11
+ no-repeat,
12
+ repeat-y !important;
13
+ background-size:
14
+ 100% auto,
15
+ 100% auto !important;
16
+ margin: 0 !important;
17
+ padding: 0 !important;
18
+ min-height: 100vh !important;
19
+ }
20
+
21
+ /* --- Fixed bottom banner using ::after on body --- */
22
+ body::after {
23
+ content: '' !important;
24
+ position: fixed !important;
25
+ bottom: 0 !important;
26
+ left: 0 !important;
27
+ right: 0 !important;
28
+ height: 130px !important;
29
+ background-image: url('https://huggingface.co/spaces/ESCP/RX12WorkshopTemplate/resolve/main/background_bottom.png') !important;
30
+ background-size: 100% 100% !important;
31
+ background-repeat: no-repeat !important;
32
+ background-position: bottom center !important;
33
+ pointer-events: none !important;
34
+ z-index: 9999 !important;
35
+ }
36
+
37
+ /* --- Main container: float over the banner --- */
38
+ .gradio-container {
39
+ max-width: 1400px !important;
40
+ width: 94vw !important;
41
+ margin: 0 auto !important;
42
+ padding-top: 220px !important;
43
+ padding-bottom: 150px !important; /* Space for bottom banner */
44
+ background: transparent !important;
45
+ }
46
+
47
+ /* --- Title in ESCP gold --- */
48
+ #escp_title h1 {
49
+ color: rgb(242,198,55) !important;
50
+ font-size: 3rem !important;
51
+ font-weight: 800 !important;
52
+ text-align: center !important;
53
+ margin: 0 0 12px 0 !important;
54
+ }
55
+
56
+ /* --- Subtitle --- */
57
+ #escp_title p, #escp_title em {
58
+ color: rgba(255,255,255,0.85) !important;
59
+ text-align: center !important;
60
+ }
61
+
62
+ /* --- Tab bar background --- */
63
+ .tabs > .tab-nav,
64
+ .tab-nav,
65
+ div[role="tablist"],
66
+ .svelte-tabs > .tab-nav {
67
+ background: rgba(40,9,109,0.6) !important;
68
+ border-radius: 10px 10px 0 0 !important;
69
+ padding: 4px !important;
70
+ }
71
+
72
+ /* --- ALL tab buttons: force white text --- */
73
+ .tabs > .tab-nav button,
74
+ .tab-nav button,
75
+ div[role="tablist"] button,
76
+ button[role="tab"],
77
+ .svelte-tabs button,
78
+ .tab-nav > button,
79
+ .tabs button {
80
+ color: #ffffff !important;
81
+ font-weight: 600 !important;
82
+ border: none !important;
83
+ background: transparent !important;
84
+ padding: 10px 20px !important;
85
+ border-radius: 8px 8px 0 0 !important;
86
+ opacity: 1 !important;
87
+ }
88
+
89
+ /* --- Selected tab: ESCP gold --- */
90
+ .tabs > .tab-nav button.selected,
91
+ .tab-nav button.selected,
92
+ button[role="tab"][aria-selected="true"],
93
+ button[role="tab"].selected,
94
+ div[role="tablist"] button[aria-selected="true"],
95
+ .svelte-tabs button.selected {
96
+ color: rgb(242,198,55) !important;
97
+ background: rgba(255,255,255,0.12) !important;
98
+ }
99
+
100
+ /* --- Unselected tabs: ensure visibility --- */
101
+ .tabs > .tab-nav button:not(.selected),
102
+ .tab-nav button:not(.selected),
103
+ button[role="tab"][aria-selected="false"],
104
+ button[role="tab"]:not(.selected),
105
+ div[role="tablist"] button:not([aria-selected="true"]) {
106
+ color: #ffffff !important;
107
+ opacity: 1 !important;
108
+ }
109
+
110
+ /* --- White card panels --- */
111
+ .gradio-container .gr-block,
112
+ .gradio-container .gr-box,
113
+ .gradio-container .gr-panel,
114
+ .gradio-container .gr-group {
115
+ background: #ffffff !important;
116
+ border-radius: 10px !important;
117
+ }
118
+
119
+ /* --- Tab content area --- */
120
+ .tabitem {
121
+ background: rgba(255,255,255,0.95) !important;
122
+ border-radius: 0 0 10px 10px !important;
123
+ padding: 16px !important;
124
+ }
125
+
126
+ /* --- Inputs --- */
127
+ .gradio-container input,
128
+ .gradio-container textarea,
129
+ .gradio-container select {
130
+ background: #ffffff !important;
131
+ border: 1px solid #d1d5db !important;
132
+ border-radius: 8px !important;
133
+ }
134
+
135
+ /* --- Buttons: ESCP purple primary --- */
136
+ .gradio-container button:not([role="tab"]) {
137
+ font-weight: 600 !important;
138
+ padding: 10px 16px !important;
139
+ border-radius: 10px !important;
140
+ }
141
+
142
+ button.primary {
143
+ background-color: rgb(40,9,109) !important;
144
+ color: #ffffff !important;
145
+ border: none !important;
146
+ }
147
+
148
+ button.primary:hover {
149
+ background-color: rgb(60,20,140) !important;
150
+ }
151
+
152
+ button.secondary {
153
+ background-color: #ffffff !important;
154
+ color: rgb(40,9,109) !important;
155
+ border: 2px solid rgb(40,9,109) !important;
156
+ }
157
+
158
+ button.secondary:hover {
159
+ background-color: rgb(240,238,250) !important;
160
+ }
161
+
162
+ /* --- Dataframes --- */
163
+ [data-testid="dataframe"] {
164
+ background-color: #ffffff !important;
165
+ border-radius: 10px !important;
166
+ }
167
+
168
+ table {
169
+ font-size: 0.85rem !important;
170
+ }
171
+
172
+ /* --- Chatbot (AI Dashboard tab) --- */
173
+ .gr-chatbot {
174
+ min-height: 380px !important;
175
+ background-color: #ffffff !important;
176
+ border-radius: 12px !important;
177
+ }
178
+
179
+ .gr-chatbot .message.user {
180
+ background-color: rgb(232,225,250) !important;
181
+ border-radius: 12px !important;
182
+ }
183
+
184
+ .gr-chatbot .message.bot {
185
+ background-color: #f3f4f6 !important;
186
+ border-radius: 12px !important;
187
+ }
188
+
189
+ /* --- Gallery --- */
190
+ .gallery {
191
+ background: #ffffff !important;
192
+ border-radius: 10px !important;
193
+ }
194
+
195
+ /* --- Log textbox --- */
196
+ textarea {
197
+ font-family: monospace !important;
198
+ font-size: 0.8rem !important;
199
+ }
200
+
201
+ /* --- Markdown headings inside tabs --- */
202
+ .tabitem h3 {
203
+ color: rgb(40,9,109) !important;
204
+ font-weight: 700 !important;
205
+ }
206
+
207
+ .tabitem h4 {
208
+ color: #374151 !important;
209
+ }
210
+
211
+ /* --- Examples row (AI Dashboard) --- */
212
+ .examples-row button {
213
+ background: rgb(240,238,250) !important;
214
+ color: rgb(40,9,109) !important;
215
+ border: 1px solid rgb(40,9,109) !important;
216
+ border-radius: 8px !important;
217
+ font-size: 0.85rem !important;
218
+ }
219
+
220
+ .examples-row button:hover {
221
+ background: rgb(232,225,250) !important;
222
+ }
223
+
224
+ /* --- Header / footer: transparent over banner --- */
225
+ header, header *,
226
+ footer, footer * {
227
+ background: transparent !important;
228
+ box-shadow: none !important;
229
+ }
230
+
231
+ footer a, footer button,
232
+ header a, header button {
233
+ background: transparent !important;
234
+ border: none !important;
235
+ box-shadow: none !important;
236
+ }
237
+
238
+ #footer, #footer *,
239
+ [class*="footer"], [class*="footer"] *,
240
+ [class*="chip"], [class*="pill"], [class*="chip"] *, [class*="pill"] * {
241
+ background: transparent !important;
242
+ border: none !important;
243
+ box-shadow: none !important;
244
+ }
245
+
246
+ [data-testid*="api"], [data-testid*="settings"],
247
+ [id*="api"], [id*="settings"],
248
+ [class*="api"], [class*="settings"],
249
+ [class*="bottom"], [class*="toolbar"], [class*="controls"] {
250
+ background: transparent !important;
251
+ box-shadow: none !important;
252
+ }
253
+
254
+ [data-testid*="api"] *, [data-testid*="settings"] *,
255
+ [id*="api"] *, [id*="settings"] *,
256
+ [class*="api"] *, [class*="settings"] * {
257
+ background: transparent !important;
258
+ box-shadow: none !important;
259
+ }
260
+
261
+ section footer {
262
+ background: transparent !important;
263
+ }
264
+
265
+ section footer button,
266
+ section footer a {
267
+ background: transparent !important;
268
+ background-color: transparent !important;
269
+ border: none !important;
270
+ box-shadow: none !important;
271
+ color: white !important;
272
+ }
273
+
274
+ section footer button:hover,
275
+ section footer button:focus,
276
+ section footer a:hover,
277
+ section footer a:focus {
278
+ background: transparent !important;
279
+ background-color: transparent !important;
280
+ box-shadow: none !important;
281
+ }
282
+
283
+ section footer button,
284
+ section footer button * {
285
+ background: transparent !important;
286
+ background-color: transparent !important;
287
+ background-image: none !important;
288
+ box-shadow: none !important;
289
+ filter: none !important;
290
+ }
291
+
292
+ section footer button::before,
293
+ section footer button::after {
294
+ background: transparent !important;
295
+ background-color: transparent !important;
296
+ background-image: none !important;
297
+ box-shadow: none !important;
298
+ filter: none !important;
299
+ }
300
+
301
+ section footer a,
302
+ section footer a * {
303
+ background: transparent !important;
304
+ background-color: transparent !important;
305
+ box-shadow: none !important;
306
+ }
307
+
308
+ .gradio-container footer button,
309
+ .gradio-container footer button *,
310
+ .gradio-container .footer button,
311
+ .gradio-container .footer button * {
312
+ background: transparent !important;
313
+ background-color: transparent !important;
314
+ background-image: none !important;
315
+ box-shadow: none !important;
316
+ }
317
+
318
+ .gradio-container footer button::before,
319
+ .gradio-container footer button::after,
320
+ .gradio-container .footer button::before,
321
+ .gradio-container .footer button::after {
322
+ background: transparent !important;
323
+ background-color: transparent !important;
324
+ background-image: none !important;
325
+ box-shadow: none !important;
326
+ }