peinan commited on
Commit
c597776
1 Parent(s): 4869103

add downloading chat history feature (#6)

Browse files
Files changed (2) hide show
  1. .gitignore +1 -0
  2. src/pdfchat/app.py +25 -0
.gitignore CHANGED
@@ -16,3 +16,4 @@ gradio_cached_examples
16
  local_qdrant
17
 
18
  .vscode
 
 
16
  local_qdrant
17
 
18
  .vscode
19
+ history.json
src/pdfchat/app.py CHANGED
@@ -1,3 +1,4 @@
 
1
  import os
2
  import re
3
  import time
@@ -57,6 +58,9 @@ class Chat:
57
  def to_list(self) -> list[str, str]:
58
  return [self.query, self.response]
59
 
 
 
 
60
 
61
  @dataclass
62
  class ChatHistory:
@@ -80,6 +84,11 @@ class ChatHistory:
80
  def clear_last_response(self):
81
  self.history[-1].response = ""
82
 
 
 
 
 
 
83
 
84
  def open_file(file_path: str) -> str:
85
  file_path = Path(file_path)
@@ -168,6 +177,15 @@ def main(history: ChatHistory, query: str, file_path: str | None) -> ChatHistory
168
  yield history
169
 
170
 
 
 
 
 
 
 
 
 
 
171
  with gr.Blocks() as app:
172
  gr.Markdown("# Chat with PDF")
173
  with gr.Row():
@@ -181,6 +199,7 @@ with gr.Blocks() as app:
181
  label="Document",
182
  )
183
  with gr.Accordion("Parameters", open=False):
 
184
  temperature_slider = gr.Slider(
185
  minimum=0.1, maximum=1.0, value=0.5, label="Temperature"
186
  )
@@ -189,6 +208,8 @@ with gr.Blocks() as app:
189
  minimum=0.1, maximum=1.0, value=0.5, label="Top P"
190
  )
191
  top_p_slider.change(lambda x: x, [top_p_slider])
 
 
192
  with gr.Column(scale=65):
193
  chatbot = gr.Chatbot(
194
  bubble_full_width=False,
@@ -215,6 +236,10 @@ with gr.Blocks() as app:
215
  fn=main,
216
  inputs=[chatbot, text_box, file_box],
217
  outputs=chatbot,
 
 
 
 
218
  )
219
  examples = gr.Examples(
220
  examples=[
 
1
+ import json
2
  import os
3
  import re
4
  import time
 
58
  def to_list(self) -> list[str, str]:
59
  return [self.query, self.response]
60
 
61
+ def to_dict(self) -> dict[str, str]:
62
+ return {"query": self.query, "response": self.response}
63
+
64
 
65
  @dataclass
66
  class ChatHistory:
 
84
  def clear_last_response(self):
85
  self.history[-1].response = ""
86
 
87
+ def to_json(self) -> str:
88
+ return json.dumps(
89
+ [chat.to_dict() for chat in self.history], ensure_ascii=False, indent=4
90
+ )
91
+
92
 
93
  def open_file(file_path: str) -> str:
94
  file_path = Path(file_path)
 
177
  yield history
178
 
179
 
180
+ def save_chat_history(history: ChatHistory = []) -> str:
181
+ history = ChatHistory(history)
182
+ file_path = Path("history.json")
183
+ with open(file_path, "w") as f:
184
+ f.write(history.to_json())
185
+
186
+ return str(file_path)
187
+
188
+
189
  with gr.Blocks() as app:
190
  gr.Markdown("# Chat with PDF")
191
  with gr.Row():
 
199
  label="Document",
200
  )
201
  with gr.Accordion("Parameters", open=False):
202
+ gr.Markdown("⚠️Warning⚠️ Not implemented yet")
203
  temperature_slider = gr.Slider(
204
  minimum=0.1, maximum=1.0, value=0.5, label="Temperature"
205
  )
 
208
  minimum=0.1, maximum=1.0, value=0.5, label="Top P"
209
  )
210
  top_p_slider.change(lambda x: x, [top_p_slider])
211
+ with gr.Accordion("Save Chat History", open=False):
212
+ history_file = gr.File()
213
  with gr.Column(scale=65):
214
  chatbot = gr.Chatbot(
215
  bubble_full_width=False,
 
236
  fn=main,
237
  inputs=[chatbot, text_box, file_box],
238
  outputs=chatbot,
239
+ ).then(
240
+ lambda history: save_chat_history(history),
241
+ inputs=[chatbot],
242
+ outputs=history_file,
243
  )
244
  examples = gr.Examples(
245
  examples=[