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