File size: 10,040 Bytes
64bfc45
 
 
 
8da1c57
64bfc45
4858261
ea2b64e
 
 
4858261
 
 
 
8b3d2bd
1fe38bb
4858261
 
 
 
 
1fe38bb
4858261
 
 
 
 
8b3d2bd
1fe38bb
4858261
 
 
 
 
 
8b3d2bd
1fe38bb
4858261
 
8b3d2bd
1fe38bb
4858261
1fe38bb
 
 
4858261
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8b3d2bd
1fe38bb
 
4858261
 
 
 
 
 
 
 
 
8b3d2bd
ea2b64e
4858261
 
 
 
 
 
8b3d2bd
4858261
 
 
 
 
 
 
 
 
 
64bfc45
4858261
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
64bfc45
4858261
 
 
 
 
 
 
 
64bfc45
4858261
 
 
64bfc45
4858261
 
 
 
8da1c57
4858261
 
 
 
64bfc45
4858261
64bfc45
4858261
 
 
 
64bfc45
4858261
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ea2b64e
4858261
 
 
 
 
 
 
 
 
 
 
 
 
8b3d2bd
 
4858261
 
 
 
 
 
 
 
 
 
 
 
 
 
8b3d2bd
4858261
 
 
1fe38bb
4858261
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1fe38bb
4858261
 
 
 
 
 
 
 
 
 
1fe38bb
 
8b3d2bd
4858261
 
 
 
 
 
8b3d2bd
4858261
 
 
 
 
 
 
1fe38bb
4858261
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1fe38bb
4858261
 
 
 
 
 
 
 
1fe38bb
 
8b3d2bd
4858261
8b3d2bd
4858261
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
import streamlit as st
import g4f
import json
import os
import uuid

# Custom CSS untuk tampilan premium
st.markdown(
    """
    <style>
    /* Main container styling */
    .main {
        background-color: #f5f5f5;
        padding: 20px 5%;
    }

    /* Chat container styling */
    .chat-container {
        max-width: 800px;
        margin: 0 auto;
        background: white;
        border-radius: 15px;
        box-shadow: 0 2px 10px rgba(0,0,0,0.1);
        padding: 20px;
        min-height: 70vh;
        max-height: 70vh;
        overflow-y: auto;
    }

    /* Message styling */
    .message {
        margin: 15px 0;
        display: flex;
        gap: 12px;
        align-items: start;
    }

    .user-message {
        flex-direction: row-reverse;
    }

    .avatar {
        width: 40px;
        height: 40px;
        border-radius: 50%;
        display: flex;
        align-items: center;
        justify-content: center;
        flex-shrink: 0;
    }

    .user-avatar {
        background: #10a37f;
        color: white;
    }

    .bot-avatar {
        background: #e5e7eb;
        color: #6b7280;
    }

    .message-content {
        max-width: 70%;
        padding: 12px 16px;
        border-radius: 12px;
        line-height: 1.5;
        font-size: 16px;
    }

    .user-content {
        background: #10a37f;
        color: white;
        border-radius: 12px 12px 0 12px;
    }

    .bot-content {
        background: #f3f4f6;
        color: #374151;
        border-radius: 12px 12px 12px 0;
    }

    /* Input area styling */
    .input-container {
        max-width: 800px;
        margin: 20px auto;
        position: relative;
    }

    /* Loading animation */
    .dot-flashing {
        position: relative;
        width: 10px;
        height: 10px;
        border-radius: 5px;
        background-color: #10a37f;
        color: #10a37f;
        animation: dotFlashing 1s infinite linear alternate;
        animation-delay: .5s;
    }

    .dot-flashing::before, .dot-flashing::after {
        content: '';
        display: inline-block;
        position: absolute;
        top: 0;
    }

    .dot-flashing::before {
        left: -15px;
        width: 10px;
        height: 10px;
        border-radius: 5px;
        background-color: #10a37f;
        color: #10a37f;
        animation: dotFlashing 1s infinite alternate;
        animation-delay: 0s;
    }

    .dot-flashing::after {
        left: 15px;
        width: 10px;
        height: 10px;
        border-radius: 5px;
        background-color: #10a37f;
        color: #10a37f;
        animation: dotFlashing 1s infinite alternate;
        animation-delay: 1s;
    }

    @keyframes dotFlashing {
        0% { background-color: #10a37f; }
        50%, 100% { background-color: rgba(16, 163, 127, 0.2); }
    }

    /* Scrollbar styling */
    ::-webkit-scrollbar {
        width: 8px;
    }

    ::-webkit-scrollbar-track {
        background: #f1f1f1;
    }

    ::-webkit-scrollbar-thumb {
        background: #888;
        border-radius: 4px;
    }

    /* Session list styling */
    .session-item {
        padding: 10px;
        margin: 5px 0;
        border-radius: 8px;
        cursor: pointer;
        transition: all 0.2s;
    }

    .session-item:hover {
        background: #e5e7eb;
    }

    .active-session {
        background: #10a37f !important;
        color: white;
    }

    </style>
    """,
    unsafe_allow_html=True,
)

# Fungsi database tetap sama...

# Inisialisasi state
# ... (tetap sama dengan sebelumnya) ...

