Russel-Morant commited on
Commit
8d1dac1
Β·
verified Β·
1 Parent(s): 190f654

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +244 -177
app.py CHANGED
@@ -1,213 +1,280 @@
 
 
 
1
  import json
2
  import random
3
- import uuid
4
  import traceback
5
 
6
  import gradio as gr
7
  import numpy as np
8
- import matplotlib.pyplot as plt
9
- from matplotlib import cm
10
  from transformers import pipeline, AutoTokenizer, AutoModelForSeq2SeqLM
 
11
 
12
- # --- Trait Definitions ---
13
- BASE_TRAITS = [
14
- "Guilt-Proneness", "Anxiety", "Aggression", "Callousness",
15
- "Depression", "Grandiosity", "Manipulativeness", "Narcissism",
16
- "Impulsivity", "Risk-Taking", "Responsibility", "Empathy",
17
- "Conscientiousness"
18
- ]
19
 
20
- # Optionally extend
21
- EXTENDED_TRAITS = BASE_TRAITS + ["Resilience", "Adaptability"]
22
-
23
- # --- Model Initialization & Caching ---
24
- _MODEL_CACHE = None
25
- def get_generator(model_name="google/flan-t5-small"):
26
- global _MODEL_CACHE
27
- if _MODEL_CACHE is None:
28
  tokenizer = AutoTokenizer.from_pretrained(model_name)
29
  model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
30
- _MODEL_CACHE = pipeline(
31
  "text2text-generation",
32
  model=model,
33
  tokenizer=tokenizer,
34
  device_map="auto" if torch.cuda.is_available() else None,
35
- max_length=64,
36
  )
37
- return _MODEL_CACHE
38
-
39
- # --- Session Storage ---
40
- sessions = {} # session_id -> { responses: [], times: [], means: np.array, sigmas: np.array }
41
-
42
- # --- SDE Simulation ---
43
- def simulate_sde(init_mean, init_sigma, steps=10):
44
- dt = 1.0 / steps
45
- means = [init_mean.copy()]
46
- sigmas = [init_sigma.copy()]
47
- for _ in range(steps):
48
- dW = np.random.normal(scale=np.sqrt(dt), size=init_mean.shape)
49
- means.append(means[-1] + sigmas[-1] * dW)
50
- sigmas.append(np.maximum(0.1, sigmas[-1] * (1 - dt) + 0.01))
51
- return np.array(means), np.array(sigmas)
52
-
53
- # --- Scoring Function ---
54
- def score_response(text, traits, disable_sde):
55
- generator = get_generator()
56
- prompt = (
57
- f"Rate the following traits on scale 0–1 based on: '{text}'.\n"
58
- + "\n".join(f"{i+1}. {t}" for i, t in enumerate(traits))
59
- + "\nProvide CSV of values only."
60
- )
61
- try:
62
- raw = generator(prompt, do_sample=False)[0]["generated_text"]
63
- values = [float(x) for x in raw.strip().split(",")]
64
- means = np.array(values)
65
- sigmas = np.full_like(means, 0.1)
66
- except Exception:
67
- # fallback uniform neutral profile
68
- means = np.full(len(traits), 0.5)
69
- sigmas = np.full(len(traits), 0.1)
70
-
71
- if disable_sde:
72
- # skip dynamics: flat trajectory
73
- times = np.arange(0, len(sessions[current_session]["times"]) + 1)
74
- dyn_means = np.vstack([means] * len(times))
75
- dyn_sigmas = np.vstack([sigmas] * len(times))
76
- else:
77
- dyn_means, dyn_sigmas = simulate_sde(means, sigmas, steps=10)
78
- return dyn_means, dyn_sigmas
79
-
80
- # --- Dynamic Question Generation ---
81
  def next_question(traits):
82
- generator = get_generator()
 
83
  trait = random.choice(traits)
84
- prompt = f"Generate a single open-ended question to probe the trait '{trait}'."
85
  try:
86
- q = generator(prompt, do_sample=True, temperature=0.7)[0]["generated_text"].strip()
87
  except Exception:
88
- q = "What matters most to you right now?"
89
  return q
90
 
