-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
125 lines (101 loc) · 4.89 KB
/
app.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
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import streamlit as st
import random
from LLM import send_to_front
chat_button_style = """
width: 150px;
height: 50px;
font-size: 16px;
"""
# Creating the page configuration
st.set_page_config(
page_title="DEEPs",
layout="wide",
initial_sidebar_state="expanded",
)
#st.image("https://bloombot.ai/wp-content/uploads/2023/02/[email protected]", width=200)
st.title("Your BioRa (Biomedical Research AI Agent)")
st.write('BioRa is your helper in doing bio and medical researches. It offers a range of features that grant the user the possibility to use veridic and up-to-time data.')
st.markdown("""
<style>
@import url('https://fonts.googleapis.com/css2?family=Nokora&family=Space+Grotesk&display=swap%27);
p {
font-family: 'Space Grotesk', sans-serif !important;
}
h1, h2, h3, h4, h5, h6 {
font-family: 'Nokora', sans-serif;
}
#bloom-reading-reimagined {
font-weight: 400;
}
</style>""", unsafe_allow_html=True)
# Creating the sidebar
st.sidebar.header("DEEPs")
# if "research_mode" not in st.session_state.keys():
# st.session_state.research_mode = st.sidebar.toggle("Research Mode", False)
if "number_of_conversations" not in st.session_state.keys():
st.session_state.number_of_conversations = 1
st.session_state.current_conversation_index = 1
st.session_state.current_conversation = f"conversation_{st.session_state.current_conversation_index}"
# Creating the conversations
if "conversations" not in st.session_state.keys():
st.session_state.conversations = dict()
st.session_state.current_conversation = f"conversation_{st.session_state.current_conversation_index}"
st.session_state.conversations[st.session_state.current_conversation] = [{"role": "assistant", "message": "How may I help you?"}]
if "BOOKMARKED_LINKS" not in st.session_state.keys():
st.session_state.BOOKMARKED_LINKS = set()
# Creating the buttons for a new chat
if st.sidebar.button("New Chat", use_container_width=True):
st.session_state.number_of_conversations += 1
st.session_state.current_conversation = f"conversation_{st.session_state.number_of_conversations}"
st.session_state.conversations[st.session_state.current_conversation] = [{"role": "assistant", "message": "How may I help you?"}]
# Creatin the buttons for all the chats
for conversation_index in range(1, st.session_state.number_of_conversations + 1):
if st.sidebar.button(f"Chat {conversation_index}", use_container_width=True):
st.session_state.current_conversation = f"conversation_{conversation_index}"
# Display chat messages along with te lists of links
for prompt in st.session_state.conversations[st.session_state.current_conversation]:
with st.chat_message(prompt["role"]):
st.write(prompt["message"])
if prompt["role"] == "assistant" and "links" in prompt.keys():
for link in prompt["links"]:
checkbox_id = f"checkbox_{link.replace(' ', '_')}_{prompt['message']}"
is_checked = st.checkbox(link, key=checkbox_id)
if is_checked:
st.session_state.BOOKMARKED_LINKS.add(link)
# User-provided prompt
if prompt := st.chat_input():
st.session_state.conversations[st.session_state.current_conversation].append({"role": "user", "message": prompt})
with st.chat_message("user"):
st.write(prompt)
if st.session_state.conversations[st.session_state.current_conversation][-1]["role"] != "assistant":
with st.chat_message("assistant"):
with st.spinner("Thinking..."):
response, links = send_to_front(prompt)
# Display the response
st.markdown(response)
# Display links
for link in links:
checkbox_id = f"checkbox_{link.replace(' ', '_')}"
is_checked = st.checkbox(link, key=checkbox_id)
if is_checked:
st.session_state.BOOKMARKED_LINKS.add(link)
st.session_state.conversations[st.session_state.current_conversation].append(
{
"role": "assistant",
"message": response,
"links": links,
}
)
# Display bookmarked links
st.sidebar.markdown("### Bookmarked Links")
new_bookmarked_links = st.session_state.BOOKMARKED_LINKS.copy()
for bookmarked_link in st.session_state.BOOKMARKED_LINKS:
# create a sidebar checbkox for each bookmarked link
checkbox_id = f"checkbox_{bookmarked_link.replace(' ', '_')}_{st.session_state.current_conversation}"
is_checked = st.sidebar.checkbox(bookmarked_link, key=checkbox_id)
if is_checked:
# remove the bookmarked link if the checkbox is checked
new_bookmarked_links.remove(bookmarked_link)
if new_bookmarked_links != st.session_state.BOOKMARKED_LINKS:
st.session_state.BOOKMARKED_LINKS = new_bookmarked_links
st.experimental_rerun()