File size: 3,317 Bytes
19f4fce
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import time

import streamlit as st

def display_chat_history(model_name: str):
    for message in st.session_state[model_name]:
        with st.chat_message(message["role"]):
            st.markdown(message["content"])

def chat_input(model_name: str):
    if prompt := st.chat_input("Say something"):
        # Display user message in chat message container
        st.chat_message("user").markdown(prompt)

        # Add user message to chat history
        st.session_state[model_name].append({"role": "user", "content": prompt})

        return prompt

def display_bot_msg(model_name: str, bot_response: str):
    # Display assistant response in chat message container
    with st.chat_message("assistant"):
        message_placeholder = st.empty()
        full_response = ""

        # simulate the chatbot "thinking" before responding
        # (or stream its response)
        for chunk in bot_response.split():
            full_response += chunk + " "
            time.sleep(0.05)

            # add a blinking cursor to simulate typing
            message_placeholder.markdown(full_response + "▌")

        message_placeholder.markdown(full_response)
        # st.markdown(response)

    # Add assistant response to chat history
    st.session_state[model_name].append(
        {"model_name": model_name, "role": "assistant", "content": full_response}
    )

# @st.cache_data
def chatbox(model_name: str, model: None):
    # Display chat messages from history on app rerun
    for message in st.session_state.messages:
        if (message["model_name"] == model_name):
            with st.chat_message(message["role"]):
                st.markdown(message["content"])

    if prompt := st.chat_input("Say something"):
        # Display user message in chat message container
        st.chat_message("user").markdown(prompt)

        # Add user message to chat history
        st.session_state.messages.append({"model_name": model_name, "role": "user", "content": prompt})

        with st.spinner("Processing your query..."):
            bot_response = model.get_response(prompt)

        print("bot: ", bot_response)

        # Display assistant response in chat message container
        with st.chat_message("assistant"):
            message_placeholder = st.empty()
            full_response = ""

            # simulate the chatbot "thinking" before responding
            # (or stream its response)
            for chunk in bot_response.split():
                full_response += chunk + " "
                time.sleep(0.05)

                # add a blinking cursor to simulate typing
                message_placeholder.markdown(full_response + "▌")

            message_placeholder.markdown(full_response)
            # st.markdown(response)

        # Add assistant response to chat history
        st.session_state.messages.append(
            {"model_name": model_name, "role": "assistant", "content": full_response}
        )

        # Scroll to the bottom of the chat container
        # st.markdown(
        #     """
        #     <script>
        #     const chatContainer = document.getElementsByClassName("css-1n76uvr")[0];
        #     chatContainer.scrollTop = chatContainer.scrollHeight;
        #     </script>
        #     """,
        #     unsafe_allow_html=True,
        # )