91
- # --- Plotting Helpers ---
92
- def plot_trajectories(means, traits):
93
- fig, ax = plt.subplots(figsize=(6, 4))
94
- for idx, t in enumerate(traits):
95
- ax.plot(means[:, idx], label=t)
96
- ax.set_title("Trait Trajectories")
97
- ax.set_xlabel("Step")
98
- ax.set_ylabel("Intensity")
99
- ax.legend(bbox_to_anchor=(1.05, 1), loc='upper left')
100
- plt.tight_layout()
101
- return fig
102
-
103
- def plot_heatmap(sigmas, traits):
104
- fig, ax = plt.subplots(figsize=(6, 4))
105
- cax = ax.imshow(sigmas.T, aspect="auto", cmap=cm.viridis)
106
- ax.set_xticks(np.arange(sigmas.shape[0]))
107
- ax.set_yticks(np.arange(len(traits)))
108
- ax.set_yticklabels(traits)
109
- ax.set_title("Uncertainty Heatmap")
110
- fig.colorbar(cax)
111
- plt.tight_layout()
112
- return fig
113
-
114
- def plot_radar(means, traits):
115
- # use final means
116
- data = means[-1]
117
- angles = np.linspace(0, 2 * np.pi, len(traits), endpoint=False).tolist()
118
- data = np.concatenate((data, [data[0]]))
119
- angles += angles[:1]
120
-
121
- fig = plt.figure(figsize=(5,5))
122
- ax = fig.add_subplot(111, polar=True)
123
- ax.plot(angles, data, 'o-', linewidth=2)
124
- ax.fill(angles, data, alpha=0.25)
125
- ax.set_thetagrids(np.degrees(angles), traits + [traits[0]])
126
- ax.set_title("Final Profile Radar")
127
- return fig
128
-
129
- # --- Gradio Callbacks ---
130
- def submit_response(response_text, disable_sde_flag):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
131
  global current_session
132
- if current_session is None:
133
- current_session = str(uuid.uuid4())
134
- sessions[current_session] = {
135
- "responses": [], "times": [], "means": None, "sigmas": None
136
- }
137
- sess = sessions[current_session]
138
-
139
- # Score
140
- dyn_means, dyn_sigmas = score_response(response_text, EXTENDED_TRAITS, disable_sde_flag)
141
- sess["responses"].append(response_text)
142
- sess["times"].append(len(sess["responses"]))
143
- sess["means"] = dyn_means
144
- sess["sigmas"] = dyn_sigmas
145
-
146
- # Plots
147
- traj_fig = plot_trajectories(dyn_means, EXTENDED_TRAITS)
148
- heat_fig = plot_heatmap(dyn_sigmas, EXTENDED_TRAITS)
149
- radar_fig = plot_radar(dyn_means, EXTENDED_TRAITS)
150
-
151
- # Next question
152
- q = next_question(EXTENDED_TRAITS)
153
- return q, traj_fig, heat_fig, radar_fig
154
-
155
- def export_session():
156
- data = sessions.get(current_session, {})
157
- return json.dumps(data, indent=2)
158
-
159
- def import_session(json_str):
160
  global current_session
161
  try:
162
  data = json.loads(json_str)
163
  sid = str(uuid.uuid4())
164
  sessions[sid] = data
165
  current_session = sid
166
- return "Session loaded successfully!"
 
167
  except Exception:
168
- return "Failed to load session. Check your JSON."
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
169
 
170
- # --- Build UI ---
171
  with gr.Blocks() as demo:
172
- gr.Markdown("""
173
- # Behavioral Modulation Profiler
174
-
175
- This app models and visualizes personality trait dynamics based on your answers.
176
- Use open-ended responses to probe deep behavioral dimensions.
177
- You can export/import your session as JSON, toggle simulation on/off, and explore:
178
- - Trajectory plot of trait intensities
179
- - Uncertainty heatmap
180
- - Radar chart of final profile
181
- """)
 
 
 
 
 
 
 
 
 
 
182
  with gr.Row():
