neuralworm commited on
Commit
64ad029
·
1 Parent(s): b3585ba
Files changed (1) hide show
  1. app.py +43 -27
app.py CHANGED
@@ -9,56 +9,85 @@ from cognitive_mapping_probe.auto_experiment import run_auto_suite, get_curated_
9
  from cognitive_mapping_probe.prompts import RESONANCE_PROMPTS
10
  from cognitive_mapping_probe.utils import dbg
11
 
 
12
  theme = gr.themes.Soft(primary_hue="indigo", secondary_hue="blue").set(body_background_fill="#f0f4f9", block_background_fill="white")
13
 
 
 
14
  def cleanup_memory():
15
- """Eine zentrale Funktion zum Aufräumen des Speichers nach einem Lauf."""
16
  dbg("Cleaning up memory...")
17
  gc.collect()
18
  if torch.cuda.is_available():
19
  torch.cuda.empty_cache()
20
  dbg("Memory cleanup complete.")
21
 
 
 
22
  def run_single_analysis_display(*args, progress=gr.Progress(track_tqdm=True)):
23
  """Wrapper für ein einzelnes manuelles Experiment."""
24
  try:
 
25
  results = run_seismic_analysis(*args, progress_callback=progress)
26
  stats = results.get("stats", {})
27
  deltas = results.get("state_deltas", [])
 
 
28
  df = pd.DataFrame({"Internal Step": range(len(deltas)), "State Change (Delta)": deltas})
29
  stats_md = f"### Statistical Signature\n- **Mean Delta:** {stats.get('mean_delta', 0):.4f}\n- **Std Dev Delta:** {stats.get('std_delta', 0):.4f}\n- **Max Delta:** {stats.get('max_delta', 0):.4f}\n"
30
 
31
- cleanup_memory()
32
  return f"{results.get('verdict', 'Error')}\n\n{stats_md}", df, results
33
  except Exception:
34
- cleanup_memory()
35
  return f"### ❌ Analysis Failed\n```\n{traceback.format_exc()}\n```", pd.DataFrame(), {}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36
 
37
  def run_auto_suite_display(model_id, num_steps, seed, experiment_name, progress=gr.Progress(track_tqdm=True)):
38
- """Wrapper für die automatisierte Experiment-Suite mit Visualisierung."""
 
 
 
39
  try:
40
  summary_df, plot_df, all_results = run_auto_suite(model_id, int(num_steps), int(seed), experiment_name, progress)
41
 
42
- # DEBUG-Ausgabe zur Überprüfung der DataFrame-Struktur
43
- dbg("Plot DataFrame Head:\n", plot_df.head())
44
- dbg("Plot DataFrame Dtypes:\n", plot_df.dtypes)
45
 
46
- cleanup_memory()
47
- return summary_df, plot_df, all_results
 
 
 
48
  except Exception:
 
 
 
 
49
  cleanup_memory()
50
- return pd.DataFrame(), pd.DataFrame(), f"### ❌ Auto-Experiment Failed\n```\n{traceback.format_exc()}\n```"
 
51
 
52
  with gr.Blocks(theme=theme, title="Cognitive Seismograph 2.3") as demo:
53
- gr.Markdown("# 🧠 Cognitive Seismograph 2.3: Machine Psychology")
54
 
55
  with gr.Tabs():
56
  with gr.TabItem("🔬 Manual Single Run"):
57
- # ... (Dieser Tab bleibt unverändert) ...
58
  gr.Markdown("Führe ein einzelnes Experiment mit manuellen Parametern durch, um Hypothesen zu explorieren.")
59
  with gr.Row(variant='panel'):
60
  with gr.Column(scale=1):
61
- # ... (Parameter unverändert) ...
62
  gr.Markdown("### 1. General Parameters")
63
  manual_model_id = gr.Textbox(value="google/gemma-3-1b-it", label="Model ID")
64
  manual_prompt_type = gr.Radio(choices=list(RESONANCE_PROMPTS.keys()), value="resonance_prompt", label="Prompt Type")
