Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,387 +1,49 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
from dataclasses import dataclass
|
| 10 |
-
|
| 11 |
-
import numpy as np
|
| 12 |
-
import gradio as gr
|
| 13 |
-
import torch
|
| 14 |
-
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
| 15 |
-
from sklearn.cluster import KMeans
|
| 16 |
-
|
| 17 |
-
import matplotlib
|
| 18 |
-
matplotlib.use('Agg') # Use non-GUI backend to avoid GLX dependencies
|
| 19 |
-
import matplotlib.pyplot as plt
|
| 20 |
-
|
| 21 |
-
try:
|
| 22 |
-
from datasets import Dataset
|
| 23 |
-
HAS_DATASETS = True
|
| 24 |
-
except ImportError:
|
| 25 |
-
HAS_DATASETS = False
|
| 26 |
-
|
| 27 |
-
# ---------------------------
|
| 28 |
-
# Config
|
| 29 |
-
# ---------------------------
|
| 30 |
-
MODEL_ID_DEFAULT = os.environ.get('FLAN_MODEL', 'google/flan-t5-small')
|
| 31 |
-
DEVICE = 'cuda' if torch.cuda.is_available() else 'cpu'
|
| 32 |
-
SEED = int(os.environ.get('APP_SEED', 42))
|
| 33 |
-
np.random.seed(SEED); random.seed(SEED)
|
| 34 |
-
|
| 35 |
-
LABELS = [
|
| 36 |
-
"Attention", "Empathy", "Control", "Masking", "Interest", "Relevance", "Strategy",
|
| 37 |
-
"Fear", "Trust", "Validation", "DominanceThreshold", "Unpredictability", "RiskAppetite"
|
| 38 |
-
]
|
| 39 |
-
|
| 40 |
-
INITIAL_QUESTIONS = [
|
| 41 |
-
"What situations make you feel most alert or withdrawn?",
|
| 42 |
-
"How do you handle high-pressure challenges?",
|
| 43 |
-
"What drives you to take charge in group settings?",
|
| 44 |
-
"How do you respond to unexpected changes?"
|
| 45 |
-
]
|
| 46 |
-
|
| 47 |
-
FALLBACK_QUESTIONS = {
|
| 48 |
-
"Attention": "How does your focus shift in demanding situations?",
|
| 49 |
-
"Empathy": "What role does understanding others play in your decisions?",
|
| 50 |
-
"Control": "How do you maintain influence over a situation?",
|
| 51 |
-
"Masking": "When do you hide your true intentions?",
|
| 52 |
-
"Interest": "What keeps you engaged in a task?",
|
| 53 |
-
"Relevance": "How do you decide what matters most in a scenario?",
|
| 54 |
-
"Strategy": "How do you plan your approach to challenges?",
|
| 55 |
-
"Fear": "What makes you feel cautious or hesitant?",
|
| 56 |
-
"Trust": "When do you feel comfortable relying on others?",
|
| 57 |
-
"Validation": "What makes you feel recognized or valued?",
|
| 58 |
-
"DominanceThreshold": "How much control do you need to feel secure?",
|
| 59 |
-
"Unpredictability": "How do you handle unexpected twists?",
|
| 60 |
-
"RiskAppetite": "What drives you to take bold risks?"
|
| 61 |
-
}
|
| 62 |
-
|
| 63 |
-
# Simple regex patterns for scoring (cleaned with \b boundaries)
|
| 64 |
-
import re
|
| 65 |
-
_PATTERNS = {
|
| 66 |
-
"control": (re.compile(r"\b(manipulat|control|dominat|exploit|test|punish|revenge|hurt)\b", re.IGNORECASE),
|
| 67 |
-
{"Control": 0.12, "Strategy": 0.08, "Masking": 0.05}),
|
| 68 |
-
"empathy": (re.compile(r"\b(trust|like|care|feel|empath|compass)\b", re.IGNORECASE),
|
| 69 |
-
{"Empathy": 0.12, "Trust": 0.08}),
|
| 70 |
-
"bored": (re.compile(r"\b(bore|boring|meh|dont care|who cares|whatever)\b", re.IGNORECASE),
|
| 71 |
-
{"Interest": -0.15, "Relevance": 0.10}),
|
| 72 |
-
"happy": (re.compile(r"\b(happy|fun|enjoy|excite|pleasur|thrill)\b", re.IGNORECASE),
|
| 73 |
-
{"Validation": 0.08, "Interest": 0.08}),
|
| 74 |
-
"mask": (re.compile(r"\b(fake|act|mask|perform|play a role)\b", re.IGNORECASE),
|
| 75 |
-
{"Masking": 0.12}),
|
| 76 |
-
"fear": (re.compile(r"\b(fear|scared|nervous|anxious|afraid)\b", re.IGNORECASE),
|
| 77 |
-
{"Fear": 0.12, "Unpredictability": 0.05}),
|
| 78 |
-
"think": (re.compile(r"\b(think|analyse|analyz|calculate|model|simulate|reason)\b", re.IGNORECASE),
|
| 79 |
-
{"Strategy": 0.12, "Attention": 0.05}),
|
| 80 |
-
"risk_up": (re.compile(r"\b(risk|gamble|bet|danger|reckless|thrill|adventur|dare)\b", re.IGNORECASE),
|
| 81 |
-
{"RiskAppetite": 0.12}),
|
| 82 |
-
"risk_down": (re.compile(r"\b(cautious|safe|avoid|conservativ|hesitant|secure|careful)\b", re.IGNORECASE),
|
| 83 |
-
{"RiskAppetite": -0.12}),
|
| 84 |
-
}
|
| 85 |
-
|
| 86 |
-
# ---------------------------
|
| 87 |
-
# Session state
|
| 88 |
-
# ---------------------------
|
| 89 |
-
@dataclass
|
| 90 |
-
class SessionState:
|
| 91 |
-
values: Dict[str, float]
|
| 92 |
-
round: int = 0
|
| 93 |
-
last_target: Optional[str] = None
|
| 94 |
-
last_q: Optional[str] = None
|
| 95 |
-
used_targets: set = None
|
| 96 |
-
used_questions: set = None
|
| 97 |
-
history: List = None
|
| 98 |
-
|
| 99 |
-
def __post_init__(self):
|
| 100 |
-
if self.used_targets is None:
|
| 101 |
-
self.used_targets = set()
|
| 102 |
-
if self.used_questions is None:
|
| 103 |
-
self.used_questions = set()
|
| 104 |
-
if self.history is None:
|
| 105 |
-
self.history = []
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
def init_state() -> SessionState:
|
| 109 |
-
base = {
|
| 110 |
-
"Attention": 0.5, "Empathy": 0.2, "Control": 0.8, "Masking": 0.6, "Interest": 0.5,
|
| 111 |
-
"Relevance": 0.5, "Strategy": 0.7, "Fear": 0.5, "Trust": 0.3, "Validation": 0.5,
|
| 112 |
-
"DominanceThreshold": 0.4, "Unpredictability": 0.3, "RiskAppetite": 0.5
|
| 113 |
-
}
|
| 114 |
-
return SessionState(values=defaultdict(lambda:0.5, base))
|
| 115 |
-
|
| 116 |
-
# Simple in-memory session registry (for Spaces this works; for multi-instance you need external store)
|
| 117 |
-
SESSIONS: Dict[str, SessionState] = {}
|
| 118 |
-
|
| 119 |
-
# ---------------------------
|
| 120 |
-
# HF Flan wrapper (lazy load)
|
| 121 |
-
# ---------------------------
|
| 122 |
-
_HF_TOKENIZER = None
|
| 123 |
-
_HF_MODEL = None
|
| 124 |
-
|
| 125 |
-
def load_flan(model_id: str = MODEL_ID_DEFAULT, device: str = DEVICE):
|
| 126 |
-
global _HF_TOKENIZER, _HF_MODEL
|
| 127 |
-
if _HF_MODEL is None:
|
| 128 |
-
try:
|
| 129 |
-
print(f"[info] Loading Flan model {model_id} on {device}...")
|
| 130 |
-
_HF_TOKENIZER = AutoTokenizer.from_pretrained(model_id)
|
| 131 |
-
_HF_MODEL = AutoModelForSeq2SeqLM.from_pretrained(model_id)
|
| 132 |
-
_HF_MODEL.to(torch.device(device))
|
| 133 |
-
except Exception as e:
|
| 134 |
-
print(f"[error] Failed to load model {model_id}: {e}")
|
| 135 |
-
raise
|
| 136 |
-
return _HF_TOKENIZER, _HF_MODEL
|
| 137 |
-
|
| 138 |
-
PROMPT_TEMPLATE = """INSTRUCTION: Given the profiles and situation, output compact JSON with keys: mu, k, A_lowrank, sigma.
|
| 139 |
-
Return arrays as lists only. Example: {{ "mu":[...], "k":[...], "A_lowrank":{{"U":[[...]], "V":[[...]]}}, "sigma":[...] }}
|
| 140 |
-
|
| 141 |
-
PROFILES:
|
| 142 |
-
{profiles_text}
|
| 143 |
-
|
| 144 |
-
SITUATION:
|
| 145 |
-
{scenario_text}
|
| 146 |
-
|
| 147 |
-
OUTPUT JSON:"""
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
def build_prompt_from_profile(session: SessionState, extra_situation: Optional[Dict[str,Any]]=None) -> str:
|
| 151 |
-
profiles_text = "- candidate: role=candidate, trust={:.3f}, dominance={:.3f}, vec=[{}]".format(
|
| 152 |
-
session.values['Trust'], session.values['DominanceThreshold'],
|
| 153 |
-
','.join(f"{session.values.get(k,0.5):.3f}" for k in ["Strategy","Empathy","Control","Masking","RiskAppetite"])
|
| 154 |
-
)
|
| 155 |
-
situation = {'scenario':'random_scene','stakes':0.5,'duration_mins':10,'unpredictability':session.values.get('Unpredictability',0.3)}
|
| 156 |
-
if extra_situation:
|
| 157 |
-
situation.update(extra_situation)
|
| 158 |
-
scenario_text = ', '.join([f"{k}={v}" for k,v in situation.items()])
|
| 159 |
-
return PROMPT_TEMPLATE.format(profiles_text=profiles_text, scenario_text=scenario_text)
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
def flan_generate_params(prompt: str, model_id: str = MODEL_ID_DEFAULT, device: str = DEVICE, max_length: int = 256) -> Dict[str,Any]:
|
| 163 |
try:
|
| 164 |
-
|
| 165 |
-
inputs = tokenizer(prompt, truncation=True, padding=True, return_tensors='pt', max_length=1024).to(model.device)
|
| 166 |
-
outs = model.generate(**inputs, max_length=max_length, do_sample=False)
|
| 167 |
-
text = tokenizer.decode(outs[0], skip_special_tokens=True)
|
| 168 |
-
parsed = _parse_json_loose(text)
|
| 169 |
-
# Validate shapes
|
| 170 |
-
if len(parsed.get('mu', [])) != 5 or len(parsed.get('k', [])) != 5 or len(parsed.get('sigma', [])) != 5:
|
| 171 |
-
raise ValueError("Invalid parameter shapes")
|
| 172 |
-
U = parsed['A_lowrank'].get('U', [])
|
| 173 |
-
V = parsed['A_lowrank'].get('V', [])
|
| 174 |
-
if len(U) != 5 or any(len(row) != len(V[0]) for row in U) or len(V) != 5:
|
| 175 |
-
raise ValueError("Invalid A_lowrank shapes")
|
| 176 |
-
return parsed
|
| 177 |
-
except Exception as e:
|
| 178 |
-
print('[warn] Flan generation failed:', e)
|
| 179 |
-
return _fallback_theta()
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
def _parse_json_loose(text: str) -> Dict[str,Any]:
|
| 183 |
-
try:
|
| 184 |
-
return json.loads(text)
|
| 185 |
except Exception:
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
V = [[0.0,0.0],[0.0,0.0],[0.0,0.0],[0.0,0.0],[0.0,-0.1]]
|
| 203 |
-
return {'mu':mu,'k':k,'A_lowrank':{'U':U,'V':V},'sigma':sigma}
|
| 204 |
-
|
| 205 |
-
# ---------------------------
|
| 206 |
-
# SDE simulator (EulerβMaruyama)
|
| 207 |
-
# ---------------------------
|
| 208 |
-
|
| 209 |
-
def _reconstruct_A(U: List[List[float]], V: List[List[float]]):
|
| 210 |
-
Ua = np.array(U); Va = np.array(V)
|
| 211 |
-
return Ua.dot(Va.T)
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
def simulate_sde(mu, k, A, sigma, X0, T=1.0, dt=0.002, n_samps=512, seed: int = SEED):
|
| 215 |
-
rng = np.random.default_rng(seed)
|
| 216 |
-
mu = np.array(mu); k = np.array(k); sigma = np.array(sigma); A = np.array(A)
|
| 217 |
-
d = len(X0); steps = int(T/dt)
|
| 218 |
-
X = np.tile(X0, (n_samps,1)).astype(float)
|
| 219 |
-
paths = np.zeros((n_samps,d,steps+1), dtype=float)
|
| 220 |
-
paths[:,:,0] = X
|
| 221 |
-
for i in range(steps):
|
| 222 |
-
drift_revert = - (X - mu) * k
|
| 223 |
-
drift_direct = X.dot(A.T)
|
| 224 |
-
drift = drift_revert + drift_direct
|
| 225 |
-
dW = rng.normal(scale=np.sqrt(dt), size=(n_samps,d))
|
| 226 |
-
diffusion = dW * sigma
|
| 227 |
-
X = X + drift * dt + diffusion
|
| 228 |
-
X = np.clip(X, 0.0, 1.5)
|
| 229 |
-
paths[:,:,i+1] = X
|
| 230 |
-
return paths
|
| 231 |
-
|
| 232 |
-
# ---------------------------
|
| 233 |
-
# Postprocess + decode
|
| 234 |
-
# ---------------------------
|
| 235 |
-
TEMPLATE_UTTERANCES = [
|
| 236 |
-
'(calm, structured) I appreciate the question. My approach is... [concise, no personal detail].',
|
| 237 |
-
'(guarded) I prefer to focus on the professional aspects.',
|
| 238 |
-
'(bold) I\'m willing to take that responsibility and here\'s why...',
|
| 239 |
-
'(hesitant) That\'s an interesting point β could you clarify?',
|
| 240 |
-
'(neutral) I tend to be methodical: step 1..., step 2...'
|
| 241 |
-
]
|
| 242 |
-
|
| 243 |
-
def extract_modes_from_paths(paths: np.ndarray, n_modes: int = 3):
|
| 244 |
-
n_samps, d, steps = paths.shape
|
| 245 |
-
if n_samps < n_modes:
|
| 246 |
-
n_modes = n_samps
|
| 247 |
-
feats = np.concatenate([paths.mean(axis=2), paths[:,:, -1]], axis=1)
|
| 248 |
-
kmeans = KMeans(n_clusters=n_modes, random_state=0).fit(feats)
|
| 249 |
-
modes = []
|
| 250 |
-
labels = kmeans.labels_
|
| 251 |
-
for mode in range(kmeans.n_clusters):
|
| 252 |
-
idx = np.where(labels == mode)[0]
|
| 253 |
-
weight = len(idx)/n_samps if n_samps > 0 else 0
|
| 254 |
-
mean_path = paths[idx].mean(axis=0) if len(idx)>0 else np.zeros((d, steps))
|
| 255 |
-
last_state = mean_path[:, -1]
|
| 256 |
-
modes.append({'mode': mode, 'weight': float(weight), 'last_state': last_state.tolist()})
|
| 257 |
-
modes = sorted(modes, key=lambda m: -m['weight'])
|
| 258 |
-
return modes
|
| 259 |
-
|
| 260 |
-
|
| 261 |
-
def decode_state(state_arr):
|
| 262 |
-
S, Emp, C, M, R = state_arr[:5]
|
| 263 |
-
if M > 0.8 and C > 0.8:
|
| 264 |
-
return TEMPLATE_UTTERANCES[0]
|
| 265 |
-
if M > 0.7 and Emp < 0.4:
|
| 266 |
-
return TEMPLATE_UTTERANCES[1]
|
| 267 |
-
if R > 0.6 and S > 0.8:
|
| 268 |
-
return TEMPLATE_UTTERANCES[2]
|
| 269 |
-
if Emp < 0.3 and R < 0.2:
|
| 270 |
-
return TEMPLATE_UTTERANCES[3]
|
| 271 |
-
return TEMPLATE_UTTERANCES[4]
|
| 272 |
-
|
| 273 |
-
# ---------------------------
|
| 274 |
-
# Simple profiler: scoring & question selection
|
| 275 |
-
# ---------------------------
|
| 276 |
-
|
| 277 |
-
def score_updates(answer: str):
|
| 278 |
-
updates = defaultdict(float)
|
| 279 |
-
ans = answer.lower()
|
| 280 |
-
for name, (pat, upd) in _PATTERNS.items():
|
| 281 |
-
if pat.search(ans):
|
| 282 |
-
for k,v in upd.items():
|
| 283 |
-
updates[k] += v
|
| 284 |
-
return updates
|
| 285 |
-
|
| 286 |
-
|
| 287 |
-
def apply_updates(session: SessionState, updates: Dict[str,float]):
|
| 288 |
-
for k,v in updates.items():
|
| 289 |
-
session.values[k] = min(1.0, max(0.0, session.values.get(k,0.5) + v))
|
| 290 |
-
|
| 291 |
-
|
| 292 |
-
def most_uncertain_dims(session: SessionState, k: int = 5):
|
| 293 |
-
uncertainties = [(label, abs(session.values[label]-0.5)) for label in LABELS]
|
| 294 |
-
return [label for label,_ in sorted(uncertainties, key=lambda x: x[1])[:k]]
|
| 295 |
-
|
| 296 |
-
|
| 297 |
-
def next_target(session: SessionState):
|
| 298 |
-
dims = most_uncertain_dims(session)
|
| 299 |
-
used = session.used_targets
|
| 300 |
-
available = [d for d in dims if d not in used]
|
| 301 |
-
if not available or len(used) >= len(LABELS)-1:
|
| 302 |
-
used.clear(); available = dims
|
| 303 |
-
last = session.last_target
|
| 304 |
-
if last and last in available and len(available)>1:
|
| 305 |
-
available.remove(last)
|
| 306 |
-
target = random.choice(available) if available else dims[0]
|
| 307 |
-
session.used_targets.add(target)
|
| 308 |
-
return target
|
| 309 |
-
|
| 310 |
-
|
| 311 |
-
def build_followup_prompt(prev_q: Optional[str], prev_a: Optional[str], target: str, session: SessionState):
|
| 312 |
-
top3 = most_uncertain_dims(session, k=3)
|
| 313 |
-
state_str = " | ".join(f"{d}:{session.values[d]:.2f}" for d in top3)
|
| 314 |
-
return (
|
| 315 |
-
f"Generate a short, unique question to probe {target.lower()}. Context: Previous question was '{prev_q or 'None'}'"
|
| 316 |
-
f" with answer '{prev_a or 'None'}'. Current state: {state_str}. Avoid repeating prior questions."
|
| 317 |
-
)
|
| 318 |
-
|
| 319 |
-
|
| 320 |
-
def get_initial_question(session: SessionState):
|
| 321 |
-
available = [q for q in INITIAL_QUESTIONS if q not in session.used_questions]
|
| 322 |
-
if not available:
|
| 323 |
-
session.used_questions.clear(); available = INITIAL_QUESTIONS
|
| 324 |
-
q = random.choice(available); session.used_questions.add(q)
|
| 325 |
-
return q
|
| 326 |
-
|
| 327 |
-
# ---------------------------
|
| 328 |
-
# Logging (local JSONL + optional HF Hub push)
|
| 329 |
-
# ---------------------------
|
| 330 |
-
LOG_DIR = 'runs'
|
| 331 |
-
os.makedirs(LOG_DIR, exist_ok=True)
|
| 332 |
-
LOG_PATH = os.path.join(LOG_DIR, 'logs.jsonl')
|
| 333 |
-
|
| 334 |
-
|
| 335 |
-
def log_run(entry: Dict[str,Any]):
|
| 336 |
-
try:
|
| 337 |
-
with open(LOG_PATH, 'a', encoding='utf-8') as f:
|
| 338 |
-
f.write(json.dumps(entry, ensure_ascii=False) + '\n')
|
| 339 |
-
# Optional: Push to HF Hub if configured
|
| 340 |
-
if HAS_DATASETS and 'HF_TOKEN' in os.environ and 'HF_DATASET_REPO' in os.environ:
|
| 341 |
-
try:
|
| 342 |
-
ds = Dataset.from_json(LOG_PATH)
|
| 343 |
-
ds.push_to_hub(os.environ['HF_DATASET_REPO'], token=os.environ['HF_TOKEN'])
|
| 344 |
-
print("[info] Pushed logs to HF Hub dataset")
|
| 345 |
-
except Exception as e:
|
| 346 |
-
print(f"[warn] Failed to push logs to HF Hub: {e}")
|
| 347 |
-
except Exception as e:
|
| 348 |
-
print(f'Warning: failed to write log: {e}')
|
| 349 |
-
|
| 350 |
-
# ---------------------------
|
| 351 |
-
# Gradio UI callbacks
|
| 352 |
-
# ---------------------------
|
| 353 |
-
|
| 354 |
-
def start_session() -> Tuple[str, Dict[str,float], str]:
|
| 355 |
-
sid = str(int(time.time()*1000))
|
| 356 |
-
SESSIONS[sid] = init_state()
|
| 357 |
-
q = get_initial_question(SESSIONS[sid])
|
| 358 |
-
# return session id, initial profile snapshot, first question
|
| 359 |
-
return sid, dict(SESSIONS[sid].values), q
|
| 360 |
-
|
| 361 |
-
|
| 362 |
-
def user_reply(session_id: str, user_input: str, use_flan: bool = True, model_id: str = MODEL_ID_DEFAULT, n_samps: int = 256):
|
| 363 |
-
# handle user reply, update profile, and produce next question
|
| 364 |
if session_id not in SESSIONS:
|
| 365 |
return 'ERROR: unknown session', {}, ''
|
| 366 |
session = SESSIONS[session_id]
|
| 367 |
cmd = user_input.strip().upper()
|
| 368 |
if cmd == 'RESET':
|
| 369 |
-
SESSIONS[session_id] = init_state()
|
|
|
|
|
|
|
| 370 |
return session_id, dict(SESSIONS[session_id].values), q
|
| 371 |
if cmd == 'STOP':
|
| 372 |
-
|
| 373 |
-
out = predict_from_session(session, use_flan=use_flan, model_id=model_id, n_samps=n_samps)
|
| 374 |
entry = {'timestamp': time.time(), 'session_id': session_id, 'profile_start': dict(session.values), 'situation': out['prompt'], 'theta': out['theta'], 'modes': out['modes']}
|
| 375 |
log_run(entry)
|
| 376 |
return session_id, dict(session.values), 'FINAL:' + json.dumps({'modes': out['modes']})
|
| 377 |
-
|
| 378 |
-
# normal flow: score, update, generate next question
|
| 379 |
updates = score_updates(user_input)
|
| 380 |
apply_updates(session, updates)
|
| 381 |
target = next_target(session)
|
| 382 |
session.last_target = target
|
| 383 |
prompt = build_followup_prompt(session.last_q, user_input, target, session)
|
| 384 |
-
# generate a question (lightweight) via Flan or fallback text
|
| 385 |
question = FALLBACK_QUESTIONS.get(target)
|
| 386 |
if use_flan:
|
| 387 |
try:
|
|
@@ -399,39 +61,81 @@ def user_reply(session_id: str, user_input: str, use_flan: bool = True, model_id
|
|
| 399 |
session.history.append({'user': user_input, 'bot': question})
|
| 400 |
return session_id, dict(session.values), question
|
| 401 |
|
| 402 |
-
|
| 403 |
-
|
| 404 |
-
|
|
|
|
|
|
|
|
|
|
| 405 |
if use_flan:
|
| 406 |
theta = flan_generate_params(prompt, model_id=model_id, device=DEVICE)
|
| 407 |
else:
|
| 408 |
theta = _fallback_theta()
|
| 409 |
-
mu = theta
|
| 410 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 411 |
X0 = np.array([session.values.get('Strategy',0.5), session.values.get('Empathy',0.5), session.values.get('Control',0.5), session.values.get('Masking',0.5), session.values.get('RiskAppetite',0.5)])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 412 |
paths = simulate_sde(mu, k, A, sigma, X0, n_samps=n_samps)
|
| 413 |
modes = extract_modes_from_paths(paths, n_modes=3)
|
| 414 |
for m in modes:
|
| 415 |
m['utterance'] = decode_state(np.array(m['last_state']))
|
| 416 |
return {'modes': modes, 'prompt': prompt, 'theta': theta, 'paths': paths}
|
| 417 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 418 |
|
| 419 |
-
|
| 420 |
-
|
| 421 |
-
|
| 422 |
-
fig, ax = plt.subplots(figsize=(8,4))
|
| 423 |
-
mean = paths.mean(axis=0)
|
| 424 |
-
for i in range(d):
|
| 425 |
-
ax.plot(t, mean[i], label=(labels[i] if labels else f'dim{i}'))
|
| 426 |
-
ax.set_ylim(0,1.2)
|
| 427 |
-
ax.legend(loc='upper right')
|
| 428 |
-
ax.set_xlabel('Normalized time'); ax.set_ylabel('State')
|
| 429 |
-
plt.tight_layout()
|
| 430 |
-
return fig
|
| 431 |
-
|
| 432 |
-
# ---------------------------
|
| 433 |
-
# Gradio app layout
|
| 434 |
-
# ---------------------------
|
| 435 |
with gr.Blocks(title='Flan-SDE Behavioral Profiler') as demo:
|
| 436 |
gr.Markdown("# Flan-SDE Behavioral Profiler. Ask questions, build a profile, and simulate behavior using a Flan-T5 generated SDE.")
|
| 437 |
with gr.Row():
|
|
@@ -445,6 +149,7 @@ with gr.Blocks(title='Flan-SDE Behavioral Profiler') as demo:
|
|
| 445 |
use_flan_chk = gr.Checkbox(value=True, label='Use real Flan-T5 (may be slow on CPU)')
|
| 446 |
model_in = gr.Textbox(value=MODEL_ID_DEFAULT, label='Flan model id')
|
| 447 |
n_samps_slider = gr.Slider(minimum=64, maximum=1024, value=256, step=64, label='Monte Carlo paths')
|
|
|
|
| 448 |
with gr.Column(scale=1):
|
| 449 |
out_console = gr.Textbox(label='Console', interactive=False)
|
| 450 |
modes_json = gr.JSON(label='Predicted Modes')
|
|
@@ -454,24 +159,42 @@ with gr.Blocks(title='Flan-SDE Behavioral Profiler') as demo:
|
|
| 454 |
sid, prof, q = start_session()
|
| 455 |
return sid, prof, q, ''
|
| 456 |
|
| 457 |
-
def on_send(sid, user_text, prof, q, use_flan, model_id, n_samps):
|
| 458 |
-
|
| 459 |
-
|
| 460 |
-
|
| 461 |
-
|
| 462 |
-
|
| 463 |
-
|
| 464 |
-
try:
|
| 465 |
-
modes = json.loads(next_q[6:])['modes']
|
| 466 |
-
out = predict_from_session(SESSIONS[sid2], use_flan=use_flan, model_id=model_id, n_samps=int(n_samps))
|
| 467 |
-
fig = plot_paths(out['paths'], labels=["Strategy", "Empathy", "Control", "Masking", "RiskAppetite"])
|
| 468 |
-
except Exception as e:
|
| 469 |
-
print(f"[error] Prediction failed: {e}")
|
| 470 |
modes = []
|
| 471 |
fig = None
|
| 472 |
-
|
| 473 |
-
|
| 474 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 475 |
|
| 476 |
start_btn.click(on_start, outputs=[sid_out, profile_view, qbox, out_console])
|
| 477 |
-
send_btn.click(on_send, inputs=[sid_out, user_in, profile_view, qbox, use_flan_chk, model_in, n_samps_slider
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# -----------------------------
|
| 2 |
+
# 1) Parse situation helper
|
| 3 |
+
# -----------------------------
|
| 4 |
+
def parse_situation_input(s: Optional[str]) -> Optional[Dict[str,Any]]:
|
| 5 |
+
if not s:
|
| 6 |
+
return None
|
| 7 |
+
s = s.strip()
|
| 8 |
+
# Try JSON first
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
try:
|
| 10 |
+
return json.loads(s)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
except Exception:
|
| 12 |
+
# fallback: parse simple key=value pairs separated by commas
|
| 13 |
+
out = {}
|
| 14 |
+
for part in re.split(r'[,\n]+', s):
|
| 15 |
+
if '=' in part:
|
| 16 |
+
k,v = part.split('=',1)
|
| 17 |
+
try:
|
| 18 |
+
out[k.strip()] = float(v.strip())
|
| 19 |
+
except Exception:
|
| 20 |
+
out[k.strip()] = v.strip()
|
| 21 |
+
return out if out else None
|
| 22 |
+
|
| 23 |
+
# -----------------------------
|
| 24 |
+
# 2) Make user_reply accept extra_situation
|
| 25 |
+
# -----------------------------
|
| 26 |
+
def user_reply(session_id: str, user_input: str, use_flan: bool = True, model_id: str = MODEL_ID_DEFAULT,
|
| 27 |
+
n_samps: int = 256, extra_situation: Optional[Dict[str,Any]] = None):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
if session_id not in SESSIONS:
|
| 29 |
return 'ERROR: unknown session', {}, ''
|
| 30 |
session = SESSIONS[session_id]
|
| 31 |
cmd = user_input.strip().upper()
|
| 32 |
if cmd == 'RESET':
|
| 33 |
+
SESSIONS[session_id] = init_state()
|
| 34 |
+
# regenerate an initial question from the new state
|
| 35 |
+
q = get_initial_question(SESSIONS[session_id])
|
| 36 |
return session_id, dict(SESSIONS[session_id].values), q
|
| 37 |
if cmd == 'STOP':
|
| 38 |
+
out = predict_from_session(session, use_flan=use_flan, model_id=model_id, n_samps=n_samps, extra_situation=extra_situation)
|
|
|
|
| 39 |
entry = {'timestamp': time.time(), 'session_id': session_id, 'profile_start': dict(session.values), 'situation': out['prompt'], 'theta': out['theta'], 'modes': out['modes']}
|
| 40 |
log_run(entry)
|
| 41 |
return session_id, dict(session.values), 'FINAL:' + json.dumps({'modes': out['modes']})
|
|
|
|
|
|
|
| 42 |
updates = score_updates(user_input)
|
| 43 |
apply_updates(session, updates)
|
| 44 |
target = next_target(session)
|
| 45 |
session.last_target = target
|
| 46 |
prompt = build_followup_prompt(session.last_q, user_input, target, session)
|
|
|
|
| 47 |
question = FALLBACK_QUESTIONS.get(target)
|
| 48 |
if use_flan:
|
| 49 |
try:
|
|
|
|
| 61 |
session.history.append({'user': user_input, 'bot': question})
|
| 62 |
return session_id, dict(session.values), question
|
| 63 |
|
| 64 |
+
# -----------------------------
|
| 65 |
+
# 3) Make predict_from_session accept extra_situation and be robust
|
| 66 |
+
# -----------------------------
|
| 67 |
+
def predict_from_session(session: SessionState, use_flan: bool = True, model_id: str = MODEL_ID_DEFAULT,
|
| 68 |
+
n_samps: int = 512, extra_situation: Optional[Dict[str,Any]] = None):
|
| 69 |
+
prompt = build_prompt_from_profile(session, extra_situation=extra_situation)
|
| 70 |
if use_flan:
|
| 71 |
theta = flan_generate_params(prompt, model_id=model_id, device=DEVICE)
|
| 72 |
else:
|
| 73 |
theta = _fallback_theta()
|
| 74 |
+
mu = theta.get('mu', [])
|
| 75 |
+
k = theta.get('k', [])
|
| 76 |
+
sigma = theta.get('sigma', [])
|
| 77 |
+
A_lowrank = theta.get('A_lowrank', {})
|
| 78 |
+
U = A_lowrank.get('U', [])
|
| 79 |
+
V = A_lowrank.get('V', [])
|
| 80 |
+
# Reconstruct A safely: if U/V invalid lengths, use zeros fallback
|
| 81 |
+
try:
|
| 82 |
+
A = _reconstruct_A(U, V) if (len(U)>0 and len(V)>0 and len(U)==len(mu) and len(V)==len(mu)) else np.zeros((len(mu), len(mu)))
|
| 83 |
+
except Exception:
|
| 84 |
+
A = np.zeros((len(mu), len(mu)))
|
| 85 |
+
# Ensure X0 dims align with expected dims => use 5 dims as before
|
| 86 |
X0 = np.array([session.values.get('Strategy',0.5), session.values.get('Empathy',0.5), session.values.get('Control',0.5), session.values.get('Masking',0.5), session.values.get('RiskAppetite',0.5)])
|
| 87 |
+
# If mu/k/sigma shorter than X0, pad; if longer, truncate
|
| 88 |
+
def pad_or_trunc(arr, target_len, fill=0.0):
|
| 89 |
+
if arr is None:
|
| 90 |
+
return np.array([fill]*target_len)
|
| 91 |
+
a = np.array(arr)
|
| 92 |
+
if len(a) < target_len:
|
| 93 |
+
return np.concatenate([a, np.full(target_len-len(a), fill)])
|
| 94 |
+
return a[:target_len]
|
| 95 |
+
mu = pad_or_trunc(mu, 5, fill=0.5)
|
| 96 |
+
k = pad_or_trunc(k, 5, fill=0.5)
|
| 97 |
+
sigma = pad_or_trunc(sigma, 5, fill=0.02)
|
| 98 |
paths = simulate_sde(mu, k, A, sigma, X0, n_samps=n_samps)
|
| 99 |
modes = extract_modes_from_paths(paths, n_modes=3)
|
| 100 |
for m in modes:
|
| 101 |
m['utterance'] = decode_state(np.array(m['last_state']))
|
| 102 |
return {'modes': modes, 'prompt': prompt, 'theta': theta, 'paths': paths}
|
| 103 |
|
| 104 |
+
# -----------------------------
|
| 105 |
+
# 4) Make flan_generate_params tolerant to shapes
|
| 106 |
+
# -----------------------------
|
| 107 |
+
def flan_generate_params(prompt: str, model_id: str = MODEL_ID_DEFAULT, device: str = DEVICE, max_length: int = 256) -> Dict[str,Any]:
|
| 108 |
+
try:
|
| 109 |
+
tokenizer, model = load_flan(model_id, device)
|
| 110 |
+
inputs = tokenizer(prompt, truncation=True, padding=True, return_tensors='pt', max_length=1024).to(model.device)
|
| 111 |
+
outs = model.generate(**inputs, max_length=max_length, do_sample=False)
|
| 112 |
+
text = tokenizer.decode(outs[0], skip_special_tokens=True)
|
| 113 |
+
parsed = _parse_json_loose(text)
|
| 114 |
+
# If parsed lacks expected keys or sizes, fallback
|
| 115 |
+
if not isinstance(parsed, dict):
|
| 116 |
+
raise ValueError("Parsed FLAN output not dict")
|
| 117 |
+
# Basic shape checks (tolerant)
|
| 118 |
+
mu = parsed.get('mu') or []
|
| 119 |
+
k = parsed.get('k') or []
|
| 120 |
+
sigma = parsed.get('sigma') or []
|
| 121 |
+
A_lowrank = parsed.get('A_lowrank') or {}
|
| 122 |
+
U = A_lowrank.get('U') or []
|
| 123 |
+
V = A_lowrank.get('V') or []
|
| 124 |
+
# If sizes are too different, use fallback
|
| 125 |
+
if not (isinstance(mu, list) and isinstance(k, list) and isinstance(sigma, list)):
|
| 126 |
+
raise ValueError("Invalid mu/k/sigma types")
|
| 127 |
+
# Prefer returning parsed but validate minimal sizes
|
| 128 |
+
if len(mu) < 3 or len(k) < 3 or len(sigma) < 3:
|
| 129 |
+
raise ValueError("Insufficient parameter lengths")
|
| 130 |
+
# if U/V shapes look wrong, still return parsed (we'll handle later)
|
| 131 |
+
return parsed
|
| 132 |
+
except Exception as e:
|
| 133 |
+
print('[warn] Flan generation failed or invalid shapes:', e)
|
| 134 |
+
return _fallback_theta()
|
| 135 |
|
| 136 |
+
# -----------------------------
|
| 137 |
+
# 5) UI: add Situation textbox and robust on_send with launch
|
| 138 |
+
# -----------------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 139 |
with gr.Blocks(title='Flan-SDE Behavioral Profiler') as demo:
|
| 140 |
gr.Markdown("# Flan-SDE Behavioral Profiler. Ask questions, build a profile, and simulate behavior using a Flan-T5 generated SDE.")
|
| 141 |
with gr.Row():
|
|
|
|
| 149 |
use_flan_chk = gr.Checkbox(value=True, label='Use real Flan-T5 (may be slow on CPU)')
|
| 150 |
model_in = gr.Textbox(value=MODEL_ID_DEFAULT, label='Flan model id')
|
| 151 |
n_samps_slider = gr.Slider(minimum=64, maximum=1024, value=256, step=64, label='Monte Carlo paths')
|
| 152 |
+
situation_in = gr.Textbox(label='Situation JSON (optional)', placeholder='{"scenario":"meeting","stakes":0.7,"duration_mins":15}', lines=3)
|
| 153 |
with gr.Column(scale=1):
|
| 154 |
out_console = gr.Textbox(label='Console', interactive=False)
|
| 155 |
modes_json = gr.JSON(label='Predicted Modes')
|
|
|
|
| 159 |
sid, prof, q = start_session()
|
| 160 |
return sid, prof, q, ''
|
| 161 |
|
| 162 |
+
def on_send(sid, user_text, prof, q, use_flan, model_id, n_samps, situation_raw):
|
| 163 |
+
try:
|
| 164 |
+
if not sid:
|
| 165 |
+
return gr.update(value=''), {}, '', 'ERROR: start a session first', {}, None
|
| 166 |
+
parsed_sit = parse_situation_input(situation_raw)
|
| 167 |
+
sid2, profdict, next_q = user_reply(sid, user_text, use_flan=use_flan, model_id=model_id, n_samps=int(n_samps), extra_situation=parsed_sit)
|
| 168 |
+
if isinstance(next_q, str) and next_q.startswith('FINAL:'):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 169 |
modes = []
|
| 170 |
fig = None
|
| 171 |
+
try:
|
| 172 |
+
modes = json.loads(next_q[6:])['modes']
|
| 173 |
+
out = predict_from_session(SESSIONS[sid2], use_flan=use_flan, model_id=model_id, n_samps=int(n_samps), extra_situation=parsed_sit)
|
| 174 |
+
fig = plot_paths(out['paths'], labels=["Strategy", "Empathy", "Control", "Masking", "RiskAppetite"])
|
| 175 |
+
modes = out['modes']
|
| 176 |
+
except Exception as e:
|
| 177 |
+
tb = traceback.format_exc()
|
| 178 |
+
print('[error] Prediction failed:', e, tb)
|
| 179 |
+
return sid2, profdict, '', f'Prediction failed: {e}\n{tb}', {}, None
|
| 180 |
+
return sid2, profdict, '', 'Prediction complete', modes, fig
|
| 181 |
+
else:
|
| 182 |
+
# normal question response
|
| 183 |
+
return sid2, profdict, next_q, '', {}, None
|
| 184 |
+
except Exception as e:
|
| 185 |
+
tb = traceback.format_exc()
|
| 186 |
+
print('[error] on_send crashed:', e, tb)
|
| 187 |
+
return sid, prof or {}, '', f'Exception: {e}\n{tb}', {}, None
|
| 188 |
|
| 189 |
start_btn.click(on_start, outputs=[sid_out, profile_view, qbox, out_console])
|
| 190 |
+
send_btn.click(on_send, inputs=[sid_out, user_in, profile_view, qbox, use_flan_chk, model_in, n_samps_slider, situation_in],
|
| 191 |
+
outputs=[sid_out, profile_view, qbox, out_console, modes_json, plot_out])
|
| 192 |
+
|
| 193 |
+
# -----------------------------
|
| 194 |
+
# 6) Launch the app (important)
|
| 195 |
+
# -----------------------------
|
| 196 |
+
if __name__ == "__main__":
|
| 197 |
+
# Use PORT from env if provided; default 7860
|
| 198 |
+
port = int(os.environ.get('PORT', 7860))
|
| 199 |
+
# host 0.0.0.0 is helpful in container environments
|
| 200 |
+
demo.launch(server_name='0.0.0.0', server_port=port, share=False)
|