183
- with gr.Column(scale=3):
184
- question_display = gr.Textbox(value="Tell me something about yourself.", label="Question")
185
- response_box = gr.Textbox(label="Your Answer", placeholder="Type here...")
186
- disable_sde = gr.Checkbox(label="Disable dynamic simulation", value=False)
187
- submit_btn = gr.Button("Submit")
188
- with gr.Column(scale=2):
189
- export_btn = gr.Button("Export Session JSON")
190
- export_out = gr.Textbox(label="Session JSON", interactive=False)
191
- import_box = gr.Textbox(label="Paste Session JSON", placeholder="Paste here to load…")
192
- import_btn = gr.Button("Load Session")
193
- import_status = gr.Textbox(label="Status", interactive=False)
194
-
195
- with gr.Tabs():
196
- with gr.TabItem("Trajectories"):
197
- traj_plot = gr.Plot()
198
- with gr.TabItem("Uncertainty Heatmap"):
199
- heat_plot = gr.Plot()
200
- with gr.TabItem("Radar Chart"):
201
- radar_plot = gr.Plot()
202
-
203
- submit_btn.click(
204
- fn=submit_response,
205
- inputs=[response_box, disable_sde],
206
- outputs=[question_display, traj_plot, heat_plot, radar_plot],
207
- api_name="answer_and_plot"
 
 
 
 
208
  )
209
- export_btn.click(fn=export_session, inputs=None, outputs=export_out)
210
- import_btn.click(fn=import_session, inputs=import_box, outputs=import_status)
211
 
212
  if __name__ == "__main__":
213
- demo.launch()
 
1
+ # app.py
2
+
3
+ import uuid
4
  import json
5
  import random
 
6
  import traceback
7
 
8
  import gradio as gr
9
  import numpy as np
10
+ import torch
 
11
  from transformers import pipeline, AutoTokenizer, AutoModelForSeq2SeqLM
12
+ from sentence_transformers import SentenceTransformer, util
13
 
14
+ # -----------------------------------------------------------------------------
15
+ # 1. Model & Embedding Initialization
16
+ # -----------------------------------------------------------------------------
 
 
 
 
17
 
18
+ # LLM pipeline cache
19
+ LLM_CACHE = None
20
+ def get_llm(model_name="google/flan-t5-small"):
21
+ global LLM_CACHE
22
+ if LLM_CACHE is None:
 
 
 
23
  tokenizer = AutoTokenizer.from_pretrained(model_name)
24
  model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
25
+ LLM_CACHE = pipeline(
26
  "text2text-generation",
27
  model=model,
28
  tokenizer=tokenizer,
29
  device_map="auto" if torch.cuda.is_available() else None,
30
+ max_length=128,
31
  )
32
+ return LLM_CACHE
33
+
34
+ # Embedding model for memory & retrieval
35
+ EMB_MODEL = SentenceTransformer("all-MiniLM-L6-v2")
36
+
37
+ # -----------------------------------------------------------------------------
38
+ # 2. Trait & Session Definitions
39
+ # -----------------------------------------------------------------------------
40
+
41
+ BASE_TRAITS = [
42
+ "Guilt-Proneness", "Anxiety", "Aggression", "Callousness",
43
+ "Depression", "Grandiosity", "Manipulativeness", "Narcissism",
44
+ "Impulsivity", "Risk-Taking", "Responsibility", "Empathy",
45
+ "Conscientiousness"
46
+ ]
47
+ EXTENDED_TRAITS = BASE_TRAITS + ["Resilience", "Adaptability"]
48
+
49
+ # In‐memory session store
50
+ sessions = {} # session_id -> session data
51
+ current_session = None
52
+
53
+ # -----------------------------------------------------------------------------
54
+ # 3. Question Generation & Scoring
55
+ # -----------------------------------------------------------------------------
56
+
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
57
  def next_question(traits):
58
+ """Use LLM to generate a probing question for a random trait."""
59
+ llm = get_llm()
60
  trait = random.choice(traits)
61
+ prompt = f"Generate an open‐ended question to probe the trait '{trait}'."
62
  try:
63
+ q = llm(prompt, do_sample=True, temperature=0.7)[0]["generated_text"].strip()
64
  except Exception:
65
+ q = f"Tell me about a time you felt high in {trait}."
66
  return q
67
 
