Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,25 +1,26 @@
|
|
| 1 |
# app.py
|
| 2 |
-
import os, shutil, subprocess, zipfile, traceback,
|
| 3 |
from pathlib import Path
|
| 4 |
from datetime import datetime
|
| 5 |
import gradio as gr
|
| 6 |
|
|
|
|
| 7 |
ROOT = Path(__file__).resolve().parent
|
| 8 |
DATA = ROOT / "dataset.jsonl"
|
| 9 |
LOG = ROOT / "train.log"
|
| 10 |
RUNS = ROOT / "runs"
|
| 11 |
RUNS.mkdir(exist_ok=True)
|
| 12 |
|
| 13 |
-
#
|
| 14 |
def append_log(msg: str):
|
| 15 |
-
msg = msg.rstrip()
|
| 16 |
with open(LOG, "a", encoding="utf-8") as lf:
|
| 17 |
lf.write(msg + "\n")
|
| 18 |
|
| 19 |
def read_logs():
|
| 20 |
return LOG.read_text(encoding="utf-8")[-20000:] if LOG.exists() else "β³ Waitingβ¦"
|
| 21 |
|
| 22 |
-
#
|
| 23 |
def ls_workspace() -> str:
|
| 24 |
rows = []
|
| 25 |
for p in sorted(ROOT.iterdir(), key=lambda x: (x.is_file(), x.name.lower())):
|
|
@@ -46,7 +47,7 @@ def dropdown_update_safe(models, prefer=None):
|
|
| 46 |
val = prefer if (prefer and prefer in models) else (models[0] if models else None)
|
| 47 |
return gr.update(choices=models, value=val)
|
| 48 |
|
| 49 |
-
#
|
| 50 |
def upload_dataset(file):
|
| 51 |
if not file:
|
| 52 |
return "β No file selected.", ls_workspace()
|
|
@@ -55,12 +56,19 @@ def upload_dataset(file):
|
|
| 55 |
return f"β
Uploaded β {DATA.name}", ls_workspace()
|
| 56 |
return "β Unexpected item; please upload a .jsonl file.", ls_workspace()
|
| 57 |
|
| 58 |
-
#
|
| 59 |
def start_training_live(run_name):
|
| 60 |
"""
|
| 61 |
Streams training logs to the UI while the subprocess runs.
|
| 62 |
Yields tuples for outputs: [status, download_file, workspace, logs, model_dropdown]
|
| 63 |
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
run_id = (run_name or "").strip() or datetime.now().strftime("run_%Y%m%d_%H%M%S")
|
| 65 |
out_dir = RUNS / run_id
|
| 66 |
zip_path = RUNS / f"{run_id}.zip"
|
|
@@ -89,18 +97,25 @@ def start_training_live(run_name):
|
|
| 89 |
append_log("βΆ " + " ".join(cmd))
|
| 90 |
|
| 91 |
# start subprocess with live stdout
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 101 |
|
| 102 |
live_log = io.StringIO()
|
| 103 |
status_msg = f"π Training run '{run_id}' in progressβ¦"
|
|
|
|
| 104 |
# stream loop
|
| 105 |
while True:
|
| 106 |
line = proc.stdout.readline()
|
|
@@ -154,7 +169,7 @@ def refresh_download():
|
|
| 154 |
dropdown_update_safe(models)
|
| 155 |
)
|
| 156 |
|
| 157 |
-
#
|
| 158 |
def import_zip(zfile):
|
| 159 |
if not zfile:
|
| 160 |
return "β No zip selected.", list_models()
|
|
@@ -166,7 +181,7 @@ def import_zip(zfile):
|
|
| 166 |
z.extractall(dest)
|
| 167 |
return f"β
Imported to {dest.name}", list_models()
|
| 168 |
|
| 169 |
-
#
|
| 170 |
_GEN_CACHE = {"path": None, "pipe": None}
|
| 171 |
|
| 172 |
def get_generation_pipeline(model_path: str):
|
|
@@ -201,52 +216,93 @@ def get_generation_pipeline(model_path: str):
|
|
| 201 |
append_log("β
Pipeline loaded.")
|
| 202 |
return pipe
|
| 203 |
|
| 204 |
-
|
| 205 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 206 |
|
| 207 |
-
# Coerce
|
| 208 |
if isinstance(model_path, list):
|
| 209 |
model_path = model_path[0] if model_path else None
|
| 210 |
|
| 211 |
# validate
|
| 212 |
if not model_path:
|
| 213 |
-
|
|
|
|
| 214 |
if not isinstance(model_path, str):
|
| 215 |
-
|
| 216 |
-
|
| 217 |
if not Path(model_path).exists():
|
| 218 |
-
|
|
|
|
| 219 |
if not prompt or not prompt.strip():
|
| 220 |
-
|
|
|
|
| 221 |
|
| 222 |
try:
|
| 223 |
pipe = get_generation_pipeline(model_path)
|
| 224 |
-
|
| 225 |
-
|
|
|
|
| 226 |
prompt.strip(),
|
| 227 |
-
max_new_tokens=
|
| 228 |
do_sample=True,
|
| 229 |
-
temperature=0.
|
| 230 |
top_p=0.9,
|
| 231 |
repetition_penalty=1.15,
|
| 232 |
no_repeat_ngram_size=4,
|
| 233 |
-
truncation=True
|
| 234 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 235 |
append_log("β
Generation OK.")
|
| 236 |
-
|
| 237 |
except Exception as e:
|
| 238 |
tb = traceback.format_exc()
|
| 239 |
append_log("β Generation error:\n" + tb)
|
| 240 |
-
|
| 241 |
|
| 242 |
-
#
|
| 243 |
with gr.Blocks(title="Python AI β Train & Test") as app:
|
| 244 |
gr.Markdown("## π§ Python AI β Train & Test\nβ’ Unique runs β’ Safe download β’ Cached generation β’ Live logs\n")
|
| 245 |
|
| 246 |
-
# Test
|
| 247 |
with gr.Tab("Test"):
|
| 248 |
gr.Markdown("### Choose a model folder or upload a .zip, then prompt it")
|
| 249 |
-
|
|
|
|
|
|
|
| 250 |
model_list = gr.Dropdown(
|
| 251 |
choices=list_models(),
|
| 252 |
label="Available AIs",
|
|
@@ -254,12 +310,21 @@ with gr.Blocks(title="Python AI β Train & Test") as app:
|
|
| 254 |
allow_custom_value=True, # keeps UI quiet when empty
|
| 255 |
multiselect=False # force single selection
|
| 256 |
)
|
|
|
|
|
|
|
|
|
|
| 257 |
zip_in = gr.File(label="Or upload a model .zip", file_types=[".zip"])
|
| 258 |
import_status = gr.Textbox(label="Import Status", interactive=False)
|
| 259 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 260 |
go = gr.Button("Generate")
|
| 261 |
out = gr.Textbox(label="AI Response", lines=20)
|
| 262 |
|
|
|
|
| 263 |
with gr.Tab("Train"):
|
| 264 |
with gr.Row():
|
| 265 |
ds = gr.File(label="π₯ Upload JSONL", file_types=[".jsonl"])
|
|
@@ -272,22 +337,30 @@ with gr.Blocks(title="Python AI β Train & Test") as app:
|
|
| 272 |
download_file = gr.File(label="π¦ Latest trained zip", visible=False)
|
| 273 |
refresh_dl_btn = gr.Button("Refresh Download")
|
| 274 |
|
| 275 |
-
#
|
|
|
|
| 276 |
ds.change(upload_dataset, inputs=ds, outputs=[up_status, ws])
|
| 277 |
|
| 278 |
-
#
|
| 279 |
start.click(
|
| 280 |
start_training_live,
|
| 281 |
inputs=[run_name],
|
| 282 |
outputs=[status, download_file, ws, logs, model_list]
|
| 283 |
)
|
| 284 |
|
|
|
|
| 285 |
refresh_dl_btn.click(
|
| 286 |
refresh_download,
|
| 287 |
outputs=[download_file, ws, model_list]
|
| 288 |
)
|
|
|
|
|
|
|
| 289 |
refresh_btn.click(lambda: dropdown_update_safe(list_models()), outputs=model_list)
|
|
|
|
|
|
|
| 290 |
zip_in.change(import_zip, inputs=zip_in, outputs=[import_status, model_list])
|
| 291 |
-
|
|
|
|
|
|
|
| 292 |
|
| 293 |
app.queue(default_concurrency_limit=1).launch()
|
|
|
|
| 1 |
# app.py
|
| 2 |
+
import os, shutil, subprocess, zipfile, traceback, io
|
| 3 |
from pathlib import Path
|
| 4 |
from datetime import datetime
|
| 5 |
import gradio as gr
|
| 6 |
|
| 7 |
+
# ----------------- Paths -----------------
|
| 8 |
ROOT = Path(__file__).resolve().parent
|
| 9 |
DATA = ROOT / "dataset.jsonl"
|
| 10 |
LOG = ROOT / "train.log"
|
| 11 |
RUNS = ROOT / "runs"
|
| 12 |
RUNS.mkdir(exist_ok=True)
|
| 13 |
|
| 14 |
+
# ----------------- Logging -----------------
|
| 15 |
def append_log(msg: str):
|
| 16 |
+
msg = (msg or "").rstrip("\n")
|
| 17 |
with open(LOG, "a", encoding="utf-8") as lf:
|
| 18 |
lf.write(msg + "\n")
|
| 19 |
|
| 20 |
def read_logs():
|
| 21 |
return LOG.read_text(encoding="utf-8")[-20000:] if LOG.exists() else "β³ Waitingβ¦"
|
| 22 |
|
| 23 |
+
# ----------------- Workspace & Models -----------------
|
| 24 |
def ls_workspace() -> str:
|
| 25 |
rows = []
|
| 26 |
for p in sorted(ROOT.iterdir(), key=lambda x: (x.is_file(), x.name.lower())):
|
|
|
|
| 47 |
val = prefer if (prefer and prefer in models) else (models[0] if models else None)
|
| 48 |
return gr.update(choices=models, value=val)
|
| 49 |
|
| 50 |
+
# ----------------- Dataset Upload -----------------
|
| 51 |
def upload_dataset(file):
|
| 52 |
if not file:
|
| 53 |
return "β No file selected.", ls_workspace()
|
|
|
|
| 56 |
return f"β
Uploaded β {DATA.name}", ls_workspace()
|
| 57 |
return "β Unexpected item; please upload a .jsonl file.", ls_workspace()
|
| 58 |
|
| 59 |
+
# ----------------- Training (Live Logs) -----------------
|
| 60 |
def start_training_live(run_name):
|
| 61 |
"""
|
| 62 |
Streams training logs to the UI while the subprocess runs.
|
| 63 |
Yields tuples for outputs: [status, download_file, workspace, logs, model_dropdown]
|
| 64 |
"""
|
| 65 |
+
# Quick guard: dataset must exist
|
| 66 |
+
if not DATA.exists():
|
| 67 |
+
msg = "β dataset.jsonl not found. Upload a JSONL dataset first."
|
| 68 |
+
append_log(msg)
|
| 69 |
+
yield (msg, gr.update(value=None, visible=False), ls_workspace(), read_logs(), dropdown_update_safe(list_models()))
|
| 70 |
+
return
|
| 71 |
+
|
| 72 |
run_id = (run_name or "").strip() or datetime.now().strftime("run_%Y%m%d_%H%M%S")
|
| 73 |
out_dir = RUNS / run_id
|
| 74 |
zip_path = RUNS / f"{run_id}.zip"
|
|
|
|
| 97 |
append_log("βΆ " + " ".join(cmd))
|
| 98 |
|
| 99 |
# start subprocess with live stdout
|
| 100 |
+
try:
|
| 101 |
+
proc = subprocess.Popen(
|
| 102 |
+
cmd,
|
| 103 |
+
stdout=subprocess.PIPE,
|
| 104 |
+
stderr=subprocess.STDOUT,
|
| 105 |
+
bufsize=1,
|
| 106 |
+
universal_newlines=True,
|
| 107 |
+
encoding="utf-8",
|
| 108 |
+
errors="replace",
|
| 109 |
+
)
|
| 110 |
+
except Exception as e:
|
| 111 |
+
err = "β Failed to start train.py: " + "".join(traceback.format_exception_only(type(e), e))
|
| 112 |
+
append_log(err)
|
| 113 |
+
yield (err, gr.update(value=None, visible=False), ls_workspace(), read_logs(), dropdown_update_safe(list_models()))
|
| 114 |
+
return
|
| 115 |
|
| 116 |
live_log = io.StringIO()
|
| 117 |
status_msg = f"π Training run '{run_id}' in progressβ¦"
|
| 118 |
+
|
| 119 |
# stream loop
|
| 120 |
while True:
|
| 121 |
line = proc.stdout.readline()
|
|
|
|
| 169 |
dropdown_update_safe(models)
|
| 170 |
)
|
| 171 |
|
| 172 |
+
# ----------------- Import a Zip as Model Folder -----------------
|
| 173 |
def import_zip(zfile):
|
| 174 |
if not zfile:
|
| 175 |
return "β No zip selected.", list_models()
|
|
|
|
| 181 |
z.extractall(dest)
|
| 182 |
return f"β
Imported to {dest.name}", list_models()
|
| 183 |
|
| 184 |
+
# ----------------- Generation (cached pipeline) -----------------
|
| 185 |
_GEN_CACHE = {"path": None, "pipe": None}
|
| 186 |
|
| 187 |
def get_generation_pipeline(model_path: str):
|
|
|
|
| 216 |
append_log("β
Pipeline loaded.")
|
| 217 |
return pipe
|
| 218 |
|
| 219 |
+
# ----------------- Test Tab Helpers -----------------
|
| 220 |
+
def ping():
|
| 221 |
+
append_log("π Ping pressed (UI wiring OK)")
|
| 222 |
+
return "β
UI is connected and responding."
|
| 223 |
+
|
| 224 |
+
def load_selected_model(model_path):
|
| 225 |
+
# Dropdown may pass a list; coerce to string
|
| 226 |
+
if isinstance(model_path, list):
|
| 227 |
+
model_path = model_path[0] if model_path else None
|
| 228 |
+
if not model_path:
|
| 229 |
+
return "β Select a model first."
|
| 230 |
+
if not isinstance(model_path, str):
|
| 231 |
+
return f"β Invalid model path type: {type(model_path)._name_}"
|
| 232 |
+
p = Path(model_path)
|
| 233 |
+
if not p.exists() or not p.is_dir():
|
| 234 |
+
return f"β Model folder not found: {model_path}"
|
| 235 |
+
try:
|
| 236 |
+
append_log(f"π¦ Load request β {model_path}")
|
| 237 |
+
_ = get_generation_pipeline(model_path)
|
| 238 |
+
append_log(f"β
Loaded pipeline: {model_path}")
|
| 239 |
+
return f"β
Loaded: {model_path}"
|
| 240 |
+
except Exception as e:
|
| 241 |
+
tb = traceback.format_exc()
|
| 242 |
+
append_log("β Load error:\n" + tb)
|
| 243 |
+
return "β Error while loading model:\n" + "".join(traceback.format_exception_only(type(e), e))
|
| 244 |
+
|
| 245 |
+
def generate_stream(model_path, prompt):
|
| 246 |
+
"""Stream intermediate status to prove the button is working, then final text."""
|
| 247 |
+
# immediate feedback
|
| 248 |
+
yield "β³ Loading modelβ¦"
|
| 249 |
+
append_log("βΆ Generate clicked")
|
| 250 |
|
| 251 |
+
# Coerce
|
| 252 |
if isinstance(model_path, list):
|
| 253 |
model_path = model_path[0] if model_path else None
|
| 254 |
|
| 255 |
# validate
|
| 256 |
if not model_path:
|
| 257 |
+
msg = "β Select a model from the dropdown first."
|
| 258 |
+
append_log(msg); yield msg; return
|
| 259 |
if not isinstance(model_path, str):
|
| 260 |
+
msg = f"β Invalid model path type: {type(model_path)._name_}"
|
| 261 |
+
append_log(msg); yield msg; return
|
| 262 |
if not Path(model_path).exists():
|
| 263 |
+
msg = f"β Model folder not found: {model_path}"
|
| 264 |
+
append_log(msg); yield msg; return
|
| 265 |
if not prompt or not prompt.strip():
|
| 266 |
+
msg = "β Enter a prompt."
|
| 267 |
+
append_log(msg); yield msg; return
|
| 268 |
|
| 269 |
try:
|
| 270 |
pipe = get_generation_pipeline(model_path)
|
| 271 |
+
yield "β Generatingβ¦ (this may take a bit on CPU)"
|
| 272 |
+
append_log(f"π Generatingβ¦ prompt_len={len(prompt)}")
|
| 273 |
+
result = pipe(
|
| 274 |
prompt.strip(),
|
| 275 |
+
max_new_tokens=80, # quicker to show something
|
| 276 |
do_sample=True,
|
| 277 |
+
temperature=0.3,
|
| 278 |
top_p=0.9,
|
| 279 |
repetition_penalty=1.15,
|
| 280 |
no_repeat_ngram_size=4,
|
| 281 |
+
truncation=True,
|
| 282 |
+
return_full_text=True,
|
| 283 |
+
)
|
| 284 |
+
text = result[0].get("generated_text", "")
|
| 285 |
+
if not text:
|
| 286 |
+
append_log("β Empty generated_text")
|
| 287 |
+
yield "β Model returned empty text. Try lowering temperature or adding more context."
|
| 288 |
+
return
|
| 289 |
append_log("β
Generation OK.")
|
| 290 |
+
yield text
|
| 291 |
except Exception as e:
|
| 292 |
tb = traceback.format_exc()
|
| 293 |
append_log("β Generation error:\n" + tb)
|
| 294 |
+
yield "β Error during generation:\n" + "".join(traceback.format_exception_only(type(e), e))
|
| 295 |
|
| 296 |
+
# ----------------- UI -----------------
|
| 297 |
with gr.Blocks(title="Python AI β Train & Test") as app:
|
| 298 |
gr.Markdown("## π§ Python AI β Train & Test\nβ’ Unique runs β’ Safe download β’ Cached generation β’ Live logs\n")
|
| 299 |
|
| 300 |
+
# ---------- Test Tab ----------
|
| 301 |
with gr.Tab("Test"):
|
| 302 |
gr.Markdown("### Choose a model folder or upload a .zip, then prompt it")
|
| 303 |
+
with gr.Row():
|
| 304 |
+
refresh_btn = gr.Button("β» Refresh Model List")
|
| 305 |
+
ping_btn = gr.Button("π Ping UI") # quick sanity check
|
| 306 |
model_list = gr.Dropdown(
|
| 307 |
choices=list_models(),
|
| 308 |
label="Available AIs",
|
|
|
|
| 310 |
allow_custom_value=True, # keeps UI quiet when empty
|
| 311 |
multiselect=False # force single selection
|
| 312 |
)
|
| 313 |
+
load_btn = gr.Button("π¦ Load Model")
|
| 314 |
+
load_status = gr.Textbox(label="Model Status", interactive=False)
|
| 315 |
+
|
| 316 |
zip_in = gr.File(label="Or upload a model .zip", file_types=[".zip"])
|
| 317 |
import_status = gr.Textbox(label="Import Status", interactive=False)
|
| 318 |
+
|
| 319 |
+
prompt = gr.Textbox(
|
| 320 |
+
label="Prompt",
|
| 321 |
+
lines=8,
|
| 322 |
+
placeholder="### Instruction:\nPython: write a function ...\n### Response:\n"
|
| 323 |
+
)
|
| 324 |
go = gr.Button("Generate")
|
| 325 |
out = gr.Textbox(label="AI Response", lines=20)
|
| 326 |
|
| 327 |
+
# ---------- Train Tab ----------
|
| 328 |
with gr.Tab("Train"):
|
| 329 |
with gr.Row():
|
| 330 |
ds = gr.File(label="π₯ Upload JSONL", file_types=[".jsonl"])
|
|
|
|
| 337 |
download_file = gr.File(label="π¦ Latest trained zip", visible=False)
|
| 338 |
refresh_dl_btn = gr.Button("Refresh Download")
|
| 339 |
|
| 340 |
+
# ---------- Wiring ----------
|
| 341 |
+
# Upload + workspace
|
| 342 |
ds.change(upload_dataset, inputs=ds, outputs=[up_status, ws])
|
| 343 |
|
| 344 |
+
# Train (live streaming)
|
| 345 |
start.click(
|
| 346 |
start_training_live,
|
| 347 |
inputs=[run_name],
|
| 348 |
outputs=[status, download_file, ws, logs, model_list]
|
| 349 |
)
|
| 350 |
|
| 351 |
+
# Download refresh
|
| 352 |
refresh_dl_btn.click(
|
| 353 |
refresh_download,
|
| 354 |
outputs=[download_file, ws, model_list]
|
| 355 |
)
|
| 356 |
+
|
| 357 |
+
# Test tab helpers
|
| 358 |
refresh_btn.click(lambda: dropdown_update_safe(list_models()), outputs=model_list)
|
| 359 |
+
ping_btn.click(ping, outputs=out)
|
| 360 |
+
load_btn.click(load_selected_model, inputs=[model_list], outputs=[load_status])
|
| 361 |
zip_in.change(import_zip, inputs=zip_in, outputs=[import_status, model_list])
|
| 362 |
+
|
| 363 |
+
# Streamed generation output
|
| 364 |
+
go.click(generate_stream, inputs=[model_list, prompt], outputs=out)
|
| 365 |
|
| 366 |
app.queue(default_concurrency_limit=1).launch()
|