Russel-Morant commited on
Commit
9487ee9
Β·
verified Β·
1 Parent(s): dd38625

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +48 -16
app.py CHANGED
@@ -1,6 +1,6 @@
 
1
  import os
2
  import json
3
- import math
4
  import time
5
  import random
6
  import traceback
@@ -9,12 +9,21 @@ from collections import defaultdict
9
  from dataclasses import dataclass
10
 
11
  import numpy as np
12
- import matplotlib.pyplot as plt
13
  import gradio as gr
14
  import torch
15
  from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
16
  from sklearn.cluster import KMeans
17
 
 
 
 
 
 
 
 
 
 
 
18
  # ---------------------------
19
  # Config
20
  # ---------------------------
@@ -116,10 +125,14 @@ _HF_MODEL = None
116
  def load_flan(model_id: str = MODEL_ID_DEFAULT, device: str = DEVICE):
117
  global _HF_TOKENIZER, _HF_MODEL
118
  if _HF_MODEL is None:
119
- print(f"[info] Loading Flan model {model_id} on {device}...")
120
- _HF_TOKENIZER = AutoTokenizer.from_pretrained(model_id)
121
- _HF_MODEL = AutoModelForSeq2SeqLM.from_pretrained(model_id)
122
- _HF_MODEL.to(torch.device(device))
 
 
 
 
123
  return _HF_TOKENIZER, _HF_MODEL
124
 
125
  PROMPT_TEMPLATE = """INSTRUCTION: Given the profiles and situation, output compact JSON with keys: mu, k, A_lowrank, sigma.
@@ -153,6 +166,13 @@ def flan_generate_params(prompt: str, model_id: str = MODEL_ID_DEFAULT, device:
153
  outs = model.generate(**inputs, max_length=max_length, do_sample=False)
154
  text = tokenizer.decode(outs[0], skip_special_tokens=True)
155
  parsed = _parse_json_loose(text)
 
 
 
 
 
 
 
156
  return parsed
157
  except Exception as e:
158
  print('[warn] Flan generation failed:', e)
@@ -170,7 +190,8 @@ def _parse_json_loose(text: str) -> Dict[str,Any]:
170
  return json.loads(text[s:e+1])
171
  except Exception:
172
  pass
173
- raise ValueError('Could not parse model output as JSON')
 
174
 
175
 
176
  def _fallback_theta() -> Dict[str,Any]:
@@ -221,13 +242,15 @@ TEMPLATE_UTTERANCES = [
221
 
222
  def extract_modes_from_paths(paths: np.ndarray, n_modes: int = 3):
223
  n_samps, d, steps = paths.shape
 
 
224
  feats = np.concatenate([paths.mean(axis=2), paths[:,:, -1]], axis=1)
225
- kmeans = KMeans(n_clusters=min(n_modes, n_samps), random_state=0).fit(feats)
226
  modes = []
227
  labels = kmeans.labels_
228
  for mode in range(kmeans.n_clusters):
229
  idx = np.where(labels == mode)[0]
230
- weight = len(idx)/n_samps
231
  mean_path = paths[idx].mean(axis=0) if len(idx)>0 else np.zeros((d, steps))
232
  last_state = mean_path[:, -1]
233
  modes.append({'mode': mode, 'weight': float(weight), 'last_state': last_state.tolist()})
@@ -302,7 +325,7 @@ def get_initial_question(session: SessionState):
302
  return q
303
 
304
  # ---------------------------
305
- # Logging (local JSONL)
306
  # ---------------------------
307
  LOG_DIR = 'runs'
308
  os.makedirs(LOG_DIR, exist_ok=True)
@@ -313,8 +336,16 @@ def log_run(entry: Dict[str,Any]):
313
  try:
314
  with open(LOG_PATH, 'a', encoding='utf-8') as f:
315
  f.write(json.dumps(entry, ensure_ascii=False) + '\n')
316
- except Exception:
317
- print('Warning: failed to write log')
 
 
 
 
 
 
 
 
318
 
319
  # ---------------------------
320
  # Gradio UI callbacks
@@ -394,7 +425,7 @@ def plot_paths(paths: np.ndarray, labels: List[str] = None):
394
  ax.plot(t, mean[i], label=(labels[i] if labels else f'dim{i}'))
395
  ax.set_ylim(0,1.2)
396
  ax.legend(loc='upper right')
397
- ax.set_xlabel('Normalized time'), ax.set_ylabel('State')
398
  plt.tight_layout()
399
  return fig
400
 
@@ -402,7 +433,7 @@ def plot_paths(paths: np.ndarray, labels: List[str] = None):
402
  # Gradio app layout
403
  # ---------------------------
404
  with gr.Blocks(title='Flan-SDE Behavioral Profiler') as demo:
405
- gr.Markdown("# Flan-SDE Behavioral Profiler'/'Ask questions, build a profile, and simulate behavior using a Flan-T5 generated SDE.")
406
  with gr.Row():
407
  with gr.Column(scale=1):
408
  start_btn = gr.Button('Start Session')
@@ -434,7 +465,8 @@ with gr.Blocks(title='Flan-SDE Behavioral Profiler') as demo:
434
  modes = json.loads(next_q[6:])['modes']
435
  out = predict_from_session(SESSIONS[sid2], use_flan=use_flan, model_id=model_id, n_samps=int(n_samps))
436
  fig = plot_paths(out['paths'], labels=["Strategy", "Empathy", "Control", "Masking", "RiskAppetite"])
437
- except Exception:
 
438
  modes = []
439
  fig = None
440
  return sid2, profdict, '', 'Prediction complete', json.dumps(modes), fig
@@ -442,4 +474,4 @@ with gr.Blocks(title='Flan-SDE Behavioral Profiler') as demo:
442
  return sid2, profdict, next_q, '', None, None
443
 
444
  start_btn.click(on_start, outputs=[sid_out, profile_view, qbox, out_console])
445
- 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])
 
1
+
2
  import os
3
  import json
 
4
  import time
5
  import random
6
  import traceback
 
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
  # ---------------------------
 
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.
 
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)
 
190
  return json.loads(text[s:e+1])
191
  except Exception:
192
  pass
193
+ print('[warn] Failed to parse JSON, using fallback')
194
+ return _fallback_theta()
195
 
196
 
197
  def _fallback_theta() -> Dict[str,Any]:
 
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()})
 
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)
 
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
 
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
 
 
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():
438
  with gr.Column(scale=1):
439
  start_btn = gr.Button('Start Session')
 
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
  return sid2, profdict, '', 'Prediction complete', json.dumps(modes), fig
 
474
  return sid2, profdict, next_q, '', None, None
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], outputs=[sid_out, profile_view, qbox, out_console, modes_json, plot_out])