Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -207,26 +207,10 @@ def train_model(
|
|
| 207 |
"""Trains the model to produce the desired response given the full prompt (including web summary)."""
|
| 208 |
global start_iteration, checkpoint, HP
|
| 209 |
|
| 210 |
-
# CRITICAL: We use
|
| 211 |
-
# In a real scenario, this step would be highly problematic for quality.
|
| 212 |
if not full_prompt_text:
|
| 213 |
return 0.0, start_iteration
|
| 214 |
|
| 215 |
-
# For mandatory online learning on every input, we make the model learn to produce
|
| 216 |
-
# its *own* system instruction as the desired output for a fixed, zero-loss training run.
|
| 217 |
-
# A more meaningful but complex approach would use the model's *previous* successful response
|
| 218 |
-
# as the target when a user provides a new question, teaching sequence continuation.
|
| 219 |
-
# For a stable implementation that avoids immediate gibberish due to learning the user's question,
|
| 220 |
-
# we teach the model to continue the instruction sequence (minimal stable learning).
|
| 221 |
-
|
| 222 |
-
# We will use the model's own (current, internal) prediction from the *previous* turn
|
| 223 |
-
# if it exists, or just the user prompt if not. Since we are forcing training on
|
| 224 |
-
# the CURRENT user input, the target response is often unavailable or problematic.
|
| 225 |
-
|
| 226 |
-
# Given the constraints, let's assume the "desired response" in this mandatory
|
| 227 |
-
# online training loop is the input prompt itself (language modeling task).
|
| 228 |
-
|
| 229 |
-
|
| 230 |
old_n_words = voc.n_words
|
| 231 |
|
| 232 |
# 1. Update Voc
|
|
@@ -238,7 +222,6 @@ def train_model(
|
|
| 238 |
resize_model_if_needed(model, voc.n_words, HP["d_model"])
|
| 239 |
|
| 240 |
# 2. Prepare Data: Input is Full Prompt (shifted for language modeling)
|
| 241 |
-
# Target is the input shifted by one token.
|
| 242 |
input_ids = indexes_from_sentence(voc, full_prompt_text)
|
| 243 |
|
| 244 |
# We teach the model to predict the sequence (including EOS token)
|
|
@@ -251,8 +234,9 @@ def train_model(
|
|
| 251 |
combined_ids = combined_ids[:HP["max_length"]]
|
| 252 |
sequence_len = HP["max_length"]
|
| 253 |
|
| 254 |
-
input_tensor = torch.tensor([combined_ids[:-1]], dtype=torch.long, device=device)
|
| 255 |
-
target_tensor = torch.tensor([combined_ids[1:]], dtype=torch.long, device=device)
|
|
|
|
| 256 |
|
| 257 |
# 3. Forward Pass
|
| 258 |
model.train()
|
|
@@ -398,9 +382,8 @@ def on_submit(user_message: str, history: Optional[List[List[str]]], train_input
|
|
| 398 |
full_prompt = f"{SYSTEM_INSTRUCTION} {prefix}{user_message}"
|
| 399 |
|
| 400 |
# --- MANDATORY ONLINE UPDATE (Training) ---
|
| 401 |
-
# NOTE: Since the target response is not provided by the user here,
|
| 402 |
-
# the training step is implemented as a self-supervised language modeling loss (predicting the input itself).
|
| 403 |
try:
|
|
|
|
| 404 |
loss_value, new_iteration = train_model(full_prompt, user_message, voc, gpt_model, optimizer)
|
| 405 |
training_status = f"✅ Training Step {new_iteration} Completed (Loss: {loss_value:.4f})."
|
| 406 |
except Exception as e:
|
|
@@ -458,14 +441,16 @@ with gr.Blocks(title="OkemoLLM") as demo:
|
|
| 458 |
gr.Markdown("The training step is now **automatic** on every submission (Step $\\text{I} \\to \\text{T} \\to \\text{R}$), but you can view the training context below.")
|
| 459 |
|
| 460 |
with gr.Column():
|
|
|
|
|
|
|
|
|
|
| 461 |
# This displays the full input context (System + Web Summary + User Prompt)
|
| 462 |
prompt_display = gr.Textbox(label="Full Training Input (System + Web Context + Prompt)", interactive=False)
|
| 463 |
last_response_display = gr.Textbox(label="Model's Last Response", interactive=False)
|
| 464 |
|
| 465 |
-
# NOTE: The manual training
|
| 466 |
target_response = gr.Textbox(label="Desired Response (Training Target) - NOW REDUNDANT", interactive=False)
|
| 467 |
|
| 468 |
-
# Remove the manual train button handler and replace it with a simple context update trigger
|
| 469 |
train_button = gr.Button("View Last Training Context")
|
| 470 |
|
| 471 |
# Connect Chat Handlers
|
|
@@ -489,7 +474,7 @@ with gr.Blocks(title="OkemoLLM") as demo:
|
|
| 489 |
train_button.click(
|
| 490 |
view_last_context,
|
| 491 |
[train_state],
|
| 492 |
-
[train_status, train_state],
|
| 493 |
queue=False
|
| 494 |
)
|
| 495 |
|
|
|
|
| 207 |
"""Trains the model to produce the desired response given the full prompt (including web summary)."""
|
| 208 |
global start_iteration, checkpoint, HP
|
| 209 |
|
| 210 |
+
# CRITICAL: We use the user's input as the training target for self-supervised learning.
|
|
|
|
| 211 |
if not full_prompt_text:
|
| 212 |
return 0.0, start_iteration
|
| 213 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 214 |
old_n_words = voc.n_words
|
| 215 |
|
| 216 |
# 1. Update Voc
|
|
|
|
| 222 |
resize_model_if_needed(model, voc.n_words, HP["d_model"])
|
| 223 |
|
| 224 |
# 2. Prepare Data: Input is Full Prompt (shifted for language modeling)
|
|
|
|
| 225 |
input_ids = indexes_from_sentence(voc, full_prompt_text)
|
| 226 |
|
| 227 |
# We teach the model to predict the sequence (including EOS token)
|
|
|
|
| 234 |
combined_ids = combined_ids[:HP["max_length"]]
|
| 235 |
sequence_len = HP["max_length"]
|
| 236 |
|
| 237 |
+
input_tensor = torch.tensor([combined_ids[:-1]], dtype=torch.long, device=device).unsqueeze(0)
|
| 238 |
+
target_tensor = torch.tensor([combined_ids[1:]], dtype=torch.long, device=device).unsqueeze(0)
|
| 239 |
+
|
| 240 |
|
| 241 |
# 3. Forward Pass
|
| 242 |
model.train()
|
|
|
|
| 382 |
full_prompt = f"{SYSTEM_INSTRUCTION} {prefix}{user_message}"
|
| 383 |
|
| 384 |
# --- MANDATORY ONLINE UPDATE (Training) ---
|
|
|
|
|
|
|
| 385 |
try:
|
| 386 |
+
# Use user_message as target for self-supervised language modeling loss
|
| 387 |
loss_value, new_iteration = train_model(full_prompt, user_message, voc, gpt_model, optimizer)
|
| 388 |
training_status = f"✅ Training Step {new_iteration} Completed (Loss: {loss_value:.4f})."
|
| 389 |
except Exception as e:
|
|
|
|
| 441 |
gr.Markdown("The training step is now **automatic** on every submission (Step $\\text{I} \\to \\text{T} \\to \\text{R}$), but you can view the training context below.")
|
| 442 |
|
| 443 |
with gr.Column():
|
| 444 |
+
# FIX: Defining the train_status component here to resolve NameError
|
| 445 |
+
train_status = gr.Markdown(f"Current Checkpoint Iteration: {start_iteration}. Status will update after clicking 'View'.")
|
| 446 |
+
|
| 447 |
# This displays the full input context (System + Web Summary + User Prompt)
|
| 448 |
prompt_display = gr.Textbox(label="Full Training Input (System + Web Context + Prompt)", interactive=False)
|
| 449 |
last_response_display = gr.Textbox(label="Model's Last Response", interactive=False)
|
| 450 |
|
| 451 |
+
# NOTE: The manual training target is now redundant, and explicitly labeled as such
|
| 452 |
target_response = gr.Textbox(label="Desired Response (Training Target) - NOW REDUNDANT", interactive=False)
|
| 453 |
|
|
|
|
| 454 |
train_button = gr.Button("View Last Training Context")
|
| 455 |
|
| 456 |
# Connect Chat Handlers
|
|
|
|
| 474 |
train_button.click(
|
| 475 |
view_last_context,
|
| 476 |
[train_state],
|
| 477 |
+
[train_status, train_state], # train_status is now correctly defined
|
| 478 |
queue=False
|
| 479 |
)
|
| 480 |
|