-
Notifications
You must be signed in to change notification settings - Fork 35
/
utils.py
50 lines (44 loc) · 2.06 KB
/
utils.py
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
import streamlit as st
import streamlit.components.v1 as components
from Conversation.conversation import ConversationalAgent
def load_tab_css():
with open("static/tab.css", "r") as f:
css = f"<style>{f.read()}</style>"
st.markdown(css, unsafe_allow_html=True)
def initialize_session_state():
if "history" not in st.session_state:
st.session_state.history = []
if "search_keywords" not in st.session_state:
st.session_state.search_keywords = []
if "doc_sources" not in st.session_state:
st.session_state.doc_sources = []
if "google_sources" not in st.session_state:
st.session_state.google_sources = []
if "token_count" not in st.session_state:
st.session_state.token_count = 0
if "chat_placeholder" not in st.session_state:
st.session_state.chat_placeholder = None
if "agent" not in st.session_state:
st.session_state.agent = ConversationalAgent()
def enterKeypress_submit_button_html():
return components.html("""
<script>
const streamlitDoc = window.parent.document;
const buttons = Array.from(
streamlitDoc.querySelectorAll('.stButton > button')
);
const submitButton = buttons.find(
el => el.innerText === 'Submit'
);
streamlitDoc.addEventListener('keydown', function(e) {
switch (e.key) {
case 'Enter':
submitButton.click();
break;
}
});
</script>
""",
height=0,
width=0,
)