-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchatui.py
66 lines (50 loc) · 1.82 KB
/
chatui.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
import streamlit as st
from source.chatbot import Chatbot
RENDERED_MESSAGES = "rendered_messages"
CHAT_HISTORY = "chat_history"
GPN_CHAT_PIPELINE = "gpn_chat_pipeline"
def main() -> None:
title = "GPN Chat"
st.set_page_config(page_title=title)
st.title(title)
configuration = configure_state()
initialize_session_state(configuration)
render_history()
run_ui()
def configure_state() -> dict:
return {
RENDERED_MESSAGES: [],
CHAT_HISTORY: [],
GPN_CHAT_PIPELINE: Chatbot(),
}
def initialize_session_state(config: dict) -> None:
"""
Initialize Streamlit session state variables using the provided configuration.
Args:
config (dict): Configuration dictionary.
"""
for key, value in config.items():
if key not in st.session_state:
st.session_state[key] = value
def render_history() -> None:
for message in st.session_state[RENDERED_MESSAGES]:
with st.chat_message(message["role"]):
st.markdown(message["content"])
def run_ui() -> None:
# Accept user input
if prompt := st.chat_input("Womit kann ich helfen?"):
# Display user message in chat message container
with st.chat_message("user"):
st.markdown(prompt)
# Add user message to chat history
st.session_state[RENDERED_MESSAGES].append({"role": "user", "content": prompt})
# Display assistant response in chat message container
with st.chat_message("assistant"):
with st.spinner("Generiere Antwort ..."):
response = st.session_state[GPN_CHAT_PIPELINE].run(prompt)
# Add assistant response to chat history
st.session_state[RENDERED_MESSAGES].append(
{"role": "assistant", "content": response}
)
if __name__ == "__main__":
main()