Spaces:
Sleeping
Sleeping
TTsamurai
commited on
Commit
·
50020ec
1
Parent(s):
9be606d
for session debugging
Browse files
app.py
CHANGED
|
@@ -1,55 +1,13 @@
|
|
| 1 |
-
import subprocess
|
| 2 |
import sys
|
| 3 |
import os
|
| 4 |
-
|
| 5 |
-
ROOT_FILE = os.path.join(os.path.dirname(os.path.abspath(__file__)), "./")
|
| 6 |
-
sys.path.append(ROOT_FILE)
|
| 7 |
-
from components.induce_personality import construct_big_five_words
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
# need to import: gradio
|
| 11 |
-
def install(package, upgrade=False):
|
| 12 |
-
if upgrade:
|
| 13 |
-
subprocess.run(
|
| 14 |
-
[
|
| 15 |
-
sys.executable,
|
| 16 |
-
"-m",
|
| 17 |
-
"pip",
|
| 18 |
-
"install",
|
| 19 |
-
"--upgrade",
|
| 20 |
-
package,
|
| 21 |
-
],
|
| 22 |
-
check=True,
|
| 23 |
-
)
|
| 24 |
-
else:
|
| 25 |
-
subprocess.run(
|
| 26 |
-
[
|
| 27 |
-
sys.executable,
|
| 28 |
-
"-m",
|
| 29 |
-
"pip",
|
| 30 |
-
"install",
|
| 31 |
-
package,
|
| 32 |
-
],
|
| 33 |
-
check=True,
|
| 34 |
-
)
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
# install("ipdb")
|
| 38 |
-
# install("gradio")
|
| 39 |
-
# install("sentence-transformers")
|
| 40 |
-
# install("git+https://github.com/terrierteam/pyterrier_t5.git")
|
| 41 |
-
# install("protobuf")
|
| 42 |
-
# install("transformers", upgrade=True)
|
| 43 |
-
import random
|
| 44 |
import json
|
| 45 |
import gradio as gr
|
| 46 |
-
import random
|
| 47 |
-
import time
|
| 48 |
import markdown
|
| 49 |
-
import gradio as gr
|
| 50 |
-
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 51 |
from together import Together
|
| 52 |
|
|
|
|
|
|
|
|
|
|
| 53 |
from components.chat_conversation import (
|
| 54 |
# format_message_history,
|
| 55 |
format_user_message,
|
|
@@ -62,62 +20,72 @@ from components.chat_conversation import (
|
|
| 62 |
conversation_window,
|
| 63 |
generate_response_local_api,
|
| 64 |
generate_response_together_api,
|
|
|
|
| 65 |
)
|
| 66 |
from components.constant import (
|
| 67 |
-
# ACCESS,
|
| 68 |
-
# QUERY_REWRITING,
|
| 69 |
-
# RAG,
|
| 70 |
-
# PERSONALITY,
|
| 71 |
-
# PERSONALITY_LIST,
|
| 72 |
-
# REWRITE_PASSAGES,
|
| 73 |
-
# NUM_PASSAGES,
|
| 74 |
-
# DEVICE,
|
| 75 |
-
RESPONSE_GENERATOR,
|
| 76 |
CONV_WINDOW,
|
| 77 |
API_URL,
|
| 78 |
-
# TEMPLATE_PAYLOAD,
|
| 79 |
)
|
| 80 |
from components.induce_personality import (
|
| 81 |
build_personality_prompt,
|
| 82 |
)
|
| 83 |
|
| 84 |
-
|
| 85 |
-
LOG_DIR = os.path.join(ROOT_FILE, "log/seperate_preference_elicitation/others/")
|
| 86 |
if os.path.exists(LOG_DIR) is False:
|
| 87 |
os.makedirs(LOG_DIR)
|
|
|
|
|
|
|
| 88 |
STATIC_FILE = os.path.join(ROOT_FILE, "_static")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
FINAL_EVALUATION = f.read()
|
| 102 |
-
with open(os.path.join(STATIC_FILE, "txt/system_instruction_with_user_persona.txt"), "r") as f:
|
| 103 |
-
SYSTEM_INSTRUCTION = f.read()
|
| 104 |
-
with open(os.path.join(STATIC_FILE, "txt/system_instruction_preference_elicitation.txt"), "r") as f:
|
| 105 |
-
SYSTEM_INSTRUECTION_PREFERENCE_ELICITATION = f.read()
|
| 106 |
-
with open(os.path.join(STATIC_FILE, "txt/system_summarization_user_preference_elicitation.txt"), "r") as f:
|
| 107 |
-
SUMMARIZATION_PROMPT = f.read()
|
| 108 |
FIRST_MESSAGE = "Hey"
|
| 109 |
INFORMATION_SEEKING = True
|
| 110 |
USER_PREFERENCE_SUMMARY = True
|
| 111 |
DEBUG = False
|
| 112 |
-
API_TYPE = "
|
| 113 |
-
assert API_TYPE in ["together", "local"], "The API should be
|
| 114 |
if API_TYPE == "together":
|
| 115 |
-
TOGETHER_CLIENT = Together(api_key=os.
|
| 116 |
-
|
| 117 |
-
|
| 118 |
|
| 119 |
|
| 120 |
-
def
|
| 121 |
# Load data from the synthetic data file
|
| 122 |
with open(synthetic_data_path, "r") as f:
|
| 123 |
data = [json.loads(line) for line in f]
|
|
@@ -165,6 +133,10 @@ def get_user_narrative_html(user_narrative):
|
|
| 165 |
return USER_NARRATIVE.replace("{user_narrative}", user_narrative).replace("\n", "<br>")
|
| 166 |
|
| 167 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 168 |
def get_task_instruction_for_user(context):
|
| 169 |
ticker_name = context["short_name"]
|
| 170 |
user_narrative = context["user_narrative"]
|
|
@@ -210,16 +182,22 @@ def display_system_instruction_with_html(
|
|
| 210 |
return html_system_instruction
|
| 211 |
|
| 212 |
|
| 213 |
-
def log_action(tab_name, action, details):
|
| 214 |
"""
|
| 215 |
Log actions for each tab (stock).
|
| 216 |
"""
|
| 217 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 218 |
with open(log_file, "a") as f:
|
| 219 |
f.write(f"Action: {action} | Details: {details}\n")
|
| 220 |
|
| 221 |
|
| 222 |
-
def add_user_profile_to_system_instruction(
|
|
|
|
|
|
|
| 223 |
if summary:
|
| 224 |
if user_preference_elicitation_data["summary_history"] == "":
|
| 225 |
# Format prompt
|
|
@@ -229,10 +207,12 @@ def add_user_profile_to_system_instruction(system_instruction, user_preference_e
|
|
| 229 |
summarization_instruction = [{"role": "system", "content": summarization_prompt}]
|
| 230 |
if API_TYPE == "local":
|
| 231 |
summ, _ = generate_response_local_api(summarization_instruction, terminator, 512, API_URL)
|
| 232 |
-
|
| 233 |
summ, _ = generate_response_together_api(summarization_instruction, 512, TOGETHER_CLIENT)
|
|
|
|
|
|
|
| 234 |
user_preference_elicitation_data["summary_history"] = summ
|
| 235 |
-
log_action("Prompt", "Preference Elicitation Summarization", summ)
|
| 236 |
print(f"Preference Summary:{summ}")
|
| 237 |
system_instruction += f"\nPrevious Conversations with the Customer about the User Profile: {user_preference_elicitation_data['summary_history']}\n"
|
| 238 |
else:
|
|
@@ -240,41 +220,17 @@ def add_user_profile_to_system_instruction(system_instruction, user_preference_e
|
|
| 240 |
return system_instruction
|
| 241 |
|
| 242 |
|
| 243 |
-
def create_demo(
|
| 244 |
-
terminator
|
| 245 |
-
|
| 246 |
-
|
| 247 |
-
raw_context_list,
|
| 248 |
-
):
|
| 249 |
-
# Store the history here and use this as an input to each tab.
|
| 250 |
-
tab_data = {}
|
| 251 |
-
user_preference_elicitation_data = {"history": "", "summary_history": ""}
|
| 252 |
-
|
| 253 |
-
if DEBUG:
|
| 254 |
-
user_preference_elicitation_data[
|
| 255 |
-
"summary_history"
|
| 256 |
-
] = """Previous Conversations with the Customer about the User Profile: Based on our previous conversation, here's a summary of your investment preferences:
|
| 257 |
-
|
| 258 |
-
1. **Preferred Industries:** You're interested in investing in the healthcare sector, without a specific preference for sub-industries such as pharmaceuticals, medical devices, biotechnology, or healthcare services.
|
| 259 |
-
2. **Value vs. Growth Stocks:** You prefer growth stocks, which have the potential for high returns but may be riskier.
|
| 260 |
-
3. **Dividend vs. Non-Dividend Stocks:** You're open to both dividend and non-dividend growth stocks, focusing on reinvesting profits for future growth.
|
| 261 |
-
4. **Cyclical vs. Non-Cyclical Stocks:** You're interested in cyclical stocks, which are sensitive to economic fluctuations and tend to perform well during economic expansions."""
|
| 262 |
-
|
| 263 |
-
def tab_creation_exploration_stage(order):
|
| 264 |
-
comp, context, general_instruction, round_instruction = get_context(order)
|
| 265 |
-
system_instruction = system_description_without_context + "\n" + context
|
| 266 |
-
tab_data[comp] = {"history": [], "selection": "", "reason": ""}
|
| 267 |
english_order = ["1", "2", "3", "4", "5"]
|
| 268 |
-
# with gr.Tab(f"{english_order[order]}: {comp}") as tab:
|
| 269 |
with gr.Tab(f"{english_order[order]}-1:Discuss"):
|
| 270 |
-
gr.HTML(
|
| 271 |
with gr.Row():
|
| 272 |
with gr.Column():
|
| 273 |
with gr.Row():
|
| 274 |
-
gr.HTML(
|
| 275 |
-
value=round_instruction,
|
| 276 |
-
label="Round Instruction",
|
| 277 |
-
)
|
| 278 |
with gr.Column():
|
| 279 |
with gr.Row():
|
| 280 |
chatbot = gr.Chatbot(height=600)
|
|
@@ -287,12 +243,6 @@ def create_demo(
|
|
| 287 |
continue_button = gr.Button(value="Show More of the Advisor’s Answer", interactive=False)
|
| 288 |
with gr.Row():
|
| 289 |
clear = gr.ClearButton([msg, chatbot])
|
| 290 |
-
if DEBUG:
|
| 291 |
-
with gr.Row():
|
| 292 |
-
display_prompt = gr.HTML(
|
| 293 |
-
value=display_system_instruction_with_html(system_instruction),
|
| 294 |
-
label="System Instruction",
|
| 295 |
-
)
|
| 296 |
with gr.Tab(f"{english_order[order]}-2:Eval"):
|
| 297 |
with gr.Row():
|
| 298 |
gr.HTML(value=EVALUATION_INSTRUCTION)
|
|
@@ -344,7 +294,7 @@ def create_demo(
|
|
| 344 |
evaluation_send_button = gr.Button(value="Send: Evaluation")
|
| 345 |
return {
|
| 346 |
"comp": comp,
|
| 347 |
-
"
|
| 348 |
"start_conversation": start_conversation,
|
| 349 |
"msg_button": msg_button,
|
| 350 |
"continue_button": continue_button,
|
|
@@ -357,6 +307,8 @@ def create_demo(
|
|
| 357 |
"knowledgeable": knowledgeable,
|
| 358 |
"helpful": helpful,
|
| 359 |
"evaluation_send_button": evaluation_send_button,
|
|
|
|
|
|
|
| 360 |
}
|
| 361 |
|
| 362 |
def tab_creation_preference_stage():
|
|
@@ -364,8 +316,7 @@ def create_demo(
|
|
| 364 |
gr.HTML(value=PREFERENCE_ELICITATION_TASK, label="Preference Elicitation Task")
|
| 365 |
with gr.Row():
|
| 366 |
with gr.Column():
|
| 367 |
-
|
| 368 |
-
gr.HTML(value=whole_user_narrative, label="User Narrative")
|
| 369 |
with gr.Column():
|
| 370 |
with gr.Row():
|
| 371 |
elicitation_chatbot = gr.Chatbot(height=600)
|
|
@@ -382,17 +333,18 @@ def create_demo(
|
|
| 382 |
"continue_button": continue_button,
|
| 383 |
"msg": msg,
|
| 384 |
"elicitation_chatbot": elicitation_chatbot,
|
|
|
|
| 385 |
}
|
| 386 |
|
| 387 |
-
def tab_final_evaluation(
|
| 388 |
with gr.Row():
|
| 389 |
gr.HTML(value=FINAL_EVALUATION)
|
| 390 |
with gr.Row():
|
| 391 |
-
ranking_first_comp = gr.Dropdown(choices=[1, 2, 3, 4, 5]
|
| 392 |
-
ranking_second_comp = gr.Dropdown(choices=[1, 2, 3, 4, 5]
|
| 393 |
-
ranking_third_comp = gr.Dropdown(choices=[1, 2, 3, 4, 5]
|
| 394 |
-
ranking_fourth_comp = gr.Dropdown(choices=[1, 2, 3, 4, 5]
|
| 395 |
-
ranking_fifth_comp = gr.Dropdown(choices=[1, 2, 3, 4, 5]
|
| 396 |
with gr.Row():
|
| 397 |
textbox = gr.HTML(
|
| 398 |
"""<div style="background-color: #f8d7da; color: #721c24; padding: 15px; border: 1px solid #f5c6cb; border-radius: 5px; margin-bottom: 20px;">
|
|
@@ -403,19 +355,21 @@ def create_demo(
|
|
| 403 |
)
|
| 404 |
submit_ranking = gr.Button(value="Submit Ranking")
|
| 405 |
return {
|
| 406 |
-
"first":
|
| 407 |
-
"second":
|
| 408 |
-
"third":
|
| 409 |
-
"fourth":
|
| 410 |
-
"fifth":
|
| 411 |
"submit_ranking": submit_ranking,
|
| 412 |
"text_box": textbox,
|
| 413 |
}
|
| 414 |
|
| 415 |
-
def click_control_exploration_stage(
|
|
|
|
|
|
|
| 416 |
(
|
| 417 |
comp,
|
| 418 |
-
|
| 419 |
start_conversation,
|
| 420 |
msg_button,
|
| 421 |
continue_button,
|
|
@@ -430,7 +384,7 @@ def create_demo(
|
|
| 430 |
evaluation_send_button,
|
| 431 |
) = (
|
| 432 |
tabs["comp"],
|
| 433 |
-
tabs["
|
| 434 |
tabs["start_conversation"],
|
| 435 |
tabs["msg_button"],
|
| 436 |
tabs["continue_button"],
|
|
@@ -444,23 +398,79 @@ def create_demo(
|
|
| 444 |
tabs["helpful"],
|
| 445 |
tabs["evaluation_send_button"],
|
| 446 |
)
|
|
|
|
| 447 |
start_conversation.click(
|
| 448 |
-
lambda
|
| 449 |
-
|
| 450 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 451 |
)
|
| 452 |
msg_button.click(
|
| 453 |
-
lambda
|
| 454 |
-
|
| 455 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 456 |
)
|
| 457 |
continue_button.click(
|
| 458 |
-
lambda history
|
| 459 |
-
|
| 460 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 461 |
)
|
| 462 |
evaluation_send_button.click(
|
| 463 |
-
lambda dropdown, reason, trust, satisfaction, knowledgeable, helpful: respond_evaluation(
|
|
|
|
|
|
|
| 464 |
{
|
| 465 |
"selection": dropdown,
|
| 466 |
"reason": reason,
|
|
@@ -471,11 +481,11 @@ def create_demo(
|
|
| 471 |
},
|
| 472 |
comp,
|
| 473 |
),
|
| 474 |
-
[dropdown, reason, trust, satisfaction, knowledgeable, helpful],
|
| 475 |
-
[dropdown, reason, trust, satisfaction, knowledgeable, helpful],
|
| 476 |
)
|
| 477 |
|
| 478 |
-
def click_control_preference_stage(tabs):
|
| 479 |
(
|
| 480 |
start_conversation,
|
| 481 |
msg_button,
|
|
@@ -489,43 +499,55 @@ def create_demo(
|
|
| 489 |
tabs["elicitation_chatbot"],
|
| 490 |
tabs["msg"],
|
| 491 |
)
|
|
|
|
| 492 |
start_conversation.click(
|
| 493 |
-
lambda history: respond_start_conversation(
|
| 494 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 495 |
),
|
| 496 |
-
[elicitation_chatbot],
|
| 497 |
-
[elicitation_chatbot, start_conversation, msg_button, continue_button],
|
| 498 |
)
|
| 499 |
msg_button.click(
|
| 500 |
-
lambda message, history: respond(
|
|
|
|
|
|
|
| 501 |
message,
|
| 502 |
-
|
| 503 |
SYSTEM_INSTRUECTION_PREFERENCE_ELICITATION,
|
| 504 |
user_elicitation=True,
|
| 505 |
),
|
| 506 |
-
[msg, elicitation_chatbot],
|
| 507 |
-
[msg, elicitation_chatbot],
|
| 508 |
)
|
| 509 |
continue_button.click(
|
| 510 |
-
lambda history: respond_continue(
|
| 511 |
-
|
|
|
|
|
|
|
| 512 |
SYSTEM_INSTRUECTION_PREFERENCE_ELICITATION,
|
| 513 |
user_elicitation=True,
|
| 514 |
),
|
| 515 |
-
[elicitation_chatbot],
|
| 516 |
-
[elicitation_chatbot],
|
| 517 |
)
|
| 518 |
|
| 519 |
-
def click_control_final_evaluation(tabs):
|
| 520 |
-
|
| 521 |
-
|
| 522 |
-
|
| 523 |
-
|
| 524 |
-
|
|
|
|
|
|
|
| 525 |
result_textbox = tabs["text_box"]
|
| 526 |
submit_ranking = tabs["submit_ranking"]
|
| 527 |
submit_ranking.click(
|
| 528 |
-
lambda ranking_first_comp, ranking_second_comp, ranking_third_comp, ranking_fourth_comp, ranking_fifth_comp: respond_final_ranking(
|
|
|
|
| 529 |
first_comp,
|
| 530 |
ranking_first_comp,
|
| 531 |
second_comp,
|
|
@@ -539,17 +561,34 @@ def create_demo(
|
|
| 539 |
),
|
| 540 |
# Input components (names and rankings)
|
| 541 |
[
|
|
|
|
| 542 |
ranking_first_comp,
|
|
|
|
| 543 |
ranking_second_comp,
|
|
|
|
| 544 |
ranking_third_comp,
|
|
|
|
| 545 |
ranking_fourth_comp,
|
|
|
|
| 546 |
ranking_fifth_comp,
|
|
|
|
| 547 |
],
|
| 548 |
# Output component(s) where you want the result to appear, e.g., result_textbox
|
| 549 |
[result_textbox],
|
| 550 |
)
|
| 551 |
|
| 552 |
-
def respond(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 553 |
"""
|
| 554 |
Return:
|
| 555 |
msg
|
|
@@ -562,22 +601,22 @@ def create_demo(
|
|
| 562 |
tab_name is not None or user_elicitation is True
|
| 563 |
), "Tab name is required for the start of the conversation unless it is not preference elicitation."
|
| 564 |
# Add user profile to system instruction
|
|
|
|
|
|
|
| 565 |
if not user_elicitation:
|
| 566 |
system_instruction = add_user_profile_to_system_instruction(
|
|
|
|
| 567 |
system_instruction,
|
| 568 |
user_preference_elicitation_data,
|
| 569 |
summary=USER_PREFERENCE_SUMMARY,
|
| 570 |
terminator=terminator,
|
| 571 |
)
|
| 572 |
-
# print(f"Tab: {tab_name}\nSystem Instruction:{system_instruction}")
|
| 573 |
# Formatting Input
|
| 574 |
print(f"User Message: {message} in Tab: {tab_name}")
|
| 575 |
# From string to list [{"role":"user", "content": message}, ...]
|
| 576 |
history = gradio_to_huggingface_message(history)
|
| 577 |
# We can implement context window here as we need all the system interaction. We can cut some of the early interactions if needed.
|
| 578 |
history = conversation_window(history, CONV_WINDOW)
|
| 579 |
-
print(f"History Length: {len(history)}")
|
| 580 |
-
print(f"History: {history}")
|
| 581 |
# Add system instruction to the history
|
| 582 |
history = format_context(system_instruction, history)
|
| 583 |
# Add user message to the history
|
|
@@ -585,32 +624,51 @@ def create_demo(
|
|
| 585 |
# Call API instead of locally handle it
|
| 586 |
if API_TYPE == "local":
|
| 587 |
outputs_text, history = generate_response_local_api(history_with_user_utterance, terminator, 128, API_URL)
|
| 588 |
-
|
| 589 |
outputs_text, history = generate_response_together_api(history_with_user_utterance, 128, TOGETHER_CLIENT)
|
|
|
|
|
|
|
| 590 |
# exclude system interaction and store the others in the history
|
| 591 |
history = huggingface_to_gradio_message(history)
|
| 592 |
if tab_name is not None:
|
| 593 |
print(f"Tab: {tab_name}\nSystem Output: {outputs_text}")
|
| 594 |
|
| 595 |
# Log the user message and response
|
| 596 |
-
log_action(tab_name, "User Message", message)
|
| 597 |
-
log_action(tab_name, "Response", outputs_text)
|
| 598 |
# Store the updated history for this tab
|
| 599 |
-
tab_data[
|
| 600 |
if user_elicitation:
|
| 601 |
print(f"User Elicitation\nSystem Output: {outputs_text}")
|
| 602 |
-
log_action("User_Elicitation", "User Message", message)
|
| 603 |
-
log_action("User_Elicitation", "Response", outputs_text)
|
| 604 |
-
|
| 605 |
-
|
| 606 |
-
|
| 607 |
-
|
| 608 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 609 |
assert (
|
| 610 |
tab_name is not None or user_elicitation is True
|
| 611 |
), "Tab name is required for the start of the conversation unless it is not preference elicitation."
|
|
|
|
|
|
|
| 612 |
if not user_elicitation:
|
|
|
|
|
|
|
| 613 |
system_instruction = add_user_profile_to_system_instruction(
|
|
|
|
| 614 |
system_instruction,
|
| 615 |
user_preference_elicitation_data,
|
| 616 |
summary=USER_PREFERENCE_SUMMARY,
|
|
@@ -623,44 +681,59 @@ def create_demo(
|
|
| 623 |
history_with_user_utterance = format_user_message(first_message, history)
|
| 624 |
if API_TYPE == "local":
|
| 625 |
outputs_text, history = generate_response_local_api(history_with_user_utterance, terminator, 128, API_URL)
|
| 626 |
-
|
| 627 |
outputs_text, history = generate_response_together_api(history_with_user_utterance, 128, TOGETHER_CLIENT)
|
|
|
|
|
|
|
| 628 |
# Format
|
| 629 |
history = huggingface_to_gradio_message(history)
|
| 630 |
if tab_name is not None:
|
| 631 |
print(f"Tab: {tab_name}\nHistory: {history}")
|
| 632 |
-
|
| 633 |
# Log the user message and response
|
| 634 |
-
log_action(tab_name, "User Message", first_message)
|
| 635 |
-
log_action(tab_name, "Response", outputs_text)
|
| 636 |
# Store the updated history for this tab
|
| 637 |
-
tab_data[
|
| 638 |
if user_elicitation:
|
| 639 |
print(f"User Elicitation\nHistory: {history}")
|
| 640 |
-
log_action("User_Elicitation", "User Message", first_message)
|
| 641 |
-
log_action("User_Elicitation", "Response", outputs_text)
|
| 642 |
-
|
| 643 |
-
|
|
|
|
| 644 |
return (
|
|
|
|
| 645 |
history,
|
| 646 |
gr.Button(value="Start Conversation", interactive=False),
|
| 647 |
gr.Button(value="Send This Message to Advisor", interactive=True),
|
| 648 |
gr.Button(value="Show More of the Advisor’s Answer", interactive=True),
|
| 649 |
)
|
| 650 |
|
| 651 |
-
def respond_continue(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 652 |
assert (
|
| 653 |
tab_name is not None or user_elicitation is True
|
| 654 |
), "Tab name is required for the start of the conversation."
|
| 655 |
# Add user profile to system instruction
|
|
|
|
|
|
|
| 656 |
if not user_elicitation:
|
| 657 |
system_instruction = add_user_profile_to_system_instruction(
|
|
|
|
| 658 |
system_instruction,
|
| 659 |
user_preference_elicitation_data,
|
| 660 |
summary=USER_PREFERENCE_SUMMARY,
|
| 661 |
terminator=terminator,
|
| 662 |
)
|
| 663 |
-
# print(f"Tab: {tab_name}\nSystem Instruction:{system_instruction}")
|
| 664 |
message = "continue"
|
| 665 |
history = gradio_to_huggingface_message(history)
|
| 666 |
history = conversation_window(history, CONV_WINDOW)
|
|
@@ -668,31 +741,35 @@ def create_demo(
|
|
| 668 |
history_with_user_utterance = format_user_message(message, history)
|
| 669 |
if API_TYPE == "local":
|
| 670 |
outputs_text, history = generate_response_local_api(history_with_user_utterance, terminator, 128, API_URL)
|
| 671 |
-
|
| 672 |
outputs_text, history = generate_response_together_api(history_with_user_utterance, 128, TOGETHER_CLIENT)
|
|
|
|
|
|
|
| 673 |
history = huggingface_to_gradio_message(history)
|
| 674 |
if tab_name is not None:
|
| 675 |
-
log_action(tab_name, "Show More of the Advisor’s Answer", "User continued the conversation")
|
| 676 |
-
log_action(tab_name, "Response", outputs_text)
|
| 677 |
|
| 678 |
# Update history for this tab
|
| 679 |
-
tab_data[
|
| 680 |
if user_elicitation:
|
| 681 |
print(f"User Elicitation\nSystem Output: {outputs_text}")
|
| 682 |
-
log_action("User_Elicitation", "Response", outputs_text)
|
| 683 |
-
|
|
|
|
|
|
|
|
|
|
| 684 |
|
| 685 |
-
|
| 686 |
-
|
| 687 |
-
def respond_evaluation(evals, tab_name):
|
| 688 |
|
| 689 |
# dropdown, readon_button, multi-evaluator
|
| 690 |
-
log_action(tab_name, "Round Evaluation", "Following")
|
| 691 |
for key, value in evals.items():
|
| 692 |
-
log_action(tab_name, key, value)
|
| 693 |
# Store the reason for this tab
|
| 694 |
-
tab_data[
|
| 695 |
return (
|
|
|
|
| 696 |
evals["selection"],
|
| 697 |
evals["reason"],
|
| 698 |
evals["trust"],
|
|
@@ -702,6 +779,7 @@ def create_demo(
|
|
| 702 |
)
|
| 703 |
|
| 704 |
def respond_final_ranking(
|
|
|
|
| 705 |
first_comp,
|
| 706 |
ranking_first_comp,
|
| 707 |
second_comp,
|
|
@@ -726,56 +804,264 @@ def create_demo(
|
|
| 726 |
<strong>Please make sure that you are not ranking the same stock multiple times.</strong>
|
| 727 |
</div>"""
|
| 728 |
else:
|
| 729 |
-
log_action("Final_Ranking", first_comp, ranking_first_comp)
|
| 730 |
-
log_action("Final_Ranking", second_comp, ranking_second_comp)
|
| 731 |
-
log_action("Final_Ranking", third_comp, ranking_third_comp)
|
| 732 |
-
log_action("Final_Ranking", fourth_comp, ranking_fourth_comp)
|
| 733 |
-
log_action("Final_Ranking", fifth_comp, ranking_fifth_comp)
|
| 734 |
return """<div style="background-color: #d4edda; color: #155724; padding: 15px; border: 1px solid #c3e6cb; border-radius: 5px; margin-bottom: 20px;">
|
| 735 |
<strong>Thank you for participating in the experiment. This concludes the session. You may now close the tab.</strong>
|
| 736 |
</div>"""
|
| 737 |
|
| 738 |
-
def get_context(index):
|
| 739 |
comp = raw_context_list[index]["short_name"]
|
| 740 |
context = stock_context_list[index]
|
| 741 |
general_instruction, round_instruction = get_task_instruction_for_user(raw_context_list[index])
|
| 742 |
return comp, context, general_instruction, round_instruction
|
| 743 |
|
| 744 |
-
|
| 745 |
-
|
| 746 |
-
|
| 747 |
-
|
| 748 |
-
|
| 749 |
-
|
| 750 |
-
|
| 751 |
-
|
| 752 |
-
|
| 753 |
-
|
| 754 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 755 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 756 |
# EXperiment Instruction
|
| 757 |
with gr.Tab("Experiment Instruction") as instruction_tab:
|
| 758 |
gr.HTML(value=INSTRUCTION_PAGE, label="Experiment Instruction")
|
| 759 |
# User Preference Elicitation Tab
|
| 760 |
with gr.Tab("Preference Elicitation Stage") as preference_elicitation_tab:
|
| 761 |
user_preference_elicitation_tab = tab_creation_preference_stage()
|
| 762 |
-
|
| 763 |
-
|
|
|
|
|
|
|
|
|
|
| 764 |
# Experiment Tag
|
| 765 |
-
first_tab = tab_creation_exploration_stage(0)
|
| 766 |
-
|
| 767 |
-
|
| 768 |
-
|
| 769 |
-
|
| 770 |
-
click_control_exploration_stage(
|
| 771 |
-
|
| 772 |
-
|
| 773 |
-
|
| 774 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 775 |
with gr.Tab("Final Evaluation Stage") as final_evaluation:
|
| 776 |
-
final_evaluation_tab = tab_final_evaluation(
|
| 777 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 778 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 779 |
return demo
|
| 780 |
|
| 781 |
|
|
@@ -793,14 +1079,7 @@ if __name__ == "__main__":
|
|
| 793 |
"jpm_1",
|
| 794 |
"jpm_2",
|
| 795 |
]
|
| 796 |
-
context_info_list =
|
| 797 |
-
# For check, just use fifth one
|
| 798 |
-
context_info = context_info_list[5]
|
| 799 |
-
# import ipdb
|
| 800 |
-
|
| 801 |
-
# ipdb.set_trace()
|
| 802 |
-
stock_context_list = build_context(context_info) # List of str
|
| 803 |
-
raw_context_list = build_raw_context_list(context_info) # List of str
|
| 804 |
# system instruction consist of Task, Personality, and Context
|
| 805 |
"""
|
| 806 |
Personality
|
|
@@ -810,20 +1089,17 @@ if __name__ == "__main__":
|
|
| 810 |
["neurotic", "emotionally stable"]
|
| 811 |
["open to experience", "closed to experience"]]
|
| 812 |
"""
|
| 813 |
-
|
| 814 |
-
personality =
|
| 815 |
-
|
| 816 |
-
|
| 817 |
-
|
| 818 |
-
|
| 819 |
-
|
| 820 |
-
|
| 821 |
-
|
| 822 |
-
|
| 823 |
-
|
| 824 |
-
# tokenizer = AutoTokenizer.from_pretrained(RESPONSE_GENERATOR)
|
| 825 |
-
# tokenizer, terminator = prepare_tokenizer(tokenizer)
|
| 826 |
terminator = ["<eos>", "<unk>", "<sep>", "<pad>", "<cls>", "<mask>"]
|
| 827 |
-
|
| 828 |
-
demo =
|
| 829 |
-
demo.launch(share=True)
|
|
|
|
|
|
|
| 1 |
import sys
|
| 2 |
import os
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
import json
|
| 4 |
import gradio as gr
|
|
|
|
|
|
|
| 5 |
import markdown
|
|
|
|
|
|
|
| 6 |
from together import Together
|
| 7 |
|
| 8 |
+
ROOT_FILE = os.path.join(os.path.dirname(os.path.abspath(__file__)), "../")
|
| 9 |
+
sys.path.append(ROOT_FILE)
|
| 10 |
+
from components.induce_personality import construct_big_five_words
|
| 11 |
from components.chat_conversation import (
|
| 12 |
# format_message_history,
|
| 13 |
format_user_message,
|
|
|
|
| 20 |
conversation_window,
|
| 21 |
generate_response_local_api,
|
| 22 |
generate_response_together_api,
|
| 23 |
+
generate_response_debugging,
|
| 24 |
)
|
| 25 |
from components.constant import (
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
CONV_WINDOW,
|
| 27 |
API_URL,
|
|
|
|
| 28 |
)
|
| 29 |
from components.induce_personality import (
|
| 30 |
build_personality_prompt,
|
| 31 |
)
|
| 32 |
|
| 33 |
+
LOG_DIR = os.path.join(ROOT_FILE, "log/api/")
|
|
|
|
| 34 |
if os.path.exists(LOG_DIR) is False:
|
| 35 |
os.makedirs(LOG_DIR)
|
| 36 |
+
|
| 37 |
+
# Load Static Files
|
| 38 |
STATIC_FILE = os.path.join(ROOT_FILE, "_static")
|
| 39 |
+
LOG_DIR = os.path.join(ROOT_FILE, "log/test_session/")
|
| 40 |
+
INSTRUCTION_PAGE_FILE = os.path.join(STATIC_FILE, "html/instruction_page.html")
|
| 41 |
+
USER_NARRATIVE_FILE = os.path.join(STATIC_FILE, "html/user_narrative.html")
|
| 42 |
+
PREFERENCE_ELICITATION_TASK_FILE = os.path.join(STATIC_FILE, "html/system_instruction_preference_elicitation.html")
|
| 43 |
+
EVALUATION_INSTRUCTION_FILE = os.path.join(STATIC_FILE, "html/evaluation_instruction.html")
|
| 44 |
+
GENERAL_INSTRUCTION_FILE = os.path.join(STATIC_FILE, "html/general_instruction.html")
|
| 45 |
+
FINAL_EVALUATION_FILE = os.path.join(STATIC_FILE, "html/final_evaluation.html")
|
| 46 |
+
SYSTEM_INSTRUCTION_FILE = os.path.join(STATIC_FILE, "txt/system_instruction_with_user_persona.txt")
|
| 47 |
+
SYSTEM_INSTRUECTION_PREFERENCE_ELICITATION_FILE = os.path.join(
|
| 48 |
+
STATIC_FILE, "txt/system_instruction_preference_elicitation.txt"
|
| 49 |
+
)
|
| 50 |
+
SUMMARIZATION_PROMPT_FILE = os.path.join(STATIC_FILE, "txt/system_summarization_user_preference_elicitation.txt")
|
| 51 |
+
|
| 52 |
+
|
| 53 |
+
# Load the required static content from files
|
| 54 |
+
def load_static_content(file_path):
|
| 55 |
+
with open(file_path, "r") as f:
|
| 56 |
+
return f.read()
|
| 57 |
+
|
| 58 |
+
|
| 59 |
+
def ensure_directory_exists(directory_path):
|
| 60 |
+
"""Ensures the given directory exists; creates it if it does not."""
|
| 61 |
+
if not os.path.exists(directory_path):
|
| 62 |
+
os.makedirs(directory_path)
|
| 63 |
+
|
| 64 |
|
| 65 |
+
INSTRUCTION_PAGE = load_static_content(INSTRUCTION_PAGE_FILE)
|
| 66 |
+
EVALUATION_INSTRUCTION = load_static_content(EVALUATION_INSTRUCTION_FILE)
|
| 67 |
+
GENERAL_INSTRUCTION = load_static_content(GENERAL_INSTRUCTION_FILE)
|
| 68 |
+
USER_NARRATIVE = load_static_content(USER_NARRATIVE_FILE)
|
| 69 |
+
PREFERENCE_ELICITATION_TASK = load_static_content(PREFERENCE_ELICITATION_TASK_FILE)
|
| 70 |
+
FINAL_EVALUATION = load_static_content(FINAL_EVALUATION_FILE)
|
| 71 |
+
SYSTEM_INSTRUCTION = load_static_content(SYSTEM_INSTRUCTION_FILE)
|
| 72 |
+
SYSTEM_INSTRUECTION_PREFERENCE_ELICITATION = load_static_content(SYSTEM_INSTRUECTION_PREFERENCE_ELICITATION_FILE)
|
| 73 |
+
SUMMARIZATION_PROMPT = load_static_content(SUMMARIZATION_PROMPT_FILE)
|
| 74 |
+
|
| 75 |
+
# Other constants
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
FIRST_MESSAGE = "Hey"
|
| 77 |
INFORMATION_SEEKING = True
|
| 78 |
USER_PREFERENCE_SUMMARY = True
|
| 79 |
DEBUG = False
|
| 80 |
+
API_TYPE = "debug"
|
| 81 |
+
assert API_TYPE in ["together", "local", "debug"], "The API should be either 'together' or 'local'"
|
| 82 |
if API_TYPE == "together":
|
| 83 |
+
TOGETHER_CLIENT = Together(api_key=os.getenv("TOGETHER_API_KEY"))
|
| 84 |
+
|
| 85 |
+
SESSION_DEBUG = True
|
| 86 |
|
| 87 |
|
| 88 |
+
def get_context_list(synthetic_data_path):
|
| 89 |
# Load data from the synthetic data file
|
| 90 |
with open(synthetic_data_path, "r") as f:
|
| 91 |
data = [json.loads(line) for line in f]
|
|
|
|
| 133 |
return USER_NARRATIVE.replace("{user_narrative}", user_narrative).replace("\n", "<br>")
|
| 134 |
|
| 135 |
|
| 136 |
+
def get_user_narrative_from_raw(raw_narrative):
|
| 137 |
+
return get_user_narrative_html(markdown.markdown(raw_narrative.replace("\n", "<br>")))
|
| 138 |
+
|
| 139 |
+
|
| 140 |
def get_task_instruction_for_user(context):
|
| 141 |
ticker_name = context["short_name"]
|
| 142 |
user_narrative = context["user_narrative"]
|
|
|
|
| 182 |
return html_system_instruction
|
| 183 |
|
| 184 |
|
| 185 |
+
def log_action(user_id, tab_name, action, details):
|
| 186 |
"""
|
| 187 |
Log actions for each tab (stock).
|
| 188 |
"""
|
| 189 |
+
log_file_dir = os.path.join(LOG_DIR, f"{user_id}")
|
| 190 |
+
if os.path.exists(log_file_dir) is False:
|
| 191 |
+
os.makedirs(log_file_dir)
|
| 192 |
+
log_file = os.path.join(log_file_dir, f"{tab_name}.txt")
|
| 193 |
+
print(log_file)
|
| 194 |
with open(log_file, "a") as f:
|
| 195 |
f.write(f"Action: {action} | Details: {details}\n")
|
| 196 |
|
| 197 |
|
| 198 |
+
def add_user_profile_to_system_instruction(
|
| 199 |
+
user_id, system_instruction, user_preference_elicitation_data, summary, terminator
|
| 200 |
+
):
|
| 201 |
if summary:
|
| 202 |
if user_preference_elicitation_data["summary_history"] == "":
|
| 203 |
# Format prompt
|
|
|
|
| 207 |
summarization_instruction = [{"role": "system", "content": summarization_prompt}]
|
| 208 |
if API_TYPE == "local":
|
| 209 |
summ, _ = generate_response_local_api(summarization_instruction, terminator, 512, API_URL)
|
| 210 |
+
elif API_TYPE == "together":
|
| 211 |
summ, _ = generate_response_together_api(summarization_instruction, 512, TOGETHER_CLIENT)
|
| 212 |
+
else:
|
| 213 |
+
summ, _ = generate_response_debugging(summarization_instruction)
|
| 214 |
user_preference_elicitation_data["summary_history"] = summ
|
| 215 |
+
log_action(user_id, "Prompt", "Preference Elicitation Summarization", summ)
|
| 216 |
print(f"Preference Summary:{summ}")
|
| 217 |
system_instruction += f"\nPrevious Conversations with the Customer about the User Profile: {user_preference_elicitation_data['summary_history']}\n"
|
| 218 |
else:
|
|
|
|
| 220 |
return system_instruction
|
| 221 |
|
| 222 |
|
| 223 |
+
def create_demo():
|
| 224 |
+
global personality_prompts, context_info_list, terminator
|
| 225 |
+
|
| 226 |
+
def tab_creation_exploration_stage(order, comp, context):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 227 |
english_order = ["1", "2", "3", "4", "5"]
|
|
|
|
| 228 |
with gr.Tab(f"{english_order[order]}-1:Discuss"):
|
| 229 |
+
general_instruction = gr.HTML(label="General Instruction")
|
| 230 |
with gr.Row():
|
| 231 |
with gr.Column():
|
| 232 |
with gr.Row():
|
| 233 |
+
round_instruction = gr.HTML(label="Round Instruction")
|
|
|
|
|
|
|
|
|
|
| 234 |
with gr.Column():
|
| 235 |
with gr.Row():
|
| 236 |
chatbot = gr.Chatbot(height=600)
|
|
|
|
| 243 |
continue_button = gr.Button(value="Show More of the Advisor’s Answer", interactive=False)
|
| 244 |
with gr.Row():
|
| 245 |
clear = gr.ClearButton([msg, chatbot])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 246 |
with gr.Tab(f"{english_order[order]}-2:Eval"):
|
| 247 |
with gr.Row():
|
| 248 |
gr.HTML(value=EVALUATION_INSTRUCTION)
|
|
|
|
| 294 |
evaluation_send_button = gr.Button(value="Send: Evaluation")
|
| 295 |
return {
|
| 296 |
"comp": comp,
|
| 297 |
+
"system_instruction_context": context,
|
| 298 |
"start_conversation": start_conversation,
|
| 299 |
"msg_button": msg_button,
|
| 300 |
"continue_button": continue_button,
|
|
|
|
| 307 |
"knowledgeable": knowledgeable,
|
| 308 |
"helpful": helpful,
|
| 309 |
"evaluation_send_button": evaluation_send_button,
|
| 310 |
+
"general_instruction": general_instruction,
|
| 311 |
+
"round_instruction": round_instruction,
|
| 312 |
}
|
| 313 |
|
| 314 |
def tab_creation_preference_stage():
|
|
|
|
| 316 |
gr.HTML(value=PREFERENCE_ELICITATION_TASK, label="Preference Elicitation Task")
|
| 317 |
with gr.Row():
|
| 318 |
with gr.Column():
|
| 319 |
+
user_narrative = gr.HTML(label="User Narrative")
|
|
|
|
| 320 |
with gr.Column():
|
| 321 |
with gr.Row():
|
| 322 |
elicitation_chatbot = gr.Chatbot(height=600)
|
|
|
|
| 333 |
"continue_button": continue_button,
|
| 334 |
"msg": msg,
|
| 335 |
"elicitation_chatbot": elicitation_chatbot,
|
| 336 |
+
"user_narrative": user_narrative,
|
| 337 |
}
|
| 338 |
|
| 339 |
+
def tab_final_evaluation():
|
| 340 |
with gr.Row():
|
| 341 |
gr.HTML(value=FINAL_EVALUATION)
|
| 342 |
with gr.Row():
|
| 343 |
+
ranking_first_comp = gr.Dropdown(choices=[1, 2, 3, 4, 5])
|
| 344 |
+
ranking_second_comp = gr.Dropdown(choices=[1, 2, 3, 4, 5])
|
| 345 |
+
ranking_third_comp = gr.Dropdown(choices=[1, 2, 3, 4, 5])
|
| 346 |
+
ranking_fourth_comp = gr.Dropdown(choices=[1, 2, 3, 4, 5])
|
| 347 |
+
ranking_fifth_comp = gr.Dropdown(choices=[1, 2, 3, 4, 5])
|
| 348 |
with gr.Row():
|
| 349 |
textbox = gr.HTML(
|
| 350 |
"""<div style="background-color: #f8d7da; color: #721c24; padding: 15px; border: 1px solid #f5c6cb; border-radius: 5px; margin-bottom: 20px;">
|
|
|
|
| 355 |
)
|
| 356 |
submit_ranking = gr.Button(value="Submit Ranking")
|
| 357 |
return {
|
| 358 |
+
"first": ranking_first_comp,
|
| 359 |
+
"second": ranking_second_comp,
|
| 360 |
+
"third": ranking_third_comp,
|
| 361 |
+
"fourth": ranking_fourth_comp,
|
| 362 |
+
"fifth": ranking_fifth_comp,
|
| 363 |
"submit_ranking": submit_ranking,
|
| 364 |
"text_box": textbox,
|
| 365 |
}
|
| 366 |
|
| 367 |
+
def click_control_exploration_stage(
|
| 368 |
+
tabs, user_id, tab_session, user_preference_elicitation_session, system_description_without_context
|
| 369 |
+
):
|
| 370 |
(
|
| 371 |
comp,
|
| 372 |
+
system_instruction_context,
|
| 373 |
start_conversation,
|
| 374 |
msg_button,
|
| 375 |
continue_button,
|
|
|
|
| 384 |
evaluation_send_button,
|
| 385 |
) = (
|
| 386 |
tabs["comp"],
|
| 387 |
+
tabs["system_instruction_context"],
|
| 388 |
tabs["start_conversation"],
|
| 389 |
tabs["msg_button"],
|
| 390 |
tabs["continue_button"],
|
|
|
|
| 398 |
tabs["helpful"],
|
| 399 |
tabs["evaluation_send_button"],
|
| 400 |
)
|
| 401 |
+
system_instruction = ""
|
| 402 |
start_conversation.click(
|
| 403 |
+
lambda user_id, tab_session, history, comp, user_preference_elicitation_session, system_description_without_context, system_instruction_context: respond_start_conversation(
|
| 404 |
+
user_id,
|
| 405 |
+
tab_session,
|
| 406 |
+
history,
|
| 407 |
+
system_instruction,
|
| 408 |
+
comp,
|
| 409 |
+
user_preference_elicitation_data=user_preference_elicitation_session,
|
| 410 |
+
system_description_without_context=system_description_without_context,
|
| 411 |
+
system_instruction_context=system_instruction_context,
|
| 412 |
+
),
|
| 413 |
+
[
|
| 414 |
+
user_id,
|
| 415 |
+
tab_session,
|
| 416 |
+
chatbot,
|
| 417 |
+
comp,
|
| 418 |
+
user_preference_elicitation_session,
|
| 419 |
+
system_description_without_context,
|
| 420 |
+
system_instruction_context,
|
| 421 |
+
],
|
| 422 |
+
[tab_session, chatbot, start_conversation, msg_button, continue_button],
|
| 423 |
)
|
| 424 |
msg_button.click(
|
| 425 |
+
lambda user_id, tab_session, message, history, comp, user_preference_elicitation_session, system_description_without_context, system_instruction_context: respond(
|
| 426 |
+
user_id,
|
| 427 |
+
tab_session,
|
| 428 |
+
message,
|
| 429 |
+
tab_session["history"],
|
| 430 |
+
system_instruction,
|
| 431 |
+
comp,
|
| 432 |
+
user_preference_elicitation_data=user_preference_elicitation_session,
|
| 433 |
+
system_description_without_context=system_description_without_context,
|
| 434 |
+
system_instruction_context=system_instruction_context,
|
| 435 |
+
),
|
| 436 |
+
[
|
| 437 |
+
user_id,
|
| 438 |
+
tab_session,
|
| 439 |
+
msg,
|
| 440 |
+
chatbot,
|
| 441 |
+
comp,
|
| 442 |
+
user_preference_elicitation_session,
|
| 443 |
+
system_description_without_context,
|
| 444 |
+
system_instruction_context,
|
| 445 |
+
],
|
| 446 |
+
[tab_session, msg, chatbot],
|
| 447 |
)
|
| 448 |
continue_button.click(
|
| 449 |
+
lambda user_id, tab_session, history, comp, user_preference_elicitation_session, system_description_without_context, system_instruction_context: respond_continue(
|
| 450 |
+
user_id,
|
| 451 |
+
tab_session,
|
| 452 |
+
tab_session["history"],
|
| 453 |
+
system_instruction,
|
| 454 |
+
comp,
|
| 455 |
+
user_preference_elicitation_data=user_preference_elicitation_session,
|
| 456 |
+
system_description_without_context=system_description_without_context,
|
| 457 |
+
system_instruction_context=system_instruction_context,
|
| 458 |
+
),
|
| 459 |
+
[
|
| 460 |
+
user_id,
|
| 461 |
+
tab_session,
|
| 462 |
+
chatbot,
|
| 463 |
+
comp,
|
| 464 |
+
user_preference_elicitation_session,
|
| 465 |
+
system_description_without_context,
|
| 466 |
+
system_instruction_context,
|
| 467 |
+
],
|
| 468 |
+
[tab_session, chatbot],
|
| 469 |
)
|
| 470 |
evaluation_send_button.click(
|
| 471 |
+
lambda user_id, comp, tab_session, dropdown, reason, trust, satisfaction, knowledgeable, helpful: respond_evaluation(
|
| 472 |
+
user_id,
|
| 473 |
+
tab_session,
|
| 474 |
{
|
| 475 |
"selection": dropdown,
|
| 476 |
"reason": reason,
|
|
|
|
| 481 |
},
|
| 482 |
comp,
|
| 483 |
),
|
| 484 |
+
[user_id, comp, tab_session, dropdown, reason, trust, satisfaction, knowledgeable, helpful],
|
| 485 |
+
[tab_session, dropdown, reason, trust, satisfaction, knowledgeable, helpful],
|
| 486 |
)
|
| 487 |
|
| 488 |
+
def click_control_preference_stage(tabs, user_id, user_preference_elicitation_session):
|
| 489 |
(
|
| 490 |
start_conversation,
|
| 491 |
msg_button,
|
|
|
|
| 499 |
tabs["elicitation_chatbot"],
|
| 500 |
tabs["msg"],
|
| 501 |
)
|
| 502 |
+
# nonlocal user_id
|
| 503 |
start_conversation.click(
|
| 504 |
+
lambda user_id, user_preference_elicitation_data, history: respond_start_conversation(
|
| 505 |
+
user_id,
|
| 506 |
+
user_preference_elicitation_data,
|
| 507 |
+
history,
|
| 508 |
+
SYSTEM_INSTRUECTION_PREFERENCE_ELICITATION,
|
| 509 |
+
user_elicitation=True,
|
| 510 |
),
|
| 511 |
+
[user_id, user_preference_elicitation_session, elicitation_chatbot],
|
| 512 |
+
[user_preference_elicitation_session, elicitation_chatbot, start_conversation, msg_button, continue_button],
|
| 513 |
)
|
| 514 |
msg_button.click(
|
| 515 |
+
lambda user_id, tab_data, message, history: respond(
|
| 516 |
+
user_id,
|
| 517 |
+
tab_data,
|
| 518 |
message,
|
| 519 |
+
tab_data["history"],
|
| 520 |
SYSTEM_INSTRUECTION_PREFERENCE_ELICITATION,
|
| 521 |
user_elicitation=True,
|
| 522 |
),
|
| 523 |
+
[user_id, user_preference_elicitation_session, msg, elicitation_chatbot],
|
| 524 |
+
[user_preference_elicitation_session, msg, elicitation_chatbot],
|
| 525 |
)
|
| 526 |
continue_button.click(
|
| 527 |
+
lambda user_id, tab_data, history: respond_continue(
|
| 528 |
+
user_id,
|
| 529 |
+
tab_data,
|
| 530 |
+
tab_data["history"],
|
| 531 |
SYSTEM_INSTRUECTION_PREFERENCE_ELICITATION,
|
| 532 |
user_elicitation=True,
|
| 533 |
),
|
| 534 |
+
[user_id, user_preference_elicitation_session, elicitation_chatbot],
|
| 535 |
+
[user_preference_elicitation_session, elicitation_chatbot],
|
| 536 |
)
|
| 537 |
|
| 538 |
+
def click_control_final_evaluation(tabs, user_id, first_comp, second_comp, third_comp, fourth_comp, fifth_comp):
|
| 539 |
+
ranking_first_comp, ranking_second_comp, ranking_third_comp, ranking_fourth_comp, ranking_fifth_comp = (
|
| 540 |
+
tabs["first"],
|
| 541 |
+
tabs["second"],
|
| 542 |
+
tabs["third"],
|
| 543 |
+
tabs["fourth"],
|
| 544 |
+
tabs["fifth"],
|
| 545 |
+
)
|
| 546 |
result_textbox = tabs["text_box"]
|
| 547 |
submit_ranking = tabs["submit_ranking"]
|
| 548 |
submit_ranking.click(
|
| 549 |
+
lambda user_id, ranking_first_comp, first_comp, ranking_second_comp, second_comp, ranking_third_comp, third_comp, ranking_fourth_comp, fourth_comp, ranking_fifth_comp, fifth_comp: respond_final_ranking(
|
| 550 |
+
user_id,
|
| 551 |
first_comp,
|
| 552 |
ranking_first_comp,
|
| 553 |
second_comp,
|
|
|
|
| 561 |
),
|
| 562 |
# Input components (names and rankings)
|
| 563 |
[
|
| 564 |
+
user_id,
|
| 565 |
ranking_first_comp,
|
| 566 |
+
first_comp,
|
| 567 |
ranking_second_comp,
|
| 568 |
+
second_comp,
|
| 569 |
ranking_third_comp,
|
| 570 |
+
third_comp,
|
| 571 |
ranking_fourth_comp,
|
| 572 |
+
fourth_comp,
|
| 573 |
ranking_fifth_comp,
|
| 574 |
+
fifth_comp,
|
| 575 |
],
|
| 576 |
# Output component(s) where you want the result to appear, e.g., result_textbox
|
| 577 |
[result_textbox],
|
| 578 |
)
|
| 579 |
|
| 580 |
+
def respond(
|
| 581 |
+
user_id,
|
| 582 |
+
tab_data,
|
| 583 |
+
message,
|
| 584 |
+
history,
|
| 585 |
+
system_instruction,
|
| 586 |
+
tab_name=None,
|
| 587 |
+
user_elicitation=False,
|
| 588 |
+
user_preference_elicitation_data=None,
|
| 589 |
+
system_description_without_context=None,
|
| 590 |
+
system_instruction_context=None,
|
| 591 |
+
):
|
| 592 |
"""
|
| 593 |
Return:
|
| 594 |
msg
|
|
|
|
| 601 |
tab_name is not None or user_elicitation is True
|
| 602 |
), "Tab name is required for the start of the conversation unless it is not preference elicitation."
|
| 603 |
# Add user profile to system instruction
|
| 604 |
+
if system_description_without_context is not None and system_instruction_context is not None:
|
| 605 |
+
system_instruction = system_description_without_context + "\n" + system_instruction_context
|
| 606 |
if not user_elicitation:
|
| 607 |
system_instruction = add_user_profile_to_system_instruction(
|
| 608 |
+
user_id,
|
| 609 |
system_instruction,
|
| 610 |
user_preference_elicitation_data,
|
| 611 |
summary=USER_PREFERENCE_SUMMARY,
|
| 612 |
terminator=terminator,
|
| 613 |
)
|
|
|
|
| 614 |
# Formatting Input
|
| 615 |
print(f"User Message: {message} in Tab: {tab_name}")
|
| 616 |
# From string to list [{"role":"user", "content": message}, ...]
|
| 617 |
history = gradio_to_huggingface_message(history)
|
| 618 |
# We can implement context window here as we need all the system interaction. We can cut some of the early interactions if needed.
|
| 619 |
history = conversation_window(history, CONV_WINDOW)
|
|
|
|
|
|
|
| 620 |
# Add system instruction to the history
|
| 621 |
history = format_context(system_instruction, history)
|
| 622 |
# Add user message to the history
|
|
|
|
| 624 |
# Call API instead of locally handle it
|
| 625 |
if API_TYPE == "local":
|
| 626 |
outputs_text, history = generate_response_local_api(history_with_user_utterance, terminator, 128, API_URL)
|
| 627 |
+
elif API_TYPE == "together":
|
| 628 |
outputs_text, history = generate_response_together_api(history_with_user_utterance, 128, TOGETHER_CLIENT)
|
| 629 |
+
else:
|
| 630 |
+
outputs_text, history = generate_response_debugging(history_with_user_utterance)
|
| 631 |
# exclude system interaction and store the others in the history
|
| 632 |
history = huggingface_to_gradio_message(history)
|
| 633 |
if tab_name is not None:
|
| 634 |
print(f"Tab: {tab_name}\nSystem Output: {outputs_text}")
|
| 635 |
|
| 636 |
# Log the user message and response
|
| 637 |
+
log_action(user_id, tab_name, "User Message", message)
|
| 638 |
+
log_action(user_id, tab_name, "Response", outputs_text)
|
| 639 |
# Store the updated history for this tab
|
| 640 |
+
tab_data["history"] = history
|
| 641 |
if user_elicitation:
|
| 642 |
print(f"User Elicitation\nSystem Output: {outputs_text}")
|
| 643 |
+
log_action(user_id, "User_Elicitation", "User Message", message)
|
| 644 |
+
log_action(user_id, "User_Elicitation", "Response", outputs_text)
|
| 645 |
+
tab_data["history"] = history
|
| 646 |
+
if SESSION_DEBUG:
|
| 647 |
+
log_action(user_id, "Session", "History", history)
|
| 648 |
+
|
| 649 |
+
return tab_data, "", history
|
| 650 |
+
|
| 651 |
+
def respond_start_conversation(
|
| 652 |
+
user_id,
|
| 653 |
+
tab_data,
|
| 654 |
+
history,
|
| 655 |
+
system_instruction,
|
| 656 |
+
tab_name=None,
|
| 657 |
+
user_elicitation=False,
|
| 658 |
+
user_preference_elicitation_data=None,
|
| 659 |
+
system_description_without_context=None,
|
| 660 |
+
system_instruction_context=None,
|
| 661 |
+
):
|
| 662 |
assert (
|
| 663 |
tab_name is not None or user_elicitation is True
|
| 664 |
), "Tab name is required for the start of the conversation unless it is not preference elicitation."
|
| 665 |
+
if system_description_without_context is not None and system_instruction_context is not None:
|
| 666 |
+
system_instruction = system_description_without_context + "\n" + system_instruction_context
|
| 667 |
if not user_elicitation:
|
| 668 |
+
print(f"User Preference Elicitation Data: {user_preference_elicitation_data}")
|
| 669 |
+
print(f"Tab data: {tab_data}")
|
| 670 |
system_instruction = add_user_profile_to_system_instruction(
|
| 671 |
+
user_id,
|
| 672 |
system_instruction,
|
| 673 |
user_preference_elicitation_data,
|
| 674 |
summary=USER_PREFERENCE_SUMMARY,
|
|
|
|
| 681 |
history_with_user_utterance = format_user_message(first_message, history)
|
| 682 |
if API_TYPE == "local":
|
| 683 |
outputs_text, history = generate_response_local_api(history_with_user_utterance, terminator, 128, API_URL)
|
| 684 |
+
elif API_TYPE == "together":
|
| 685 |
outputs_text, history = generate_response_together_api(history_with_user_utterance, 128, TOGETHER_CLIENT)
|
| 686 |
+
else:
|
| 687 |
+
outputs_text, history = generate_response_debugging(history_with_user_utterance)
|
| 688 |
# Format
|
| 689 |
history = huggingface_to_gradio_message(history)
|
| 690 |
if tab_name is not None:
|
| 691 |
print(f"Tab: {tab_name}\nHistory: {history}")
|
|
|
|
| 692 |
# Log the user message and response
|
| 693 |
+
log_action(user_id, tab_name, "User Message", first_message)
|
| 694 |
+
log_action(user_id, tab_name, "Response", outputs_text)
|
| 695 |
# Store the updated history for this tab
|
| 696 |
+
tab_data["history"] = history
|
| 697 |
if user_elicitation:
|
| 698 |
print(f"User Elicitation\nHistory: {history}")
|
| 699 |
+
log_action(user_id, "User_Elicitation", "User Message", first_message)
|
| 700 |
+
log_action(user_id, "User_Elicitation", "Response", outputs_text)
|
| 701 |
+
tab_data["history"] = history
|
| 702 |
+
if SESSION_DEBUG:
|
| 703 |
+
log_action(user_id, "Session", "History", history)
|
| 704 |
return (
|
| 705 |
+
tab_data,
|
| 706 |
history,
|
| 707 |
gr.Button(value="Start Conversation", interactive=False),
|
| 708 |
gr.Button(value="Send This Message to Advisor", interactive=True),
|
| 709 |
gr.Button(value="Show More of the Advisor’s Answer", interactive=True),
|
| 710 |
)
|
| 711 |
|
| 712 |
+
def respond_continue(
|
| 713 |
+
user_id,
|
| 714 |
+
tab_data,
|
| 715 |
+
history,
|
| 716 |
+
system_instruction,
|
| 717 |
+
tab_name=None,
|
| 718 |
+
user_elicitation=False,
|
| 719 |
+
user_preference_elicitation_data=None,
|
| 720 |
+
system_description_without_context=None,
|
| 721 |
+
system_instruction_context=None,
|
| 722 |
+
):
|
| 723 |
assert (
|
| 724 |
tab_name is not None or user_elicitation is True
|
| 725 |
), "Tab name is required for the start of the conversation."
|
| 726 |
# Add user profile to system instruction
|
| 727 |
+
if system_description_without_context is not None and system_instruction_context is not None:
|
| 728 |
+
system_instruction = system_description_without_context + "\n" + system_instruction_context
|
| 729 |
if not user_elicitation:
|
| 730 |
system_instruction = add_user_profile_to_system_instruction(
|
| 731 |
+
user_id,
|
| 732 |
system_instruction,
|
| 733 |
user_preference_elicitation_data,
|
| 734 |
summary=USER_PREFERENCE_SUMMARY,
|
| 735 |
terminator=terminator,
|
| 736 |
)
|
|
|
|
| 737 |
message = "continue"
|
| 738 |
history = gradio_to_huggingface_message(history)
|
| 739 |
history = conversation_window(history, CONV_WINDOW)
|
|
|
|
| 741 |
history_with_user_utterance = format_user_message(message, history)
|
| 742 |
if API_TYPE == "local":
|
| 743 |
outputs_text, history = generate_response_local_api(history_with_user_utterance, terminator, 128, API_URL)
|
| 744 |
+
elif API_TYPE == "together":
|
| 745 |
outputs_text, history = generate_response_together_api(history_with_user_utterance, 128, TOGETHER_CLIENT)
|
| 746 |
+
else:
|
| 747 |
+
outputs_text, history = generate_response_debugging(history_with_user_utterance)
|
| 748 |
history = huggingface_to_gradio_message(history)
|
| 749 |
if tab_name is not None:
|
| 750 |
+
log_action(user_id, tab_name, "Show More of the Advisor’s Answer", "User continued the conversation")
|
| 751 |
+
log_action(user_id, tab_name, "Response", outputs_text)
|
| 752 |
|
| 753 |
# Update history for this tab
|
| 754 |
+
tab_data["history"] = history
|
| 755 |
if user_elicitation:
|
| 756 |
print(f"User Elicitation\nSystem Output: {outputs_text}")
|
| 757 |
+
log_action(user_id, "User_Elicitation", "Response", outputs_text)
|
| 758 |
+
tab_data["history"] = history
|
| 759 |
+
if SESSION_DEBUG:
|
| 760 |
+
log_action(user_id, "Session", "History", history)
|
| 761 |
+
return tab_data, history
|
| 762 |
|
| 763 |
+
def respond_evaluation(user_id, tab_data, evals, tab_name):
|
|
|
|
|
|
|
| 764 |
|
| 765 |
# dropdown, readon_button, multi-evaluator
|
| 766 |
+
log_action(user_id, tab_name, "Round Evaluation", "Following")
|
| 767 |
for key, value in evals.items():
|
| 768 |
+
log_action(user_id, tab_name, key, value)
|
| 769 |
# Store the reason for this tab
|
| 770 |
+
tab_data["multi_evaluator"] = evals
|
| 771 |
return (
|
| 772 |
+
tab_data,
|
| 773 |
evals["selection"],
|
| 774 |
evals["reason"],
|
| 775 |
evals["trust"],
|
|
|
|
| 779 |
)
|
| 780 |
|
| 781 |
def respond_final_ranking(
|
| 782 |
+
user_id,
|
| 783 |
first_comp,
|
| 784 |
ranking_first_comp,
|
| 785 |
second_comp,
|
|
|
|
| 804 |
<strong>Please make sure that you are not ranking the same stock multiple times.</strong>
|
| 805 |
</div>"""
|
| 806 |
else:
|
| 807 |
+
log_action(user_id, "Final_Ranking", first_comp, ranking_first_comp)
|
| 808 |
+
log_action(user_id, "Final_Ranking", second_comp, ranking_second_comp)
|
| 809 |
+
log_action(user_id, "Final_Ranking", third_comp, ranking_third_comp)
|
| 810 |
+
log_action(user_id, "Final_Ranking", fourth_comp, ranking_fourth_comp)
|
| 811 |
+
log_action(user_id, "Final_Ranking", fifth_comp, ranking_fifth_comp)
|
| 812 |
return """<div style="background-color: #d4edda; color: #155724; padding: 15px; border: 1px solid #c3e6cb; border-radius: 5px; margin-bottom: 20px;">
|
| 813 |
<strong>Thank you for participating in the experiment. This concludes the session. You may now close the tab.</strong>
|
| 814 |
</div>"""
|
| 815 |
|
| 816 |
+
def get_context(index, raw_context_list, stock_context_list):
|
| 817 |
comp = raw_context_list[index]["short_name"]
|
| 818 |
context = stock_context_list[index]
|
| 819 |
general_instruction, round_instruction = get_task_instruction_for_user(raw_context_list[index])
|
| 820 |
return comp, context, general_instruction, round_instruction
|
| 821 |
|
| 822 |
+
def set_user_id(request: gr.Request):
|
| 823 |
+
user_id = request.username
|
| 824 |
+
narrative_id = user_id.split("_")[-2]
|
| 825 |
+
personality_id = user_id.split("_")[-1]
|
| 826 |
+
print(f"User ID: {user_id}, Narrative ID: {narrative_id}, Personality ID: {personality_id}")
|
| 827 |
+
return user_id, narrative_id, personality_id
|
| 828 |
+
|
| 829 |
+
def get_inst_without_context(personality_id):
|
| 830 |
+
return SYSTEM_INSTRUCTION + "\n" + personality_prompts[int(personality_id)]
|
| 831 |
+
|
| 832 |
+
def get_stock_related_context(narrative_id):
|
| 833 |
+
raw_context_list = build_raw_context_list(context_info_list[int(narrative_id)])
|
| 834 |
+
stock_context_list = build_context(context_info_list[int(narrative_id)])
|
| 835 |
+
return raw_context_list, stock_context_list
|
| 836 |
+
|
| 837 |
+
def set_initial_values(request: gr.Request):
|
| 838 |
+
# Set user specific information (Session State)
|
| 839 |
+
user_id, narrative_id, personality_id = set_user_id(request)
|
| 840 |
+
# System instruction without prompt
|
| 841 |
+
system_description_without_context = get_inst_without_context(personality_id)
|
| 842 |
+
# Stock related context
|
| 843 |
+
raw_context_list, stock_context_list = get_stock_related_context(narrative_id)
|
| 844 |
+
# User Narrative
|
| 845 |
+
user_narrative = get_user_narrative_from_raw(raw_context_list[0]["user_narrative"])
|
| 846 |
+
# Tab Context
|
| 847 |
+
first_comp, first_context, first_general_instruction, first_round_instruction = get_context(
|
| 848 |
+
0, raw_context_list, stock_context_list
|
| 849 |
+
)
|
| 850 |
+
second_comp, second_context, second_general_instruction, second_round_instruction = get_context(
|
| 851 |
+
1, raw_context_list, stock_context_list
|
| 852 |
+
)
|
| 853 |
+
third_comp, third_context, third_general_instruction, third_round_instruction = get_context(
|
| 854 |
+
2, raw_context_list, stock_context_list
|
| 855 |
+
)
|
| 856 |
+
fourth_comp, fourth_context, fourth_general_instruction, fourth_round_instruction = get_context(
|
| 857 |
+
3, raw_context_list, stock_context_list
|
| 858 |
+
)
|
| 859 |
+
fifth_comp, fifth_context, fifth_general_instruction, fifth_round_instruction = get_context(
|
| 860 |
+
4, raw_context_list, stock_context_list
|
| 861 |
+
)
|
| 862 |
+
# Final Evaluation
|
| 863 |
+
ranking_first_comp = gr.Dropdown(choices=[1, 2, 3, 4, 5], label=first_comp)
|
| 864 |
+
ranking_second_comp = gr.Dropdown(choices=[1, 2, 3, 4, 5], label=second_comp)
|
| 865 |
+
ranking_third_comp = gr.Dropdown(choices=[1, 2, 3, 4, 5], label=third_comp)
|
| 866 |
+
ranking_fourth_comp = gr.Dropdown(choices=[1, 2, 3, 4, 5], label=fourth_comp)
|
| 867 |
+
ranking_fifth_comp = gr.Dropdown(choices=[1, 2, 3, 4, 5], label=fifth_comp)
|
| 868 |
+
return (
|
| 869 |
+
user_id,
|
| 870 |
+
narrative_id,
|
| 871 |
+
personality_id,
|
| 872 |
+
system_description_without_context,
|
| 873 |
+
raw_context_list,
|
| 874 |
+
stock_context_list,
|
| 875 |
+
user_narrative,
|
| 876 |
+
first_comp,
|
| 877 |
+
first_context,
|
| 878 |
+
first_general_instruction,
|
| 879 |
+
first_round_instruction,
|
| 880 |
+
second_comp,
|
| 881 |
+
second_context,
|
| 882 |
+
second_general_instruction,
|
| 883 |
+
second_round_instruction,
|
| 884 |
+
third_comp,
|
| 885 |
+
third_context,
|
| 886 |
+
third_general_instruction,
|
| 887 |
+
third_round_instruction,
|
| 888 |
+
fourth_comp,
|
| 889 |
+
fourth_context,
|
| 890 |
+
fourth_general_instruction,
|
| 891 |
+
fourth_round_instruction,
|
| 892 |
+
fifth_comp,
|
| 893 |
+
fifth_context,
|
| 894 |
+
fifth_general_instruction,
|
| 895 |
+
fifth_round_instruction,
|
| 896 |
+
ranking_first_comp,
|
| 897 |
+
ranking_second_comp,
|
| 898 |
+
ranking_third_comp,
|
| 899 |
+
ranking_fourth_comp,
|
| 900 |
+
ranking_fifth_comp,
|
| 901 |
+
)
|
| 902 |
|
| 903 |
+
with gr.Blocks(title="RAG Chatbot Q&A", theme="Soft") as demo:
|
| 904 |
+
# Set user specific information (Session State)
|
| 905 |
+
user_id = gr.State()
|
| 906 |
+
narrative_id = gr.State()
|
| 907 |
+
personality_id = gr.State()
|
| 908 |
+
system_description_without_context = gr.State()
|
| 909 |
+
# Context data
|
| 910 |
+
raw_context_list = gr.State()
|
| 911 |
+
stock_context_list = gr.State()
|
| 912 |
+
first_comp = gr.State()
|
| 913 |
+
first_context = gr.State()
|
| 914 |
+
second_comp = gr.State()
|
| 915 |
+
second_context = gr.State()
|
| 916 |
+
third_comp = gr.State()
|
| 917 |
+
third_context = gr.State()
|
| 918 |
+
fourth_comp = gr.State()
|
| 919 |
+
fourth_context = gr.State()
|
| 920 |
+
fifth_comp = gr.State()
|
| 921 |
+
fifth_context = gr.State()
|
| 922 |
+
# Tab data
|
| 923 |
+
if DEBUG:
|
| 924 |
+
user_preference_elicitation_session = gr.State(
|
| 925 |
+
value={
|
| 926 |
+
"history": "",
|
| 927 |
+
"summary_history": """Previous Conversations with the Customer about the User Profile: Based on our previous conversation, here's a summary of your investment preferences:
|
| 928 |
+
|
| 929 |
+
# 1. **Preferred Industries:** You're interested in investing in the healthcare sector, without a specific preference for sub-industries such as pharmaceuticals, medical devices, biotechnology, or healthcare services.
|
| 930 |
+
# 2. **Value vs. Growth Stocks:** You prefer growth stocks, which have the potential for high returns but may be riskier.
|
| 931 |
+
# 3. **Dividend vs. Non-Dividend Stocks:** You're open to both dividend and non-dividend growth stocks, focusing on reinvesting profits for future growth.
|
| 932 |
+
# 4. **Cyclical vs. Non-Cyclical Stocks:** You're interested in cyclical stocks, which are sensitive to economic fluctuations and tend to perform well during economic expansions.""",
|
| 933 |
+
}
|
| 934 |
+
)
|
| 935 |
+
else:
|
| 936 |
+
user_preference_elicitation_session = gr.State(value={"history": "", "summary_history": ""})
|
| 937 |
+
first_comp_session = gr.State(value={"history": [], "selection": "", "reason": ""})
|
| 938 |
+
second_comp_session = gr.State(value={"history": [], "selection": "", "reason": ""})
|
| 939 |
+
third_comp_session = gr.State(value={"history": [], "selection": "", "reason": ""})
|
| 940 |
+
fourth_comp_session = gr.State(value={"history": [], "selection": "", "reason": ""})
|
| 941 |
+
fifth_comp_session = gr.State(value={"history": [], "selection": "", "reason": ""})
|
| 942 |
# EXperiment Instruction
|
| 943 |
with gr.Tab("Experiment Instruction") as instruction_tab:
|
| 944 |
gr.HTML(value=INSTRUCTION_PAGE, label="Experiment Instruction")
|
| 945 |
# User Preference Elicitation Tab
|
| 946 |
with gr.Tab("Preference Elicitation Stage") as preference_elicitation_tab:
|
| 947 |
user_preference_elicitation_tab = tab_creation_preference_stage()
|
| 948 |
+
user_narrative = user_preference_elicitation_tab["user_narrative"]
|
| 949 |
+
click_control_preference_stage(
|
| 950 |
+
user_preference_elicitation_tab, user_id, user_preference_elicitation_session
|
| 951 |
+
)
|
| 952 |
+
with gr.Tab("Financial Decision Stage") as financial_decision:
|
| 953 |
# Experiment Tag
|
| 954 |
+
first_tab = tab_creation_exploration_stage(0, first_comp, first_context)
|
| 955 |
+
first_general_instruction, first_round_instruction = (
|
| 956 |
+
first_tab["general_instruction"],
|
| 957 |
+
first_tab["round_instruction"],
|
| 958 |
+
)
|
| 959 |
+
click_control_exploration_stage(
|
| 960 |
+
first_tab,
|
| 961 |
+
user_id,
|
| 962 |
+
first_comp_session,
|
| 963 |
+
user_preference_elicitation_session,
|
| 964 |
+
system_description_without_context,
|
| 965 |
+
)
|
| 966 |
+
second_tab = tab_creation_exploration_stage(1, second_comp, second_context)
|
| 967 |
+
second_general_instruction, second_round_instruction = (
|
| 968 |
+
second_tab["general_instruction"],
|
| 969 |
+
second_tab["round_instruction"],
|
| 970 |
+
)
|
| 971 |
+
click_control_exploration_stage(
|
| 972 |
+
second_tab,
|
| 973 |
+
user_id,
|
| 974 |
+
second_comp_session,
|
| 975 |
+
user_preference_elicitation_session,
|
| 976 |
+
system_description_without_context,
|
| 977 |
+
)
|
| 978 |
+
third_tab = tab_creation_exploration_stage(2, third_comp, third_context)
|
| 979 |
+
third_general_instruction, third_round_instruction = (
|
| 980 |
+
third_tab["general_instruction"],
|
| 981 |
+
third_tab["round_instruction"],
|
| 982 |
+
)
|
| 983 |
+
click_control_exploration_stage(
|
| 984 |
+
third_tab,
|
| 985 |
+
user_id,
|
| 986 |
+
third_comp_session,
|
| 987 |
+
user_preference_elicitation_session,
|
| 988 |
+
system_description_without_context,
|
| 989 |
+
)
|
| 990 |
+
fourth_tab = tab_creation_exploration_stage(3, fourth_comp, fourth_context)
|
| 991 |
+
fourth_general_instruction, fourth_round_instruction = (
|
| 992 |
+
fourth_tab["general_instruction"],
|
| 993 |
+
fourth_tab["round_instruction"],
|
| 994 |
+
)
|
| 995 |
+
click_control_exploration_stage(
|
| 996 |
+
fourth_tab,
|
| 997 |
+
user_id,
|
| 998 |
+
fourth_comp_session,
|
| 999 |
+
user_preference_elicitation_session,
|
| 1000 |
+
system_description_without_context,
|
| 1001 |
+
)
|
| 1002 |
+
fifth_tab = tab_creation_exploration_stage(4, fifth_comp, fifth_context)
|
| 1003 |
+
fifth_general_instruction, fifth_round_instruction = (
|
| 1004 |
+
fifth_tab["general_instruction"],
|
| 1005 |
+
fifth_tab["round_instruction"],
|
| 1006 |
+
)
|
| 1007 |
+
click_control_exploration_stage(
|
| 1008 |
+
fifth_tab,
|
| 1009 |
+
user_id,
|
| 1010 |
+
fifth_comp_session,
|
| 1011 |
+
user_preference_elicitation_session,
|
| 1012 |
+
system_description_without_context,
|
| 1013 |
+
)
|
| 1014 |
with gr.Tab("Final Evaluation Stage") as final_evaluation:
|
| 1015 |
+
final_evaluation_tab = tab_final_evaluation()
|
| 1016 |
+
ranking_first_comp, ranking_second_comp, ranking_third_comp, ranking_fourth_comp, ranking_fifth_comp = (
|
| 1017 |
+
final_evaluation_tab["first"],
|
| 1018 |
+
final_evaluation_tab["second"],
|
| 1019 |
+
final_evaluation_tab["third"],
|
| 1020 |
+
final_evaluation_tab["fourth"],
|
| 1021 |
+
final_evaluation_tab["fifth"],
|
| 1022 |
+
)
|
| 1023 |
+
click_control_final_evaluation(
|
| 1024 |
+
final_evaluation_tab, user_id, first_comp, second_comp, third_comp, fourth_comp, fifth_comp
|
| 1025 |
+
)
|
| 1026 |
|
| 1027 |
+
demo.load(
|
| 1028 |
+
set_initial_values,
|
| 1029 |
+
inputs=None,
|
| 1030 |
+
outputs=[
|
| 1031 |
+
user_id,
|
| 1032 |
+
narrative_id,
|
| 1033 |
+
personality_id,
|
| 1034 |
+
system_description_without_context,
|
| 1035 |
+
raw_context_list,
|
| 1036 |
+
stock_context_list,
|
| 1037 |
+
user_narrative,
|
| 1038 |
+
first_comp,
|
| 1039 |
+
first_context,
|
| 1040 |
+
first_general_instruction,
|
| 1041 |
+
first_round_instruction,
|
| 1042 |
+
second_comp,
|
| 1043 |
+
second_context,
|
| 1044 |
+
second_general_instruction,
|
| 1045 |
+
second_round_instruction,
|
| 1046 |
+
third_comp,
|
| 1047 |
+
third_context,
|
| 1048 |
+
third_general_instruction,
|
| 1049 |
+
third_round_instruction,
|
| 1050 |
+
fourth_comp,
|
| 1051 |
+
fourth_context,
|
| 1052 |
+
fourth_general_instruction,
|
| 1053 |
+
fourth_round_instruction,
|
| 1054 |
+
fifth_comp,
|
| 1055 |
+
fifth_context,
|
| 1056 |
+
fifth_general_instruction,
|
| 1057 |
+
fifth_round_instruction,
|
| 1058 |
+
ranking_first_comp,
|
| 1059 |
+
ranking_second_comp,
|
| 1060 |
+
ranking_third_comp,
|
| 1061 |
+
ranking_fourth_comp,
|
| 1062 |
+
ranking_fifth_comp,
|
| 1063 |
+
],
|
| 1064 |
+
)
|
| 1065 |
return demo
|
| 1066 |
|
| 1067 |
|
|
|
|
| 1079 |
"jpm_1",
|
| 1080 |
"jpm_2",
|
| 1081 |
]
|
| 1082 |
+
context_info_list = get_context_list(file_path) # str to List of Dict
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1083 |
# system instruction consist of Task, Personality, and Context
|
| 1084 |
"""
|
| 1085 |
Personality
|
|
|
|
| 1089 |
["neurotic", "emotionally stable"]
|
| 1090 |
["open to experience", "closed to experience"]]
|
| 1091 |
"""
|
| 1092 |
+
# Global variables
|
| 1093 |
+
personality = {
|
| 1094 |
+
1: [
|
| 1095 |
+
"extroverted",
|
| 1096 |
+
"agreeable",
|
| 1097 |
+
"conscientious",
|
| 1098 |
+
"emotionally stable",
|
| 1099 |
+
"open to experience",
|
| 1100 |
+
]
|
| 1101 |
+
}
|
| 1102 |
+
personality_prompts = {i: build_personality_prompt(p) for i, p in personality.items()}
|
|
|
|
|
|
|
| 1103 |
terminator = ["<eos>", "<unk>", "<sep>", "<pad>", "<cls>", "<mask>"]
|
| 1104 |
+
demo = create_demo()
|
| 1105 |
+
demo.launch(share=True, auth=[("user_1_1", "pw1"), ("user_2_1", "pw2"), ("user_3_1", "pw3"), ("user_4_1", "pw4")])
|
|
|