Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,7 +3,7 @@ import os, shutil, subprocess, zipfile
|
|
| 3 |
from pathlib import Path
|
| 4 |
import gradio as gr
|
| 5 |
|
| 6 |
-
ROOT = Path(
|
| 7 |
DATA = ROOT / "dataset.jsonl"
|
| 8 |
LOG = ROOT / "train.log"
|
| 9 |
OUT = ROOT / "trained_model"
|
|
@@ -17,6 +17,17 @@ def ls_workspace() -> str:
|
|
| 17 |
rows.append(f"{'[DIR]' if p.is_dir() else ' '}\t{size:>10}\t{p.name}")
|
| 18 |
return "\n".join(rows) or "(empty)"
|
| 19 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
def upload_dataset(file):
|
| 21 |
if not file:
|
| 22 |
return "β No file selected.", ls_workspace()
|
|
@@ -43,35 +54,29 @@ def start_training():
|
|
| 43 |
with open(LOG, "a", encoding="utf-8") as lf:
|
| 44 |
code = subprocess.Popen(cmd, stdout=lf, stderr=subprocess.STDOUT).wait()
|
| 45 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
if code == 0 and ZIP.exists():
|
| 47 |
info = f"β
Training complete. Saved: {OUT.name} | Zip: {ZIP.name}"
|
| 48 |
-
return info, gr.update(value=str(ZIP), visible=True), ls_workspace(), read_logs()
|
| 49 |
else:
|
| 50 |
info = f"β Training failed (exit {code}). See logs."
|
| 51 |
-
return info, gr.update(visible=False), ls_workspace(), read_logs()
|
| 52 |
|
| 53 |
def read_logs():
|
| 54 |
return LOG.read_text(encoding="utf-8")[-20000:] if LOG.exists() else "β³ Waitingβ¦"
|
| 55 |
|
| 56 |
def refresh_download():
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
def list_models():
|
| 61 |
-
out = []
|
| 62 |
-
for p in ROOT.iterdir():
|
| 63 |
-
if p.is_dir() and (p / "config.json").exists() and (
|
| 64 |
-
(p / "tokenizer.json").exists() or (p / "tokenizer_config.json").exists()
|
| 65 |
-
):
|
| 66 |
-
out.append(str(p))
|
| 67 |
-
if OUT.exists() and str(OUT) not in out:
|
| 68 |
-
out.insert(0, str(OUT))
|
| 69 |
-
return sorted(out)
|
| 70 |
|
| 71 |
def import_zip(zfile):
|
| 72 |
if not zfile:
|
| 73 |
return "β No zip selected.", list_models()
|
| 74 |
-
dest = ROOT /
|
| 75 |
if dest.exists(): shutil.rmtree(dest, ignore_errors=True)
|
| 76 |
dest.mkdir(parents=True, exist_ok=True)
|
| 77 |
with zipfile.ZipFile(zfile.name, "r") as z:
|
|
@@ -98,8 +103,20 @@ def generate(model_path, prompt):
|
|
| 98 |
return f"β Error: {e}"
|
| 99 |
|
| 100 |
with gr.Blocks(title="Python AI β Train & Test") as app:
|
| 101 |
-
gr.Markdown("## π§ Python AI β Train & Test (
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 102 |
|
|
|
|
| 103 |
with gr.Tab("Train"):
|
| 104 |
with gr.Row():
|
| 105 |
ds = gr.File(label="π₯ Upload JSONL", file_types=[".jsonl", ".jsonl.gz", ".json"])
|
|
@@ -111,22 +128,18 @@ with gr.Blocks(title="Python AI β Train & Test") as app:
|
|
| 111 |
download_file = gr.File(label="π¦ trained_model.zip", visible=ZIP.exists())
|
| 112 |
refresh_dl_btn = gr.Button("Refresh Download")
|
| 113 |
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
refresh_btn.click(list_models, outputs=model_list)
|
| 129 |
-
zip_in.change(import_zip, inputs=zip_in, outputs=[import_status, model_list])
|
| 130 |
-
go.click(generate, inputs=[model_list, prompt], outputs=out)
|
| 131 |
|
| 132 |
app.launch()
|
|
|
|
| 3 |
from pathlib import Path
|
| 4 |
import gradio as gr
|
| 5 |
|
| 6 |
+
ROOT = Path(_file_).resolve().parent
|
| 7 |
DATA = ROOT / "dataset.jsonl"
|
| 8 |
LOG = ROOT / "train.log"
|
| 9 |
OUT = ROOT / "trained_model"
|
|
|
|
| 17 |
rows.append(f"{'[DIR]' if p.is_dir() else ' '}\t{size:>10}\t{p.name}")
|
| 18 |
return "\n".join(rows) or "(empty)"
|
| 19 |
|
| 20 |
+
def list_models():
|
| 21 |
+
out = []
|
| 22 |
+
for p in ROOT.iterdir():
|
| 23 |
+
if p.is_dir() and (p / "config.json").exists() and (
|
| 24 |
+
(p / "tokenizer.json").exists() or (p / "tokenizer_config.json").exists()
|
| 25 |
+
):
|
| 26 |
+
out.append(str(p))
|
| 27 |
+
if OUT.exists() and str(OUT) not in out:
|
| 28 |
+
out.insert(0, str(OUT))
|
| 29 |
+
return sorted(out)
|
| 30 |
+
|
| 31 |
def upload_dataset(file):
|
| 32 |
if not file:
|
| 33 |
return "β No file selected.", ls_workspace()
|
|
|
|
| 54 |
with open(LOG, "a", encoding="utf-8") as lf:
|
| 55 |
code = subprocess.Popen(cmd, stdout=lf, stderr=subprocess.STDOUT).wait()
|
| 56 |
|
| 57 |
+
# build model-list update payload
|
| 58 |
+
models = list_models()
|
| 59 |
+
model_update = gr.update(choices=models, value=str(OUT) if OUT.exists() else None)
|
| 60 |
+
|
| 61 |
if code == 0 and ZIP.exists():
|
| 62 |
info = f"β
Training complete. Saved: {OUT.name} | Zip: {ZIP.name}"
|
| 63 |
+
return info, gr.update(value=str(ZIP), visible=True), ls_workspace(), read_logs(), model_update
|
| 64 |
else:
|
| 65 |
info = f"β Training failed (exit {code}). See logs."
|
| 66 |
+
return info, gr.update(visible=False), ls_workspace(), read_logs(), model_update
|
| 67 |
|
| 68 |
def read_logs():
|
| 69 |
return LOG.read_text(encoding="utf-8")[-20000:] if LOG.exists() else "β³ Waitingβ¦"
|
| 70 |
|
| 71 |
def refresh_download():
|
| 72 |
+
# also refresh model dropdown
|
| 73 |
+
models = list_models()
|
| 74 |
+
return gr.update(value=str(ZIP), visible=ZIP.exists()), ls_workspace(), gr.update(choices=models)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
|
| 76 |
def import_zip(zfile):
|
| 77 |
if not zfile:
|
| 78 |
return "β No zip selected.", list_models()
|
| 79 |
+
dest = ROOT / "imported_model"
|
| 80 |
if dest.exists(): shutil.rmtree(dest, ignore_errors=True)
|
| 81 |
dest.mkdir(parents=True, exist_ok=True)
|
| 82 |
with zipfile.ZipFile(zfile.name, "r") as z:
|
|
|
|
| 103 |
return f"β Error: {e}"
|
| 104 |
|
| 105 |
with gr.Blocks(title="Python AI β Train & Test") as app:
|
| 106 |
+
gr.Markdown("## π§ Python AI β Train & Test (auto-add to Test tab)")
|
| 107 |
+
|
| 108 |
+
# --- Test tab UI FIRST so we can reference components ---
|
| 109 |
+
with gr.Tab("Test"):
|
| 110 |
+
gr.Markdown("### Pick a model or upload a .zip")
|
| 111 |
+
refresh_btn = gr.Button("β» Refresh Model List")
|
| 112 |
+
model_list = gr.Dropdown(choices=list_models(), label="Available AIs", interactive=True)
|
| 113 |
+
zip_in = gr.File(label="Or upload a model .zip", file_types=[".zip"])
|
| 114 |
+
import_status = gr.Textbox(label="Import Status", interactive=False)
|
| 115 |
+
prompt = gr.Textbox(label="Prompt", lines=8, placeholder="### Instruction:\nPython: write a function ...\n### Response:\n")
|
| 116 |
+
go = gr.Button("Generate")
|
| 117 |
+
out = gr.Textbox(label="AI Response", lines=20)
|
| 118 |
|
| 119 |
+
# --- Train tab UI ---
|
| 120 |
with gr.Tab("Train"):
|
| 121 |
with gr.Row():
|
| 122 |
ds = gr.File(label="π₯ Upload JSONL", file_types=[".jsonl", ".jsonl.gz", ".json"])
|
|
|
|
| 128 |
download_file = gr.File(label="π¦ trained_model.zip", visible=ZIP.exists())
|
| 129 |
refresh_dl_btn = gr.Button("Refresh Download")
|
| 130 |
|
| 131 |
+
# --- wiring ---
|
| 132 |
+
ds.change(upload_dataset, inputs=ds, outputs=[up_status, ws])
|
| 133 |
+
start.click(
|
| 134 |
+
start_training,
|
| 135 |
+
outputs=[status, download_file, ws, logs, model_list] # <-- update Test dropdown automatically
|
| 136 |
+
)
|
| 137 |
+
refresh_dl_btn.click(
|
| 138 |
+
refresh_download,
|
| 139 |
+
outputs=[download_file, ws, model_list] # <-- also updates Test dropdown
|
| 140 |
+
)
|
| 141 |
+
refresh_btn.click(lambda: gr.update(choices=list_models()), outputs=model_list)
|
| 142 |
+
zip_in.change(import_zip, inputs=zip_in, outputs=[import_status, model_list])
|
| 143 |
+
go.click(generate, inputs=[model_list, prompt], outputs=out)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 144 |
|
| 145 |
app.launch()
|