@@ -85,7 +114,6 @@ with gr.Blocks(theme=theme, title="Cognitive Seismograph 2.3") as demo:
85
  gr.Markdown("Führe eine vordefinierte, kuratierte Reihe von Experimenten durch und visualisiere die Ergebnisse vergleichend.")
86
  with gr.Row(variant='panel'):
87
  with gr.Column(scale=1):
88
- # ... (Parameter unverändert) ...
89
  gr.Markdown("### Auto-Experiment Parameters")
90
  auto_model_id = gr.Textbox(value="google/gemma-3-1b-it", label="Model ID")
91
  auto_num_steps = gr.Slider(50, 1000, 300, step=10, label="Steps per Run")
@@ -94,19 +122,7 @@ with gr.Blocks(theme=theme, title="Cognitive Seismograph 2.3") as demo:
94
  auto_run_btn = gr.Button("Run Curated Auto-Experiment", variant="primary")
95
  with gr.Column(scale=2):
96
  gr.Markdown("### Suite Results Summary")
97
- # FINALE KORREKTUR: Wir definieren die Spaltennamen explizit,
98
- # um jegliche Ambiguität für Gradio zu beseitigen.
99
- auto_plot_output = gr.LinePlot(
100
- x="Step",
101
- y="Delta",
102
- color="Experiment",
103
- title="Comparative Cognitive Dynamics",
104
- color_legend_title="Experiment Runs",
105
- color_legend_position="bottom",
106
- show_label=True,
107
- height=400,
108
- interactive=True
109
- )
110
  auto_summary_df = gr.DataFrame(label="Comparative Statistical Signature", wrap=True)
111
  with gr.Accordion("Raw JSON for all runs", open=False):
112
  auto_raw_json = gr.JSON()
 
9
  from cognitive_mapping_probe.prompts import RESONANCE_PROMPTS
10
  from cognitive_mapping_probe.utils import dbg
11
 
12
+ # --- UI Theme ---
13
  theme = gr.themes.Soft(primary_hue="indigo", secondary_hue="blue").set(body_background_fill="#f0f4f9", block_background_fill="white")
14
 
15
+ # --- Hilfsfunktionen ---
16
+
17
  def cleanup_memory():
18
+ """Eine zentrale Funktion zum Aufräumen des VRAM und des Python-Speichers."""
19
  dbg("Cleaning up memory...")
20
  gc.collect()
21
  if torch.cuda.is_available():
22
  torch.cuda.empty_cache()
23
  dbg("Memory cleanup complete.")
24
 
25
+ # --- Wrapper für Gradio-Funktionalität ---
26
+
27
  def run_single_analysis_display(*args, progress=gr.Progress(track_tqdm=True)):
28
  """Wrapper für ein einzelnes manuelles Experiment."""
29
  try:
30
+ # Führe die Analyse durch
31
  results = run_seismic_analysis(*args, progress_callback=progress)
32
  stats = results.get("stats", {})
33
  deltas = results.get("state_deltas", [])
34
+
35
+ # Bereite die Ausgaben vor
36
  df = pd.DataFrame({"Internal Step": range(len(deltas)), "State Change (Delta)": deltas})
37
  stats_md = f"### Statistical Signature\n- **Mean Delta:** {stats.get('mean_delta', 0):.4f}\n- **Std Dev Delta:** {stats.get('std_delta', 0):.4f}\n- **Max Delta:** {stats.get('max_delta', 0):.4f}\n"
38
 
 
39
  return f"{results.get('verdict', 'Error')}\n\n{stats_md}", df, results
40
  except Exception:
 
41
  return f"### ❌ Analysis Failed\n```\n{traceback.format_exc()}\n```", pd.DataFrame(), {}
