Train without token; publish to Hub when HF_TOKEN present
Browse files
app.py
CHANGED
|
@@ -79,7 +79,7 @@ def _train_and_push():
|
|
| 79 |
data_collator=DataCollatorForLanguageModeling(tok, mlm=False)).train()
|
| 80 |
|
| 81 |
STATE["detail"] = "Training done — merging and uploading model to the Hub…"
|
| 82 |
-
merged = model.merge_and_unload()
|
| 83 |
merged.save_pretrained("merged", safe_serialization=True)
|
| 84 |
tok.save_pretrained("merged")
|
| 85 |
|
|
@@ -101,10 +101,14 @@ Trained on {len(rows)} instruction pairs built from the live store. Try it with
|
|
| 101 |
"""
|
| 102 |
open("merged/README.md", "w", encoding="utf-8").write(card)
|
| 103 |
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 108 |
|
| 109 |
|
| 110 |
def _bootstrap():
|
|
@@ -118,14 +122,8 @@ def _bootstrap():
|
|
| 118 |
except Exception:
|
| 119 |
pass
|
| 120 |
|
| 121 |
-
if not HF_TOKEN:
|
| 122 |
-
STATE.update(phase="error",
|
| 123 |
-
detail="HF_TOKEN secret is missing — add a write token in Space settings "
|
| 124 |
-
"and restart, so the trained model can be pushed to the Hub.")
|
| 125 |
-
return
|
| 126 |
-
|
| 127 |
_train_and_push()
|
| 128 |
-
m, t = _load(
|
| 129 |
with _lock:
|
| 130 |
STATE.update(model=m, tok=t, phase="ready",
|
| 131 |
detail="Training complete. Trained GPU Mart model is live.")
|
|
|
|
| 79 |
data_collator=DataCollatorForLanguageModeling(tok, mlm=False)).train()
|
| 80 |
|
| 81 |
STATE["detail"] = "Training done — merging and uploading model to the Hub…"
|
| 82 |
+
merged = model.merge_and_unload().to(torch.bfloat16)
|
| 83 |
merged.save_pretrained("merged", safe_serialization=True)
|
| 84 |
tok.save_pretrained("merged")
|
| 85 |
|
|
|
|
| 101 |
"""
|
| 102 |
open("merged/README.md", "w", encoding="utf-8").write(card)
|
| 103 |
|
| 104 |
+
if HF_TOKEN:
|
| 105 |
+
api = HfApi(token=HF_TOKEN)
|
| 106 |
+
api.create_repo(MODEL_REPO, repo_type="model", exist_ok=True)
|
| 107 |
+
api.upload_folder(folder_path="merged", repo_id=MODEL_REPO, repo_type="model")
|
| 108 |
+
STATE["detail"] = "Model pushed to the Hub — loading it for serving…"
|
| 109 |
+
else:
|
| 110 |
+
STATE["detail"] = ("Model trained (serving locally). Add an HF_TOKEN write-token secret in "
|
| 111 |
+
"Space settings and restart to publish it to the Hub.")
|
| 112 |
|
| 113 |
|
| 114 |
def _bootstrap():
|
|
|
|
| 122 |
except Exception:
|
| 123 |
pass
|
| 124 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 125 |
_train_and_push()
|
| 126 |
+
m, t = _load("merged")
|
| 127 |
with _lock:
|
| 128 |
STATE.update(model=m, tok=t, phase="ready",
|
| 129 |
detail="Training complete. Trained GPU Mart model is live.")
|