# Sidebar yang lebih modern
with st.sidebar:
    st.markdown("## πŸ’¬ Chat Sessions")
    st.markdown("---")
    
    # Buat session baru
    with st.expander("βž• New Session", expanded=True):
        new_session_name = st.text_input("Session name", key="new_session")
        if st.button("Create", key="create_btn"):
            if new_session_name:
                if new_session_name not in st.session_state.sessions:
                    st.session_state.sessions[new_session_name] = []
                    st.session_state.current_session = new_session_name
                    database[user_id] = st.session_state.sessions
                    save_database(database)
                    st.rerun()
                else:
                    st.error("Name already exists!")
            else:
                st.error("Please enter a name")

    st.markdown("---")
    st.markdown("### πŸ“š Your Sessions")
    
    # Daftar session
    sessions = list(st.session_state.sessions.keys())
    for idx, session in enumerate(sessions):
        is_active = session == st.session_state.current_session
        session_class = "active-session" if is_active else "session-item"
        st.markdown(
            f'<div class="{session_class}" onclick="setSession({idx})">{session}</div>',
            unsafe_allow_html=True
        )

    # JavaScript untuk session selection
    st.markdown(
        """
        <script>
        function setSession(index) {
            window.parent.postMessage({
                type: 'setCurrentSession',
                index: index
            }, '*');
        }
        </script>
        """,
        unsafe_allow_html=True
    )

# Area chat utama
st.markdown('<div class="main">', unsafe_allow_html=True)
st.markdown('<div class="chat-container">', unsafe_allow_html=True)

if st.session_state.current_session:
    # Tampilkan chat history
    for message in st.session_state.sessions[st.session_state.current_session]:
        if message["role"] == "user":
            st.markdown(
                f'''
                <div class="message user-message">
                    <div class="message-content user-content">
                        {message["content"]}
                    </div>
                    <div class="avatar user-avatar">
                        <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-user"><path d="M20 21v-2a4 4 0 0 0-4-4H8a4 4 0 0 0-4 4v2"></path><circle cx="12" cy="7" r="4"></circle></svg>
                    </div>
                </div>
                ''',
                unsafe_allow_html=True
            )
        else:
            st.markdown(
                f'''
                <div class="message">
                    <div class="avatar bot-avatar">
                        <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-message-circle"><path d="M21 11.5a8.38 8.38 0 0 1-.9 3.8 8.5 8.5 0 0 1-7.6 4.7 8.38 8.38 0 0 1-3.8-.9L3 21l1.9-5.7a8.38 8.38 0 0 1-.9-3.8 8.5 8.5 0 0 1 4.7-7.6 8.38 8.38 0 0 1 3.8-.9h.5a8.48 8.48 0 0 1 8 8v.5z"></path></svg>
                    </div>
                    <div class="message-content bot-content">
                        {message["content"]}
                    </div>
                </div>
                ''',
                unsafe_allow_html=True
            )

    # Input area
    st.markdown('</div>', unsafe_allow_html=True)  # Tutup chat container
    st.markdown('<div class="input-container">', unsafe_allow_html=True)
    
    if prompt := st.chat_input("Message ChatGPT..."):
        # Tambahkan pesan user
        st.session_state.sessions[st.session_state.current_session].append({"role": "user", "content": prompt})
        
        # Tampilkan loading
        loading_placeholder = st.empty()
        loading_placeholder.markdown(
            '<div class="message"><div class="avatar bot-avatar"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-message-circle"><path d="M21 11.5a8.38 8.38 0 0 1-.9 3.8 8.5 8.5 0 0 1-7.6 4.7 8.38 8.38 0 0 1-3.8-.9L3 21l1.9-5.7a8.38 8.38 0 0 1-.9-3.8 8.5 8.5 0 0 1 4.7-7.6 8.38 8.38 0 0 1 3.8-.9h.5a8.48 8.48 0 0 1 8 8v.5z"></path></svg></div><div class="dot-flashing"></div></div>',
            unsafe_allow_html=True
        )

        # Dapatkan respons
        try:
            response = g4f.ChatCompletion.create(
                model="blackboxai",
                messages=st.session_state.sessions[st.session_state.current_session],
                provider=g4f.Provider.Blackbox
            )
            
            # Hapus loading dan tampilkan respons
            loading_placeholder.empty()
            st.session_state.sessions[st.session_state.current_session].append({"role": "assistant", "content": response})
            database[user_id] = st.session_state.sessions
            save_database(database)
            st.rerun()
            
        except Exception as e:
            loading_placeholder.empty()
            st.markdown(
                f'''
                <div class="message">
                    <div class="avatar bot-avatar">⚠️</div>
                    <div class="message-content bot-content" style="color: #dc2626;">
                        Error: {str(e)}
                    </div>
                </div>
                ''',
                unsafe_allow_html=True
            )

    st.markdown('</div></div>', unsafe_allow_html=True)  # Tutup input container dan main
else:
    st.markdown('</div>', unsafe_allow_html=True)  # Tutup chat container
    st.markdown('<div class="input-container">', unsafe_allow_html=True)
    st.info("Please select or create a session from the sidebar")
    st.markdown('</div></div>', unsafe_allow_html=True)

# Auto-scroll ke pesan terakhir
st.markdown(
    """
    <script>
    window.addEventListener('load', function() {
        var chatContainer = window.parent.document.querySelector('.chat-container');
        if (chatContainer) {
            chatContainer.scrollTop = chatContainer.scrollHeight;
        }
    });
    </script>
    """,
    unsafe_allow_html=True
)