Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
|
@@ -108,6 +108,60 @@ async def health(authorization: Optional[str] = Header(None)):
|
|
| 108 |
"auth": "protected" if _API_KEY else "open",
|
| 109 |
}
|
| 110 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 111 |
|
| 112 |
@app.post("/v1/chat/completions")
|
| 113 |
async def chat_completions(
|
|
|
|
| 108 |
"auth": "protected" if _API_KEY else "open",
|
| 109 |
}
|
| 110 |
|
| 111 |
+
# ββ Training & Data Ops Trigger ββββββββββββββββββββββββββββββββββββββββββββββ
|
| 112 |
+
# How to trigger Training/Export/Validation outside HF (e.g., Git Actions):
|
| 113 |
+
#
|
| 114 |
+
# # 1. Export Dataset to JSONL:
|
| 115 |
+
# curl -X POST "https://codey-lab-smollm2-customs.hf.space/v1/train/execute?mode=export" \
|
| 116 |
+
# -H "Authorization: Bearer ${{ secrets.SMOLLM_API_KEY }}"
|
| 117 |
+
#
|
| 118 |
+
# # 2. Validate ADI Weights:
|
| 119 |
+
# curl -X POST "https://codey-lab-smollm2-customs.hf.space/v1/train/execute?mode=validate" \
|
| 120 |
+
# -H "Authorization: Bearer ${{ secrets.SMOLLM_API_KEY }}"
|
| 121 |
+
#
|
| 122 |
+
# # 3. Finetune SmolLM2:
|
| 123 |
+
# curl -X POST "https://codey-lab-smollm2-customs.hf.space/v1/train/execute?mode=finetune" \
|
| 124 |
+
# -H "Authorization: Bearer ${{ secrets.SMOLLM_API_KEY }}"
|
| 125 |
+
|
| 126 |
+
@app.post("/v1/train/execute")
|
| 127 |
+
async def execute_train_ops(
|
| 128 |
+
mode: str = "export",
|
| 129 |
+
authorization: Optional[str] = Header(None)
|
| 130 |
+
):
|
| 131 |
+
"""
|
| 132 |
+
Remote Trigger for train.py execution.
|
| 133 |
+
Supports: export (JSONL dump), validate (ADI accuracy check), finetune (Training).
|
| 134 |
+
"""
|
| 135 |
+
_check_auth(authorization)
|
| 136 |
+
|
| 137 |
+
import subprocess
|
| 138 |
+
import sys
|
| 139 |
+
|
| 140 |
+
# Map the allowed modes from your train.py
|
| 141 |
+
valid_modes = ["export", "validate", "finetune"]
|
| 142 |
+
if mode not in valid_modes:
|
| 143 |
+
raise HTTPException(
|
| 144 |
+
status_code=400,
|
| 145 |
+
detail=f"Invalid mode. Supported: {', '.join(valid_modes)}"
|
| 146 |
+
)
|
| 147 |
+
|
| 148 |
+
try:
|
| 149 |
+
# We use Popen for a nonblocking background proces
|
| 150 |
+
# so the API call returns immediately without timing out.
|
| 151 |
+
subprocess.Popen([sys.executable, "train.py", "--mode", mode])
|
| 152 |
+
|
| 153 |
+
logger.info(f"TRAIN-OPS | Background task started: train.py --mode {mode}")
|
| 154 |
+
return {
|
| 155 |
+
"status": "queued",
|
| 156 |
+
"mode": mode,
|
| 157 |
+
"message": f"Task 'train.py --mode {mode}' triggered successfully.",
|
| 158 |
+
"timestamp": time.time()
|
| 159 |
+
}
|
| 160 |
+
except Exception as e:
|
| 161 |
+
logger.error(f"TRAIN-OPS Failed to trigger: {str(e)}")
|
| 162 |
+
raise HTTPException(status_code=500, detail="Internal Execution Error")
|
| 163 |
+
|
| 164 |
+
# ββ chat/completions ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 165 |
|
| 166 |
@app.post("/v1/chat/completions")
|
| 167 |
async def chat_completions(
|