File size: 30,034 Bytes
28d8f8e e4256df 3349c56 e4256df 28d8f8e e4256df 28d8f8e e4256df 28d8f8e e4256df 28d8f8e e4256df 3349c56 9774f95 e4256df 3349c56 aba82e3 3349c56 e4256df 3349c56 e4256df fbe7ca1 3349c56 9774f95 3349c56 aba82e3 e4256df 3349c56 aba82e3 3349c56 e4256df 3349c56 9774f95 e4256df aba82e3 e4256df aba82e3 3349c56 e4256df 3349c56 28d8f8e 3349c56 e4256df 28d8f8e 3349c56 9774f95 e4256df 9774f95 aba82e3 9774f95 28d8f8e 9774f95 28d8f8e 9774f95 e4256df aba82e3 3349c56 e4256df 9774f95 e4256df 3349c56 e4256df 3349c56 e4256df 28d8f8e aba82e3 9774f95 e4256df aba82e3 e4256df 3349c56 aba82e3 9774f95 e4256df 10b3fe6 e4256df 9774f95 e4256df 9774f95 10b3fe6 e4256df aba82e3 e4256df 9774f95 3349c56 9774f95 e4256df 3349c56 aba82e3 e4256df 28d8f8e e4256df 28d8f8e 3349c56 28d8f8e 3349c56 28d8f8e 3349c56 28d8f8e 3349c56 e4256df 3349c56 0e7f220 e4256df 0e7f220 28d8f8e e4256df 28d8f8e fbe7ca1 28d8f8e e4256df 28d8f8e e4256df 28d8f8e e4256df 28d8f8e e4256df 28d8f8e e4256df 28d8f8e e4256df 28d8f8e fbe7ca1 28d8f8e e4256df |
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 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 |
from flask import Flask, jsonify, request, send_file
import threading
import time
import os
import tempfile
import shutil
import uuid
import zipfile
import io
from datetime import datetime, timedelta
app = Flask(__name__)
# Global variables to track training progress
training_jobs = {}
class TrainingProgress:
def __init__(self, job_id):
self.job_id = job_id
self.status = "initializing"
self.progress = 0
self.current_step = 0
self.total_steps = 0
self.start_time = time.time()
self.estimated_finish_time = None
self.message = "Starting training..."
self.error = None
self.model_path = None
self.detected_columns = None
def update_progress(self, current_step, total_steps, message=""):
self.current_step = current_step
self.total_steps = total_steps
self.progress = (current_step / total_steps) * 100 if total_steps > 0 else 0
self.message = message
# Calculate estimated finish time
if current_step > 0:
elapsed_time = time.time() - self.start_time
time_per_step = elapsed_time / current_step
remaining_steps = total_steps - current_step
estimated_remaining_time = remaining_steps * time_per_step
self.estimated_finish_time = datetime.now() + timedelta(seconds=estimated_remaining_time)
def to_dict(self):
return {
"job_id": self.job_id,
"status": self.status,
"progress": round(self.progress, 2),
"current_step": self.current_step,
"total_steps": self.total_steps,
"message": self.message,
"estimated_finish_time": self.estimated_finish_time.isoformat() if self.estimated_finish_time else None,
"error": self.error,
"model_path": self.model_path,
"detected_columns": self.detected_columns
}
def detect_qa_columns(dataset):
"""Automatically detect question and answer columns in the dataset"""
# Common patterns for question columns
question_patterns = [
'question', 'prompt', 'input', 'query', 'patient', 'user', 'human',
'instruction', 'context', 'q', 'text', 'source'
]
# Common patterns for answer columns
answer_patterns = [
'answer', 'response', 'output', 'reply', 'doctor', 'assistant', 'ai',
'completion', 'target', 'a', 'label', 'ground_truth'
]
# Get column names
columns = list(dataset.column_names)
# Find question column
question_col = None
for pattern in question_patterns:
for col in columns:
if pattern.lower() in col.lower():
question_col = col
break
if question_col:
break
# Find answer column
answer_col = None
for pattern in answer_patterns:
for col in columns:
if pattern.lower() in col.lower() and col != question_col:
answer_col = col
break
if answer_col:
break
# Fallback: use first two text columns if patterns don't match
if not question_col or not answer_col:
text_columns = []
for col in columns:
# Check if column contains text data
sample = dataset[0][col]
if isinstance(sample, str) and len(sample.strip()) > 0:
text_columns.append(col)
if len(text_columns) >= 2:
question_col = text_columns[0]
answer_col = text_columns[1]
elif len(text_columns) == 1:
# Single column case - use it for both (self-supervised)
question_col = answer_col = text_columns[0]
return question_col, answer_col
def train_model_background(job_id, dataset_name, base_model_name=None):
"""Background training function with progress tracking"""
progress = training_jobs[job_id]
try:
# Create a temporary directory for this job
temp_dir = tempfile.mkdtemp(prefix=f"train_{job_id}_")
# Set environment variables for caching
os.environ['HF_HOME'] = temp_dir
os.environ['TRANSFORMERS_CACHE'] = temp_dir
os.environ['HF_DATASETS_CACHE'] = temp_dir
os.environ['TORCH_HOME'] = temp_dir
progress.status = "loading_libraries"
progress.message = "Loading required libraries..."
# Import heavy libraries after setting cache paths
import torch
from datasets import load_dataset, Dataset
from huggingface_hub import login
from transformers import (
AutoModelForCausalLM,
AutoTokenizer,
TrainingArguments,
Trainer,
TrainerCallback,
)
from peft import (
LoraConfig,
get_peft_model,
)
# === Authentication ===
hf_token = os.getenv('HF_TOKEN')
if hf_token:
login(token=hf_token)
progress.status = "loading_model"
progress.message = "Loading base model and tokenizer..."
# === Configuration ===
base_model = base_model_name or "microsoft/DialoGPT-small"
new_model = f"trained-model-{job_id}"
max_length = 256
# === Load Model and Tokenizer ===
model = AutoModelForCausalLM.from_pretrained(
base_model,
cache_dir=temp_dir,
torch_dtype=torch.float32,
device_map="auto" if torch.cuda.is_available() else "cpu",
trust_remote_code=True
)
tokenizer = AutoTokenizer.from_pretrained(
base_model,
cache_dir=temp_dir,
trust_remote_code=True
)
# Add padding token if not present
if tokenizer.pad_token is None:
tokenizer.pad_token = tokenizer.eos_token
# Resize token embeddings if needed
model.resize_token_embeddings(len(tokenizer))
progress.status = "preparing_model"
progress.message = "Setting up LoRA configuration..."
# === LoRA Config ===
peft_config = LoraConfig(
r=8,
lora_alpha=16,
lora_dropout=0.1,
bias="none",
task_type="CAUSAL_LM",
)
model = get_peft_model(model, peft_config)
progress.status = "loading_dataset"
progress.message = "Loading and preparing dataset..."
# === Load & Prepare Dataset ===
dataset = load_dataset(
dataset_name,
split="train" if "train" in load_dataset(dataset_name, cache_dir=temp_dir).keys() else "all",
cache_dir=temp_dir,
trust_remote_code=True
)
# Automatically detect question and answer columns
question_col, answer_col = detect_qa_columns(dataset)
if not question_col or not answer_col:
raise ValueError("Could not automatically detect question and answer columns in the dataset")
progress.detected_columns = {"question": question_col, "answer": answer_col}
progress.message = f"Detected columns - Question: {question_col}, Answer: {answer_col}"
# Use subset for faster testing (can be made configurable)
dataset = dataset.shuffle(seed=65).select(range(min(100, len(dataset))))
# Custom dataset class for proper handling
class CustomDataset(torch.utils.data.Dataset):
def __init__(self, texts, tokenizer, max_length):
self.texts = texts
self.tokenizer = tokenizer
self.max_length = max_length
def __len__(self):
return len(self.texts)
def __getitem__(self, idx):
text = self.texts[idx]
# Tokenize the text
encoding = self.tokenizer(
text,
truncation=True,
padding='max_length',
max_length=self.max_length,
return_tensors='pt'
)
# Flatten the tensors (remove batch dimension)
input_ids = encoding['input_ids'].squeeze()
attention_mask = encoding['attention_mask'].squeeze()
# For causal language modeling, labels are the same as input_ids
labels = input_ids.clone()
# Set labels to -100 for padding tokens (they won't contribute to loss)
labels[attention_mask == 0] = -100
return {
'input_ids': input_ids,
'attention_mask': attention_mask,
'labels': labels
}
# Prepare texts using detected columns
texts = []
for item in dataset:
question = str(item[question_col]).strip()
answer = str(item[answer_col]).strip()
text = f"Question: {question}\nAnswer: {answer}{tokenizer.eos_token}"
texts.append(text)
# Create custom dataset
train_dataset = CustomDataset(texts, tokenizer, max_length)
# Calculate total training steps
batch_size = 2
gradient_accumulation_steps = 1
num_epochs = 1
steps_per_epoch = len(train_dataset) // (batch_size * gradient_accumulation_steps)
total_steps = steps_per_epoch * num_epochs
progress.total_steps = total_steps
progress.status = "training"
progress.message = "Starting training..."
# === Training Arguments ===
output_dir = os.path.join(temp_dir, new_model)
os.makedirs(output_dir, exist_ok=True)
training_args = TrainingArguments(
output_dir=output_dir,
per_device_train_batch_size=batch_size,
gradient_accumulation_steps=gradient_accumulation_steps,
num_train_epochs=num_epochs,
logging_steps=1,
save_steps=max(1, total_steps // 2),
save_total_limit=1,
learning_rate=5e-5,
warmup_steps=2,
logging_strategy="steps",
save_strategy="steps",
fp16=False,
bf16=False,
dataloader_num_workers=0,
remove_unused_columns=False,
report_to=None,
prediction_loss_only=True,
)
# Custom callback to track progress
class ProgressCallback(TrainerCallback):
def __init__(self, progress_tracker):
self.progress_tracker = progress_tracker
self.last_update = time.time()
def on_log(self, args, state, control, model=None, logs=None, **kwargs):
current_time = time.time()
# Update every 3 seconds
if current_time - self.last_update >= 3:
self.progress_tracker.update_progress(
state.global_step,
state.max_steps,
f"Training step {state.global_step}/{state.max_steps}"
)
self.last_update = current_time
# Log training metrics if available
if logs:
loss = logs.get('train_loss', logs.get('loss', 'N/A'))
self.progress_tracker.message = f"Step {state.global_step}/{state.max_steps}, Loss: {loss}"
def on_train_begin(self, args, state, control, **kwargs):
self.progress_tracker.status = "training"
self.progress_tracker.message = "Training started..."
def on_train_end(self, args, state, control, **kwargs):
self.progress_tracker.status = "saving"
self.progress_tracker.message = "Training complete, saving model..."
# === Trainer Initialization ===
trainer = Trainer(
model=model,
args=training_args,
train_dataset=train_dataset,
callbacks=[ProgressCallback(progress)],
tokenizer=tokenizer,
)
# === Train & Save ===
trainer.train()
trainer.save_model(output_dir)
tokenizer.save_pretrained(output_dir)
# Save model info
progress.model_path = output_dir
progress.status = "completed"
progress.progress = 100
progress.message = f"Training completed! Model ready for download."
# Keep the temp directory for download (cleanup after 1 hour)
def cleanup_temp_dir():
time.sleep(3600) # Wait 1 hour before cleanup
try:
shutil.rmtree(temp_dir)
# Remove from training_jobs after cleanup
if job_id in training_jobs:
del training_jobs[job_id]
except:
pass
cleanup_thread = threading.Thread(target=cleanup_temp_dir)
cleanup_thread.daemon = True
cleanup_thread.start()
except Exception as e:
progress.status = "error"
progress.error = str(e)
progress.message = f"Training failed: {str(e)}"
# Clean up on error
try:
if 'temp_dir' in locals():
shutil.rmtree(temp_dir)
except:
pass
def train_model_background(job_id, dataset_name, base_model_name=None):
"""Background training function with improved configuration"""
progress = training_jobs[job_id]
try:
# Create a temporary directory for this job
temp_dir = tempfile.mkdtemp(prefix=f"train_{job_id}_")
# Set environment variables for caching
os.environ['HF_HOME'] = temp_dir
os.environ['TRANSFORMERS_CACHE'] = temp_dir
os.environ['HF_DATASETS_CACHE'] = temp_dir
os.environ['TORCH_HOME'] = temp_dir
progress.status = "loading_libraries"
progress.message = "Loading required libraries..."
# Import heavy libraries after setting cache paths
import torch
from datasets import load_dataset, Dataset
from huggingface_hub import login
from transformers import (
AutoModelForCausalLM,
AutoTokenizer,
TrainingArguments,
Trainer,
TrainerCallback,
DataCollatorForLanguageModeling
)
from peft import (
LoraConfig,
get_peft_model,
TaskType
)
# === Authentication ===
hf_token = os.getenv('HF_TOKEN')
if hf_token:
login(token=hf_token)
progress.status = "loading_model"
progress.message = "Loading base model and tokenizer..."
# === Better Model Selection ===
# Use a more suitable model for medical conversations
base_model = base_model_name or "microsoft/DialoGPT-medium" # Better than small
new_model = f"trained-model-{job_id}"
max_length = 512 # Increased for better context
# === Load Model and Tokenizer ===
model = AutoModelForCausalLM.from_pretrained(
base_model,
cache_dir=temp_dir,
torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,
device_map="auto" if torch.cuda.is_available() else "cpu",
trust_remote_code=True,
low_cpu_mem_usage=True
)
tokenizer = AutoTokenizer.from_pretrained(
base_model,
cache_dir=temp_dir,
trust_remote_code=True,
padding_side="right" # Important for causal LM
)
# Add padding token if not present
if tokenizer.pad_token is None:
tokenizer.pad_token = tokenizer.eos_token
tokenizer.pad_token_id = tokenizer.eos_token_id
# Resize token embeddings if needed
model.resize_token_embeddings(len(tokenizer))
progress.status = "preparing_model"
progress.message = "Setting up improved LoRA configuration..."
# === Better LoRA Config ===
peft_config = LoraConfig(
r=16, # Increased rank for better learning
lora_alpha=32, # Increased alpha
lora_dropout=0.05, # Reduced dropout
bias="none",
task_type=TaskType.CAUSAL_LM,
target_modules=["c_attn", "c_proj"], # Target specific modules for DialoGPT
)
model = get_peft_model(model, peft_config)
# Print trainable parameters
model.print_trainable_parameters()
progress.status = "loading_dataset"
progress.message = "Loading and preparing dataset..."
# === Load & Prepare Dataset ===
dataset = load_dataset(
dataset_name,
split="train" if "train" in load_dataset(dataset_name, cache_dir=temp_dir).keys() else "all",
cache_dir=temp_dir,
trust_remote_code=True
)
# Automatically detect question and answer columns
question_col, answer_col = detect_qa_columns(dataset)
if not question_col or not answer_col:
raise ValueError("Could not automatically detect question and answer columns in the dataset")
progress.detected_columns = {"question": question_col, "answer": answer_col}
progress.message = f"Detected columns - Question: {question_col}, Answer: {answer_col}"
# Use more data for better training
dataset_size = min(1000, len(dataset)) # Increased from 100 to 1000
dataset = dataset.shuffle(seed=42).select(range(dataset_size))
# === Better Text Formatting ===
def format_conversation(example):
question = str(example[question_col]).strip()
answer = str(example[answer_col]).strip()
# Better formatting with special tokens
conversation = f"<|user|>{question}<|assistant|>{answer}<|endoftext|>"
return {"text": conversation}
# Apply formatting
dataset = dataset.map(format_conversation, remove_columns=dataset.column_names)
# Filter out very short or very long examples
dataset = dataset.filter(lambda x: 10 < len(x["text"]) < max_length * 2)
# === Improved Training Arguments ===
batch_size = 4 if torch.cuda.is_available() else 2
gradient_accumulation_steps = 2
num_epochs = 3 # Increased epochs
learning_rate = 2e-4 # Better learning rate
steps_per_epoch = len(dataset) // (batch_size * gradient_accumulation_steps)
total_steps = steps_per_epoch * num_epochs
warmup_steps = max(10, total_steps // 10) # 10% warmup
progress.total_steps = total_steps
progress.status = "training"
progress.message = "Starting improved training..."
output_dir = os.path.join(temp_dir, new_model)
os.makedirs(output_dir, exist_ok=True)
training_args = TrainingArguments(
output_dir=output_dir,
per_device_train_batch_size=batch_size,
gradient_accumulation_steps=gradient_accumulation_steps,
num_train_epochs=num_epochs,
learning_rate=learning_rate,
warmup_steps=warmup_steps,
logging_steps=5,
save_steps=max(10, total_steps // 4),
save_total_limit=2,
evaluation_strategy="no",
logging_strategy="steps",
save_strategy="steps",
fp16=torch.cuda.is_available(),
bf16=False,
dataloader_num_workers=0,
remove_unused_columns=False,
report_to=None,
prediction_loss_only=True,
optim="adamw_torch",
weight_decay=0.01,
lr_scheduler_type="cosine",
gradient_checkpointing=True,
dataloader_pin_memory=False,
)
# === Data Collator ===
data_collator = DataCollatorForLanguageModeling(
tokenizer=tokenizer,
mlm=False, # We're doing causal LM, not masked LM
return_tensors="pt",
pad_to_multiple_of=8,
)
# Custom tokenization function
def tokenize_function(examples):
# Tokenize the text
tokenized = tokenizer(
examples["text"],
truncation=True,
padding=False, # Will be handled by data collator
max_length=max_length,
return_tensors=None,
)
# For causal LM, labels are the same as input_ids
tokenized["labels"] = tokenized["input_ids"].copy()
return tokenized
# Tokenize dataset
tokenized_dataset = dataset.map(
tokenize_function,
batched=True,
remove_columns=dataset.column_names,
desc="Tokenizing dataset",
)
# Custom callback to track progress
class ProgressCallback(TrainerCallback):
def __init__(self, progress_tracker):
self.progress_tracker = progress_tracker
self.last_update = time.time()
def on_log(self, args, state, control, model=None, logs=None, **kwargs):
current_time = time.time()
# Update every 5 seconds
if current_time - self.last_update >= 5:
self.progress_tracker.update_progress(
state.global_step,
state.max_steps,
f"Training step {state.global_step}/{state.max_steps}"
)
self.last_update = current_time
# Log training metrics if available
if logs:
loss = logs.get('train_loss', logs.get('loss', 'N/A'))
lr = logs.get('learning_rate', 'N/A')
self.progress_tracker.message = f"Step {state.global_step}/{state.max_steps}, Loss: {loss:.4f}, LR: {lr}"
def on_train_begin(self, args, state, control, **kwargs):
self.progress_tracker.status = "training"
self.progress_tracker.message = "Training started with improved configuration..."
def on_train_end(self, args, state, control, **kwargs):
self.progress_tracker.status = "saving"
self.progress_tracker.message = "Training complete, saving improved model..."
# === Trainer Initialization ===
trainer = Trainer(
model=model,
args=training_args,
train_dataset=tokenized_dataset,
data_collator=data_collator,
callbacks=[ProgressCallback(progress)],
tokenizer=tokenizer,
)
# === Train & Save ===
trainer.train()
# Save the model properly
trainer.save_model(output_dir)
tokenizer.save_pretrained(output_dir)
# Also save the base model name for inference
with open(os.path.join(output_dir, "base_model.txt"), "w") as f:
f.write(base_model)
# Save training info
training_info = {
"base_model": base_model,
"dataset_name": dataset_name,
"dataset_size": len(dataset),
"max_length": max_length,
"batch_size": batch_size,
"learning_rate": learning_rate,
"num_epochs": num_epochs,
"total_steps": total_steps,
"detected_columns": progress.detected_columns
}
with open(os.path.join(output_dir, "training_info.json"), "w") as f:
import json
json.dump(training_info, f, indent=2)
# Save model info
progress.model_path = output_dir
progress.status = "completed"
progress.progress = 100
progress.message = f"Improved training completed! Model ready for download."
# Keep the temp directory for download (cleanup after 2 hours for larger model)
def cleanup_temp_dir():
time.sleep(7200) # Wait 2 hours before cleanup
try:
shutil.rmtree(temp_dir)
# Remove from training_jobs after cleanup
if job_id in training_jobs:
del training_jobs[job_id]
except:
pass
cleanup_thread = threading.Thread(target=cleanup_temp_dir)
cleanup_thread.daemon = True
cleanup_thread.start()
except Exception as e:
progress.status = "error"
progress.error = str(e)
progress.message = f"Training failed: {str(e)}"
# Clean up on error
try:
if 'temp_dir' in locals():
shutil.rmtree(temp_dir)
except:
pass
def create_model_zip(model_path, job_id):
"""Create a zip file containing the trained model"""
memory_file = io.BytesIO()
with zipfile.ZipFile(memory_file, 'w', zipfile.ZIP_DEFLATED) as zf:
for root, dirs, files in os.walk(model_path):
for file in files:
file_path = os.path.join(root, file)
arc_name = os.path.relpath(file_path, model_path)
zf.write(file_path, arc_name)
memory_file.seek(0)
return memory_file
# ============== API ROUTES ==============
@app.route('/api/train', methods=['POST'])
def start_training():
"""Start training and return job ID for tracking"""
try:
data = request.get_json() if request.is_json else {}
dataset_name = data.get('dataset_name', 'ruslanmv/ai-medical-chatbot')
base_model_name = data.get('base_model', 'microsoft/DialoGPT-small')
job_id = str(uuid.uuid4())[:8] # Short UUID
progress = TrainingProgress(job_id)
training_jobs[job_id] = progress
# Start training in background thread
training_thread = threading.Thread(
target=train_model_background,
args=(job_id, dataset_name, base_model_name)
)
training_thread.daemon = True
training_thread.start()
return jsonify({
"status": "started",
"job_id": job_id,
"dataset_name": dataset_name,
"base_model": base_model_name,
"message": "Training started. Use /api/status/<job_id> to track progress."
})
except Exception as e:
return jsonify({"status": "error", "message": str(e)}), 500
@app.route('/api/status/<job_id>', methods=['GET'])
def get_training_status(job_id):
"""Get training progress and estimated completion time"""
if job_id not in training_jobs:
return jsonify({"status": "error", "message": "Job not found"}), 404
progress = training_jobs[job_id]
return jsonify(progress.to_dict())
@app.route('/api/download/<job_id>', methods=['GET'])
def download_model(job_id):
"""Download the trained model as a zip file"""
if job_id not in training_jobs:
return jsonify({"status": "error", "message": "Job not found"}), 404
progress = training_jobs[job_id]
if progress.status != "completed":
return jsonify({
"status": "error",
"message": f"Model not ready for download. Current status: {progress.status}"
}), 400
if not progress.model_path or not os.path.exists(progress.model_path):
return jsonify({
"status": "error",
"message": "Model files not found. They may have been cleaned up."
}), 404
try:
# Create zip file in memory
zip_file = create_model_zip(progress.model_path, job_id)
return send_file(
zip_file,
as_attachment=True,
download_name=f"trained_model_{job_id}.zip",
mimetype='application/zip'
)
except Exception as e:
return jsonify({"status": "error", "message": f"Download failed: {str(e)}"}), 500
@app.route('/api/jobs', methods=['GET'])
def list_jobs():
"""List all training jobs"""
jobs = {job_id: progress.to_dict() for job_id, progress in training_jobs.items()}
return jsonify({"jobs": jobs})
@app.route('/')
def home():
return jsonify({
"message": "Welcome to Enhanced LLaMA Fine-tuning API!",
"features": [
"Automatic question/answer column detection",
"Configurable base model and dataset",
"Local model download",
"Progress tracking with ETA"
],
"endpoints": {
"POST /api/train": "Start training (accepts dataset_name and base_model in JSON)",
"GET /api/status/<job_id>": "Get training status and detected columns",
"GET /api/download/<job_id>": "Download trained model as zip",
"GET /api/jobs": "List all jobs"
},
"usage_example": {
"start_training": {
"method": "POST",
"url": "/api/train",
"body": {
"dataset_name": "your-dataset-name",
"base_model": "microsoft/DialoGPT-small"
}
}
}
})
@app.route('/health')
def health():
return jsonify({"status": "healthy"})
if __name__ == '__main__':
port = int(os.environ.get('PORT', 7860)) # HF Spaces uses port 7860
app.run(host='0.0.0.0', port=port, debug=False) |