Spaces:
Sleeping
Sleeping
Update app.py
Browse files122345677891044 unmodified lines55565758595859606162636465664 unmodified lines7172737475767778797 unmodified lines878889848590919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139921401411421439596979814414514614714863 unmodified lines21221321421521621721822 unmodified lines24124224319624424524624718 unmodified lines266267268221222269270271272273274275276277278279230280281282283284285286287# app.pyimport os, shutil, subprocess, zipfile, tracebackimport os, shutil, subprocess, zipfile, traceback, time, iofrom pathlib import Pathfrom datetime import datetimeimport gradio as gr
ROOT = Path(__file__).resolve().parentROOT = Path(_file_).resolve().parentDATA = ROOT / "dataset.jsonl"LOG = ROOT / "train.log"RUNS = ROOT / "runs"44 unmodified lines return f"β
Uploaded β {DATA.name}", ls_workspace() return "β Unexpected item; please upload a .jsonl file.", ls_workspace()
# -------- training --------def start_training(run_name):# -------- training (LIVE LOGS) --------def start_training_live(run_name): """ Streams training logs to the UI while the subprocess runs. Yields tuples for outputs: [status, download_file, workspace, logs, model_dropdown] """ run_id = (run_name or "").strip() or datetime.now().strftime("run_%Y%m%d_%H%M%S") out_dir = RUNS / run_id zip_path = RUNS / f"{run_id}.zip"4 unmodified lines if zip_path.exists(): zip_path.unlink()
# init log LOG.write_text(f"π₯ Training startedβ¦\nRun: {run_id}\n", encoding="utf-8") append_log(f"Workspace:\n{ls_workspace()}")
cmd = [ "python", str(ROOT / "train.py"),7 unmodified lines "--learning_rate", "5e-5", ] append_log("βΆ " + " ".join(cmd)) with open(LOG, "a", encoding="utf-8") as lf: code = subprocess.Popen(cmd, stdout=lf, stderr=subprocess.STDOUT).wait()
# start subprocess with live stdout proc = subprocess.Popen( cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, bufsize=1, universal_newlines=True, encoding="utf-8", errors="replace", )
live_log = io.StringIO() status_msg = f"π Training run '{run_id}' in progressβ¦" # stream loop while True: line = proc.stdout.readline() if line == "" and proc.poll() is not None: break if line: append_log(line.rstrip("\n")) live_log.write(line) # Trim to last ~20k chars for UI text = live_log.getvalue()[-20000:] # yield with download hidden (until zip exists) yield ( status_msg, gr.update(value=None, visible=False), ls_workspace(), text, dropdown_update_safe(list_models(), prefer=None), ) # if zip appears during training (e.g., early save), surface it if zip_path.exists(): yield ( "π¦ Model zip created during run.", gr.update(value=str(zip_path), visible=True), ls_workspace(), text, dropdown_update_safe(list_models(), prefer=None), )
code = proc.wait()
models = list_models() model_update = dropdown_update_safe(models, prefer=str(out_dir) if out_dir.exists() else None) final_logs = read_logs()
if code == 0 and zip_path.exists(): info = f"β
Training complete. Saved: {out_dir.name} | Zip: {zip_path.name}" dl_update = gr.update(value=str(zip_path), visible=True) append_log(info) yield (info, gr.update(value=str(zip_path), visible=True), ls_workspace(), final_logs, model_update) else: info = f"β Training failed (exit {code}). Check logs below." dl_update = gr.update(value=None, visible=False)
append_log(info) return info, dl_update, ls_workspace(), read_logs(), model_update append_log(info) yield (info, gr.update(value=None, visible=False), ls_workspace(), final_logs, model_update)
def refresh_download(): zips = sorted(RUNS.glob("*.zip"), key=lambda p: p.stat().st_mtime, reverse=True)63 unmodified lines if not model_path: return "β Select a model from the dropdown first." if not isinstance(model_path, str): # fix minor bug: _name_ not name return f"β Invalid model path type: {type(model_path)._name_}" if not Path(model_path).exists(): return f"β Model folder not found: {model_path}"22 unmodified lines
# -------- UI --------with gr.Blocks(title="Python AI β Train & Test") as app: gr.Markdown("## π§ Python AI β Train & Test\nβ’ Unique runs β’ Safe download β’ Cached generation\n") gr.Markdown("## π§ Python AI β Train & Test\nβ’ Unique runs β’ Safe download β’ Cached generation β’ Live logs\n")
# Test first (so Train can update its dropdown) with gr.Tab("Test"):18 unmodified lines ws = gr.Textbox(label="Workspace", lines=16, value=ls_workspace()) run_name = gr.Textbox(label="Run name (optional)", placeholder="e.g., python_small_v1") up_status = gr.Textbox(label="Upload Status", interactive=False) start = gr.Button("π Start Training", variant="primary") logs = gr.Textbox(label="π Training Logs", lines=18, value=read_logs()) start = gr.Button("π Start Training (Live Logs)", variant="primary") logs = gr.Textbox(label="π Training Logs (live)", lines=18, value=read_logs()) status = gr.Textbox(label="Status", interactive=False) download_file = gr.File(label="π¦ Latest trained zip", visible=False) refresh_dl_btn = gr.Button("Refresh Download")
# wiring ds.change(upload_dataset, inputs=ds, outputs=[up_status, ws])
# STREAMED training: function yields updates start.click( start_training, start_training_live, inputs=[run_name], outputs=[status, download_file, ws, logs, model_list] )
refresh_dl_btn.click( refresh_download, outputs=[download_file, ws, model_list]app.py+66-15