-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathapp.py
80 lines (57 loc) · 2.21 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
import streamlit as st
import os
from io import StringIO
import requests
st.set_page_config(page_title='Gemma2 Chatbot',
page_icon = "images/gemma_avatar.jpg",
initial_sidebar_state = 'auto')
background_color = "#252740"
avatars = {
"assistant" : "images/gemma_avatar.jpg",
"user": "images/user_avatar.png"
}
st.markdown("<h2 style='text-align: center; color: #3184a0;'>Gemma2 Chatbot</h2>", unsafe_allow_html=True)
with st.sidebar:
st.image("images/gemma.jpg")
if "messages" not in st.session_state.keys():
st.session_state.messages = [
{"role": "assistant", "content": "How may I assist you today?"}
]
for message in st.session_state.messages:
with st.chat_message(message["role"],
avatar=avatars[message["role"]]):
st.write(message["content"])
def clear_chat_history():
st.session_state.messages = [
{"role": "assistant", "content": "How may I assist you today?"}
]
st.sidebar.button("Clear Chat History", on_click=clear_chat_history)
def run_query(input_text):
"""
"""
data={'input_text': input_text}
r = requests.post('http://127.0.0.1:8000/chat_messages', json = data)
if r.status_code == 200:
print(r.content)
agent_message = r.json()["agent"]
return agent_message
return "Error"
output = st.empty()
if prompt := st.chat_input():
st.session_state.messages.append({"role": "user", "content": prompt})
with st.chat_message("user", avatar=avatars["user"]):
st.write(prompt)
if st.session_state.messages[-1]["role"] != "assistant":
with st.chat_message("assistant", avatar=avatars["assistant"]):
with st.spinner("Thinking..."):
response = run_query(prompt)
placeholder = st.empty()
full_response = ""
for item in response:
full_response += item
placeholder.markdown(full_response, unsafe_allow_html=True)
placeholder.markdown(response, unsafe_allow_html=True)
message = {"role": "assistant",
"content": response,
"avatar": avatars["assistant"]}
st.session_state.messages.append(message)