-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathstreamlit_demo.py
69 lines (58 loc) · 2.5 KB
/
streamlit_demo.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
# Jeeves is back, and this time he's playing for keeps
import streamlit as st
from streamlit_chat import message as st_message
import streamlit.components.v1 as components
import random
import string
from qa.bot import GroundedQaBot
source_url = ''
iframe = None
source_display = None
### CHAT UPDATED HERE
def getReply():
user_message = st.session_state.input_text
history = []
for chat in st.session_state['history']:
name = "user" if chat['is_user'] else "bot"
history.append(f"{name}: {chat['message']}")
bot.set_chat_history(history)
reply, source_urls, source_texts = bot.answer(user_message,
verbosity=2,
n_paragraphs=2,
model=st.session_state.model,
url=st.session_state.url)
sources_str = "\n".join(list(set(source_urls)))
reply_incl_sources=f"{reply}\nSource:\n{sources_str}"
print(reply_incl_sources)
with col2:
st.subheader("sources")
st.markdown(sources_str)
st.session_state.input_text = ''
st.session_state.history.append({"message": user_message, "is_user": True, "avatar_style": "gridy"})
st.session_state.history.append({"message": reply, "is_user": False})
if __name__ == "__main__":
print("new bot")
bot = GroundedQaBot(st.secrets["cohere_api_token"], st.secrets["serp_api_token"])
st.set_page_config(layout="wide")
### CHAT HISTORY SETUP
if "history" not in st.session_state:
st.session_state.history = []
### MAIN PAGE LAYOUT
col1, col2 = st.columns(2)
with st.sidebar:
st.title("Grounded Question Answering")
st.markdown('This is a Cohere API / Serp API powered contextualized factual question answering bot!')
#used for any extra settings
with st.expander("Advanced Settings"):
st.text_input("Restrict replies to domain:", key="url", placeholder="Ex: shopify.com")
st.selectbox('Model:', ('xlarge', 'command-xlarge-20221108'), key="model")
#chat
with col1:
for chat in st.session_state['history']:
# call random.choices() string module to find the string in Uppercase + numeric data.
ran = ''.join(random.choices(string.ascii_uppercase + string.digits, k=10))
st_message(**chat, key=ran) # laying out messages
st.text_input("",
key="input_text",
on_change=getReply,
placeholder="Ask me a question...like 'how far away is the moon'")