Spaces:
Runtime error
Runtime error
Update app.py
Browse files
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
|
| 9 |
-
from matplotlib import cm
|
| 10 |
from transformers import pipeline, AutoTokenizer, AutoModelForSeq2SeqLM
|
|
|
|
| 11 |
|
| 12 |
-
# ---
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
"Depression", "Grandiosity", "Manipulativeness", "Narcissism",
|
| 16 |
-
"Impulsivity", "Risk-Taking", "Responsibility", "Empathy",
|
| 17 |
-
"Conscientiousness"
|
| 18 |
-
]
|
| 19 |
|
| 20 |
-
#
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 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 |
-
|
| 31 |
"text2text-generation",
|
| 32 |
model=model,
|
| 33 |
tokenizer=tokenizer,
|
| 34 |
device_map="auto" if torch.cuda.is_available() else None,
|
| 35 |
-
max_length=
|
| 36 |
)
|
| 37 |
-
return
|
| 38 |
-
|
| 39 |
-
#
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
# ---
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 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 |
-
|
|
|
|
| 83 |
trait = random.choice(traits)
|
| 84 |
-
prompt = f"Generate
|
| 85 |
try:
|
| 86 |
-
q =
|
| 87 |
except Exception:
|
| 88 |
-
q = "
|
| 89 |
return q
|
| 90 |
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
for
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
#
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 131 |
global current_session
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 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 |
-
|
|
|
|
| 167 |
except Exception:
|
| 168 |
-
return "Failed to load session
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 169 |
|
| 170 |
-
# --- Build UI ---
|
| 171 |
with gr.Blocks() as demo:
|
| 172 |
-
gr.Markdown(""
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 182 |
with gr.Row():
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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()
|