Spaces:
Sleeping
Sleeping
TTsamurai
commited on
Commit
·
fc80039
1
Parent(s):
5180b54
update for testing
Browse files- .gitignore +1 -1
- app.py +26 -33
.gitignore
CHANGED
|
@@ -1,5 +1,5 @@
|
|
| 1 |
log/
|
| 2 |
-
|
| 3 |
# Byte-compiled / optimized / DLL files
|
| 4 |
__pycache__/
|
| 5 |
*.py[cod]
|
|
|
|
| 1 |
log/
|
| 2 |
+
user_feedback/
|
| 3 |
# Byte-compiled / optimized / DLL files
|
| 4 |
__pycache__/
|
| 5 |
*.py[cod]
|
app.py
CHANGED
|
@@ -4,7 +4,8 @@ from datetime import datetime
|
|
| 4 |
import json
|
| 5 |
import uuid
|
| 6 |
from pathlib import Path
|
| 7 |
-
from huggingface_hub import CommitScheduler
|
|
|
|
| 8 |
import gradio as gr
|
| 9 |
import markdown
|
| 10 |
from together import Together
|
|
@@ -94,6 +95,7 @@ def save_feedback(user_id: str, uuid: str, type: str, value, feedback_file) -> N
|
|
| 94 |
|
| 95 |
# Load the required static content from files
|
| 96 |
def load_static_content(file_path):
|
|
|
|
| 97 |
with open(file_path, "r") as f:
|
| 98 |
return f.read()
|
| 99 |
|
|
@@ -121,13 +123,29 @@ PERSONALITY_INT = load_static_content(PERSONALITY_INT_FILE)
|
|
| 121 |
# Other constants
|
| 122 |
FIRST_MESSAGE = "Hey"
|
| 123 |
USER_PREFERENCE_SUMMARY = True
|
| 124 |
-
DEBUG =
|
| 125 |
API_TYPE = "together"
|
| 126 |
assert API_TYPE in ["together", "local", "debug"], "The API should be either 'together' or 'local'"
|
| 127 |
if API_TYPE == "together":
|
| 128 |
TOGETHER_CLIENT = Together(api_key=os.getenv("TOGETHER_API_KEY"))
|
| 129 |
|
| 130 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 131 |
def get_context_list(synthetic_data_path):
|
| 132 |
# Load data from the synthetic data file
|
| 133 |
with open(synthetic_data_path, "r") as f:
|
|
@@ -233,7 +251,6 @@ def log_action(user_id, tab_name, action, details):
|
|
| 233 |
if os.path.exists(log_file_dir) is False:
|
| 234 |
os.makedirs(log_file_dir)
|
| 235 |
log_file = os.path.join(log_file_dir, f"{tab_name}.txt")
|
| 236 |
-
print(log_file)
|
| 237 |
with open(log_file, "a") as f:
|
| 238 |
f.write(f"Action: {action} | Details: {details}\n")
|
| 239 |
|
|
@@ -242,8 +259,8 @@ def add_user_profile_to_system_instruction(
|
|
| 242 |
user_id, system_instruction, user_preference_elicitation_data, summary, terminator
|
| 243 |
):
|
| 244 |
exp_id = int(user_id.split("_")[-3])
|
| 245 |
-
# exp_id =
|
| 246 |
-
if exp_id ==
|
| 247 |
print("Non-personalizationかチェック:" + system_instruction)
|
| 248 |
return system_instruction
|
| 249 |
if summary:
|
|
@@ -268,7 +285,6 @@ def add_user_profile_to_system_instruction(
|
|
| 268 |
{"summarization": summ},
|
| 269 |
feedback_file_summarization,
|
| 270 |
)
|
| 271 |
-
# print(f"Preference Summary:{summ}")
|
| 272 |
system_instruction += f"\nUser Profile collected in the previous conversations: {user_preference_elicitation_data['summary_history']}\n"
|
| 273 |
else:
|
| 274 |
system_instruction += (
|
|
@@ -415,7 +431,7 @@ def create_demo():
|
|
| 415 |
providing_information = likert_evaluation("The advisor provides the financial knowledge needed")
|
| 416 |
with gr.Row():
|
| 417 |
textbox = gr.HTML()
|
| 418 |
-
submit_ranking = gr.Button(value="Submit
|
| 419 |
return {
|
| 420 |
"first": ranking_first_comp,
|
| 421 |
"second": ranking_second_comp,
|
|
@@ -722,8 +738,6 @@ def create_demo():
|
|
| 722 |
summary=USER_PREFERENCE_SUMMARY,
|
| 723 |
terminator=terminator,
|
| 724 |
)
|
| 725 |
-
# Formatting Input
|
| 726 |
-
print(f"User Message: {message} in Tab: {tab_name}")
|
| 727 |
# From string to list [{"role":"user", "content": message}, ...]
|
| 728 |
history = gradio_to_huggingface_message(history)
|
| 729 |
# We can implement context window here as we need all the system interaction. We can cut some of the early interactions if needed.
|
|
@@ -742,8 +756,6 @@ def create_demo():
|
|
| 742 |
# exclude system interaction and store the others in the history
|
| 743 |
history = huggingface_to_gradio_message(history)
|
| 744 |
if tab_name is not None:
|
| 745 |
-
print(f"Tab: {tab_name}\nSystem Output: {outputs_text}")
|
| 746 |
-
|
| 747 |
# Log the user message and response
|
| 748 |
save_feedback(
|
| 749 |
user_id,
|
|
@@ -764,7 +776,6 @@ def create_demo():
|
|
| 764 |
# Store the updated history for this tab
|
| 765 |
tab_data["history"] = history
|
| 766 |
if user_elicitation:
|
| 767 |
-
print(f"User Elicitation\nSystem Output: {outputs_text}")
|
| 768 |
save_feedback(
|
| 769 |
user_id,
|
| 770 |
uuid_this_session,
|
|
@@ -802,8 +813,6 @@ def create_demo():
|
|
| 802 |
if system_description_without_context is not None and system_instruction_context is not None:
|
| 803 |
system_instruction = system_description_without_context + "\n" + system_instruction_context
|
| 804 |
if not user_elicitation:
|
| 805 |
-
print(f"User Preference Elicitation Data: {user_preference_elicitation_data}")
|
| 806 |
-
print(f"Tab data: {tab_data}")
|
| 807 |
system_instruction = add_user_profile_to_system_instruction(
|
| 808 |
user_id,
|
| 809 |
system_instruction,
|
|
@@ -829,7 +838,6 @@ def create_demo():
|
|
| 829 |
# Format
|
| 830 |
history = huggingface_to_gradio_message(history)
|
| 831 |
if tab_name is not None:
|
| 832 |
-
print(f"Tab: {tab_name}\nHistory: {history}")
|
| 833 |
# Log the user message and response
|
| 834 |
save_feedback(
|
| 835 |
user_id,
|
|
@@ -850,7 +858,6 @@ def create_demo():
|
|
| 850 |
# Store the updated history for this tab
|
| 851 |
tab_data["history"] = history
|
| 852 |
if user_elicitation:
|
| 853 |
-
print(f"User Elicitation\nHistory: {history}")
|
| 854 |
save_feedback(
|
| 855 |
user_id,
|
| 856 |
uuid_this_session,
|
|
@@ -930,13 +937,10 @@ def create_demo():
|
|
| 930 |
{"type": tab_name, "role": "assistant", "content": outputs_text},
|
| 931 |
feedback_file_interaction,
|
| 932 |
)
|
| 933 |
-
# log_action(user_id, tab_name, "Show More of the Advisor’s Answer", "User continued the conversation")
|
| 934 |
-
# log_action(user_id, tab_name, "Response", outputs_text)
|
| 935 |
|
| 936 |
# Update history for this tab
|
| 937 |
tab_data["history"] = history
|
| 938 |
if user_elicitation:
|
| 939 |
-
print(f"User Elicitation\nSystem Output: {outputs_text}")
|
| 940 |
save_feedback(
|
| 941 |
user_id,
|
| 942 |
uuid_this_session,
|
|
@@ -957,7 +961,6 @@ def create_demo():
|
|
| 957 |
def respond_evaluation(user_id, tab_data, evals, tab_name, evaluation_send_button, textbox):
|
| 958 |
|
| 959 |
# dropdown, readon_button, multi-evaluator
|
| 960 |
-
print(f"Tab: {tab_name}\nEvaluation: {evals}")
|
| 961 |
if evals["likelihood"] is None or evals["confidence"] is None or evals["familiarity"] is None:
|
| 962 |
return (
|
| 963 |
tab_data,
|
|
@@ -1093,7 +1096,6 @@ def create_demo():
|
|
| 1093 |
user_in_narrative_id = user_id.split("_")[-1]
|
| 1094 |
narrative_id = user_id.split("_")[-2]
|
| 1095 |
experiment_id = user_id.split("_")[-3]
|
| 1096 |
-
print(f"User ID: {user_id}, User in Narrative ID: {user_in_narrative_id}, Narrative ID: {narrative_id}")
|
| 1097 |
return user_id, user_in_narrative_id, narrative_id, experiment_id
|
| 1098 |
|
| 1099 |
def get_inst_without_context(experiment_id):
|
|
@@ -1113,8 +1115,6 @@ def create_demo():
|
|
| 1113 |
def get_stock_related_context(narrative_id, user_in_narrative_id):
|
| 1114 |
raw_context_list = build_raw_context_list(context_info_list[int(narrative_id)])
|
| 1115 |
stock_context_list = build_context(context_info_list[int(narrative_id)])
|
| 1116 |
-
print("Raw Context List: ", len(raw_context_list))
|
| 1117 |
-
print("Stock Context List: ", len(stock_context_list))
|
| 1118 |
raw_context_list = reorder_list_based_on_user_in_narrative_id(user_in_narrative_id, raw_context_list)
|
| 1119 |
stock_context_list = reorder_list_based_on_user_in_narrative_id(user_in_narrative_id, stock_context_list)
|
| 1120 |
return raw_context_list, stock_context_list
|
|
@@ -1124,8 +1124,6 @@ def create_demo():
|
|
| 1124 |
user_id, user_in_narrative_id, narrative_id, experiment_id = set_user_id(request)
|
| 1125 |
# System instruction without prompt
|
| 1126 |
system_description_without_context = get_inst_without_context(experiment_id)
|
| 1127 |
-
print(f"Experiment ID: {experiment_id}")
|
| 1128 |
-
print(f"System Description: {system_description_without_context}")
|
| 1129 |
# Stock related context
|
| 1130 |
raw_context_list, stock_context_list = get_stock_related_context(narrative_id, user_in_narrative_id)
|
| 1131 |
# User Narrative
|
|
@@ -1353,13 +1351,8 @@ if __name__ == "__main__":
|
|
| 1353 |
# Global variables
|
| 1354 |
terminator = ["<eos>", "<unk>", "<sep>", "<pad>", "<cls>", "<mask>"]
|
| 1355 |
demo = create_demo()
|
| 1356 |
-
|
| 1357 |
demo.launch(
|
| 1358 |
-
share=
|
| 1359 |
-
auth=
|
| 1360 |
-
+ [
|
| 1361 |
-
("user_1_1_1", "pw1"),
|
| 1362 |
-
("user_2_1_1", "pw2"),
|
| 1363 |
-
("user_3_1_1", "pw3"),
|
| 1364 |
-
],
|
| 1365 |
)
|
|
|
|
| 4 |
import json
|
| 5 |
import uuid
|
| 6 |
from pathlib import Path
|
| 7 |
+
from huggingface_hub import CommitScheduler, login
|
| 8 |
+
from datasets import load_dataset
|
| 9 |
import gradio as gr
|
| 10 |
import markdown
|
| 11 |
from together import Together
|
|
|
|
| 95 |
|
| 96 |
# Load the required static content from files
|
| 97 |
def load_static_content(file_path):
|
| 98 |
+
|
| 99 |
with open(file_path, "r") as f:
|
| 100 |
return f.read()
|
| 101 |
|
|
|
|
| 123 |
# Other constants
|
| 124 |
FIRST_MESSAGE = "Hey"
|
| 125 |
USER_PREFERENCE_SUMMARY = True
|
| 126 |
+
DEBUG = False
|
| 127 |
API_TYPE = "together"
|
| 128 |
assert API_TYPE in ["together", "local", "debug"], "The API should be either 'together' or 'local'"
|
| 129 |
if API_TYPE == "together":
|
| 130 |
TOGETHER_CLIENT = Together(api_key=os.getenv("TOGETHER_API_KEY"))
|
| 131 |
|
| 132 |
|
| 133 |
+
def generate_username_pwd_list(data):
|
| 134 |
+
user_list = []
|
| 135 |
+
demo_list = []
|
| 136 |
+
for index, row in data.iterrows():
|
| 137 |
+
user_list.append((row["user"], str(row["pwd"])))
|
| 138 |
+
demo_list.append((row["demo"], str(row["pwd"])))
|
| 139 |
+
return user_list, demo_list
|
| 140 |
+
|
| 141 |
+
|
| 142 |
+
def load_username_and_pwd():
|
| 143 |
+
dataset = load_dataset(os.getenv("USER_PWD_FILE"))
|
| 144 |
+
df = dataset["train"].to_pandas()
|
| 145 |
+
user_list, demo_list = generate_username_pwd_list(df)
|
| 146 |
+
return user_list, demo_list
|
| 147 |
+
|
| 148 |
+
|
| 149 |
def get_context_list(synthetic_data_path):
|
| 150 |
# Load data from the synthetic data file
|
| 151 |
with open(synthetic_data_path, "r") as f:
|
|
|
|
| 251 |
if os.path.exists(log_file_dir) is False:
|
| 252 |
os.makedirs(log_file_dir)
|
| 253 |
log_file = os.path.join(log_file_dir, f"{tab_name}.txt")
|
|
|
|
| 254 |
with open(log_file, "a") as f:
|
| 255 |
f.write(f"Action: {action} | Details: {details}\n")
|
| 256 |
|
|
|
|
| 259 |
user_id, system_instruction, user_preference_elicitation_data, summary, terminator
|
| 260 |
):
|
| 261 |
exp_id = int(user_id.split("_")[-3])
|
| 262 |
+
# exp_id = 1 => No personalization
|
| 263 |
+
if exp_id == 1:
|
| 264 |
print("Non-personalizationかチェック:" + system_instruction)
|
| 265 |
return system_instruction
|
| 266 |
if summary:
|
|
|
|
| 285 |
{"summarization": summ},
|
| 286 |
feedback_file_summarization,
|
| 287 |
)
|
|
|
|
| 288 |
system_instruction += f"\nUser Profile collected in the previous conversations: {user_preference_elicitation_data['summary_history']}\n"
|
| 289 |
else:
|
| 290 |
system_instruction += (
|
|
|
|
| 431 |
providing_information = likert_evaluation("The advisor provides the financial knowledge needed")
|
| 432 |
with gr.Row():
|
| 433 |
textbox = gr.HTML()
|
| 434 |
+
submit_ranking = gr.Button(value="Submit Final Evaluation")
|
| 435 |
return {
|
| 436 |
"first": ranking_first_comp,
|
| 437 |
"second": ranking_second_comp,
|
|
|
|
| 738 |
summary=USER_PREFERENCE_SUMMARY,
|
| 739 |
terminator=terminator,
|
| 740 |
)
|
|
|
|
|
|
|
| 741 |
# From string to list [{"role":"user", "content": message}, ...]
|
| 742 |
history = gradio_to_huggingface_message(history)
|
| 743 |
# We can implement context window here as we need all the system interaction. We can cut some of the early interactions if needed.
|
|
|
|
| 756 |
# exclude system interaction and store the others in the history
|
| 757 |
history = huggingface_to_gradio_message(history)
|
| 758 |
if tab_name is not None:
|
|
|
|
|
|
|
| 759 |
# Log the user message and response
|
| 760 |
save_feedback(
|
| 761 |
user_id,
|
|
|
|
| 776 |
# Store the updated history for this tab
|
| 777 |
tab_data["history"] = history
|
| 778 |
if user_elicitation:
|
|
|
|
| 779 |
save_feedback(
|
| 780 |
user_id,
|
| 781 |
uuid_this_session,
|
|
|
|
| 813 |
if system_description_without_context is not None and system_instruction_context is not None:
|
| 814 |
system_instruction = system_description_without_context + "\n" + system_instruction_context
|
| 815 |
if not user_elicitation:
|
|
|
|
|
|
|
| 816 |
system_instruction = add_user_profile_to_system_instruction(
|
| 817 |
user_id,
|
| 818 |
system_instruction,
|
|
|
|
| 838 |
# Format
|
| 839 |
history = huggingface_to_gradio_message(history)
|
| 840 |
if tab_name is not None:
|
|
|
|
| 841 |
# Log the user message and response
|
| 842 |
save_feedback(
|
| 843 |
user_id,
|
|
|
|
| 858 |
# Store the updated history for this tab
|
| 859 |
tab_data["history"] = history
|
| 860 |
if user_elicitation:
|
|
|
|
| 861 |
save_feedback(
|
| 862 |
user_id,
|
| 863 |
uuid_this_session,
|
|
|
|
| 937 |
{"type": tab_name, "role": "assistant", "content": outputs_text},
|
| 938 |
feedback_file_interaction,
|
| 939 |
)
|
|
|
|
|
|
|
| 940 |
|
| 941 |
# Update history for this tab
|
| 942 |
tab_data["history"] = history
|
| 943 |
if user_elicitation:
|
|
|
|
| 944 |
save_feedback(
|
| 945 |
user_id,
|
| 946 |
uuid_this_session,
|
|
|
|
| 961 |
def respond_evaluation(user_id, tab_data, evals, tab_name, evaluation_send_button, textbox):
|
| 962 |
|
| 963 |
# dropdown, readon_button, multi-evaluator
|
|
|
|
| 964 |
if evals["likelihood"] is None or evals["confidence"] is None or evals["familiarity"] is None:
|
| 965 |
return (
|
| 966 |
tab_data,
|
|
|
|
| 1096 |
user_in_narrative_id = user_id.split("_")[-1]
|
| 1097 |
narrative_id = user_id.split("_")[-2]
|
| 1098 |
experiment_id = user_id.split("_")[-3]
|
|
|
|
| 1099 |
return user_id, user_in_narrative_id, narrative_id, experiment_id
|
| 1100 |
|
| 1101 |
def get_inst_without_context(experiment_id):
|
|
|
|
| 1115 |
def get_stock_related_context(narrative_id, user_in_narrative_id):
|
| 1116 |
raw_context_list = build_raw_context_list(context_info_list[int(narrative_id)])
|
| 1117 |
stock_context_list = build_context(context_info_list[int(narrative_id)])
|
|
|
|
|
|
|
| 1118 |
raw_context_list = reorder_list_based_on_user_in_narrative_id(user_in_narrative_id, raw_context_list)
|
| 1119 |
stock_context_list = reorder_list_based_on_user_in_narrative_id(user_in_narrative_id, stock_context_list)
|
| 1120 |
return raw_context_list, stock_context_list
|
|
|
|
| 1124 |
user_id, user_in_narrative_id, narrative_id, experiment_id = set_user_id(request)
|
| 1125 |
# System instruction without prompt
|
| 1126 |
system_description_without_context = get_inst_without_context(experiment_id)
|
|
|
|
|
|
|
| 1127 |
# Stock related context
|
| 1128 |
raw_context_list, stock_context_list = get_stock_related_context(narrative_id, user_in_narrative_id)
|
| 1129 |
# User Narrative
|
|
|
|
| 1351 |
# Global variables
|
| 1352 |
terminator = ["<eos>", "<unk>", "<sep>", "<pad>", "<cls>", "<mask>"]
|
| 1353 |
demo = create_demo()
|
| 1354 |
+
user_list, demo_list = load_username_and_pwd()
|
| 1355 |
demo.launch(
|
| 1356 |
+
share=False,
|
| 1357 |
+
auth=user_list + demo_list,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1358 |
)
|