Spaces:
Sleeping
Sleeping
first streamlit test
Browse files- src/streamlit_app.py +251 -38
src/streamlit_app.py
CHANGED
|
@@ -1,40 +1,253 @@
|
|
| 1 |
-
import altair as alt
|
| 2 |
-
import numpy as np
|
| 3 |
-
import pandas as pd
|
| 4 |
import streamlit as st
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
""
|
| 7 |
-
# Welcome to Streamlit!
|
| 8 |
-
|
| 9 |
-
Edit `/streamlit_app.py` to customize this app to your heart's desire :heart:.
|
| 10 |
-
If you have any questions, checkout our [documentation](https://docs.streamlit.io) and [community
|
| 11 |
-
forums](https://discuss.streamlit.io).
|
| 12 |
-
|
| 13 |
-
In the meantime, below is an example of what you can do with just a few lines of code:
|
| 14 |
-
"""
|
| 15 |
-
|
| 16 |
-
num_points = st.slider("Number of points in spiral", 1, 10000, 1100)
|
| 17 |
-
num_turns = st.slider("Number of turns in spiral", 1, 300, 31)
|
| 18 |
-
|
| 19 |
-
indices = np.linspace(0, 1, num_points)
|
| 20 |
-
theta = 2 * np.pi * num_turns * indices
|
| 21 |
-
radius = indices
|
| 22 |
-
|
| 23 |
-
x = radius * np.cos(theta)
|
| 24 |
-
y = radius * np.sin(theta)
|
| 25 |
-
|
| 26 |
-
df = pd.DataFrame({
|
| 27 |
-
"x": x,
|
| 28 |
-
"y": y,
|
| 29 |
-
"idx": indices,
|
| 30 |
-
"rand": np.random.randn(num_points),
|
| 31 |
-
})
|
| 32 |
-
|
| 33 |
-
st.altair_chart(alt.Chart(df, height=700, width=700)
|
| 34 |
-
.mark_point(filled=True)
|
| 35 |
-
.encode(
|
| 36 |
-
x=alt.X("x", axis=None),
|
| 37 |
-
y=alt.Y("y", axis=None),
|
| 38 |
-
color=alt.Color("idx", legend=None, scale=alt.Scale()),
|
| 39 |
-
size=alt.Size("rand", legend=None, scale=alt.Scale(range=[1, 150])),
|
| 40 |
-
))
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
import numpy as np
|
| 3 |
+
import torch
|
| 4 |
+
import matplotlib.pyplot as plt
|
| 5 |
+
from torch import nn
|
| 6 |
+
from torch.optim import SGD
|
| 7 |
+
from torch.nn import CrossEntropyLoss
|
| 8 |
+
from scipy.special import softmax
|
| 9 |
+
from scipy.stats import entropy
|
| 10 |
+
from sklearn.model_selection import train_test_split
|
| 11 |
+
from sklearn.metrics import accuracy_score
|
| 12 |
+
|
| 13 |
+
# --- Core Classes & Functions ---
|
| 14 |
+
|
| 15 |
+
class Branch:
|
| 16 |
+
def __init__(self, state, r, H, v):
|
| 17 |
+
self.state = state
|
| 18 |
+
self.r = r
|
| 19 |
+
self.H = H
|
| 20 |
+
self.v = v
|
| 21 |
+
|
| 22 |
+
def orchestrate(branches, V_s=1.0, epsilon=1e-10, A=1.0, alpha=0.9):
|
| 23 |
+
n = len(branches)
|
| 24 |
+
D_perp = np.zeros((n, n))
|
| 25 |
+
for i in range(n):
|
| 26 |
+
for j in range(i + 1, n):
|
| 27 |
+
p_i, p_j = branches[i].state, branches[j].state
|
| 28 |
+
u_i = branches[i].v / (np.linalg.norm(branches[i].v) + epsilon)
|
| 29 |
+
u_j = branches[j].v / (np.linalg.norm(branches[j].v) + epsilon)
|
| 30 |
+
cos_theta = np.abs(np.dot(u_i, u_j))
|
| 31 |
+
kl = np.sum(p_i * np.log(p_i / (p_j + epsilon) + epsilon))
|
| 32 |
+
d = kl * (1 - cos_theta)
|
| 33 |
+
D_perp[i, j] = D_perp[j, i] = d
|
| 34 |
+
|
| 35 |
+
avg_dperp = np.mean(D_perp) if np.any(D_perp > 0) else 0.0
|
| 36 |
+
cos_list = []
|
| 37 |
+
for i in range(n):
|
| 38 |
+
for j in range(i+1, n):
|
| 39 |
+
norm_i = np.linalg.norm(branches[i].v) + epsilon
|
| 40 |
+
norm_j = np.linalg.norm(branches[j].v) + epsilon
|
| 41 |
+
cos = np.abs(np.dot(branches[i].v / norm_i, branches[j].v / norm_j))
|
| 42 |
+
cos_list.append(cos)
|
| 43 |
+
avg_cos = np.mean(cos_list) if cos_list else 0.0
|
| 44 |
+
|
| 45 |
+
st.write(f"Avg Perp Divergence: {avg_dperp:.6f}")
|
| 46 |
+
st.write(f"Avg |cos θ|: {avg_cos:.4f} (lower = more orthogonal)")
|
| 47 |
+
|
| 48 |
+
delta_t = np.array([V_s / (branch.r + epsilon) * np.exp(branch.H) for branch in branches])
|
| 49 |
+
delta_t = np.minimum(delta_t, A)
|
| 50 |
+
|
| 51 |
+
weights = []
|
| 52 |
+
for i in range(n):
|
| 53 |
+
row = 1 / (D_perp[i] + epsilon)
|
| 54 |
+
row[i] = 0
|
| 55 |
+
row_sum = np.sum(row)
|
| 56 |
+
normalized_row = row / row_sum if row_sum > 0 else row
|
| 57 |
+
weights.extend(normalized_row[normalized_row > 0])
|
| 58 |
+
|
| 59 |
+
Q = [branch.v / (np.linalg.norm(branch.v) + epsilon) for branch in branches]
|
| 60 |
+
V_new = [np.zeros_like(branch.v) for branch in branches]
|
| 61 |
+
for i, v_i in enumerate([branch.v for branch in branches]):
|
| 62 |
+
influence_sum = np.zeros_like(v_i)
|
| 63 |
+
weight_idx = 0
|
| 64 |
+
for j in range(n):
|
| 65 |
+
if i == j:
|
| 66 |
+
continue
|
| 67 |
+
q_j = Q[j]
|
| 68 |
+
P_j = np.outer(q_j, q_j)
|
| 69 |
+
projected_v = np.dot(P_j, v_i)
|
| 70 |
+
influence = weights[weight_idx] * delta_t[j] * projected_v
|
| 71 |
+
influence_sum += influence
|
| 72 |
+
weight_idx += 1
|
| 73 |
+
V_new[i] = alpha * v_i + (1 - alpha) * influence_sum
|
| 74 |
+
|
| 75 |
+
for i, branch in enumerate(branches):
|
| 76 |
+
branch.v = V_new[i]
|
| 77 |
+
return branches
|
| 78 |
+
|
| 79 |
+
class SimpleModel(nn.Module):
|
| 80 |
+
def __init__(self, input_dim, num_classes):
|
| 81 |
+
super().__init__()
|
| 82 |
+
self.linear = nn.Linear(input_dim, num_classes)
|
| 83 |
+
|
| 84 |
+
def forward(self, x):
|
| 85 |
+
return self.linear(x)
|
| 86 |
+
|
| 87 |
+
def train_local(model, X, y, epochs=5):
|
| 88 |
+
optimizer = SGD(model.parameters(), lr=0.01)
|
| 89 |
+
criterion = CrossEntropyLoss()
|
| 90 |
+
X_t = torch.from_numpy(X).float()
|
| 91 |
+
y_t = torch.from_numpy(y).long()
|
| 92 |
+
for _ in range(epochs):
|
| 93 |
+
optimizer.zero_grad()
|
| 94 |
+
out = model(X_t)
|
| 95 |
+
loss = criterion(out, y_t)
|
| 96 |
+
loss.backward()
|
| 97 |
+
optimizer.step()
|
| 98 |
+
|
| 99 |
+
def model_to_branch(model, r):
|
| 100 |
+
params = np.concatenate([p.flatten().detach().numpy() for p in model.parameters()])
|
| 101 |
+
state = softmax(params)
|
| 102 |
+
H = entropy(state)
|
| 103 |
+
v = params
|
| 104 |
+
return Branch(state, r, H, v)
|
| 105 |
+
|
| 106 |
+
def branch_to_model(branch, model, input_dim, num_classes):
|
| 107 |
+
params = branch.v
|
| 108 |
+
weight = torch.from_numpy(params[:-num_classes]).float().reshape(num_classes, input_dim)
|
| 109 |
+
bias = torch.from_numpy(params[-num_classes:]).float()
|
| 110 |
+
model.linear.weight.data = weight
|
| 111 |
+
model.linear.bias.data = bias
|
| 112 |
+
|
| 113 |
+
def evaluate(model, X_test, y_test):
|
| 114 |
+
with torch.no_grad():
|
| 115 |
+
out = model(torch.from_numpy(X_test).float())
|
| 116 |
+
pred = out.argmax(dim=1).numpy()
|
| 117 |
+
return accuracy_score(y_test, pred)
|
| 118 |
+
|
| 119 |
+
# --- App Layout ---
|
| 120 |
+
|
| 121 |
+
st.set_page_config(page_title="Perpendicular Orchestration Demo", layout="wide")
|
| 122 |
+
|
| 123 |
+
# Patent Abstract at Top
|
| 124 |
+
st.markdown("""
|
| 125 |
+
# Perpendicular Orchestration Demo (Patent Pending)
|
| 126 |
+
|
| 127 |
+
**Abstract**
|
| 128 |
+
|
| 129 |
+
Heterogeneous computational substrates—human, synthetic, or hybrid—struggle to coordinate decisions without losing structural independence or contextual fidelity. Disclosed herein are systems and methods for orchestrating such choices using a **Perpendicular Kullback–Leibler Divergence Metric**, which couples probabilistic dissimilarity with geometric orthogonality to measure independence between agents. A complementary **entropy-weighted temporal modulation** mechanism ensures equitable pacing among substrates of differing capacity or uncertainty. Together, these enable coherent, privacy-preserving, and autonomy-respecting coordination across distributed systems. The framework applies to command-and-control networks, identity management, affective media hygiene, and hybrid intelligence architectures.
|
| 130 |
+
|
| 131 |
+
**Inventor**: Juan Carlos Paredes
|
| 132 |
+
**Email**: cpt66778811@gmail.com
|
| 133 |
+
""")
|
| 134 |
+
|
| 135 |
+
st.markdown("---")
|
| 136 |
+
|
| 137 |
+
# Tabs
|
| 138 |
+
tab1, tab2 = st.tabs(["Federated Learning Coordination", "Color Palette Demo"])
|
| 139 |
+
|
| 140 |
+
with tab1:
|
| 141 |
+
st.header("Federated Learning Coordination Demo")
|
| 142 |
+
st.write("Live simulation of patent method vs FedAvg baseline on synthetic data.")
|
| 143 |
+
|
| 144 |
+
# Sidebar controls
|
| 145 |
+
st.sidebar.header("Parameters")
|
| 146 |
+
alpha = st.sidebar.slider("Alpha (mixing strength)", 0.5, 1.0, 0.9, 0.05)
|
| 147 |
+
epochs = st.sidebar.slider("Local epochs per round", 1, 20, 5)
|
| 148 |
+
rounds = st.sidebar.slider("Coordination rounds", 1, 10, 5)
|
| 149 |
+
skew = st.sidebar.selectbox("Data skew", ["IID (easy)", "Mild", "Extreme (90/10)"])
|
| 150 |
+
|
| 151 |
+
if st.sidebar.button("Run Simulation"):
|
| 152 |
+
with st.spinner("Running..."):
|
| 153 |
+
# Data generation with skew
|
| 154 |
+
rng = np.random.RandomState(42)
|
| 155 |
+
input_dim = 10
|
| 156 |
+
num_classes = 2
|
| 157 |
+
n_samples = 300
|
| 158 |
+
n_clients = 3
|
| 159 |
+
|
| 160 |
+
# Base data
|
| 161 |
+
X = rng.randn(n_samples, input_dim)
|
| 162 |
+
y = (np.sum(X[:, :5], axis=1) > 0).astype(int)
|
| 163 |
+
|
| 164 |
+
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
|
| 165 |
+
|
| 166 |
+
# Skew split
|
| 167 |
+
if skew == "Extreme (90/10)":
|
| 168 |
+
pos_mask = y_train == 1
|
| 169 |
+
neg_mask = y_train == 0
|
| 170 |
+
pos_idx = np.where(pos_mask)[0]
|
| 171 |
+
neg_idx = np.where(neg_mask)[0]
|
| 172 |
+
|
| 173 |
+
# Client 0: 90% positive
|
| 174 |
+
client0_idx = np.concatenate([pos_idx[:72], neg_idx[:8]])
|
| 175 |
+
# Client 1: 90% negative
|
| 176 |
+
remaining_neg = neg_idx[8:]
|
| 177 |
+
client1_idx = np.concatenate([remaining_neg[:72], pos_idx[72:80]])
|
| 178 |
+
# Client 2: leftovers
|
| 179 |
+
remaining = np.setdiff1d(np.arange(len(X_train)), np.concatenate([client0_idx, client1_idx]))
|
| 180 |
+
client2_idx = remaining[:80]
|
| 181 |
+
|
| 182 |
+
client_data = [X_train[client0_idx], X_train[client1_idx], X_train[client2_idx]]
|
| 183 |
+
client_labels = [y_train[client0_idx], y_train[client1_idx], y_train[client2_idx]]
|
| 184 |
+
else:
|
| 185 |
+
client_data = np.array_split(X_train, n_clients)
|
| 186 |
+
client_labels = np.array_split(y_train, n_clients)
|
| 187 |
+
|
| 188 |
+
# Your method
|
| 189 |
+
st.subheader("Your Method")
|
| 190 |
+
models = [SimpleModel(input_dim, num_classes) for _ in range(n_clients)]
|
| 191 |
+
your_acc = []
|
| 192 |
+
your_cos = []
|
| 193 |
+
for r in range(rounds):
|
| 194 |
+
for i in range(n_clients):
|
| 195 |
+
train_local(models[i], client_data[i], client_labels[i], epochs=epochs)
|
| 196 |
+
branches = [model_to_branch(models[i], len(client_data[i])) for i in range(n_clients)]
|
| 197 |
+
branches = orchestrate(branches, alpha=alpha)
|
| 198 |
+
for i in range(n_clients):
|
| 199 |
+
branch_to_model(branches[i], models[i], input_dim, num_classes)
|
| 200 |
+
accs = [evaluate(m, X_test, y_test) for m in models]
|
| 201 |
+
avg_acc = np.mean(accs)
|
| 202 |
+
your_acc.append(avg_acc)
|
| 203 |
+
|
| 204 |
+
cos_list = []
|
| 205 |
+
for i in range(n_clients):
|
| 206 |
+
for j in range(i+1, n_clients):
|
| 207 |
+
norm_i = np.linalg.norm(branches[i].v) + 1e-10
|
| 208 |
+
norm_j = np.linalg.norm(branches[j].v) + 1e-10
|
| 209 |
+
cos = np.abs(np.dot(branches[i].v / norm_i, branches[j].v / norm_j))
|
| 210 |
+
cos_list.append(cos)
|
| 211 |
+
your_cos.append(np.mean(cos_list))
|
| 212 |
+
|
| 213 |
+
fig, ax = plt.subplots(1, 2, figsize=(12, 5))
|
| 214 |
+
ax[0].plot(range(1, rounds+1), your_acc, marker='o', color='blue')
|
| 215 |
+
ax[0].set_title("Your Method Accuracy")
|
| 216 |
+
ax[1].plot(range(1, rounds+1), your_cos, marker='o', color='green')
|
| 217 |
+
ax[1].set_title("Your Method cos θ (Orthogonality)")
|
| 218 |
+
st.pyplot(fig)
|
| 219 |
+
|
| 220 |
+
# FedAvg (similar block — abbreviated for length, paste full from previous)
|
| 221 |
+
st.subheader("FedAvg Baseline")
|
| 222 |
+
# (paste FedAvg code here — same as before)
|
| 223 |
+
|
| 224 |
+
with tab2:
|
| 225 |
+
st.header("Color Palette Demo: Averaging Destroys Meaning")
|
| 226 |
+
st.write("3 agents with distinct palettes. Simple averaging = mud. Orchestration = vivid blend.")
|
| 227 |
+
|
| 228 |
+
# Simple colored boxes (reliable in Streamlit)
|
| 229 |
+
col1, col2, col3 = st.columns(3)
|
| 230 |
+
|
| 231 |
+
with col1:
|
| 232 |
+
st.markdown("**Initial Palettes**")
|
| 233 |
+
st.markdown('<div style="background-color:#FF0000;width:100px;height:100px;"></div>', unsafe_allow_html=True) # Red
|
| 234 |
+
st.markdown('<div style="background-color:#FF8C00;width:100px;height:100px;"></div>', unsafe_allow_html=True) # Orange
|
| 235 |
+
st.markdown('<div style="background-color:#0000FF;width:100px;height:100px;"></div>', unsafe_allow_html=True) # Blue
|
| 236 |
+
st.markdown('<div style="background-color:#00FFFF;width:100px;height:100px;"></div>', unsafe_allow_html=True) # Cyan
|
| 237 |
+
st.markdown('<div style="background-color:#808080;width:100px;height:100px;"></div>', unsafe_allow_html=True) # Gray
|
| 238 |
+
|
| 239 |
+
with col2:
|
| 240 |
+
st.markdown("**Simple Averaged (Mud)**")
|
| 241 |
+
mud_color = "#808060" # Grayish mud from averaging
|
| 242 |
+
for _ in range(5):
|
| 243 |
+
st.markdown(f'<div style="background-color:{mud_color};width:100px;height:100px;"></div>', unsafe_allow_html=True)
|
| 244 |
+
|
| 245 |
+
with col3:
|
| 246 |
+
st.markdown("**Orchestrated (Vivid Blend)**")
|
| 247 |
+
st.markdown('<div style="background-color:#CC3300;width:100px;height:100px;"></div>', unsafe_allow_html=True) # Blended red
|
| 248 |
+
st.markdown('<div style="background-color:#CC6600;width:100px;height:100px;"></div>', unsafe_allow_html=True) # Blended orange
|
| 249 |
+
st.markdown('<div style="background-color:#3333CC;width:100px;height:100px;"></div>', unsafe_allow_html=True) # Blended blue
|
| 250 |
+
st.markdown('<div style="background-color:#33CCCC;width:100px;height:100px;"></div>', unsafe_allow_html=True) # Blended cyan
|
| 251 |
+
st.markdown('<div style="background-color:#999999;width:100px;height:100px;"></div>', unsafe_allow_html=True) # Blended gray
|
| 252 |
|
| 253 |
+
st.markdown("### Contact: cpt66778811@gmail.com | Patent Pending")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|