Spaces:
Paused
Paused
File size: 12,719 Bytes
6dfd72e |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 |
#!/usr/bin/env python3
"""
์ฌ๋ฐ๋ฅธ AutoTrain ๋ช
๋ น์ด๋ฅผ ์ฌ์ฉํ EXAONE Fine-tuning Space FastAPI ์ ํ๋ฆฌ์ผ์ด์
"""
import os
import json
import subprocess
import asyncio
from pathlib import Path
from typing import Dict, Any
import logging
from fastapi import FastAPI, HTTPException, BackgroundTasks
from fastapi.responses import StreamingResponse
from pydantic import BaseModel
import uvicorn
# ๋ก๊น
์ค์
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
app = FastAPI(
title="EXAONE Fine-tuning",
description="EXAONE 4.0 1.2B ๋ชจ๋ธ ํ์ธํ๋ API",
version="1.0.0"
)
# ์ ์ญ ๋ณ์
training_status = {
"is_running": False,
"progress": 0,
"current_epoch": 0,
"total_epochs": 3,
"loss": 0.0,
"status": "idle",
"log_file": "/tmp/training.log"
}
class TrainingRequest(BaseModel):
model_name: str = "amis5895/exaone-1p2b-nutrition-kdri"
@app.get("/")
async def root():
"""๋ฃจํธ ์๋ํฌ์ธํธ"""
return {
"message": "EXAONE Fine-tuning API",
"status": "running",
"version": "1.0.0"
}
@app.post("/start_training")
async def start_training(request: TrainingRequest, background_tasks: BackgroundTasks):
"""ํ์ต ์์"""
global training_status
if training_status["is_running"]:
raise HTTPException(status_code=400, detail="Training is already running")
training_status.update({
"is_running": True,
"progress": 0,
"current_epoch": 0,
"status": "starting"
})
# ๋ฐฑ๊ทธ๋ผ์ด๋์์ ํ์ต ์์
background_tasks.add_task(run_corrected_training, request)
return {
"message": "Training started",
"status": "starting",
"model_name": request.model_name
}
async def run_corrected_training(request: TrainingRequest):
"""์์ ๋ AutoTrain์ ์ฌ์ฉํ ํ์ต ์คํ"""
global training_status
try:
logger.info("Starting corrected AutoTrain training process...")
training_status["status"] = "running"
# ๋ฐ์ดํฐ ํ์ผ ํ์ธ
train_file = Path("/app/train.csv")
val_file = Path("/app/validation.csv")
config_file = Path("/app/autotrain_ultra_low_final.yaml")
if not train_file.exists():
logger.error(f"Training file not found: {train_file}")
training_status.update({
"is_running": False,
"status": "failed",
"error": "Training file not found"
})
return
if not val_file.exists():
logger.error(f"Validation file not found: {val_file}")
training_status.update({
"is_running": False,
"status": "failed",
"error": "Validation file not found"
})
return
if not config_file.exists():
logger.error(f"Config file not found: {config_file}")
training_status.update({
"is_running": False,
"status": "failed",
"error": "Config file not found"
})
return
logger.info("All files found, starting corrected AutoTrain training...")
# ๋ก๊ทธ ํ์ผ ์ด๊ธฐํ
log_file = Path(training_status["log_file"])
try:
log_file.write_text("Starting corrected AutoTrain training...\n", encoding="utf-8")
except Exception as e:
logger.warning(f"Could not write to log file: {e}")
training_status["log_content"] = "Starting corrected AutoTrain training...\n"
# ํ๊ฒฝ๋ณ์ ์ค์
env = os.environ.copy()
env["TRANSFORMERS_CACHE"] = "/tmp/huggingface_cache"
env["HF_HOME"] = "/tmp/huggingface"
env["OMP_NUM_THREADS"] = "1"
# ์์ ๋ AutoTrain ๋ช
๋ น์ด (์ฌ๋ฐ๋ฅธ ํ์ ์ฌ์ฉ)
cmd = [
"autotrain", "llm",
"--train",
"--project_name", "exaone-finetuning",
"--model", "LGAI-EXAONE/EXAONE-4.0-1.2B",
"--data_path", "/app",
"--text_column", "text",
"--use-peft", # --use_peft ๋์ --use-peft
"--quantization", "int4",
"--lora-r", "16", # --lora_r ๋์ --lora-r
"--lora-alpha", "32", # --lora_alpha ๋์ --lora-alpha
"--lora-dropout", "0.05", # --lora_dropout ๋์ --lora-dropout
"--target-modules", "all-linear", # --target_modules ๋์ --target-modules
"--epochs", "3",
"--batch-size", "4", # --batch_size ๋์ --batch-size
"--gradient-accumulation", "4", # --gradient_accumulation ๋์ --gradient-accumulation
"--learning-rate", "2e-4", # --learning_rate ๋์ --learning-rate
"--warmup-ratio", "0.03", # --warmup_ratio ๋์ --warmup-ratio
"--mixed-precision", "fp16", # --mixed_precision ๋์ --mixed-precision
"--push-to-hub", # --push_to_hub ๋์ --push-to-hub
"--hub-model-id", request.model_name, # --hub_model_id ๋์ --hub-model-id
"--username", "amis5895"
]
logger.info(f"Running corrected command: {' '.join(cmd)}")
# ๋ก๊ทธ ํ์ผ์ ๋ช
๋ น์ด ๊ธฐ๋ก
try:
with open(log_file, "a", encoding="utf-8") as f:
f.write(f"Corrected Command: {' '.join(cmd)}\n")
f.write("=" * 50 + "\n")
except:
if "log_content" not in training_status:
training_status["log_content"] = ""
training_status["log_content"] += f"Corrected Command: {' '.join(cmd)}\n" + "=" * 50 + "\n"
# AutoTrain ํ๋ก์ธ์ค ์คํ
process = subprocess.Popen(
cmd,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
text=True,
bufsize=1,
universal_newlines=True,
cwd="/app",
env=env
)
# ํ์ต ์งํ ์ํฉ ๋ชจ๋ํฐ๋ง
for line in process.stdout:
logger.info(line.strip())
# ๋ก๊ทธ ํ์ผ์ ๊ธฐ๋ก
try:
with open(log_file, "a", encoding="utf-8") as f:
f.write(line)
except:
if "log_content" not in training_status:
training_status["log_content"] = ""
training_status["log_content"] += line
# ์งํ๋ฅ ํ์ฑ
if "epoch" in line.lower() and "/" in line:
try:
# "Epoch 1/3" ํํ์์ ์งํ๋ฅ ์ถ์ถ
parts = line.split()
for i, part in enumerate(parts):
if part.lower() == "epoch" and i + 1 < len(parts):
epoch_info = parts[i + 1]
if "/" in epoch_info:
current, total = epoch_info.split("/")
training_status["current_epoch"] = int(current)
training_status["total_epochs"] = int(total)
training_status["progress"] = (int(current) / int(total)) * 100
break
except:
pass
# ์์ค๊ฐ ํ์ฑ
if "loss" in line.lower():
try:
parts = line.split()
for i, part in enumerate(parts):
if part.lower() == "loss" and i + 1 < len(parts):
loss_value = float(parts[i + 1])
training_status["loss"] = loss_value
break
except:
pass
process.wait()
if process.returncode == 0:
training_status.update({
"is_running": False,
"progress": 100,
"status": "completed"
})
logger.info("Training completed successfully!")
# ์๋ฃ ๋ก๊ทธ ๊ธฐ๋ก
try:
with open(log_file, "a", encoding="utf-8") as f:
f.write("\n" + "=" * 50 + "\n")
f.write("Training completed successfully!\n")
except:
if "log_content" not in training_status:
training_status["log_content"] = ""
training_status["log_content"] += "\n" + "=" * 50 + "\nTraining completed successfully!\n"
else:
training_status.update({
"is_running": False,
"status": "failed"
})
logger.error("Training failed!")
# ์คํจ ๋ก๊ทธ ๊ธฐ๋ก
try:
with open(log_file, "a", encoding="utf-8") as f:
f.write("\n" + "=" * 50 + "\n")
f.write(f"Training failed with return code: {process.returncode}\n")
except:
if "log_content" not in training_status:
training_status["log_content"] = ""
training_status["log_content"] += "\n" + "=" * 50 + f"\nTraining failed with return code: {process.returncode}\n"
except Exception as e:
logger.error(f"Training error: {str(e)}")
training_status.update({
"is_running": False,
"status": "error",
"error": str(e)
})
# ์ค๋ฅ ๋ก๊ทธ ๊ธฐ๋ก
try:
with open(log_file, "a", encoding="utf-8") as f:
f.write(f"\nError: {str(e)}\n")
except:
if "log_content" not in training_status:
training_status["log_content"] = ""
training_status["log_content"] += f"\nError: {str(e)}\n"
@app.get("/status")
async def get_status():
"""ํ์ต ์ํ ์กฐํ"""
return training_status
@app.get("/logs")
async def get_logs():
"""๋ก๊ทธ ์กฐํ"""
log_file = Path(training_status["log_file"])
if log_file.exists():
try:
with open(log_file, "r", encoding="utf-8") as f:
logs = f.read()
return {"logs": logs}
except:
pass
# ํ์ผ์ ์ฝ์ ์ ์์ผ๋ฉด ๋ฉ๋ชจ๋ฆฌ์์ ๊ฐ์ ธ์ค๊ธฐ
if "log_content" in training_status:
return {"logs": training_status["log_content"]}
else:
return {"logs": "No logs available"}
@app.get("/logs/stream")
async def stream_logs():
"""์ค์๊ฐ ๋ก๊ทธ ์คํธ๋ฆฌ๋ฐ"""
def generate_logs():
log_file = Path(training_status["log_file"])
if log_file.exists():
try:
with open(log_file, "r", encoding="utf-8") as f:
for line in f:
yield f"data: {line}\\n\\n"
except:
pass
# ํ์ผ์ ์ฝ์ ์ ์์ผ๋ฉด ๋ฉ๋ชจ๋ฆฌ์์ ๊ฐ์ ธ์ค๊ธฐ
if "log_content" in training_status:
for line in training_status["log_content"].split('\n'):
yield f"data: {line}\\n\\n"
else:
yield "data: No logs available\\n\\n"
return StreamingResponse(generate_logs(), media_type="text/plain")
@app.post("/stop_training")
async def stop_training():
"""ํ์ต ์ค์ง"""
global training_status
if not training_status["is_running"]:
raise HTTPException(status_code=400, detail="No training is running")
training_status.update({
"is_running": False,
"status": "stopped"
})
return {"message": "Training stopped"}
@app.get("/health")
async def health_check():
"""ํฌ์ค ์ฒดํฌ"""
return {"status": "healthy", "timestamp": "2024-01-01T00:00:00Z"}
@app.get("/data_info")
async def get_data_info():
"""๋ฐ์ดํฐ ์ ๋ณด ์กฐํ"""
train_file = Path("/app/train.csv")
val_file = Path("/app/validation.csv")
config_file = Path("/app/autotrain_ultra_low_final.yaml")
info = {
"train_file_exists": train_file.exists(),
"validation_file_exists": val_file.exists(),
"config_file_exists": config_file.exists(),
"train_file_size": train_file.stat().st_size if train_file.exists() else 0,
"validation_file_size": val_file.stat().st_size if val_file.exists() else 0,
"config_file_size": config_file.stat().st_size if config_file.exists() else 0
}
return info
if __name__ == "__main__":
uvicorn.run(app, host="0.0.0.0", port=7860)
|