Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -57,17 +57,6 @@ OVERALL_OUTPUT_ENTROPY_REG_WEIGHT_APP = 0.01
|
|
57 |
GATE_SPARSITY_LOSS_WEIGHT_APP = 0.001
|
58 |
WIRING_PHASE_EPOCHS_APP = 1
|
59 |
|
60 |
-
def set_model_debug_prints(model, seed_parser_debug, block_debug, model_debug):
|
61 |
-
if model:
|
62 |
-
model.debug_prints_enabled = model_debug
|
63 |
-
if hasattr(model, 'seed_parser'):
|
64 |
-
model.seed_parser.debug_prints_enabled = seed_parser_debug
|
65 |
-
if hasattr(model, 'adaptive_blocks'):
|
66 |
-
for block in model.adaptive_blocks:
|
67 |
-
block.debug_prints_enabled = block_debug
|
68 |
-
print(f"App: Model debug prints set - SeedParser: {seed_parser_debug}, Blocks: {block_debug}, SWCKModel: {model_debug}")
|
69 |
-
|
70 |
-
|
71 |
def build_vocab_from_corpus_text_app(corpus_text):
|
72 |
global VOCAB_SIZE_APP
|
73 |
print("App: Building vocabulary...")
|
@@ -84,8 +73,7 @@ def build_vocab_from_corpus_text_app(corpus_text):
|
|
84 |
print(f"App: Built vocab of size {VOCAB_SIZE_APP}")
|
85 |
return temp_word_to_idx, temp_idx_to_word
|
86 |
|
87 |
-
|
88 |
-
def initialize_or_load_model_app(enable_initial_debug=True):
|
89 |
global swck_model_global, optimizer_global, word_to_idx_global, idx_to_word_global, \
|
90 |
VOCAB_SIZE_APP, model_load_status_global
|
91 |
|
@@ -104,19 +92,19 @@ def initialize_or_load_model_app(enable_initial_debug=True):
|
|
104 |
'num_sub_modules_per_block': NUM_SUB_MODULES_PER_BLOCK_APP
|
105 |
}
|
106 |
|
107 |
-
|
108 |
-
print("App: Initializing SWCKModel with FULL DEBUG ON by default for init...")
|
109 |
-
|
110 |
-
# Temporarily disable sub-component debug before SWCKModel init if enable_initial_debug is False,
|
111 |
-
# so SWCKModel's own init prints don't get mixed with sub-component init prints prematurely.
|
112 |
-
# SeedParser's internal debug_prints_enabled will control its own prints during its __init__.
|
113 |
|
114 |
swck_model_global = SWCKModel(**model_args).to(device_global)
|
115 |
-
#
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
|
|
|
|
|
|
|
|
|
|
120 |
|
121 |
|
122 |
if os.path.exists(CHECKPOINT_FILENAME):
|
@@ -141,28 +129,30 @@ def initialize_or_load_model_app(enable_initial_debug=True):
|
|
141 |
else:
|
142 |
print("App: word_to_idx not in checkpoint, using app's rebuilt vocab.")
|
143 |
|
144 |
-
#
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
|
|
|
|
149 |
|
150 |
model_load_status_global = f"Model loaded successfully from {CHECKPOINT_FILENAME}."
|
151 |
print(model_load_status_global)
|
152 |
except Exception as e:
|
153 |
-
print(f"App: Error loading model from checkpoint: {e}. Re-initializing new model
|
154 |
swck_model_global = SWCKModel(**model_args).to(device_global)
|
155 |
-
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
optimizer_global = optim.AdamW(swck_model_global.parameters(), lr=0.001)
|
160 |
-
model_load_status_global =
|
161 |
else:
|
162 |
-
print(f"App: Checkpoint {CHECKPOINT_FILENAME} not found. Initializing new model
|
163 |
-
#
|
164 |
optimizer_global = optim.AdamW(swck_model_global.parameters(), lr=0.001)
|
165 |
-
model_load_status_global =
|
166 |
|
167 |
swck_model_global.eval()
|
168 |
return model_load_status_global
|
@@ -199,15 +189,14 @@ def run_short_training_session(num_epochs_app, batch_size_app, learning_rate_app
|
|
199 |
if swck_model_global is None or word_to_idx_global is None:
|
200 |
return "Model not initialized. Cannot train."
|
201 |
|
202 |
-
print("\n--- App: Starting Short Training Session (Full Debug ON for ALL batches/epochs) ---")
|
203 |
progress(0, desc="Preparing training data...")
|
204 |
|
205 |
-
|
206 |
|
207 |
training_corpus = SEED_PHRASE_APP + " " + EXTENDED_TEXT_FOR_TRAINING_APP
|
208 |
app_dataset = AppSWCKDataset(training_corpus, word_to_idx_global, SEQ_LEN_APP, SOS_TOKEN, EOS_TOKEN, PAD_TOKEN)
|
209 |
if not app_dataset.samples:
|
210 |
-
set_model_debug_prints(swck_model_global, False, False, False)
|
211 |
return "App Training Error: No samples created from the corpus."
|
212 |
|
213 |
app_dataloader = DataLoader(app_dataset, batch_size=int(batch_size_app), shuffle=True, collate_fn=app_swck_collate_fn)
|
@@ -226,10 +215,11 @@ def run_short_training_session(num_epochs_app, batch_size_app, learning_rate_app
|
|
226 |
for epoch in progress.tqdm(range(int(num_epochs_app)), desc="Training Epochs"):
|
227 |
swck_model_global.set_wiring_phase(epoch < WIRING_PHASE_EPOCHS_APP)
|
228 |
epoch_loss = 0.0
|
229 |
-
|
230 |
|
231 |
for batch_idx, (src_batch, tgt_batch) in enumerate(app_dataloader):
|
232 |
-
|
|
|
233 |
|
234 |
src_batch, tgt_batch = src_batch.to(device_global), tgt_batch.to(device_global)
|
235 |
decoder_input_tokens = src_batch[:, :-1]
|
@@ -277,17 +267,17 @@ def run_short_training_session(num_epochs_app, batch_size_app, learning_rate_app
|
|
277 |
epoch_loss += combined_loss.item()
|
278 |
|
279 |
log_line = f" Epoch {epoch+1}, Batch {batch_idx+1}/{len(app_dataloader)}, Loss: {combined_loss.item():.4f}"
|
280 |
-
print(log_line)
|
281 |
if batch_idx % max(1, len(app_dataloader)//2) == 0 or batch_idx == len(app_dataloader)-1 :
|
282 |
-
training_log_output += log_line + "\n"
|
283 |
|
284 |
avg_epoch_loss = epoch_loss / len(app_dataloader) if len(app_dataloader) > 0 else epoch_loss
|
285 |
epoch_summary = f"Epoch {epoch+1}/{num_epochs_app} - Avg Loss: {avg_epoch_loss:.4f}\n"
|
286 |
print(epoch_summary)
|
287 |
training_log_output += epoch_summary
|
288 |
|
289 |
-
print("--- App: Training Session Finished.
|
290 |
-
|
291 |
swck_model_global.eval()
|
292 |
|
293 |
try:
|
@@ -321,9 +311,8 @@ def generate_text_for_app(prompt_str, max_len_gen, temperature_gen):
|
|
321 |
swck_model_global.eval()
|
322 |
swck_model_global.set_wiring_phase(False)
|
323 |
|
324 |
-
|
325 |
-
|
326 |
-
|
327 |
print(f"App: Generating for prompt: '{prompt_str}', max_len: {max_len_gen}, temp: {temperature_gen}")
|
328 |
|
329 |
tokens = [SOS_TOKEN] + [word_to_idx_global.get(w, UNK_TOKEN) for w in prompt_str.lower().split()]
|
@@ -332,6 +321,7 @@ def generate_text_for_app(prompt_str, max_len_gen, temperature_gen):
|
|
332 |
|
333 |
with torch.no_grad():
|
334 |
for i in range(int(max_len_gen)):
|
|
|
335 |
print(f"\n--- Generation Step {i+1} ---")
|
336 |
context_start_idx = max(0, len(generated_ids_app) - SEQ_LEN_APP)
|
337 |
current_context_ids = generated_ids_app[context_start_idx:]
|
@@ -381,11 +371,11 @@ def generate_text_for_app(prompt_str, max_len_gen, temperature_gen):
|
|
381 |
|
382 |
debug_output_str = "\n".join(debug_info_lines)
|
383 |
|
384 |
-
|
385 |
-
|
386 |
return final_text, debug_output_str
|
387 |
|
388 |
-
# Initialize model
|
389 |
initial_load_status = initialize_or_load_model_app(enable_initial_debug=True)
|
390 |
|
391 |
with gr.Blocks(title="SWCK Conceptual Demo") as demo:
|
@@ -393,7 +383,7 @@ with gr.Blocks(title="SWCK Conceptual Demo") as demo:
|
|
393 |
|
394 |
gr.Markdown(f"""
|
395 |
# Self-Wired Conscious Kernel (SWCK) - Conceptual Demo
|
396 |
-
This demo showcases a conceptual text generation model with **FULL KERNEL DEBUGGING ON by default** for
|
397 |
Seed Phrase: "{SEED_PHRASE_APP[:100]}..." | Seed Number: "{SEED_NUMBER_STR_APP}".
|
398 |
(Note: If checkpoint is not found or fails to load, an *untrained* model is used.)
|
399 |
""")
|
@@ -402,9 +392,8 @@ with gr.Blocks(title="SWCK Conceptual Demo") as demo:
|
|
402 |
with gr.TabItem("Generate Text"):
|
403 |
with gr.Row():
|
404 |
prompt_input = gr.Textbox(label="Enter your prompt:", placeholder="e.g., the meaning of existence is", scale=3)
|
405 |
-
# Removed debug checkbox from here
|
406 |
with gr.Row():
|
407 |
-
generate_button = gr.Button("Generate (Full Debug to Console)", scale=1)
|
408 |
with gr.Row():
|
409 |
max_len_slider = gr.Slider(minimum=10, maximum=150, value=50, step=1, label="Max Generation Length")
|
410 |
temp_slider = gr.Slider(minimum=0.0, maximum=2.0, value=0.8, step=0.1, label="Temperature (0 for greedy)")
|
@@ -427,7 +416,7 @@ with gr.Blocks(title="SWCK Conceptual Demo") as demo:
|
|
427 |
|
428 |
generate_button.click(
|
429 |
fn=generate_text_for_app,
|
430 |
-
inputs=[prompt_input, max_len_slider, temp_slider],
|
431 |
outputs=[output_text, debug_text_area]
|
432 |
)
|
433 |
|
|
|
57 |
GATE_SPARSITY_LOSS_WEIGHT_APP = 0.001
|
58 |
WIRING_PHASE_EPOCHS_APP = 1
|
59 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
60 |
def build_vocab_from_corpus_text_app(corpus_text):
|
61 |
global VOCAB_SIZE_APP
|
62 |
print("App: Building vocabulary...")
|
|
|
73 |
print(f"App: Built vocab of size {VOCAB_SIZE_APP}")
|
74 |
return temp_word_to_idx, temp_idx_to_word
|
75 |
|
76 |
+
def initialize_or_load_model_app():
|
|
|
77 |
global swck_model_global, optimizer_global, word_to_idx_global, idx_to_word_global, \
|
78 |
VOCAB_SIZE_APP, model_load_status_global
|
79 |
|
|
|
92 |
'num_sub_modules_per_block': NUM_SUB_MODULES_PER_BLOCK_APP
|
93 |
}
|
94 |
|
95 |
+
print("App: Initializing SWCKModel. Debug prints are ON by default in model components.")
|
|
|
|
|
|
|
|
|
|
|
96 |
|
97 |
swck_model_global = SWCKModel(**model_args).to(device_global)
|
98 |
+
# Ensure debug flags are True on all components after initialization
|
99 |
+
# (assuming model.py might have them False by default, this makes them True)
|
100 |
+
if swck_model_global:
|
101 |
+
swck_model_global.debug_prints_enabled = True
|
102 |
+
if hasattr(swck_model_global, 'seed_parser'):
|
103 |
+
swck_model_global.seed_parser.debug_prints_enabled = True
|
104 |
+
if hasattr(swck_model_global, 'adaptive_blocks'):
|
105 |
+
for block in swck_model_global.adaptive_blocks:
|
106 |
+
block.debug_prints_enabled = True
|
107 |
+
print("App: Confirmed debug prints ON for SWCKModel and its components.")
|
108 |
|
109 |
|
110 |
if os.path.exists(CHECKPOINT_FILENAME):
|
|
|
129 |
else:
|
130 |
print("App: word_to_idx not in checkpoint, using app's rebuilt vocab.")
|
131 |
|
132 |
+
# After loading, ensure debug flags are still True
|
133 |
+
if swck_model_global:
|
134 |
+
swck_model_global.debug_prints_enabled = True
|
135 |
+
if hasattr(swck_model_global, 'seed_parser'): swck_model_global.seed_parser.debug_prints_enabled = True
|
136 |
+
for block in swck_model_global.adaptive_blocks: block.debug_prints_enabled = True
|
137 |
+
print("App: Re-confirmed debug prints ON after loading checkpoint.")
|
138 |
+
|
139 |
|
140 |
model_load_status_global = f"Model loaded successfully from {CHECKPOINT_FILENAME}."
|
141 |
print(model_load_status_global)
|
142 |
except Exception as e:
|
143 |
+
print(f"App: Error loading model from checkpoint: {e}. Re-initializing new model.")
|
144 |
swck_model_global = SWCKModel(**model_args).to(device_global)
|
145 |
+
if swck_model_global: # Ensure debug is on for the new instance too
|
146 |
+
swck_model_global.debug_prints_enabled = True
|
147 |
+
if hasattr(swck_model_global, 'seed_parser'): swck_model_global.seed_parser.debug_prints_enabled = True
|
148 |
+
for block in swck_model_global.adaptive_blocks: block.debug_prints_enabled = True
|
149 |
optimizer_global = optim.AdamW(swck_model_global.parameters(), lr=0.001)
|
150 |
+
model_load_status_global = "Error loading checkpoint. Using new (untrained) model."
|
151 |
else:
|
152 |
+
print(f"App: Checkpoint {CHECKPOINT_FILENAME} not found. Initializing new model.")
|
153 |
+
# Debug flags already set for a new model instance above
|
154 |
optimizer_global = optim.AdamW(swck_model_global.parameters(), lr=0.001)
|
155 |
+
model_load_status_global = "Initialized a new (untrained) model."
|
156 |
|
157 |
swck_model_global.eval()
|
158 |
return model_load_status_global
|
|
|
189 |
if swck_model_global is None or word_to_idx_global is None:
|
190 |
return "Model not initialized. Cannot train."
|
191 |
|
192 |
+
print("\n--- App: Starting Short Training Session (Full Debug ON for ALL batches/epochs by default) ---")
|
193 |
progress(0, desc="Preparing training data...")
|
194 |
|
195 |
+
# Model debug flags are assumed to be already ON from initialize_or_load_model_app()
|
196 |
|
197 |
training_corpus = SEED_PHRASE_APP + " " + EXTENDED_TEXT_FOR_TRAINING_APP
|
198 |
app_dataset = AppSWCKDataset(training_corpus, word_to_idx_global, SEQ_LEN_APP, SOS_TOKEN, EOS_TOKEN, PAD_TOKEN)
|
199 |
if not app_dataset.samples:
|
|
|
200 |
return "App Training Error: No samples created from the corpus."
|
201 |
|
202 |
app_dataloader = DataLoader(app_dataset, batch_size=int(batch_size_app), shuffle=True, collate_fn=app_swck_collate_fn)
|
|
|
215 |
for epoch in progress.tqdm(range(int(num_epochs_app)), desc="Training Epochs"):
|
216 |
swck_model_global.set_wiring_phase(epoch < WIRING_PHASE_EPOCHS_APP)
|
217 |
epoch_loss = 0.0
|
218 |
+
# No need to toggle debug here; it's globally on for the model instance
|
219 |
|
220 |
for batch_idx, (src_batch, tgt_batch) in enumerate(app_dataloader):
|
221 |
+
# Print statements within model.py's forward methods will now trigger automatically
|
222 |
+
print(f"\n--- Training Batch {batch_idx+1}/{len(app_dataloader)} (Epoch {epoch+1}) ---")
|
223 |
|
224 |
src_batch, tgt_batch = src_batch.to(device_global), tgt_batch.to(device_global)
|
225 |
decoder_input_tokens = src_batch[:, :-1]
|
|
|
267 |
epoch_loss += combined_loss.item()
|
268 |
|
269 |
log_line = f" Epoch {epoch+1}, Batch {batch_idx+1}/{len(app_dataloader)}, Loss: {combined_loss.item():.4f}"
|
270 |
+
print(log_line) # This will go to console
|
271 |
if batch_idx % max(1, len(app_dataloader)//2) == 0 or batch_idx == len(app_dataloader)-1 :
|
272 |
+
training_log_output += log_line + "\n" # Summary to UI
|
273 |
|
274 |
avg_epoch_loss = epoch_loss / len(app_dataloader) if len(app_dataloader) > 0 else epoch_loss
|
275 |
epoch_summary = f"Epoch {epoch+1}/{num_epochs_app} - Avg Loss: {avg_epoch_loss:.4f}\n"
|
276 |
print(epoch_summary)
|
277 |
training_log_output += epoch_summary
|
278 |
|
279 |
+
print("--- App: Training Session Finished. Debug prints remain ON for the model instance. ---")
|
280 |
+
# No need to turn off debugs here if they are meant to be globally on for the app session
|
281 |
swck_model_global.eval()
|
282 |
|
283 |
try:
|
|
|
311 |
swck_model_global.eval()
|
312 |
swck_model_global.set_wiring_phase(False)
|
313 |
|
314 |
+
# Model debug flags are assumed to be already ON globally from initialize_or_load_model_app()
|
315 |
+
print("\n--- App: Generating Text (Full Debug ON by default) ---")
|
|
|
316 |
print(f"App: Generating for prompt: '{prompt_str}', max_len: {max_len_gen}, temp: {temperature_gen}")
|
317 |
|
318 |
tokens = [SOS_TOKEN] + [word_to_idx_global.get(w, UNK_TOKEN) for w in prompt_str.lower().split()]
|
|
|
321 |
|
322 |
with torch.no_grad():
|
323 |
for i in range(int(max_len_gen)):
|
324 |
+
# Print statements inside SWCKModel's forward and AdaptiveBlock's forward will trigger
|
325 |
print(f"\n--- Generation Step {i+1} ---")
|
326 |
context_start_idx = max(0, len(generated_ids_app) - SEQ_LEN_APP)
|
327 |
current_context_ids = generated_ids_app[context_start_idx:]
|
|
|
371 |
|
372 |
debug_output_str = "\n".join(debug_info_lines)
|
373 |
|
374 |
+
# Debug flags remain ON for the model instance for subsequent calls unless changed elsewhere
|
375 |
+
print("--- App: Generation Finished. Debug prints remain ON for the model instance. ---")
|
376 |
return final_text, debug_output_str
|
377 |
|
378 |
+
# Initialize model with debug ON by default for the whole app session
|
379 |
initial_load_status = initialize_or_load_model_app(enable_initial_debug=True)
|
380 |
|
381 |
with gr.Blocks(title="SWCK Conceptual Demo") as demo:
|
|
|
383 |
|
384 |
gr.Markdown(f"""
|
385 |
# Self-Wired Conscious Kernel (SWCK) - Conceptual Demo
|
386 |
+
This demo showcases a conceptual text generation model with **FULL KERNEL DEBUGGING ON by default** for all operations (output to Space console logs).
|
387 |
Seed Phrase: "{SEED_PHRASE_APP[:100]}..." | Seed Number: "{SEED_NUMBER_STR_APP}".
|
388 |
(Note: If checkpoint is not found or fails to load, an *untrained* model is used.)
|
389 |
""")
|
|
|
392 |
with gr.TabItem("Generate Text"):
|
393 |
with gr.Row():
|
394 |
prompt_input = gr.Textbox(label="Enter your prompt:", placeholder="e.g., the meaning of existence is", scale=3)
|
|
|
395 |
with gr.Row():
|
396 |
+
generate_button = gr.Button("Generate (Full Debug to Console)", scale=1)
|
397 |
with gr.Row():
|
398 |
max_len_slider = gr.Slider(minimum=10, maximum=150, value=50, step=1, label="Max Generation Length")
|
399 |
temp_slider = gr.Slider(minimum=0.0, maximum=2.0, value=0.8, step=0.1, label="Temperature (0 for greedy)")
|
|
|
416 |
|
417 |
generate_button.click(
|
418 |
fn=generate_text_for_app,
|
419 |
+
inputs=[prompt_input, max_len_slider, temp_slider],
|
420 |
outputs=[output_text, debug_text_area]
|
421 |
)
|
422 |
|