neuralworm's picture
add control experiments
09d65a8
raw
history blame
6.86 kB
import gradio as gr
import pandas as pd
import gc
import torch
import json
from cognitive_mapping_probe.orchestrator_seismograph import run_seismic_analysis
from cognitive_mapping_probe.auto_experiment import run_auto_suite, get_curated_experiments
from cognitive_mapping_probe.prompts import RESONANCE_PROMPTS
from cognitive_mapping_probe.utils import dbg
theme = gr.themes.Soft(primary_hue="indigo", secondary_hue="blue").set(body_background_fill="#f0f4f9", block_background_fill="white")
def cleanup_memory():
"""Räumt Speicher nach jedem Experimentlauf auf."""
dbg("Cleaning up memory...")
gc.collect()
if torch.cuda.is_available():
torch.cuda.empty_cache()
dbg("Memory cleanup complete.")
def run_single_analysis_display(*args, progress=gr.Progress(track_tqdm=True)):
"""Wrapper für den 'Manual Single Run'-Tab."""
# (Bleibt unverändert)
pass # Platzhalter
PLOT_PARAMS_DEFAULT = {
"x": "Step", "y": "Value", "color": "Metric",
"title": "Comparative Cognitive Dynamics", "color_legend_title": "Metrics",
"color_legend_position": "bottom", "show_label": True, "height": 400, "interactive": True
}
def run_auto_suite_display(model_id, num_steps, seed, experiment_name, progress=gr.Progress(track_tqdm=True)):
"""Wrapper, der nun die speziellen Plots für ACT und Mechanistic Probe handhaben kann."""
summary_df, plot_df, all_results = run_auto_suite(model_id, int(num_steps), int(seed), experiment_name, progress)
dataframe_component = gr.DataFrame(label="Comparative Statistical Signature", value=summary_df, wrap=True, row_count=(len(summary_df), "dynamic"))
if experiment_name == "ACT Titration (Point of No Return)":
plot_params_act = {
"x": "Patch Step", "y": "Post-Patch Mean Delta",
"title": "Attractor Capture Time (ACT) - Phase Transition",
"mark": "line", "show_label": True, "height": 400, "interactive": True
}
new_plot = gr.LinePlot(value=plot_df, **plot_params_act)
# --- NEU: Spezielle Plot-Logik für die mechanistische Sonde ---
elif experiment_name == "Mechanistic Probe (Attention Entropies)":
plot_params_mech = {
"x": "Step", "y": "Value", "color": "Metric",
"title": "Mechanistic Analysis: State Delta vs. Attention Entropy",
"color_legend_title": "Metric", "show_label": True, "height": 400, "interactive": True
}
new_plot = gr.LinePlot(value=plot_df, **plot_params_mech)
else:
# Passe die Parameter an, um mit der geschmolzenen DataFrame-Struktur zu arbeiten
plot_params_dynamic = PLOT_PARAMS_DEFAULT.copy()
plot_params_dynamic['y'] = 'Delta'
plot_params_dynamic['color'] = 'Experiment'
new_plot = gr.LinePlot(value=plot_df, **plot_params_dynamic)
serializable_results = json.dumps(all_results, indent=2, default=str)
cleanup_memory()
return dataframe_component, new_plot, serializable_results
with gr.Blocks(theme=theme, title="Cognitive Seismograph 2.3") as demo:
gr.Markdown("# 🧠 Cognitive Seismograph 2.3: Advanced Experiment Suite")
with gr.Tabs():
with gr.TabItem("🔬 Manual Single Run"):
gr.Markdown("Run a single experiment with manual parameters to explore specific hypotheses.")
with gr.Row(variant='panel'):
with gr.Column(scale=1):
gr.Markdown("### 1. General Parameters")
manual_model_id = gr.Textbox(value="google/gemma-3-1b-it", label="Model ID")
manual_prompt_type = gr.Radio(choices=list(RESONANCE_PROMPTS.keys()), value="resonance_prompt", label="Prompt Type")
manual_seed = gr.Slider(1, 1000, 42, step=1, label="Seed")
manual_num_steps = gr.Slider(50, 1000, 300, step=10, label="Number of Internal Steps")
gr.Markdown("### 2. Modulation Parameters")
manual_concept = gr.Textbox(label="Concept to Inject", placeholder="e.g., 'calmness'")
manual_strength = gr.Slider(0.0, 5.0, 1.5, step=0.1, label="Injection Strength")
manual_run_btn = gr.Button("Run Single Analysis", variant="primary")
with gr.Column(scale=2):
gr.Markdown("### Single Run Results")
manual_verdict = gr.Markdown("Analysis results will appear here.")
manual_plot = gr.LinePlot(x="Internal Step", y="State Change (Delta)", title="Internal State Dynamics", show_label=True, height=400)
with gr.Accordion("Raw JSON Output", open=False):
manual_raw_json = gr.JSON()
manual_run_btn.click(
fn=run_single_analysis_display,
inputs=[manual_model_id, manual_prompt_type, manual_seed, manual_num_steps, manual_concept, manual_strength],
outputs=[manual_verdict, manual_plot, manual_raw_json]
)
with gr.TabItem("🚀 Automated Suite"):
gr.Markdown("Run a predefined, curated suite of experiments and visualize the results comparatively.")
with gr.Row(variant='panel'):
with gr.Column(scale=1):
gr.Markdown("### Auto-Experiment Parameters")
auto_model_id = gr.Textbox(value="google/gemma-3-4b-it", label="Model ID")
auto_num_steps = gr.Slider(50, 1000, 300, step=10, label="Steps per Run")
auto_seed = gr.Slider(1, 1000, 42, step=1, label="Seed")
auto_experiment_name = gr.Dropdown(
choices=list(get_curated_experiments().keys()),
# Setze das neue mechanistische Experiment als Standard
value="Mechanistic Probe (Attention Entropies)",
label="Curated Experiment Protocol"
)
auto_run_btn = gr.Button("Run Curated Auto-Experiment", variant="primary")
with gr.Column(scale=2):
gr.Markdown("### Suite Results Summary")
auto_plot_output = gr.LinePlot(**PLOT_PARAMS_DEFAULT)
auto_summary_df = gr.DataFrame(label="Comparative Statistical Signature", wrap=True)
with gr.Accordion("Raw JSON for all runs", open=False):
auto_raw_json = gr.JSON()
auto_run_btn.click(
fn=run_auto_suite_display,
inputs=[auto_model_id, auto_num_steps, auto_seed, auto_experiment_name],
outputs=[auto_summary_df, auto_plot_output, auto_raw_json]
)
if __name__ == "__main__":
# (launch() wird durch Gradio's __main__-Block aufgerufen)
demo.launch(server_name="0.0.0.0", server_port=7860, debug=True)