-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathapp.py
More file actions
65 lines (49 loc) · 1.7 KB
/
Copy pathapp.py
File metadata and controls
65 lines (49 loc) · 1.7 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
import streamlit as st
import inference_rick_app
import base64
def conver_image_in_url(path_im):
file_ = open(path_im, "rb")
contents = file_.read()
data_url = base64.b64encode(contents).decode("utf-8")
file_.close()
return data_url
#create cache object for the "chat"
@st.cache(allow_output_mutation=True)
def Chat():
return []
# Instance Bot
Rick_bot = inference_rick_app.Bot_rick()
# create object to sotrage messages
messages_history = Chat()
# create a sidebar for select Bot
bot_value = st.sidebar.radio(label="Select Character:", options=["Rick Sanchez", "Yoda", "Batman"])
# Rick Sanchez bot
if bot_value == "Rick Sanchez":
# Load gif of Rick
# transform gif in url for shows with markdown
data_url = conver_image_in_url("content/Rick.gif")
st.markdown(f"<img src='data:image/gif;base64,{data_url}' width='300'>",
unsafe_allow_html=True)
#
st.text("")
# Display History chat
history_chat = st.empty()
# Input text
current_message = st.text_area("", value=f"Hello {bot_value}, how are you?")
if st.button("Send"):
# Add user response to History
messages_history.append(current_message)
# Response bot
response = Rick_bot.get_response(messages_history)
# Add bot response to History
messages_history.append(response)
# format message:
Res = []
for i,res in enumerate(messages_history):
if i%2==0: # User
Res.append(f">> User: {res}\n ")
else: # Bot
Res.append(f">> Rick bot: {res}\n ")
# Show history chat
history_chat.text("".join(Res))
# TODO: enviar input text usando text_area