File size: 2,522 Bytes
d09befd
 
 
 
 
 
 
147d548
d09befd
 
 
147d548
d09befd
147d548
d09befd
 
147d548
d09befd
 
147d548
d09befd
 
 
 
147d548
d09befd
 
 
147d548
d09befd
 
 
 
 
 
 
 
 
 
 
 
 
147d548
d09befd
 
 
 
 
 
 
147d548
d09befd
 
 
147d548
d09befd
 
 
 
 
 
 
147d548
d09befd
 
 
 
 
 
147d548
d09befd
c43f786
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
import streamlit as st
from chatbot import (
    get_googlegenai_client,
    get_default_model,
    load_chat_history,
    save_chat_history
)

st.markdown("""
    <h1 style='text-align: center;'>Welcome, ChatGPT Clone</h1>
""", unsafe_allow_html=True)

client = get_googlegenai_client()

if "googlegenai_model" not in st.session_state:
    st.session_state["googlegenai_model"] = get_default_model()

if "messages" not in st.session_state:
    st.session_state.messages = load_chat_history()

with st.sidebar:
    if st.button("Delete Chat History"):
        st.session_state.messages = []
        save_chat_history([])

for msg in st.session_state.messages:
    role = msg["role"]
    content = msg["content"]

    if role == "user":
        st.markdown(f"""
        <div style='text-align: right; background-color: #f0f0f5; padding: 10px 15px; border-radius: 20px; margin: 10px 0; display: inline-block; max-width: 80%; float: right; clear: both;'>
            {content}
        </div>
        """, unsafe_allow_html=True)
    else:
        st.markdown(f"""
        <div style='text-align: left; margin: 10px 0; max-width: 80%; float: left; clear: both;'>
            {content}
        </div>
        """, unsafe_allow_html=True)
    st.markdown("<div style='clear: both'></div>", unsafe_allow_html=True)

if prompt := st.chat_input("Ask anything"):
    st.session_state.messages.append({"role": "user", "content": prompt})
    st.markdown(f"""
    <div style='text-align: right; background-color: #f0f0f5; padding: 10px 15px; border-radius: 20px; margin: 10px 0; display: inline-block; max-width: 80%; float: right; clear: both;'>
        {prompt}
    </div>
    """, unsafe_allow_html=True)

    full_response = ""
    response_container = st.empty()
    response = client.stream(st.session_state.messages)

    for chunk in response:
        full_response += chunk.content or ""
        response_container.markdown(f"""
        <div style='text-align: left; margin: 10px 0; max-width: 80%; float: left; clear: both;'>
            {full_response + "▌"}
        </div>
        """, unsafe_allow_html=True)

    response_container.markdown(f"""
    <div style='text-align: left; margin: 10px 0; max-width: 80%; float: left; clear: both;'>
        {full_response}
    </div>
    """, unsafe_allow_html=True)
    st.markdown("<div style='clear: both'></div>", unsafe_allow_html=True)

    st.session_state.messages.append({"role": "assistant", "content": full_response})
    save_chat_history(st.session_state.messages)