Yulu1 commited on
Commit
b8b7cf0
·
verified ·
1 Parent(s): 5d8abfd

Delete app.py

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