42
+ finally:
43
+ # Stelle sicher, dass der Speicher in jedem Fall aufgeräumt wird
44
+ cleanup_memory()
45
+
46
+ # Definiere die Plot-Parameter an einer zentralen Stelle für Konsistenz
47
+ PLOT_PARAMS = {
48
+ "x": "Step",
49
+ "y": "Delta",
50
+ "color": "Experiment",
51
+ "title": "Comparative Cognitive Dynamics",
52
+ "color_legend_title": "Experiment Runs",
53
+ "color_legend_position": "bottom",
54
+ "show_label": True,
55
+ "height": 400,
56
+ "interactive": True
57
+ }
58
 
59
  def run_auto_suite_display(model_id, num_steps, seed, experiment_name, progress=gr.Progress(track_tqdm=True)):
60
+ """
61
+ Wrapper für die automatisierte Experiment-Suite.
62
+ Gibt eine neue `gr.LinePlot`-Instanz zurück, um den State-Leak-Bug zu beheben.
63
+ """
64
  try:
65
  summary_df, plot_df, all_results = run_auto_suite(model_id, int(num_steps), int(seed), experiment_name, progress)
66
 
67
+ dbg("Plot DataFrame Head for Auto-Suite:\n", plot_df.head())
 
 
68
 
69
+ # WISSENSCHAFTLICHE KORREKTUR: Erzeuge eine komplett neue Plot-Komponente
70
+ # mit den neuen Daten. Dies zwingt Gradio, den alten Zustand zu verwerfen.
71
+ new_plot = gr.LinePlot(value=plot_df, **PLOT_PARAMS)
72
+
73
+ return summary_df, new_plot, all_results
74
  except Exception:
75
+ # Im Fehlerfall, gib leere, aber korrekt typisierte Komponenten zurück
76
+ empty_plot = gr.LinePlot(value=pd.DataFrame(), **PLOT_PARAMS)
77
+ return pd.DataFrame(), empty_plot, f"### ❌ Auto-Experiment Failed\n```\n{traceback.format_exc()}\n```"
78
+ finally:
79
  cleanup_memory()
80
+
81
+ # --- Gradio UI-Definition ---
82
 
83
  with gr.Blocks(theme=theme, title="Cognitive Seismograph 2.3") as demo:
84
+ gr.Markdown("# 🧠 Cognitive Seismograph 2.3: Advanced Experiment Suite")
85
 
86
  with gr.Tabs():
87
  with gr.TabItem("🔬 Manual Single Run"):
 
88
  gr.Markdown("Führe ein einzelnes Experiment mit manuellen Parametern durch, um Hypothesen zu explorieren.")
89
  with gr.Row(variant='panel'):
90
  with gr.Column(scale=1):
 
91
  gr.Markdown("### 1. General Parameters")
92
  manual_model_id = gr.Textbox(value="google/gemma-3-1b-it", label="Model ID")
93
  manual_prompt_type = gr.Radio(choices=list(RESONANCE_PROMPTS.keys()), value="resonance_prompt", label="Prompt Type")
 
114
  gr.Markdown("Führe eine vordefinierte, kuratierte Reihe von Experimenten durch und visualisiere die Ergebnisse vergleichend.")
115
  with gr.Row(variant='panel'):
116
  with gr.Column(scale=1):
 
117
  gr.Markdown("### Auto-Experiment Parameters")
118
  auto_model_id = gr.Textbox(value="google/gemma-3-1b-it", label="Model ID")
119
  auto_num_steps = gr.Slider(50, 1000, 300, step=10, label="Steps per Run")
 
122
  auto_run_btn = gr.Button("Run Curated Auto-Experiment", variant="primary")
123
  with gr.Column(scale=2):
124
  gr.Markdown("### Suite Results Summary")
125
+ auto_plot_output = gr.LinePlot(**PLOT_PARAMS)
 
 
 
 
 
 
 
 
 
 
 
 
126
  auto_summary_df = gr.DataFrame(label="Comparative Statistical Signature", wrap=True)
127
  with gr.Accordion("Raw JSON for all runs", open=False):
128
  auto_raw_json = gr.JSON()