Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
|
|
| 1 |
import os, shutil, subprocess, zipfile
|
| 2 |
from pathlib import Path
|
| 3 |
import gradio as gr
|
| 4 |
|
| 5 |
ROOT = Path(__file__).resolve().parent
|
| 6 |
-
DATA = ROOT / "dataset.jsonl"
|
| 7 |
LOG = ROOT / "train.log"
|
| 8 |
OUT = ROOT / "trained_model"
|
| 9 |
ZIP = ROOT / "trained_model.zip"
|
|
@@ -30,15 +31,15 @@ def list_models():
|
|
| 30 |
out.insert(0, str(OUT))
|
| 31 |
return sorted(out)
|
| 32 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
# ---------- train tab ----------
|
| 34 |
def upload_dataset(file):
|
| 35 |
-
"""
|
| 36 |
-
If user uploads a file -> copy to dataset.jsonl
|
| 37 |
-
If user uploads a folder -> we DO NOT move it, they’ll pass folder path via a textbox if needed.
|
| 38 |
-
"""
|
| 39 |
if not file:
|
| 40 |
return "❌ No file selected.", ls_workspace()
|
| 41 |
-
# If it's a file object, copy to DATA
|
| 42 |
if hasattr(file, "name") and os.path.isfile(file.name):
|
| 43 |
shutil.copy(file.name, DATA)
|
| 44 |
return f"✅ Uploaded → {DATA.name}", ls_workspace()
|
|
@@ -52,10 +53,9 @@ def start_training():
|
|
| 52 |
ZIP.unlink(missing_ok=True)
|
| 53 |
LOG.write_text("🔥 Training started…\n", encoding="utf-8")
|
| 54 |
|
| 55 |
-
# Run trainer (blocking) and capture output in train.log
|
| 56 |
cmd = [
|
| 57 |
"python", str(ROOT / "train.py"),
|
| 58 |
-
"--dataset", str(DATA),
|
| 59 |
"--output", str(OUT),
|
| 60 |
"--zip_path", str(ZIP),
|
| 61 |
"--model_name", "Salesforce/codegen-350M-multi",
|
|
@@ -67,10 +67,9 @@ def start_training():
|
|
| 67 |
with open(LOG, "a", encoding="utf-8") as lf:
|
| 68 |
code = subprocess.Popen(cmd, stdout=lf, stderr=subprocess.STDOUT).wait()
|
| 69 |
|
| 70 |
-
# Refresh model list & set selection only if it’s present
|
| 71 |
models = list_models()
|
| 72 |
-
|
| 73 |
-
model_update =
|
| 74 |
|
| 75 |
if code == 0 and ZIP.exists():
|
| 76 |
info = f"✅ Training complete. Saved: {OUT.name} | Zip: {ZIP.name}"
|
|
@@ -84,7 +83,11 @@ def read_logs():
|
|
| 84 |
|
| 85 |
def refresh_download():
|
| 86 |
models = list_models()
|
| 87 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
|
| 89 |
# ---------- test tab ----------
|
| 90 |
def import_zip(zfile):
|
|
@@ -125,11 +128,16 @@ def generate(model_path, prompt):
|
|
| 125 |
with gr.Blocks(title="Python AI — Train & Test") as app:
|
| 126 |
gr.Markdown("## 🧠 Python AI — Train & Test\nTrainer saves & zips. UI only shows existing artifacts.\n")
|
| 127 |
|
| 128 |
-
# Test
|
| 129 |
with gr.Tab("Test"):
|
| 130 |
gr.Markdown("### Choose a model folder or upload a .zip, then prompt it")
|
| 131 |
refresh_btn = gr.Button("↻ Refresh Model List")
|
| 132 |
-
model_list = gr.Dropdown(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 133 |
zip_in = gr.File(label="Or upload a model .zip", file_types=[".zip"])
|
| 134 |
import_status = gr.Textbox(label="Import Status", interactive=False)
|
| 135 |
prompt = gr.Textbox(label="Prompt", lines=8, placeholder="### Instruction:\nPython: write a function ...\n### Response:\n")
|
|
@@ -159,7 +167,7 @@ with gr.Blocks(title="Python AI — Train & Test") as app:
|
|
| 159 |
outputs=[download_file, ws, model_list]
|
| 160 |
)
|
| 161 |
|
| 162 |
-
refresh_btn.click(lambda:
|
| 163 |
zip_in.change(import_zip, inputs=zip_in, outputs=[import_status, model_list])
|
| 164 |
go.click(generate, inputs=[model_list, prompt], outputs=out)
|
| 165 |
|
|
|
|
| 1 |
+
# app.py
|
| 2 |
import os, shutil, subprocess, zipfile
|
| 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"
|
| 10 |
ZIP = ROOT / "trained_model.zip"
|
|
|
|
| 31 |
out.insert(0, str(OUT))
|
| 32 |
return sorted(out)
|
| 33 |
|
| 34 |
+
def dropdown_update_safe(models, prefer=None):
|
| 35 |
+
"""Return a gr.update that always uses a value present in choices (or None)."""
|
| 36 |
+
val = prefer if (prefer and prefer in models) else (models[0] if models else None)
|
| 37 |
+
return gr.update(choices=models, value=val)
|
| 38 |
+
|
| 39 |
# ---------- train tab ----------
|
| 40 |
def upload_dataset(file):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
if not file:
|
| 42 |
return "❌ No file selected.", ls_workspace()
|
|
|
|
| 43 |
if hasattr(file, "name") and os.path.isfile(file.name):
|
| 44 |
shutil.copy(file.name, DATA)
|
| 45 |
return f"✅ Uploaded → {DATA.name}", ls_workspace()
|
|
|
|
| 53 |
ZIP.unlink(missing_ok=True)
|
| 54 |
LOG.write_text("🔥 Training started…\n", encoding="utf-8")
|
| 55 |
|
|
|
|
| 56 |
cmd = [
|
| 57 |
"python", str(ROOT / "train.py"),
|
| 58 |
+
"--dataset", str(DATA),
|
| 59 |
"--output", str(OUT),
|
| 60 |
"--zip_path", str(ZIP),
|
| 61 |
"--model_name", "Salesforce/codegen-350M-multi",
|
|
|
|
| 67 |
with open(LOG, "a", encoding="utf-8") as lf:
|
| 68 |
code = subprocess.Popen(cmd, stdout=lf, stderr=subprocess.STDOUT).wait()
|
| 69 |
|
|
|
|
| 70 |
models = list_models()
|
| 71 |
+
prefer = str(OUT) if OUT.exists() else None
|
| 72 |
+
model_update = dropdown_update_safe(models, prefer=prefer)
|
| 73 |
|
| 74 |
if code == 0 and ZIP.exists():
|
| 75 |
info = f"✅ Training complete. Saved: {OUT.name} | Zip: {ZIP.name}"
|
|
|
|
| 83 |
|
| 84 |
def refresh_download():
|
| 85 |
models = list_models()
|
| 86 |
+
return (
|
| 87 |
+
gr.update(value=(str(ZIP) if ZIP.exists() else None), visible=ZIP.exists()),
|
| 88 |
+
ls_workspace(),
|
| 89 |
+
dropdown_update_safe(models)
|
| 90 |
+
)
|
| 91 |
|
| 92 |
# ---------- test tab ----------
|
| 93 |
def import_zip(zfile):
|
|
|
|
| 128 |
with gr.Blocks(title="Python AI — Train & Test") as app:
|
| 129 |
gr.Markdown("## 🧠 Python AI — Train & Test\nTrainer saves & zips. UI only shows existing artifacts.\n")
|
| 130 |
|
| 131 |
+
# Test first (so Train can update it)
|
| 132 |
with gr.Tab("Test"):
|
| 133 |
gr.Markdown("### Choose a model folder or upload a .zip, then prompt it")
|
| 134 |
refresh_btn = gr.Button("↻ Refresh Model List")
|
| 135 |
+
model_list = gr.Dropdown(
|
| 136 |
+
choices=list_models(),
|
| 137 |
+
label="Available AIs",
|
| 138 |
+
interactive=True,
|
| 139 |
+
allow_custom_value=True # <-- stops warnings when choices are empty
|
| 140 |
+
)
|
| 141 |
zip_in = gr.File(label="Or upload a model .zip", file_types=[".zip"])
|
| 142 |
import_status = gr.Textbox(label="Import Status", interactive=False)
|
| 143 |
prompt = gr.Textbox(label="Prompt", lines=8, placeholder="### Instruction:\nPython: write a function ...\n### Response:\n")
|
|
|
|
| 167 |
outputs=[download_file, ws, model_list]
|
| 168 |
)
|
| 169 |
|
| 170 |
+
refresh_btn.click(lambda: dropdown_update_safe(list_models()), outputs=model_list)
|
| 171 |
zip_in.change(import_zip, inputs=zip_in, outputs=[import_status, model_list])
|
| 172 |
go.click(generate, inputs=[model_list, prompt], outputs=out)
|
| 173 |
|