Update app.py
Browse files
app.py
CHANGED
|
@@ -1,99 +1,3 @@
|
|
| 1 |
-
import torch
|
| 2 |
-
import numpy as np
|
| 3 |
-
import gc
|
| 4 |
-
from typing import Dict, Any, Optional, List
|
| 5 |
-
|
| 6 |
-
from .llm_iface import get_or_load_model, LLM
|
| 7 |
-
from .resonance_seismograph import run_cogitation_loop, run_silent_cogitation_seismic
|
| 8 |
-
from .concepts import get_concept_vector
|
| 9 |
-
from .introspection import generate_introspective_report
|
| 10 |
-
from .signal_analysis import analyze_cognitive_signal, get_power_spectrum_for_plotting
|
| 11 |
-
from .utils import dbg
|
| 12 |
-
|
| 13 |
-
def run_seismic_analysis(
|
| 14 |
-
model_id: str,
|
| 15 |
-
prompt_type: str,
|
| 16 |
-
seed: int,
|
| 17 |
-
num_steps: int,
|
| 18 |
-
concept_to_inject: str,
|
| 19 |
-
injection_strength: float,
|
| 20 |
-
progress_callback,
|
| 21 |
-
llm_instance: Optional[LLM] = None,
|
| 22 |
-
injection_vector_cache: Optional[torch.Tensor] = None
|
| 23 |
-
) -> Dict[str, Any]:
|
| 24 |
-
"""
|
| 25 |
-
Orchestriert eine einzelne seismische Analyse und integriert nun standardmäßig
|
| 26 |
-
die fortgeschrittene Signal-Analyse.
|
| 27 |
-
"""
|
| 28 |
-
local_llm_instance = False
|
| 29 |
-
if llm_instance is None:
|
| 30 |
-
progress_callback(0.0, desc=f"Loading model '{model_id}'...")
|
| 31 |
-
llm = get_or_load_model(model_id, seed)
|
| 32 |
-
local_llm_instance = True
|
| 33 |
-
else:
|
| 34 |
-
llm = llm_instance
|
| 35 |
-
llm.set_all_seeds(seed)
|
| 36 |
-
|
| 37 |
-
injection_vector = None
|
| 38 |
-
if concept_to_inject and concept_to_inject.strip():
|
| 39 |
-
if injection_vector_cache is not None:
|
| 40 |
-
dbg(f"Using cached injection vector for '{concept_to_inject}'.")
|
| 41 |
-
injection_vector = injection_vector_cache
|
| 42 |
-
else:
|
| 43 |
-
progress_callback(0.2, desc=f"Vectorizing '{concept_to_inject}'...")
|
| 44 |
-
injection_vector = get_concept_vector(llm, concept_to_inject.strip())
|
| 45 |
-
|
| 46 |
-
progress_callback(0.3, desc=f"Recording dynamics for '{prompt_type}'...")
|
| 47 |
-
|
| 48 |
-
state_deltas = run_silent_cogitation_seismic(
|
| 49 |
-
llm=llm, prompt_type=prompt_type,
|
| 50 |
-
num_steps=num_steps, temperature=0.1,
|
| 51 |
-
injection_vector=injection_vector, injection_strength=injection_strength
|
| 52 |
-
)
|
| 53 |
-
|
| 54 |
-
progress_callback(0.9, desc="Analyzing...")
|
| 55 |
-
|
| 56 |
-
stats = {}
|
| 57 |
-
results = {}
|
| 58 |
-
verdict = "### ⚠️ Analysis Warning\nNo state changes recorded."
|
| 59 |
-
|
| 60 |
-
if state_deltas:
|
| 61 |
-
deltas_np = np.array(state_deltas)
|
| 62 |
-
stats = {
|
| 63 |
-
"mean_delta": float(np.mean(deltas_np)),
|
| 64 |
-
"std_delta": float(np.std(deltas_np)),
|
| 65 |
-
"max_delta": float(np.max(deltas_np)),
|
| 66 |
-
"min_delta": float(np.min(deltas_np)),
|
| 67 |
-
}
|
| 68 |
-
|
| 69 |
-
# FINALE KORREKTUR: Führe die Signal-Analyse hier standardmäßig durch
|
| 70 |
-
signal_metrics = analyze_cognitive_signal(deltas_np)
|
| 71 |
-
stats.update(signal_metrics)
|
| 72 |
-
|
| 73 |
-
freqs, power = get_power_spectrum_for_plotting(deltas_np)
|
| 74 |
-
|
| 75 |
-
verdict = f"### ✅ Seismic Analysis Complete\nRecorded {len(deltas_np)} steps for '{prompt_type}'."
|
| 76 |
-
if injection_vector is not None:
|
| 77 |
-
verdict += f"\nModulated with **'{concept_to_inject}'** at strength **{injection_strength:.2f}**."
|
| 78 |
-
|
| 79 |
-
results["power_spectrum"] = {"frequencies": freqs.tolist(), "power": power.tolist()}
|
| 80 |
-
|
| 81 |
-
results.update({ "verdict": verdict, "stats": stats, "state_deltas": state_deltas })
|
| 82 |
-
|
| 83 |
-
if local_llm_instance:
|
| 84 |
-
dbg(f"Releasing locally created model instance for '{model_id}'.")
|
| 85 |
-
del llm, injection_vector
|
| 86 |
-
gc.collect()
|
| 87 |
-
if torch.cuda.is_available(): torch.cuda.empty_cache()
|
| 88 |
-
|
| 89 |
-
return results
|
| 90 |
-
|
| 91 |
-
# Die anderen Orchestrator-Funktionen (run_triangulation_probe, run_causal_surgery_probe, run_act_titration_probe)
|
| 92 |
-
# bleiben unverändert, da sie ihre eigene, spezifische Analyse-Logik enthalten.```
|
| 93 |
-
[File Ends] `cognitive_mapping_probe/orchestrator_seismograph.py`
|
| 94 |
-
|
| 95 |
-
[File Begins] `app.py`
|
| 96 |
-
```python
|
| 97 |
import gradio as gr
|
| 98 |
import pandas as pd
|
| 99 |
import gc
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import pandas as pd
|
| 3 |
import gc
|