Spaces:
Sleeping
Sleeping
File size: 7,332 Bytes
496bb9a | 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 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 | from __future__ import annotations
import json
import os
import shutil
import tempfile
import traceback
import zipfile
from pathlib import Path
from typing import List, Tuple
import gradio as gr
import nbformat
import pandas as pd
from nbclient import NotebookClient
ROOT = Path(__file__).resolve().parent
DEFAULT_NOTEBOOK = ROOT / "2a_Python_Analysis_Charlotte_Gers.ipynb"
DEFAULT_REVIEWS = ROOT / "synthetic_book_reviews.csv"
DEFAULT_SALES = ROOT / "synthetic_sales_data.csv"
def _safe_copy(src: Path, dst: Path) -> Path:
target = dst / src.name
shutil.copy2(src, target)
return target
def _find_files(workdir: Path) -> dict:
figures = sorted((workdir / "artifacts" / "py" / "figures").glob("*.png"))
tables = sorted((workdir / "artifacts" / "py" / "tables").glob("*.csv"))
json_files = sorted((workdir / "artifacts" / "py" / "figures").glob("*.json"))
return {
"figures": figures,
"tables": tables,
"json": json_files,
}
def _zip_results(workdir: Path) -> Path:
zip_path = workdir / "hf_space_outputs.zip"
with zipfile.ZipFile(zip_path, "w", zipfile.ZIP_DEFLATED) as zf:
for path in workdir.rglob("*"):
if path.is_file() and path.name != zip_path.name:
zf.write(path, arcname=path.relative_to(workdir))
return zip_path
def _read_csv_preview(csv_path: Path, rows: int = 15) -> pd.DataFrame:
return pd.read_csv(csv_path).head(rows)
def _run_notebook(notebook_path: Path, workdir: Path) -> Tuple[Path, List[str]]:
with notebook_path.open("r", encoding="utf-8") as f:
nb = nbformat.read(f, as_version=4)
for cell in nb.cells:
if cell.cell_type == "code" and cell.source.lstrip().startswith("!pip install"):
cell.source = 'print("Skipping notebook package install cell because dependencies are handled by requirements.txt")'
client = NotebookClient(
nb,
timeout=600,
kernel_name="python3",
allow_errors=False,
resources={"metadata": {"path": str(workdir)}},
)
client.execute()
executed_path = workdir / f"executed_{notebook_path.name}"
with executed_path.open("w", encoding="utf-8") as f:
nbformat.write(nb, f)
logs = []
for idx, cell in enumerate(nb.cells):
if cell.cell_type != "code":
continue
for output in cell.get("outputs", []):
if output.get("output_type") == "stream":
text = output.get("text", "").strip()
if text:
logs.append(f"Cell {idx}: {text}")
return executed_path, logs
def execute_pipeline(use_default_files, notebook_file, reviews_file, sales_file):
temp_root = Path(tempfile.mkdtemp(prefix="hf_space_run_"))
try:
if use_default_files:
if not (DEFAULT_NOTEBOOK.exists() and DEFAULT_REVIEWS.exists() and DEFAULT_SALES.exists()):
raise FileNotFoundError(
"Bundled default files are missing. Upload the notebook and both CSV files, or add them to the Space repository root."
)
notebook_src = DEFAULT_NOTEBOOK
reviews_src = DEFAULT_REVIEWS
sales_src = DEFAULT_SALES
else:
if notebook_file is None or reviews_file is None or sales_file is None:
raise ValueError("Please upload the notebook file and both CSV files.")
notebook_src = Path(notebook_file)
reviews_src = Path(reviews_file)
sales_src = Path(sales_file)
notebook_local = _safe_copy(notebook_src, temp_root)
_safe_copy(reviews_src, temp_root)
_safe_copy(sales_src, temp_root)
executed_path, logs = _run_notebook(notebook_local, temp_root)
found = _find_files(temp_root)
zip_path = _zip_results(temp_root)
gallery = [(str(p), p.name) for p in found["figures"]]
table_choices = [p.name for p in found["tables"]]
first_table = _read_csv_preview(found["tables"][0]) if found["tables"] else pd.DataFrame()
summary_lines = [
"Execution completed successfully.",
f"Notebook: {notebook_local.name}",
f"Figures generated: {len(found['figures'])}",
f"Tables generated: {len(found['tables'])}",
f"JSON artifacts: {len(found['json'])}",
]
if logs:
summary_lines.append("\nExecution log highlights:")
summary_lines.extend(logs[:20])
json_text = ""
if found["json"]:
json_text = "\n\nKPI JSON:\n" + found["json"][0].read_text(encoding="utf-8")
all_downloads = [str(executed_path), str(zip_path)] + [str(p) for p in found["tables"]] + [str(p) for p in found["json"]]
return (
"\n".join(summary_lines) + json_text,
gallery,
gr.update(choices=table_choices, value=table_choices[0] if table_choices else None),
first_table,
all_downloads,
)
except Exception:
error = traceback.format_exc()
return (
f"Execution failed.\n\n{error}",
[],
gr.update(choices=[], value=None),
pd.DataFrame(),
[],
)
def load_selected_table(table_name, use_default_files, notebook_file, reviews_file, sales_file):
if not table_name:
return pd.DataFrame()
candidate_roots = sorted(Path(tempfile.gettempdir()).glob("hf_space_run_*"), key=os.path.getmtime, reverse=True)
for root in candidate_roots:
candidate = root / "artifacts" / "py" / "tables" / table_name
if candidate.exists():
return _read_csv_preview(candidate)
return pd.DataFrame()
with gr.Blocks(title="Notebook Runner for Book Analytics") as demo:
gr.Markdown(
"""
# Notebook Runner for Book Analytics
Upload a Jupyter notebook and two CSV files, or run the bundled defaults.
The app executes the notebook, collects exported figures and tables, and returns a ZIP with all results.
"""
)
with gr.Row():
use_default_files = gr.Checkbox(value=True, label="Use bundled notebook and CSV files")
with gr.Row():
notebook_file = gr.File(label="Notebook (.ipynb)", file_count="single", type="filepath")
reviews_file = gr.File(label="Reviews CSV", file_count="single", type="filepath")
sales_file = gr.File(label="Sales CSV", file_count="single", type="filepath")
run_btn = gr.Button("Run notebook", variant="primary")
status_box = gr.Textbox(label="Run status", lines=18)
gallery = gr.Gallery(label="Generated figures", columns=1, height="auto")
table_selector = gr.Dropdown(label="Preview generated table")
table_preview = gr.Dataframe(label="Generated table preview")
downloads = gr.Files(label="Download outputs")
run_btn.click(
fn=execute_pipeline,
inputs=[use_default_files, notebook_file, reviews_file, sales_file],
outputs=[status_box, gallery, table_selector, table_preview, downloads],
)
table_selector.change(
fn=load_selected_table,
inputs=[table_selector, use_default_files, notebook_file, reviews_file, sales_file],
outputs=table_preview,
)
if __name__ == "__main__":
demo.launch()
|