TTsamurai commited on
Commit
a4f70c9
·
1 Parent(s): 8d493b8

add loggiong function

Browse files
Files changed (1) hide show
  1. app.py +123 -27
app.py CHANGED
@@ -1,6 +1,10 @@
1
  import sys
2
  import os
 
3
  import json
 
 
 
4
  import gradio as gr
5
  import markdown
6
  from together import Together
@@ -49,6 +53,32 @@ SYSTEM_INSTRUECTION_PREFERENCE_ELICITATION_FILE = os.path.join(
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):
@@ -212,8 +242,9 @@ def add_user_profile_to_system_instruction(
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:
219
  system_instruction += f"\nPrevious Conversations with the Customer about the User Profile: {user_preference_elicitation_data['history']}\n"
@@ -634,17 +665,38 @@ def create_demo():
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
 
@@ -690,17 +742,38 @@ def create_demo():
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,
@@ -747,25 +820,47 @@ def create_demo():
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 (
@@ -804,11 +899,12 @@ def create_demo():
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>"""
 
1
  import sys
2
  import os
3
+ 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
 
53
  )
54
  SUMMARIZATION_PROMPT_FILE = os.path.join(STATIC_FILE, "txt/system_summarization_user_preference_elicitation.txt")
55
 
56
+ uuid_this_session = str(uuid.uuid4())
57
+ feedback_file = Path("user_feedback/") / f"data_{uuid_this_session}.json"
58
+ feedback_folder = feedback_file.parent
59
+ feedback_folder.mkdir(parents=True, exist_ok=True) # Ensure the directory exists
60
+ scheduler = CommitScheduler(
61
+ repo_id="report-translation-feedback",
62
+ repo_type="dataset",
63
+ folder_path=feedback_folder,
64
+ path_in_repo="data",
65
+ every=10,
66
+ )
67
+
68
+
69
+ # Function to save user feedback
70
+ def save_feedback(user_id: str, uuid: str, type: str, value) -> None:
71
+ """
72
+ Append input/outputs and user feedback to a JSON Lines file using a thread lock to avoid concurrent writes from different users.
73
+ """
74
+ timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
75
+ with scheduler.lock:
76
+ with feedback_file.open("a") as f:
77
+ f.write(
78
+ json.dumps({"user_id": user_id, "uuid": uuid, "timestamp": timestamp, "type": type, "value": value})
79
+ )
80
+ f.write("\n")
81
+
82
 
83
  # Load the required static content from files
84
  def load_static_content(file_path):
 
242
  else:
243
  summ, _ = generate_response_debugging(summarization_instruction)
244
  user_preference_elicitation_data["summary_history"] = summ
245
+ # log_action(user_id, "Prompt", "Preference Elicitation Summarization", summ)
246
+ save_feedback(user_id, uuid_this_session, "Preference_Elicitation_Summarization", summ)
247
+ # print(f"Preference Summary:{summ}")
248
  system_instruction += f"\nPrevious Conversations with the Customer about the User Profile: {user_preference_elicitation_data['summary_history']}\n"
249
  else:
250
  system_instruction += f"\nPrevious Conversations with the Customer about the User Profile: {user_preference_elicitation_data['history']}\n"
 
665
  print(f"Tab: {tab_name}\nSystem Output: {outputs_text}")
666
 
667
  # Log the user message and response
668
+ save_feedback(
669
+ user_id, uuid_this_session, "Interaction", {"type": tab_name, "role": "user", "content": message}
670
+ )
671
+ save_feedback(
672
+ user_id,
673
+ uuid_this_session,
674
+ "Interaction",
675
+ {"type": tab_name, "role": "assistant", "content": outputs_text},
676
+ )
677
+ # log_action(user_id, tab_name, "User Message", message)
678
+ # log_action(user_id, tab_name, "Response", outputs_text)
679
  # Store the updated history for this tab
680
  tab_data["history"] = history
681
  if user_elicitation:
682
  print(f"User Elicitation\nSystem Output: {outputs_text}")
683
+ save_feedback(
684
+ user_id,
685
+ uuid_this_session,
686
+ "Interaction",
687
+ {"type": "User_Elicitation", "role": "user", "content": message},
688
+ )
689
+ save_feedback(
690
+ user_id,
691
+ uuid_this_session,
692
+ "Interaction",
693
+ {"type": "User_Elicitation", "role": "assistant", "content": outputs_text},
694
+ )
695
+ # log_action(user_id, "User_Elicitation", "User Message", message)
696
+ # log_action(user_id, "User_Elicitation", "Response", outputs_text)
697
  tab_data["history"] = history
698
+ # if SESSION_DEBUG:
699
+ # log_action(user_id, "Session", "History", history)
700
 
701
  return tab_data, "", history
702
 
 
742
  if tab_name is not None:
743
  print(f"Tab: {tab_name}\nHistory: {history}")
744
  # Log the user message and response
745
+ save_feedback(
746
+ user_id, uuid_this_session, "Interaction", {"type": tab_name, "role": "user", "content": first_message}
747
+ )
748
+ save_feedback(
749
+ user_id,
750
+ uuid_this_session,
751
+ "Interaction",
752
+ {"type": tab_name, "role": "assistant", "content": outputs_text},
753
+ )
754
+ # log_action(user_id, tab_name, "User Message", first_message)
755
+ # log_action(user_id, tab_name, "Response", outputs_text)
756
  # Store the updated history for this tab
757
  tab_data["history"] = history
758
  if user_elicitation:
759
  print(f"User Elicitation\nHistory: {history}")
760
+ save_feedback(
761
+ user_id,
762
+ uuid_this_session,
763
+ "Interaction",
764
+ {"type": "User_Elicitation", "role": "user", "content": first_message},
765
+ )
766
+ save_feedback(
767
+ user_id,
768
+ uuid_this_session,
769
+ "Interaction",
770
+ {"type": "User_Elicitation", "role": "assistant", "content": outputs_text},
771
+ )
772
+ # log_action(user_id, "User_Elicitation", "User Message", first_message)
773
+ # log_action(user_id, "User_Elicitation", "Response", outputs_text)
774
  tab_data["history"] = history
775
+ # if SESSION_DEBUG:
776
+ # log_action(user_id, "Session", "History", history)
777
  return (
778
  tab_data,
779
  history,
 
820
  outputs_text, history = generate_response_debugging(history_with_user_utterance)
821
  history = huggingface_to_gradio_message(history)
822
  if tab_name is not None:
823
+ save_feedback(
824
+ user_id, uuid_this_session, "Interaction", {"type": tab_name, "role": "user", "content": message}
825
+ )
826
+ save_feedback(
827
+ user_id,
828
+ uuid_this_session,
829
+ "Interaction",
830
+ {"type": tab_name, "role": "assistant", "content": outputs_text},
831
+ )
832
+ # log_action(user_id, tab_name, "Show More of the Advisor’s Answer", "User continued the conversation")
833
+ # log_action(user_id, tab_name, "Response", outputs_text)
834
 
835
  # Update history for this tab
836
  tab_data["history"] = history
837
  if user_elicitation:
838
  print(f"User Elicitation\nSystem Output: {outputs_text}")
839
+ save_feedback(
840
+ user_id,
841
+ uuid_this_session,
842
+ "Interaction",
843
+ {"type": "User_Elicitation", "role": "user", "content": message},
844
+ )
845
+ save_feedback(
846
+ user_id,
847
+ uuid_this_session,
848
+ "Interaction",
849
+ {"type": "User_Elicitation", "role": "assistant", "content": outputs_text},
850
+ )
851
+ # log_action(user_id, "User_Elicitation", "Response", outputs_text)
852
  tab_data["history"] = history
853
+ # if SESSION_DEBUG:
854
+ # log_action(user_id, "Session", "History", history)
855
  return tab_data, history
856
 
857
  def respond_evaluation(user_id, tab_data, evals, tab_name):
858
 
859
  # dropdown, readon_button, multi-evaluator
860
+ save_feedback(user_id, uuid_this_session, "Round_Evaluation", evals)
861
+ # log_action(user_id, tab_name, "Round Evaluation", "Following")
862
+ # for key, value in evals.items():
863
+ # log_action(user_id, tab_name, key, value)
864
  # Store the reason for this tab
865
  tab_data["multi_evaluator"] = evals
866
  return (
 
899
  <strong>Please make sure that you are not ranking the same stock multiple times.</strong>
900
  </div>"""
901
  else:
902
+ save_feedback(user_id, uuid_this_session, "Final_Ranking", {first_comp: ranking_first_comp, second_comp: ranking_second_comp, third_comp: ranking_third_comp, fourth_comp: ranking_fourth_comp, fifth_comp: ranking_fifth_comp})
903
+ # log_action(user_id, "Final_Ranking", first_comp, ranking_first_comp)
904
+ # log_action(user_id, "Final_Ranking", second_comp, ranking_second_comp)
905
+ # log_action(user_id, "Final_Ranking", third_comp, ranking_third_comp)
906
+ # log_action(user_id, "Final_Ranking", fourth_comp, ranking_fourth_comp)
907
+ # log_action(user_id, "Final_Ranking", fifth_comp, ranking_fifth_comp)
908
  return """<div style="background-color: #d4edda; color: #155724; padding: 15px; border: 1px solid #c3e6cb; border-radius: 5px; margin-bottom: 20px;">
909
  <strong>Thank you for participating in the experiment. This concludes the session. You may now close the tab.</strong>
910
  </div>"""