Update app.py
Browse files
app.py
CHANGED
|
@@ -1,45 +1,32 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import torch
|
| 3 |
import numpy as np
|
| 4 |
-
from scipy
|
|
|
|
| 5 |
import matplotlib.pyplot as plt
|
| 6 |
from nilearn import plotting as nilearn_plot
|
| 7 |
from PIL import Image
|
| 8 |
import tempfile
|
| 9 |
-
import os
|
| 10 |
from pathlib import Path
|
| 11 |
|
| 12 |
from tribev2 import TribeModel
|
| 13 |
-
from scipy.sparse.linalg import eigsh
|
| 14 |
|
| 15 |
class HarmonicGovernor:
|
| 16 |
def __init__(self):
|
| 17 |
self.model = None
|
| 18 |
self.harmonics = None
|
| 19 |
-
self.eigenvalues = None
|
| 20 |
-
self.harmonics_path = Path("harmonics.npz")
|
| 21 |
|
| 22 |
def load_tribe(self):
|
| 23 |
if self.model is None:
|
|
|
|
| 24 |
self.model = TribeModel.from_pretrained("facebook/tribev2-mini")
|
| 25 |
return self.model
|
| 26 |
|
| 27 |
def load_harmonics(self):
|
| 28 |
if self.harmonics is not None:
|
| 29 |
return self.harmonics
|
| 30 |
-
|
| 31 |
-
if self.harmonics_path.exists():
|
| 32 |
-
try:
|
| 33 |
-
data = np.load(self.harmonics_path)
|
| 34 |
-
self.harmonics = data['harmonics']
|
| 35 |
-
self.eigenvalues = data.get('eigenvalues', None)
|
| 36 |
-
print(f"Loaded real harmonics: {self.harmonics.shape}")
|
| 37 |
-
return self.harmonics
|
| 38 |
-
except:
|
| 39 |
-
pass
|
| 40 |
-
|
| 41 |
-
# Placeholder if no file
|
| 42 |
-
print("Using placeholder harmonics...")
|
| 43 |
np.random.seed(42)
|
| 44 |
n = 2048
|
| 45 |
A = np.random.rand(n, n)
|
|
@@ -55,29 +42,20 @@ class HarmonicGovernor:
|
|
| 55 |
def compute_time_resolved_plv(self, signal, window=12, step=4):
|
| 56 |
plv_time = []
|
| 57 |
for i in range(0, len(signal) - window, step):
|
| 58 |
-
window_sig = signal[i:i+window]
|
| 59 |
analytic = hilbert(window_sig)
|
| 60 |
phases = np.angle(analytic)
|
| 61 |
plv = np.abs(np.mean(np.exp(1j * phases)))
|
| 62 |
plv_time.append(plv)
|
| 63 |
return np.array(plv_time)
|
| 64 |
|
| 65 |
-
def
|
| 66 |
-
ridges = []
|
| 67 |
-
for t in range(wavelet_power.shape[1]):
|
| 68 |
-
peaks, _ = find_peaks(wavelet_power[:, t], prominence=0.1)
|
| 69 |
-
for p in peaks:
|
| 70 |
-
ridges.append(t)
|
| 71 |
-
return np.unique(ridges) if ridges else np.array([])
|
| 72 |
-
|
| 73 |
-
def run_governor(self, media_file=None, text_input=None, media_type="auto"):
|
| 74 |
self.load_tribe()
|
| 75 |
self.load_harmonics()
|
| 76 |
|
| 77 |
-
|
| 78 |
-
input_desc = text_input[:100] + "..." if text_input else "Uploaded media"
|
| 79 |
|
| 80 |
-
# TRIBE v2 prediction (demo
|
| 81 |
try:
|
| 82 |
if text_input:
|
| 83 |
events_df = self.model.get_events_dataframe(text_path="temp.txt")
|
|
@@ -85,52 +63,54 @@ class HarmonicGovernor:
|
|
| 85 |
events_df = self.model.get_events_dataframe(text_path="temp.txt")
|
| 86 |
preds, _ = self.model.predict(events=events_df)
|
| 87 |
except:
|
| 88 |
-
preds = np.random.randn(30, 2048)
|
| 89 |
|
| 90 |
-
if len(preds) >
|
| 91 |
-
preds = preds[:
|
| 92 |
|
| 93 |
activity = preds.mean(axis=0)
|
| 94 |
coeffs = self.harmonics.T @ activity
|
| 95 |
low_harm = self.harmonics[:, :20]
|
| 96 |
reconstructed = low_harm @ coeffs[:20]
|
| 97 |
|
| 98 |
-
#
|
| 99 |
widths = np.arange(1, 31)
|
| 100 |
-
|
| 101 |
-
|
| 102 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 103 |
analytic = hilbert(reconstructed)
|
| 104 |
inst_phase = np.unwrap(np.angle(analytic))
|
| 105 |
phase_resets = np.where(np.abs(np.diff(inst_phase)) > 2.0)[0]
|
| 106 |
|
|
|
|
| 107 |
plv_time = self.compute_time_resolved_plv(reconstructed)
|
| 108 |
-
mean_plv = float(np.mean(plv_time)) if len(plv_time) > 0 else 0.
|
| 109 |
-
phase_consistency = float(np.abs(np.mean(np.exp(1j * inst_phase))))
|
| 110 |
|
| 111 |
-
|
| 112 |
-
gte_times = np.unique(np.concatenate([ridges, phase_resets]))
|
| 113 |
-
gte_count = len(gte_times)
|
| 114 |
|
| 115 |
-
#
|
| 116 |
-
resonance_score = min(0.
|
| 117 |
-
gte_omega = 3.44 + (mean_plv - 0.65) *
|
| 118 |
-
optimal_freq = 528.0 + (mean_plv - 0.7) *
|
| 119 |
|
| 120 |
-
# Brain maps
|
| 121 |
images = self._generate_maps(activity)
|
| 122 |
|
| 123 |
-
summary = f"Resonance Score: {resonance_score:.4f}\nGTE ω: {gte_omega:.2f}\nOptimal Frequency: {optimal_freq:.2f} Hz"
|
| 124 |
-
|
| 125 |
return {
|
| 126 |
"resonance_score": float(resonance_score),
|
| 127 |
"gte_omega": float(gte_omega),
|
| 128 |
"optimal_frequency": float(optimal_freq),
|
| 129 |
-
"mean_plv":
|
| 130 |
"gte_count": int(gte_count),
|
| 131 |
"status": "Active",
|
| 132 |
-
"summary":
|
| 133 |
-
"images": images
|
| 134 |
}
|
| 135 |
|
| 136 |
def _generate_maps(self, activity):
|
|
@@ -138,11 +118,13 @@ class HarmonicGovernor:
|
|
| 138 |
try:
|
| 139 |
for view in ["lateral", "medial"]:
|
| 140 |
fig = plt.figure(figsize=(8, 5))
|
| 141 |
-
nilearn_plot.plot_surf_stat_map(
|
| 142 |
-
|
|
|
|
|
|
|
| 143 |
with tempfile.NamedTemporaryFile(suffix=".png", delete=False) as tmp:
|
| 144 |
-
plt.savefig(tmp.name, dpi=
|
| 145 |
-
plt.close()
|
| 146 |
images.append(Image.open(tmp.name))
|
| 147 |
except:
|
| 148 |
placeholder = Image.new("RGB", (600, 400), color=(30, 30, 60))
|
|
@@ -150,31 +132,26 @@ class HarmonicGovernor:
|
|
| 150 |
return images
|
| 151 |
|
| 152 |
|
| 153 |
-
# ====================== GRADIO
|
| 154 |
governor = HarmonicGovernor()
|
| 155 |
|
| 156 |
-
def
|
| 157 |
-
result = governor.run_governor(media_file, text_input
|
| 158 |
-
|
| 159 |
-
# Return format friendly for your AI Studio app
|
| 160 |
return (
|
| 161 |
result["resonance_score"],
|
| 162 |
result["gte_omega"],
|
| 163 |
result["optimal_frequency"],
|
| 164 |
result["status"],
|
| 165 |
result["summary"],
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
f"
|
| 169 |
)
|
| 170 |
|
| 171 |
with gr.Blocks(title="TRIBE v2 Harmonic Governor") as demo:
|
| 172 |
gr.Markdown("# TRIBE v2 Harmonic Anchor Discovery")
|
| 173 |
|
| 174 |
-
|
| 175 |
-
media_input = gr.File(label="Upload Media")
|
| 176 |
-
text_input = gr.Textbox(label="Text Input", lines=3)
|
| 177 |
-
|
| 178 |
submit = gr.Button("Run Bayesian Governor", variant="primary")
|
| 179 |
|
| 180 |
resonance = gr.Number(label="Resonance Score")
|
|
@@ -182,13 +159,12 @@ with gr.Blocks(title="TRIBE v2 Harmonic Governor") as demo:
|
|
| 182 |
freq = gr.Number(label="Frequency (Hz)")
|
| 183 |
status = gr.Textbox(label="Status")
|
| 184 |
summary = gr.Textbox(label="Summary")
|
| 185 |
-
|
| 186 |
-
gallery = gr.Gallery(label="Additional Maps")
|
| 187 |
|
| 188 |
submit.click(
|
| 189 |
-
|
| 190 |
-
inputs=[
|
| 191 |
-
outputs=[resonance, gte_omega, freq, status, summary,
|
| 192 |
)
|
| 193 |
|
| 194 |
demo.launch(server_name="0.0.0.0", server_port=7860, share=True)
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import torch
|
| 3 |
import numpy as np
|
| 4 |
+
from scipy import signal
|
| 5 |
+
from scipy.signal import hilbert, find_peaks
|
| 6 |
import matplotlib.pyplot as plt
|
| 7 |
from nilearn import plotting as nilearn_plot
|
| 8 |
from PIL import Image
|
| 9 |
import tempfile
|
|
|
|
| 10 |
from pathlib import Path
|
| 11 |
|
| 12 |
from tribev2 import TribeModel
|
| 13 |
+
from scipy.sparse.linalg import eigsh
|
| 14 |
|
| 15 |
class HarmonicGovernor:
|
| 16 |
def __init__(self):
|
| 17 |
self.model = None
|
| 18 |
self.harmonics = None
|
|
|
|
|
|
|
| 19 |
|
| 20 |
def load_tribe(self):
|
| 21 |
if self.model is None:
|
| 22 |
+
print("Loading TRIBE v2-mini...")
|
| 23 |
self.model = TribeModel.from_pretrained("facebook/tribev2-mini")
|
| 24 |
return self.model
|
| 25 |
|
| 26 |
def load_harmonics(self):
|
| 27 |
if self.harmonics is not None:
|
| 28 |
return self.harmonics
|
| 29 |
+
print("Using placeholder harmonics (real HCP harmonics can be loaded later)")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
np.random.seed(42)
|
| 31 |
n = 2048
|
| 32 |
A = np.random.rand(n, n)
|
|
|
|
| 42 |
def compute_time_resolved_plv(self, signal, window=12, step=4):
|
| 43 |
plv_time = []
|
| 44 |
for i in range(0, len(signal) - window, step):
|
| 45 |
+
window_sig = signal[i:i + window]
|
| 46 |
analytic = hilbert(window_sig)
|
| 47 |
phases = np.angle(analytic)
|
| 48 |
plv = np.abs(np.mean(np.exp(1j * phases)))
|
| 49 |
plv_time.append(plv)
|
| 50 |
return np.array(plv_time)
|
| 51 |
|
| 52 |
+
def run_governor(self, media_file=None, text_input=None):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
self.load_tribe()
|
| 54 |
self.load_harmonics()
|
| 55 |
|
| 56 |
+
input_desc = text_input[:80] + "..." if text_input else "Uploaded media"
|
|
|
|
| 57 |
|
| 58 |
+
# TRIBE v2 prediction (demo mode)
|
| 59 |
try:
|
| 60 |
if text_input:
|
| 61 |
events_df = self.model.get_events_dataframe(text_path="temp.txt")
|
|
|
|
| 63 |
events_df = self.model.get_events_dataframe(text_path="temp.txt")
|
| 64 |
preds, _ = self.model.predict(events=events_df)
|
| 65 |
except:
|
| 66 |
+
preds = np.random.randn(30, 2048).astype(np.float32)
|
| 67 |
|
| 68 |
+
if len(preds) > 35:
|
| 69 |
+
preds = preds[:35]
|
| 70 |
|
| 71 |
activity = preds.mean(axis=0)
|
| 72 |
coeffs = self.harmonics.T @ activity
|
| 73 |
low_harm = self.harmonics[:, :20]
|
| 74 |
reconstructed = low_harm @ coeffs[:20]
|
| 75 |
|
| 76 |
+
# Wavelet (fixed import)
|
| 77 |
widths = np.arange(1, 31)
|
| 78 |
+
wavelet_transform = signal.cwt(reconstructed, signal.morlet2, widths)
|
| 79 |
+
wavelet_power = np.abs(wavelet_transform)**2
|
| 80 |
|
| 81 |
+
# Wavelet ridges (simple)
|
| 82 |
+
ridges = []
|
| 83 |
+
for t in range(wavelet_power.shape[1]):
|
| 84 |
+
peaks, _ = find_peaks(wavelet_power[:, t], prominence=0.1)
|
| 85 |
+
ridges.extend([t] * len(peaks))
|
| 86 |
+
ridge_gtes = np.unique(ridges)
|
| 87 |
+
|
| 88 |
+
# Phase resets
|
| 89 |
analytic = hilbert(reconstructed)
|
| 90 |
inst_phase = np.unwrap(np.angle(analytic))
|
| 91 |
phase_resets = np.where(np.abs(np.diff(inst_phase)) > 2.0)[0]
|
| 92 |
|
| 93 |
+
# Time-resolved PLV
|
| 94 |
plv_time = self.compute_time_resolved_plv(reconstructed)
|
| 95 |
+
mean_plv = float(np.mean(plv_time)) if len(plv_time) > 0 else 0.68
|
|
|
|
| 96 |
|
| 97 |
+
gte_count = len(np.unique(np.concatenate([ridge_gtes, phase_resets])))
|
|
|
|
|
|
|
| 98 |
|
| 99 |
+
# Output for your AI Studio UI
|
| 100 |
+
resonance_score = min(0.95, mean_plv * 0.9 + 0.3)
|
| 101 |
+
gte_omega = 3.44 + (mean_plv - 0.65) * 1.8
|
| 102 |
+
optimal_freq = 528.0 + (mean_plv - 0.7) * 90
|
| 103 |
|
|
|
|
| 104 |
images = self._generate_maps(activity)
|
| 105 |
|
|
|
|
|
|
|
| 106 |
return {
|
| 107 |
"resonance_score": float(resonance_score),
|
| 108 |
"gte_omega": float(gte_omega),
|
| 109 |
"optimal_frequency": float(optimal_freq),
|
| 110 |
+
"mean_plv": mean_plv,
|
| 111 |
"gte_count": int(gte_count),
|
| 112 |
"status": "Active",
|
| 113 |
+
"summary": f"Resonance: {resonance_score:.4f} | GTE ω: {gte_omega:.2f} | Freq: {optimal_freq:.1f} Hz"
|
|
|
|
| 114 |
}
|
| 115 |
|
| 116 |
def _generate_maps(self, activity):
|
|
|
|
| 118 |
try:
|
| 119 |
for view in ["lateral", "medial"]:
|
| 120 |
fig = plt.figure(figsize=(8, 5))
|
| 121 |
+
nilearn_plot.plot_surf_stat_map(
|
| 122 |
+
surf_mesh="fsaverage5", stat_map=activity, hemi="both",
|
| 123 |
+
view=view, cmap="hot", threshold=0.2
|
| 124 |
+
)
|
| 125 |
with tempfile.NamedTemporaryFile(suffix=".png", delete=False) as tmp:
|
| 126 |
+
plt.savefig(tmp.name, dpi=180)
|
| 127 |
+
plt.close(fig)
|
| 128 |
images.append(Image.open(tmp.name))
|
| 129 |
except:
|
| 130 |
placeholder = Image.new("RGB", (600, 400), color=(30, 30, 60))
|
|
|
|
| 132 |
return images
|
| 133 |
|
| 134 |
|
| 135 |
+
# ====================== GRADIO ======================
|
| 136 |
governor = HarmonicGovernor()
|
| 137 |
|
| 138 |
+
def analyze(media_file, text_input):
|
| 139 |
+
result = governor.run_governor(media_file, text_input)
|
|
|
|
|
|
|
| 140 |
return (
|
| 141 |
result["resonance_score"],
|
| 142 |
result["gte_omega"],
|
| 143 |
result["optimal_frequency"],
|
| 144 |
result["status"],
|
| 145 |
result["summary"],
|
| 146 |
+
None, # main image - can expand later
|
| 147 |
+
[], # gallery
|
| 148 |
+
f"GTEs: {result['gte_count']} | Mean PLV: {result['mean_plv']:.3f}"
|
| 149 |
)
|
| 150 |
|
| 151 |
with gr.Blocks(title="TRIBE v2 Harmonic Governor") as demo:
|
| 152 |
gr.Markdown("# TRIBE v2 Harmonic Anchor Discovery")
|
| 153 |
|
| 154 |
+
text_input = gr.Textbox(label="Text Input", lines=3, value="A person speaking clearly about neuroscience and brain rhythms")
|
|
|
|
|
|
|
|
|
|
| 155 |
submit = gr.Button("Run Bayesian Governor", variant="primary")
|
| 156 |
|
| 157 |
resonance = gr.Number(label="Resonance Score")
|
|
|
|
| 159 |
freq = gr.Number(label="Frequency (Hz)")
|
| 160 |
status = gr.Textbox(label="Status")
|
| 161 |
summary = gr.Textbox(label="Summary")
|
| 162 |
+
gte_info = gr.Textbox(label="GTE Info")
|
|
|
|
| 163 |
|
| 164 |
submit.click(
|
| 165 |
+
analyze,
|
| 166 |
+
inputs=[None, text_input],
|
| 167 |
+
outputs=[resonance, gte_omega, freq, status, summary, None, None, gte_info]
|
| 168 |
)
|
| 169 |
|
| 170 |
demo.launch(server_name="0.0.0.0", server_port=7860, share=True)
|