68
+ def score_response(text, traits):
69
+ """Rate each trait 0–1 based on the text. Returns mean vector."""
70
+ llm = get_llm()
71
+ trait_list = "\n".join(f"{i+1}. {t}" for i, t in enumerate(traits))
72
+ prompt = (
73
+ f"Rate the following traits 0–1 from this response:\n\n"
74
+ f"\"{text}\"\n\n{trait_list}\n\nReturn CSV only."
75
+ )
76
+ try:
77
+ raw = llm(prompt, do_sample=False)[0]["generated_text"]
78
+ values = [float(x) for x in raw.strip().split(",")]
79
+ return np.array(values)
80
+ except Exception:
81
+ # fallback to neutral
82
+ return np.full(len(traits), 0.5)
83
+
84
+ # -----------------------------------------------------------------------------
85
+ # 4. Persona & Memory Functions
86
+ # -----------------------------------------------------------------------------
87
+
88
+ def build_persona(profile, traits):
89
+ """Convert numeric profile into textual persona descriptor."""
90
+ lines = [f"{name}: {int(score*100)}/100"
91
+ for name, score in zip(traits, profile)]
92
+ return "### Personality Profile ###\n" + "\n".join(lines)
93
+
94
+ def embed_text(text):
95
+ return EMB_MODEL.encode(text, convert_to_tensor=True)
96
+
97
+ def retrieve_memories(query, memory_store, k=3):
98
+ """Return top‐k most similar memories to the query."""
99
+ if not memory_store:
100
+ return []
101
+ q_emb = embed_text(query)
102
+ embs = torch.stack([m["emb"] for m in memory_store])
103
+ sims = util.pytorch_cos_sim(q_emb, embs)[0]
104
+ topk = sims.topk(min(k, len(sims)))
105
+ return [memory_store[i]["text"] for i in topk.indices]
106
+
107
+ # -----------------------------------------------------------------------------
108
+ # 5. Placeholder: Fine‐Tuning & RL Hooks
109
+ # -----------------------------------------------------------------------------
110
+
111
+ def train_lora_agent(session):
112
+ """
113
+ Placeholder: fine‐tune or LoRA‐adapt a small model
114
+ on (question, answer) pairs in session["qa_pairs"].
115
+ """
116
+ # TODO: integrate peft, LoRA, or Hugging Face Trainer here.
117
+ pass
118
+
119
+ def rl_finetune_agent(agent, session):
120
+ """
121
+ Placeholder: reinforce agent reward based on trait‐derived metrics.
122
+ e.g., higher reward for 'manipulativeness' if persuasion detected.
123
+ """
124
+ # TODO: hook into PPO or other RL frameworks.
125
+ pass
126
+
127
+ # -----------------------------------------------------------------------------
128
+ # 6. Evaluation Metrics
129
+ # -----------------------------------------------------------------------------
130
+
131
+ sentiment_analyzer = pipeline("sentiment-analysis")
132
+ def evaluate_response_style(text):
133
+ """Return sentiment & basic style metrics."""
134
+ sent = sentiment_analyzer(text)[0]
135
+ pronouns = sum(text.lower().count(p) for p in [" i ", " me ", " my "])
136
+ formality = ("formal" if "you" in text.lower() else "casual")
137
+ return {
138
+ "sentiment": sent,
139
+ "pronouns_used": pronouns,
140
+ "formality": formality
141
+ }
142
+
143
+ # -----------------------------------------------------------------------------
144
+ # 7. Gradio App Logic
145
+ # -----------------------------------------------------------------------------
146
+
147
+ def start_new_session(num_qs):
148
  global current_session
149
+ sid = str(uuid.uuid4())
150
+ sessions[sid] = {
151
+ "qa_pairs": [],
152
+ "profile": None,
153
+ "persona": None,
154
+ "memory": []
155
+ }
156
+ current_session = sid
157
+ first_q = next_question(EXTENDED_TRAITS)
158
+ return sid, first_q
159
+
160
+ def load_session(json_str, num_qs):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
161
  global current_session
162
  try:
163
  data = json.loads(json_str)
164
  sid = str(uuid.uuid4())
165
  sessions[sid] = data
166
  current_session = sid
167
+ # resume with next question
168
+ return sid, next_question(EXTENDED_TRAITS)
169
  except Exception:
