-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
85 lines (65 loc) · 3.16 KB
/
Copy pathapp.py
File metadata and controls
85 lines (65 loc) · 3.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import streamlit as st
from llm_chains import load_normal_chain
from langchain.memory import StreamlitChatMessageHistory
from utils import save_chat_history_json, get_timestamp, load_chat_history_json
import yaml
import os
with open("config.yaml", "r") as f:
config = yaml.safe_load(f)
def load_chain(chat_history):
return load_normal_chain(chat_history)
def clear_input_field():
st.session_state.user_question = st.session_state.user_input
st.session_state.user_input = ""
def set_send_input():
st.session_state.send_input = True
clear_input_field()
def save_chat_history():
if st.session_state.history != []:
if st.session_state.session_key == "new_session":
st.session_state.new_session_key = get_timestamp() + ".json"
save_chat_history_json(st.session_state.history, config["chat_history_path"] + st.session_state.new_session_key)
else:
save_chat_history_json(st.session_state.history, config["chat_history_path"] + st.session_state.session_key)
def main():
st.title("Multimodal Local Chat App")
# st.write(css, unsafe_allow_html=True)
chat_container = st.container()
st.sidebar.title("chat session")
chat_sessions = ["new_session"] + os.listdir(config["chat_history_path"])
print(chat_sessions)
if "send_input" not in st.session_state:
st.session_state.session_key = "new_session"
st.session_state.session_index_tracker = "new_session"
st.session_state.send_input = False
st.session_state.send_question = ""
st.session_state.new_session_key = None
if st.session_state.session_key == "new_session" and st.session_state.new_session_key != None:
st.session_state.session_index_tracker = st.session_state.new_session_key
st.session_state.new_session_key = None
index = chat_sessions.index(st.session_state.session_index_tracker)
st.sidebar.selectbox("Select a chat sessions", chat_sessions, key="session_key", index=index)
if st.session_state.session_key != "new_session":
st.session_state.history = load_chat_history_json(config["chat_history_path"] + st.session_state.session_key)
else:
st.session_state.history = []
chat_history = StreamlitChatMessageHistory(key="history")
llm_chain = load_chain(chat_history)
user_input = st.text_input("Type your message here", key="user_input", on_change=set_send_input)
send_button = st.button("Send", key="send_button")
if send_button or st.session_state.send_input:
if st.session_state.user_question != "":
with chat_container:
st.chat_message("user").write(st.session_state.user_question)
llm_response = llm_chain.run(st.session_state.user_question)
# st.chat_message("ai").write(llm_response)
st.session_state.user_question = ""
if chat_history.messages != []:
with chat_container:
st.write("Chat History:")
for message in chat_history.messages:
st.chat_message(message.type).write(message.content)
# print(chat_history.messages)
save_chat_history()
if __name__ == "__main__":
main()