Skip to content

Commit

Permalink
Save the original information besides the rewritten info
Browse files Browse the repository at this point in the history
  • Loading branch information
doviettung96 committed May 31, 2024
1 parent 20a04a4 commit dbe7c76
Showing 1 changed file with 48 additions and 34 deletions.
82 changes: 48 additions & 34 deletions libs/ktem/ktem/pages/chat/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ def on_building_ui(self):
with gr.Row():
self.chat_state = gr.State(STATE)
self.original_chat_history = gr.State([])
self.original_settings = gr.State({})
self.original_info_panel = gr.State("")

with gr.Column(scale=1, elem_id="conv-settings-panel"):
self.chat_control = ConversationControl(self._app)

Expand Down Expand Up @@ -114,6 +117,19 @@ def on_register_events(self):
],
concurrency_limit=20,
show_progress="minimal",
).success(
fn=self.backup_original_info,
inputs=[
self.chat_panel.chatbot,
self._app.settings_state,
self.info_panel,
self.original_chat_history
],
outputs=[
self.original_chat_history,
self.original_settings,
self.original_info_panel
]
).then(
fn=self.update_data_source,
inputs=[
Expand All @@ -124,14 +140,6 @@ def on_register_events(self):
+ self._indices_input,
outputs=None,
concurrency_limit=20,
).then(
fn=self.backup_original_history,
inputs=[
self.chat_panel.chatbot
],
outputs=[
self.original_chat_history
]
)

self.chat_panel.regen_btn.click(
Expand Down Expand Up @@ -172,9 +180,11 @@ def on_register_events(self):
inputs=[
self.chat_control.conversation_id,
self.chat_panel.chatbot,
self.original_chat_history,
self._app.settings_state,
self.info_panel,
self.original_chat_history,
self.original_settings,
self.original_info_panel,
],
outputs=None
)
Expand Down Expand Up @@ -502,24 +512,12 @@ def regen_fn(

state["app"]["regen"] = False

def backup_original_history(self, chat_history):
original_history = []
for message in chat_history:
original_history.append(message)
return original_history
def backup_original_info(self, chat_history, settings, info_pannel, original_chat_history):
original_chat_history.append(chat_history[-1])
return original_chat_history, settings, info_pannel


def save_log(self, conversation_id, chat_history, original_chat_history, settings, info_panel, log_dir):
with Session(engine) as session:
statement = select(Conversation).where(Conversation.id == conversation_id)
result = session.exec(statement).one()

data_source = deepcopy(result.data_source)
likes = data_source.get("likes", [])

if not likes:
return

def save_log(self, conversation_id, chat_history, settings, info_panel, original_chat_history, original_settings, original_info_panel, log_dir):
if not Path(log_dir).exists():
Path(log_dir).mkdir(parents=True)

Expand All @@ -528,22 +526,36 @@ def save_log(self, conversation_id, chat_history, original_chat_history, setting
today = datetime.now()
formatted_date = today.strftime('%d%m%Y_%H')

with Session(engine) as session:
statement = select(Conversation).where(Conversation.id == conversation_id)
result = session.exec(statement).one()

data_source = deepcopy(result.data_source)
likes = data_source.get("likes", [])
if not likes:
return

feedback = likes[-1][-1]
message_index = likes[-1][0]

current_message = chat_history[message_index[0]]
original_message = original_chat_history[message_index[0]]
is_original = all([current_item == original_item for current_item, original_item in zip(current_message, original_message)])

dataframe = [[
conversation_id,
message_index,
chat_history,
current_message[0],
current_message[1],
is_original,
feedback,
chat_history,
settings,
info_panel
info_panel,
feedback,
is_original,
original_message[1],
original_chat_history,
original_settings,
original_info_panel
]]


Expand All @@ -557,15 +569,17 @@ def save_log(self, conversation_id, chat_history, original_chat_history, setting
writer.writerow([
"Conversation ID",
"Message ID",
"Chat History",
"Question",
"Answer",
"Original/ Rewritten",
"Feedback",
"Chat History",
"Settings",
"Evidences",
"Feedback",
"Original/ Rewritten",
"Original Answer",
"Original Chat History",
"Original Settings",
"Original Evidences",
])
for item in dataframe:
item[4] = item[4].replace('\u0000', '')

writer.writerows(dataframe)

0 comments on commit dbe7c76

Please sign in to comment.