RustX commited on
Commit
ae8ab71
Β·
1 Parent(s): 14b3a47

Create history.py

Browse files
Files changed (1) hide show
  1. modules/history.py +57 -0
modules/history.py ADDED
@@ -0,0 +1,57 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import streamlit as st
3
+ from streamlit_chat import message
4
+
5
+
6
+ class ChatHistory:
7
+ def __init__(self):
8
+ self.history = st.session_state.get("history", [])
9
+ st.session_state["history"] = self.history
10
+
11
+ def default_greeting(self):
12
+ return "μ•ˆλ…• ! πŸ‘‹"
13
+
14
+ def default_prompt(self, topic):
15
+ return f"μ•ˆλ…•ν•˜μ„Έμš” ! {topic}에 λŒ€ν•΄ 무엇이든 λ¬Όμ–΄λ³΄μ„Έμš” πŸ€—"
16
+
17
+ def initialize_user_history(self):
18
+ st.session_state["user"] = [self.default_greeting()]
19
+
20
+ def initialize_assistant_history(self, uploaded_file):
21
+ st.session_state["assistant"] = [self.default_prompt(uploaded_file.name)]
22
+
23
+ def initialize(self, uploaded_file):
24
+ if "assistant" not in st.session_state:
25
+ self.initialize_assistant_history(uploaded_file)
26
+ if "user" not in st.session_state:
27
+ self.initialize_user_history()
28
+
29
+ def reset(self, uploaded_file):
30
+ st.session_state["history"] = []
31
+ self.initialize_user_history()
32
+ self.initialize_assistant_history(uploaded_file)
33
+ st.session_state["reset_chat"] = False
34
+
35
+ def append(self, mode, message):
36
+ st.session_state[mode].append(message)
37
+
38
+ def generate_messages(self, container):
39
+ if st.session_state["assistant"]:
40
+ with container:
41
+ for i in range(len(st.session_state["assistant"])):
42
+ message(
43
+ st.session_state["user"][i],
44
+ is_user=True,
45
+ key=f"{i}_user",
46
+ avatar_style="big-smile",
47
+ )
48
+ message(st.session_state["assistant"][i], key=str(i), avatar_style="thumbs")
49
+
50
+ def load(self):
51
+ if os.path.exists(self.history_file):
52
+ with open(self.history_file, "r") as f:
53
+ self.history = f.read().splitlines()
54
+
55
+ def save(self):
56
+ with open(self.history_file, "w") as f:
57
+ f.write("\n".join(self.history))