-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdemo_app.py
More file actions
118 lines (107 loc) · 3.75 KB
/
demo_app.py
File metadata and controls
118 lines (107 loc) · 3.75 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
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
import streamlit as st
from iso3166 import countries
from api.driver.logic_service import run
from api.models.messages import Role, Message
from app.rendering import render_left_message, render_style, render_right_message
from app.states import (
init_states,
get_chat_messages,
get_interface_messages,
save_customer_message,
get_all_companies,
set_chat_settings,
get_resource_loader,
get_program_loader,
)
from api.models.settings import ChatSettings
from keys import OPENAI_API_KEY
MAX_MESSAGES = 50
st.set_page_config(page_title="CSR Bot Demo", layout="wide")
with st.sidebar:
left, _, mid = st.columns((2, 0.1, 3))
right = st.container()
render_style()
init_states()
with left:
st.markdown("**Settings**")
company = st.selectbox(label="Select a Company", options=get_all_companies())
countries = [c.name for c in countries]
location = st.selectbox(
label="Your Location",
options=countries,
index=countries.index("Singapore"),
)
set_chat_settings(company_name=company, location=location)
resource_loader = get_resource_loader()
all_resources = resource_loader.get_all_resources()
resource_display = [
"- " + x.replace(".txt", "").replace("_", " ").capitalize()
for x in all_resources
]
resource_display.sort()
st.markdown(
f"Here are the enquiries that CSR Bot can help for {company}: \n"
+ "\n".join(resource_display)
+ "\n\nMore workflows and companies will be added soon!\n\n"
"Disclaimer: \n"
"This app is for demonstration purposes. "
"Do not enter your actual personal information."
)
settings = ChatSettings(
company=company,
location=location,
resource_loader=resource_loader,
program_loader=get_program_loader(),
)
st.markdown("-----")
openai_key = st.text_input(
label="OpenAI Key",
value=OPENAI_API_KEY,
help="Get a free key from https://platform.openai.com",
)
with mid:
st.markdown("### Behind the scenes of CSR Bot")
interface_messages = get_interface_messages()
for message in interface_messages:
if message.role == Role.bot:
render_right_message(delta=mid, message=message)
elif message.role == Role.app:
render_left_message(delta=mid, message=message)
with right:
st.markdown("### Chat with CSR Bot")
chat_messages = get_chat_messages()
for message in chat_messages:
if message.role == Role.customer:
render_right_message(delta=st.container(), message=message)
elif message.role == Role.bot:
render_left_message(delta=st.container(), message=message)
has_openai_key = len(openai_key) > 0
user_input = st.chat_input(
placeholder="Type your message here", max_chars=200, disabled=not has_openai_key
)
if not has_openai_key:
st.markdown(
"Please enter your OpenAI key under Settings to start chatting with CSR Bot!"
)
if user_input is not None and len((user_input := user_input.strip())) > 0:
new_message = Message(role=Role.customer, text=user_input)
save_customer_message(message=new_message)
terminated = False
messages = get_chat_messages()
interface_messages = get_interface_messages()
if (
len(interface_messages) < MAX_MESSAGES
and len(messages) > 0
and messages[-1].role != Role.bot
):
while (
len(interface_messages) < MAX_MESSAGES
and not terminated
and messages[-1].role != Role.bot
):
interface_messages = get_interface_messages()
terminated = run(
messages=interface_messages, settings=settings, openai_key=openai_key
)
messages = get_chat_messages()
st.experimental_rerun()