Spaces:
Sleeping
Sleeping
Update app.py
Browse files2 unmodified lines345678910111213141516171819202122232425262728293011 unmodified lines424344454647483 unmodified lines5253545556575859606162636426 unmodified lines9192939495969730 unmodified lines1281291301311321331342 unmodified linesfrom pathlib import Pathimport gradio as grWORKDIR = Path(".")DATASET_PATH = WORKDIR / "dataset.jsonl"LOG_PATH = WORKDIR / "train.log"MODEL_DIR = WORKDIR / "trained_model" # created by train.pyZIP_PATH = WORKDIR / "trained_model.zip" # created after train# ---------- helpers ----------def _list_models():"""List model-looking folders in the workspace."""out = []for p in WORKDIR.iterdir():if p.is_dir() and (p / "config.json").exists() and ((p / "tokenizer.json").exists() or (p / "tokenizer_config.json").exists()):out.append(str(p))# also include the default training output if presentif MODEL_DIR.exists() and str(MODEL_DIR) not in out:out.insert(0, str(MODEL_DIR))return sorted(out)def _zip_model_folder():"""Zip trained_model/ into trained_model.zip (overwrite if exists)."""if not MODEL_DIR.exists():return Falseif ZIP_PATH.exists():11 unmodified linesdef start_training():if not DATASET_PATH.exists():return ("β Upload a JSONL first.", "", gr.File.update(visible=False))# clean previous outputsif MODEL_DIR.exists():shutil.rmtree(MODEL_DIR)if ZIP_PATH.exists():3 unmodified linescmd = ["python", "train.py","--dataset", str(DATASET_PATH),"--output", str(MODEL_DIR),"--model_name", "Salesforce/codegen-350M-multi","--epochs", "1","--batch_size", "2","--block_size", "256","--learning_rate", "5e-5","--subset", "0"]with open(LOG_PATH, "a", encoding="utf-8") as lf:code = subprocess.Popen(cmd, stdout=lf, stderr=subprocess.STDOUT).wait()26 unmodified linesdef upload_model_zip(zip_file):if zip_file is None:return "β No zip selected.", _list_models()# extract to a unique folderdest = WORKDIR / f"imported_{int(time.time())}"dest.mkdir(parents=True, exist_ok=True)with zipfile.ZipFile(zip_file.name, "r") as z:30 unmodified linesreturn f"β Error: {e}"# ---------- UI ----------with gr.Blocks(title="Python AI Trainer") as app:gr.Markdown("## π§ Python AI β Train & Test\nUpload JSONL β Train β Download ZIP. Test any stored model separately.")with gr.Tab("Train"):2 unmodified lines345678910111213141516171819202122232425262728293011 unmodified lines42434445464748493 unmodified lines5354555657585960616263646526 unmodified lines92939495969730 unmodified lines1281291301311321331342 unmodified linesfrom pathlib import Pathimport gradio as gr# --- paths ---WORKDIR = Path(".")DATASET_PATH = WORKDIR / "dataset.jsonl"LOG_PATH = WORKDIR / "train.log"MODEL_DIR = WORKDIR / "trained_model"ZIP_PATH = WORKDIR / "trained_model.zip"# ---------- helpers ----------def _list_models():"""List model-like folders in workspace."""out = []for p in WORKDIR.iterdir():if p.is_dir() and (p / "config.json").exists() and ((p / "tokenizer.json").exists() or (p / "tokenizer_config.json").exists()):out.append(str(p))if MODEL_DIR.exists() and str(MODEL_DIR) not in out:out.insert(0, str(MODEL_DIR))return sorted(out)def _zip_model_folder():"""Zip trained_model/ -> trained_model.zip"""if not MODEL_DIR.exists():return Falseif ZIP_PATH.exists():11 unmodified linesdef start_training():if not DATASET_PATH.exists():return ("β Upload a JSONL first.", "", gr.File.update(visible=False))# clean previous artifactsif MODEL_DIR.exists():shutil.rmtree(MODEL_DIR)if ZIP_PATH.exists():3 unmodified linescmd = ["python", "train.py","--dataset", str(DATASET_PATH),"--output", str(MODEL_DIR),"--model_name", "Salesforce/codegen-350M-multi","--epochs", "1","--batch_size", "2","--block_size", "256","--learning_rate", "5e-5","--subset", "0",]with open(LOG_PATH, "a", encoding="utf-8") as lf:code = subprocess.Popen(cmd, stdout=lf, stderr=subprocess.STDOUT).wait()26 unmodified linesdef upload_model_zip(zip_file):if zip_file is None:return "β No zip selected.", _list_models()dest = WORKDIR / f"imported_{int(time.time())}"dest.mkdir(parents=True, exist_ok=True)with zipfile.ZipFile(zip_file.name, "r") as z:30 unmodified linesreturn f"β Error: {e}"# ---------- UI ----------with gr.Blocks(title="Python AI β Train & Test") as app:gr.Markdown("## π§ Python AI β Train & Test\nUpload JSONL β Train β Download ZIP. Test any stored model separately.")with gr.Tab("Train"):
app.py+13-13