Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,28 +3,28 @@ import os, shutil, subprocess, zipfile, time
|
|
| 3 |
from pathlib import Path
|
| 4 |
import gradio as gr
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
|
|
|
| 11 |
|
| 12 |
# ---------- helpers ----------
|
| 13 |
def _list_models():
|
| 14 |
-
"""List model-
|
| 15 |
out = []
|
| 16 |
for p in WORKDIR.iterdir():
|
| 17 |
if p.is_dir() and (p / "config.json").exists() and (
|
| 18 |
(p / "tokenizer.json").exists() or (p / "tokenizer_config.json").exists()
|
| 19 |
):
|
| 20 |
out.append(str(p))
|
| 21 |
-
# also include the default training output if present
|
| 22 |
if MODEL_DIR.exists() and str(MODEL_DIR) not in out:
|
| 23 |
out.insert(0, str(MODEL_DIR))
|
| 24 |
return sorted(out)
|
| 25 |
|
| 26 |
def _zip_model_folder():
|
| 27 |
-
"""Zip trained_model/
|
| 28 |
if not MODEL_DIR.exists():
|
| 29 |
return False
|
| 30 |
if ZIP_PATH.exists():
|
|
@@ -42,7 +42,8 @@ def upload_dataset(file):
|
|
| 42 |
def start_training():
|
| 43 |
if not DATASET_PATH.exists():
|
| 44 |
return ("β Upload a JSONL first.", "", gr.File.update(visible=False))
|
| 45 |
-
|
|
|
|
| 46 |
if MODEL_DIR.exists():
|
| 47 |
shutil.rmtree(MODEL_DIR)
|
| 48 |
if ZIP_PATH.exists():
|
|
@@ -52,13 +53,13 @@ def start_training():
|
|
| 52 |
cmd = [
|
| 53 |
"python", "train.py",
|
| 54 |
"--dataset", str(DATASET_PATH),
|
| 55 |
-
"--output",
|
| 56 |
"--model_name", "Salesforce/codegen-350M-multi",
|
| 57 |
"--epochs", "1",
|
| 58 |
"--batch_size", "2",
|
| 59 |
"--block_size", "256",
|
| 60 |
"--learning_rate", "5e-5",
|
| 61 |
-
"--subset", "0"
|
| 62 |
]
|
| 63 |
with open(LOG_PATH, "a", encoding="utf-8") as lf:
|
| 64 |
code = subprocess.Popen(cmd, stdout=lf, stderr=subprocess.STDOUT).wait()
|
|
@@ -91,7 +92,6 @@ def refresh_models():
|
|
| 91 |
def upload_model_zip(zip_file):
|
| 92 |
if zip_file is None:
|
| 93 |
return "β No zip selected.", _list_models()
|
| 94 |
-
# extract to a unique folder
|
| 95 |
dest = WORKDIR / f"imported_{int(time.time())}"
|
| 96 |
dest.mkdir(parents=True, exist_ok=True)
|
| 97 |
with zipfile.ZipFile(zip_file.name, "r") as z:
|
|
@@ -128,7 +128,7 @@ def generate(model_path, prompt):
|
|
| 128 |
return f"β Error: {e}"
|
| 129 |
|
| 130 |
# ---------- UI ----------
|
| 131 |
-
with gr.Blocks(title="Python AI
|
| 132 |
gr.Markdown("## π§ Python AI β Train & Test\nUpload JSONL β Train β Download ZIP. Test any stored model separately.")
|
| 133 |
|
| 134 |
with gr.Tab("Train"):
|
|
|
|
| 3 |
from pathlib import Path
|
| 4 |
import gradio as gr
|
| 5 |
|
| 6 |
+
# --- paths ---
|
| 7 |
+
WORKDIR = Path(".")
|
| 8 |
+
DATASET_PATH = WORKDIR / "dataset.jsonl"
|
| 9 |
+
LOG_PATH = WORKDIR / "train.log"
|
| 10 |
+
MODEL_DIR = WORKDIR / "trained_model"
|
| 11 |
+
ZIP_PATH = WORKDIR / "trained_model.zip"
|
| 12 |
|
| 13 |
# ---------- helpers ----------
|
| 14 |
def _list_models():
|
| 15 |
+
"""List model-like folders in workspace."""
|
| 16 |
out = []
|
| 17 |
for p in WORKDIR.iterdir():
|
| 18 |
if p.is_dir() and (p / "config.json").exists() and (
|
| 19 |
(p / "tokenizer.json").exists() or (p / "tokenizer_config.json").exists()
|
| 20 |
):
|
| 21 |
out.append(str(p))
|
|
|
|
| 22 |
if MODEL_DIR.exists() and str(MODEL_DIR) not in out:
|
| 23 |
out.insert(0, str(MODEL_DIR))
|
| 24 |
return sorted(out)
|
| 25 |
|
| 26 |
def _zip_model_folder():
|
| 27 |
+
"""Zip trained_model/ -> trained_model.zip"""
|
| 28 |
if not MODEL_DIR.exists():
|
| 29 |
return False
|
| 30 |
if ZIP_PATH.exists():
|
|
|
|
| 42 |
def start_training():
|
| 43 |
if not DATASET_PATH.exists():
|
| 44 |
return ("β Upload a JSONL first.", "", gr.File.update(visible=False))
|
| 45 |
+
|
| 46 |
+
# clean previous artifacts
|
| 47 |
if MODEL_DIR.exists():
|
| 48 |
shutil.rmtree(MODEL_DIR)
|
| 49 |
if ZIP_PATH.exists():
|
|
|
|
| 53 |
cmd = [
|
| 54 |
"python", "train.py",
|
| 55 |
"--dataset", str(DATASET_PATH),
|
| 56 |
+
"--output", str(MODEL_DIR),
|
| 57 |
"--model_name", "Salesforce/codegen-350M-multi",
|
| 58 |
"--epochs", "1",
|
| 59 |
"--batch_size", "2",
|
| 60 |
"--block_size", "256",
|
| 61 |
"--learning_rate", "5e-5",
|
| 62 |
+
"--subset", "0",
|
| 63 |
]
|
| 64 |
with open(LOG_PATH, "a", encoding="utf-8") as lf:
|
| 65 |
code = subprocess.Popen(cmd, stdout=lf, stderr=subprocess.STDOUT).wait()
|
|
|
|
| 92 |
def upload_model_zip(zip_file):
|
| 93 |
if zip_file is None:
|
| 94 |
return "β No zip selected.", _list_models()
|
|
|
|
| 95 |
dest = WORKDIR / f"imported_{int(time.time())}"
|
| 96 |
dest.mkdir(parents=True, exist_ok=True)
|
| 97 |
with zipfile.ZipFile(zip_file.name, "r") as z:
|
|
|
|
| 128 |
return f"β Error: {e}"
|
| 129 |
|
| 130 |
# ---------- UI ----------
|
| 131 |
+
with gr.Blocks(title="Python AI β Train & Test") as app:
|
| 132 |
gr.Markdown("## π§ Python AI β Train & Test\nUpload JSONL β Train β Download ZIP. Test any stored model separately.")
|
| 133 |
|
| 134 |
with gr.Tab("Train"):
|