170
+ return None, "Failed to load session JSON."
171
+
172
+ def submit_answer(answer, num_qs):
173
+ sess = sessions[current_session]
174
+ sess["qa_pairs"].append(answer)
175
+ sess["memory"].append({
176
+ "text": answer,
177
+ "emb": embed_text(answer)
178
+ })
179
+ if len(sess["qa_pairs"]) < num_qs:
180
+ return next_question(EXTENDED_TRAITS), None, None
181
+ # finalize profile
182
+ all_scores = np.vstack([
183
+ score_response(txt, EXTENDED_TRAITS) for txt in sess["qa_pairs"]
184
+ ])
185
+ final_profile = all_scores.mean(axis=0)
186
+ sess["profile"] = final_profile.tolist()
187
+ sess["persona"] = build_persona(final_profile, EXTENDED_TRAITS)
188
+ # optionally train or RL‐tune
189
+ train_lora_agent(sess)
190
+ rl_finetune_agent(None, sess)
191
+ return None, sess["persona"], json.dumps(sess, indent=2)
192
+
193
+ def chat_with_agent(user_msg, drift=False):
194
+ sess = sessions[current_session]
195
+ history = sess.get("history", [])
196
+ history.append(("User", user_msg))
197
+ # retrieve memories
198
+ mems = retrieve_memories(user_msg, sess["memory"])
199
+ persona = sess["persona"]
200
+ llm = get_llm()
201
+ prompt = (
202
+ f"{persona}\n\n"
203
+ f"Relevant Memories:\n" + "\n".join(f"- {m}" for m in mems) + "\n\n"
204
+ "Conversation History:\n" +
205
+ "\n".join(f"{s}: {t}" for s, t in history) +
206
+ "\nAgent:"
207
+ )
208
+ out = llm(prompt, do_sample=True, temperature=0.8)[0]["generated_text"]
209
+ reply = out.split("Agent:")[-1].strip()
210
+ history.append(("Agent", reply))
211
+ sess["history"] = history
212
+ # optional persona drift
213
+ if drift:
214
+ # tiny random walk on profile
215
+ prof = np.array(sess["profile"])
216
+ sess["profile"] = (prof + np.random.normal(0, 0.01, prof.shape)).clip(0,1).tolist()
217
+ sess["persona"] = build_persona(np.array(sess["profile"]), EXTENDED_TRAITS)
218
+ # evaluate style
219
+ style = evaluate_response_style(reply)
220
+ return reply, json.dumps(style, indent=2)
221
+
222
+ # -----------------------------------------------------------------------------
223
+ # 8. Build Gradio Interface
224
+ # -----------------------------------------------------------------------------
225
 
 
226
  with gr.Blocks() as demo:
227
+ gr.Markdown("# Session‐Driven Persona Agent")
228
+
229
+ with gr.Row():
230
+ mode = gr.Radio(
231
+ ["New Session", "Load Session"], label="Mode", value="New Session"
232
+ )
233
+ num_qs = gr.Slider(3, 10, step=1, label="Number of Profiling Questions", value=5)
234
+ with gr.Row():
235
+ load_json = gr.Textbox(
236
+ label="Paste Session JSON (if loading)", lines=4, visible=False
237
+ )
238
+ start_btn = gr.Button("Start Profiling")
239
+
240
+ sid_box = gr.Textbox(label="Session ID", interactive=False)
241
+ question_out = gr.Textbox(label="Question", interactive=False)
242
+ answer_in = gr.Textbox(label="Your Answer")
243
+ next_btn = gr.Button("Submit Answer")
244
+ persona_out = gr.Textbox(label="Persona Summary", lines=6)
245
+ export_json = gr.Textbox(label="Exported Session JSON", lines=6)
246
+
247
  with gr.Row():
248
+ user_msg = gr.Textbox(label="Chat with Agent")
249
+ drift_chk = gr.Checkbox(label="Enable Persona Drift", value=False)
250
+ chat_btn = gr.Button("Send")
251
+ chat_out = gr.Textbox(label="Agent Reply", lines=4)
252
+ style_out = gr.Textbox(label="Reply Style Metrics", lines=4)
253
+
254
+ # Show or hide load_json
255
+ mode.change(lambda m: gr.update(visible=(m=="Load Session")),
256
+ inputs=mode, outputs=load_json)
257
+
258
+ # Start or load session
259
+ start_btn.click(
260
+ fn=lambda m, n, js: load_session(js, n) if m=="Load Session" else start_new_session(n),
261
+ inputs=[mode, num_qs, load_json],
262
+ outputs=[sid_box, question_out]
263
+ )
264
+
265
+ # Submit profiling answer
266
+ next_btn.click(
267
+ fn=lambda ans, n: submit_answer(ans, int(n)),
268
+ inputs=[answer_in, num_qs],
269
+ outputs=[question_out, persona_out, export_json]
270
+ )
271
+
272
+ # Chat interface
273
+ chat_btn.click(
274
+ fn=chat_with_agent,
275
+ inputs=[user_msg, drift_chk],
276
+ outputs=[chat_out, style_out]
277
  )
 
 
278
 
279
  if __name__ == "__main__":
280
+ demo.launch()