Commit
·
b9b7087
1
Parent(s):
5ab2ded
fix
Browse files
app.py
CHANGED
|
@@ -3,57 +3,48 @@ import pandas as pd
|
|
| 3 |
import traceback
|
| 4 |
import gc
|
| 5 |
import torch
|
|
|
|
| 6 |
|
| 7 |
from cognitive_mapping_probe.orchestrator_seismograph import run_seismic_analysis
|
| 8 |
-
from cognitive_mapping_probe.auto_experiment import
|
| 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 |
-
# --- Helper Functions ---
|
| 16 |
-
|
| 17 |
def cleanup_memory():
|
| 18 |
-
"""
|
| 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 |
-
# --- Gradio Wrapper Functions ---
|
| 26 |
-
|
| 27 |
def run_single_analysis_display(*args, progress=gr.Progress(track_tqdm=True)):
|
| 28 |
-
"""Wrapper
|
| 29 |
try:
|
| 30 |
results = run_seismic_analysis(*args, progress_callback=progress)
|
| 31 |
-
stats = results.get("stats", {})
|
| 32 |
-
deltas = results.get("state_deltas", [])
|
| 33 |
-
|
| 34 |
df = pd.DataFrame({"Internal Step": range(len(deltas)), "State Change (Delta)": deltas})
|
| 35 |
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"
|
| 36 |
|
| 37 |
-
|
|
|
|
|
|
|
|
|
|
| 38 |
except Exception:
|
| 39 |
-
return f"### ❌ Analysis Failed\n```\n{traceback.format_exc()}\n```", pd.DataFrame(), {}
|
| 40 |
finally:
|
| 41 |
cleanup_memory()
|
| 42 |
|
| 43 |
PLOT_PARAMS = {
|
| 44 |
-
"x": "Step",
|
| 45 |
-
"
|
| 46 |
-
"
|
| 47 |
-
"title": "Comparative Cognitive Dynamics",
|
| 48 |
-
"color_legend_title": "Experiment Runs",
|
| 49 |
-
"color_legend_position": "bottom",
|
| 50 |
-
"show_label": True,
|
| 51 |
-
"height": 400,
|
| 52 |
-
"interactive": True
|
| 53 |
}
|
| 54 |
|
| 55 |
def run_auto_suite_display(model_id, num_steps, seed, experiment_name, progress=gr.Progress(track_tqdm=True)):
|
| 56 |
-
"""Wrapper
|
| 57 |
try:
|
| 58 |
summary_df, plot_df, all_results = run_auto_suite(model_id, int(num_steps), int(seed), experiment_name, progress)
|
| 59 |
|
|
@@ -61,20 +52,23 @@ def run_auto_suite_display(model_id, num_steps, seed, experiment_name, progress=
|
|
| 61 |
|
| 62 |
new_plot = gr.LinePlot(value=plot_df, **PLOT_PARAMS)
|
| 63 |
|
| 64 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
except Exception:
|
| 66 |
empty_plot = gr.LinePlot(value=pd.DataFrame(), **PLOT_PARAMS)
|
| 67 |
return pd.DataFrame(), empty_plot, f"### ❌ Auto-Experiment Failed\n```\n{traceback.format_exc()}\n```"
|
| 68 |
finally:
|
| 69 |
cleanup_memory()
|
| 70 |
|
| 71 |
-
# --- Gradio UI Definition ---
|
| 72 |
-
|
| 73 |
with gr.Blocks(theme=theme, title="Cognitive Seismograph 2.3") as demo:
|
| 74 |
gr.Markdown("# 🧠 Cognitive Seismograph 2.3: Advanced Experiment Suite")
|
| 75 |
|
| 76 |
with gr.Tabs():
|
| 77 |
with gr.TabItem("🔬 Manual Single Run"):
|
|
|
|
| 78 |
gr.Markdown("Run a single experiment with manual parameters to explore hypotheses.")
|
| 79 |
with gr.Row(variant='panel'):
|
| 80 |
with gr.Column(scale=1):
|
|
@@ -93,7 +87,6 @@ with gr.Blocks(theme=theme, title="Cognitive Seismograph 2.3") as demo:
|
|
| 93 |
manual_plot = gr.LinePlot(x="Internal Step", y="State Change (Delta)", title="Internal State Dynamics", show_label=True, height=400, interactive=True)
|
| 94 |
with gr.Accordion("Raw JSON Output", open=False):
|
| 95 |
manual_raw_json = gr.JSON()
|
| 96 |
-
|
| 97 |
manual_run_btn.click(
|
| 98 |
fn=run_single_analysis_display,
|
| 99 |
inputs=[manual_model_id, manual_prompt_type, manual_seed, manual_num_steps, manual_concept, manual_strength],
|
|
@@ -101,14 +94,15 @@ with gr.Blocks(theme=theme, title="Cognitive Seismograph 2.3") as demo:
|
|
| 101 |
)
|
| 102 |
|
| 103 |
with gr.TabItem("🚀 Automated Suite"):
|
|
|
|
| 104 |
gr.Markdown("Run a predefined, curated suite of experiments and visualize the results comparatively.")
|
| 105 |
with gr.Row(variant='panel'):
|
| 106 |
with gr.Column(scale=1):
|
| 107 |
gr.Markdown("### Auto-Experiment Parameters")
|
| 108 |
-
auto_model_id = gr.Textbox(value="google/gemma-3-
|
| 109 |
auto_num_steps = gr.Slider(50, 1000, 300, step=10, label="Steps per Run")
|
| 110 |
auto_seed = gr.Slider(1, 1000, 42, step=1, label="Seed")
|
| 111 |
-
auto_experiment_name = gr.Dropdown(choices=list(get_curated_experiments().keys()), value="
|
| 112 |
auto_run_btn = gr.Button("Run Curated Auto-Experiment", variant="primary")
|
| 113 |
with gr.Column(scale=2):
|
| 114 |
gr.Markdown("### Suite Results Summary")
|
|
@@ -116,7 +110,6 @@ with gr.Blocks(theme=theme, title="Cognitive Seismograph 2.3") as demo:
|
|
| 116 |
auto_summary_df = gr.DataFrame(label="Comparative Statistical Signature", wrap=True)
|
| 117 |
with gr.Accordion("Raw JSON for all runs", open=False):
|
| 118 |
auto_raw_json = gr.JSON()
|
| 119 |
-
|
| 120 |
auto_run_btn.click(
|
| 121 |
fn=run_auto_suite_display,
|
| 122 |
inputs=[auto_model_id, auto_num_steps, auto_seed, auto_experiment_name],
|
|
|
|
| 3 |
import traceback
|
| 4 |
import gc
|
| 5 |
import torch
|
| 6 |
+
import json # Importiere das json-Modul
|
| 7 |
|
| 8 |
from cognitive_mapping_probe.orchestrator_seismograph import run_seismic_analysis
|
| 9 |
+
from cognitive_mapping_probe.auto_experiment import run_auto_suite, get_curated_experiments
|
| 10 |
from cognitive_mapping_probe.prompts import RESONANCE_PROMPTS
|
| 11 |
from cognitive_mapping_probe.utils import dbg
|
| 12 |
|
|
|
|
| 13 |
theme = gr.themes.Soft(primary_hue="indigo", secondary_hue="blue").set(body_background_fill="#f0f4f9", block_background_fill="white")
|
| 14 |
|
|
|
|
|
|
|
| 15 |
def cleanup_memory():
|
| 16 |
+
"""Eine zentrale Funktion zum Aufräumen des Speichers nach einem Lauf."""
|
| 17 |
dbg("Cleaning up memory...")
|
| 18 |
gc.collect()
|
| 19 |
if torch.cuda.is_available():
|
| 20 |
torch.cuda.empty_cache()
|
| 21 |
dbg("Memory cleanup complete.")
|
| 22 |
|
|
|
|
|
|
|
| 23 |
def run_single_analysis_display(*args, progress=gr.Progress(track_tqdm=True)):
|
| 24 |
+
"""Wrapper für ein einzelnes manuelles Experiment."""
|
| 25 |
try:
|
| 26 |
results = run_seismic_analysis(*args, progress_callback=progress)
|
| 27 |
+
stats, deltas = results.get("stats", {}), 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 |
+
# Stelle sicher, dass das Ergebnis für die JSON-Komponente serialisierbar ist
|
| 32 |
+
serializable_results = json.dumps(results, indent=2, default=str)
|
| 33 |
+
|
| 34 |
+
return f"{results.get('verdict', 'Error')}\n\n{stats_md}", df, serializable_results
|
| 35 |
except Exception:
|
| 36 |
+
return f"### ❌ Analysis Failed\n```\n{traceback.format_exc()}\n```", pd.DataFrame(), "{}"
|
| 37 |
finally:
|
| 38 |
cleanup_memory()
|
| 39 |
|
| 40 |
PLOT_PARAMS = {
|
| 41 |
+
"x": "Step", "y": "Delta", "color": "Experiment",
|
| 42 |
+
"title": "Comparative Cognitive Dynamics", "color_legend_title": "Experiment Runs",
|
| 43 |
+
"color_legend_position": "bottom", "show_label": True, "height": 400, "interactive": True
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
}
|
| 45 |
|
| 46 |
def run_auto_suite_display(model_id, num_steps, seed, experiment_name, progress=gr.Progress(track_tqdm=True)):
|
| 47 |
+
"""Wrapper für die automatisierte Experiment-Suite mit korrekter JSON-Serialisierung."""
|
| 48 |
try:
|
| 49 |
summary_df, plot_df, all_results = run_auto_suite(model_id, int(num_steps), int(seed), experiment_name, progress)
|
| 50 |
|
|
|
|
| 52 |
|
| 53 |
new_plot = gr.LinePlot(value=plot_df, **PLOT_PARAMS)
|
| 54 |
|
| 55 |
+
# KORREKTUR: Serialisiere das Ergebnis-Dictionary explizit zu einem JSON-String,
|
| 56 |
+
# bevor es an die `gr.JSON`-Komponente zurückgegeben wird.
|
| 57 |
+
serializable_results = json.dumps(all_results, indent=2, default=str)
|
| 58 |
+
|
| 59 |
+
return summary_df, new_plot, serializable_results
|
| 60 |
except Exception:
|
| 61 |
empty_plot = gr.LinePlot(value=pd.DataFrame(), **PLOT_PARAMS)
|
| 62 |
return pd.DataFrame(), empty_plot, f"### ❌ Auto-Experiment Failed\n```\n{traceback.format_exc()}\n```"
|
| 63 |
finally:
|
| 64 |
cleanup_memory()
|
| 65 |
|
|
|
|
|
|
|
| 66 |
with gr.Blocks(theme=theme, title="Cognitive Seismograph 2.3") as demo:
|
| 67 |
gr.Markdown("# 🧠 Cognitive Seismograph 2.3: Advanced Experiment Suite")
|
| 68 |
|
| 69 |
with gr.Tabs():
|
| 70 |
with gr.TabItem("🔬 Manual Single Run"):
|
| 71 |
+
# ... (UI unverändert)
|
| 72 |
gr.Markdown("Run a single experiment with manual parameters to explore hypotheses.")
|
| 73 |
with gr.Row(variant='panel'):
|
| 74 |
with gr.Column(scale=1):
|
|
|
|
| 87 |
manual_plot = gr.LinePlot(x="Internal Step", y="State Change (Delta)", title="Internal State Dynamics", show_label=True, height=400, interactive=True)
|
| 88 |
with gr.Accordion("Raw JSON Output", open=False):
|
| 89 |
manual_raw_json = gr.JSON()
|
|
|
|
| 90 |
manual_run_btn.click(
|
| 91 |
fn=run_single_analysis_display,
|
| 92 |
inputs=[manual_model_id, manual_prompt_type, manual_seed, manual_num_steps, manual_concept, manual_strength],
|
|
|
|
| 94 |
)
|
| 95 |
|
| 96 |
with gr.TabItem("🚀 Automated Suite"):
|
| 97 |
+
# ... (UI unverändert)
|
| 98 |
gr.Markdown("Run a predefined, curated suite of experiments and visualize the results comparatively.")
|
| 99 |
with gr.Row(variant='panel'):
|
| 100 |
with gr.Column(scale=1):
|
| 101 |
gr.Markdown("### Auto-Experiment Parameters")
|
| 102 |
+
auto_model_id = gr.Textbox(value="google/gemma-3-4b-it", label="Model ID")
|
| 103 |
auto_num_steps = gr.Slider(50, 1000, 300, step=10, label="Steps per Run")
|
| 104 |
auto_seed = gr.Slider(1, 1000, 42, step=1, label="Seed")
|
| 105 |
+
auto_experiment_name = gr.Dropdown(choices=list(get_curated_experiments().keys()), value="Therapeutic Intervention (4B-Model)", label="Curated Experiment Protocol")
|
| 106 |
auto_run_btn = gr.Button("Run Curated Auto-Experiment", variant="primary")
|
| 107 |
with gr.Column(scale=2):
|
| 108 |
gr.Markdown("### Suite Results Summary")
|
|
|
|
| 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()
|
|
|
|
| 113 |
auto_run_btn.click(
|
| 114 |
fn=run_auto_suite_display,
|
| 115 |
inputs=[auto_model_id, auto_num_steps, auto_seed, auto_experiment_name],
|