Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -4,48 +4,57 @@ import json
|
|
4 |
import os
|
5 |
import uuid
|
6 |
|
7 |
-
#
|
8 |
st.markdown(
|
9 |
"""
|
10 |
<style>
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
|
|
|
|
17 |
}
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
|
|
|
|
22 |
}
|
|
|
|
|
23 |
.chat-message {
|
24 |
-
padding:
|
25 |
-
border-radius:
|
26 |
margin-bottom: 10px;
|
27 |
-
max-width:
|
28 |
}
|
|
|
29 |
.chat-message.user {
|
30 |
background-color: #4A90E2;
|
31 |
color: white;
|
32 |
margin-left: auto;
|
|
|
33 |
}
|
|
|
34 |
.chat-message.assistant {
|
35 |
-
background-color: #
|
36 |
color: black;
|
37 |
margin-right: auto;
|
38 |
}
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
padding: 10px;
|
47 |
-
box-shadow: 0 -2px 5px rgba(0, 0, 0, 0.1);
|
48 |
}
|
|
|
|
|
49 |
.loading {
|
50 |
text-align: center;
|
51 |
font-size: 1.2em;
|
@@ -57,7 +66,8 @@ st.markdown(
|
|
57 |
unsafe_allow_html=True,
|
58 |
)
|
59 |
|
60 |
-
|
|
|
61 |
|
62 |
# Inisialisasi file database
|
63 |
DATABASE_FILE = "database.json"
|
@@ -79,7 +89,7 @@ def save_database(database):
|
|
79 |
|
80 |
database = load_database()
|
81 |
|
82 |
-
# Generate
|
83 |
if "user_id" not in st.session_state:
|
84 |
st.session_state.user_id = str(uuid.uuid4())
|
85 |
|
@@ -91,7 +101,7 @@ if "sessions" not in st.session_state:
|
|
91 |
if "current_session" not in st.session_state:
|
92 |
st.session_state.current_session = None
|
93 |
|
94 |
-
# Sidebar
|
95 |
with st.sidebar:
|
96 |
st.header("π Session List")
|
97 |
|
@@ -134,14 +144,28 @@ if st.session_state.current_session:
|
|
134 |
st.header(f"π¬ Percakapan: {st.session_state.current_session}")
|
135 |
|
136 |
for message in st.session_state.sessions[st.session_state.current_session]:
|
137 |
-
|
138 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
139 |
|
140 |
# Input pengguna
|
141 |
if prompt := st.chat_input("Ketik pesan Anda di sini..."):
|
142 |
st.session_state.sessions[st.session_state.current_session].append({"role": "user", "content": prompt})
|
|
|
143 |
with st.chat_message("user"):
|
144 |
-
st.markdown(
|
|
|
|
|
|
|
|
|
|
|
145 |
|
146 |
# Loading Animation
|
147 |
loading_placeholder = st.empty()
|
@@ -156,7 +180,12 @@ if st.session_state.current_session:
|
|
156 |
provider=g4f.Provider.Blackbox
|
157 |
)
|
158 |
loading_placeholder.empty()
|
159 |
-
st.markdown(
|
|
|
|
|
|
|
|
|
|
|
160 |
|
161 |
# Tambahkan respons ke riwayat percakapan
|
162 |
st.session_state.sessions[st.session_state.current_session].append({"role": "assistant", "content": response})
|
|
|
4 |
import os
|
5 |
import uuid
|
6 |
|
7 |
+
# Styling dengan CSS untuk tampilan lebih mirip ChatGPT
|
8 |
st.markdown(
|
9 |
"""
|
10 |
<style>
|
11 |
+
/* Tombol menu sidebar */
|
12 |
+
.menu-btn {
|
13 |
+
position: fixed;
|
14 |
+
top: 10px;
|
15 |
+
left: 10px;
|
16 |
+
font-size: 24px;
|
17 |
+
cursor: pointer;
|
18 |
+
z-index: 1001;
|
19 |
}
|
20 |
+
|
21 |
+
/* Styling sidebar */
|
22 |
+
[data-testid="stSidebar"] {
|
23 |
+
background-color: #f8f9fa;
|
24 |
+
width: 300px;
|
25 |
+
border-right: 2px solid #ddd;
|
26 |
}
|
27 |
+
|
28 |
+
/* Styling chat bubble */
|
29 |
.chat-message {
|
30 |
+
padding: 12px 15px;
|
31 |
+
border-radius: 15px;
|
32 |
margin-bottom: 10px;
|
33 |
+
max-width: 75%;
|
34 |
}
|
35 |
+
|
36 |
.chat-message.user {
|
37 |
background-color: #4A90E2;
|
38 |
color: white;
|
39 |
margin-left: auto;
|
40 |
+
text-align: right;
|
41 |
}
|
42 |
+
|
43 |
.chat-message.assistant {
|
44 |
+
background-color: #e9ecef;
|
45 |
color: black;
|
46 |
margin-right: auto;
|
47 |
}
|
48 |
+
|
49 |
+
/* Avatar */
|
50 |
+
.chat-avatar {
|
51 |
+
width: 40px;
|
52 |
+
height: 40px;
|
53 |
+
border-radius: 50%;
|
54 |
+
object-fit: cover;
|
|
|
|
|
55 |
}
|
56 |
+
|
57 |
+
/* Loading animation */
|
58 |
.loading {
|
59 |
text-align: center;
|
60 |
font-size: 1.2em;
|
|
|
66 |
unsafe_allow_html=True,
|
67 |
)
|
68 |
|
69 |
+
# Tombol β° di pojok kiri atas untuk membuka sidebar
|
70 |
+
st.markdown('<div class="menu-btn">β°</div>', unsafe_allow_html=True)
|
71 |
|
72 |
# Inisialisasi file database
|
73 |
DATABASE_FILE = "database.json"
|
|
|
89 |
|
90 |
database = load_database()
|
91 |
|
92 |
+
# Generate user_id jika belum ada
|
93 |
if "user_id" not in st.session_state:
|
94 |
st.session_state.user_id = str(uuid.uuid4())
|
95 |
|
|
|
101 |
if "current_session" not in st.session_state:
|
102 |
st.session_state.current_session = None
|
103 |
|
104 |
+
# Sidebar dengan tombol collapse
|
105 |
with st.sidebar:
|
106 |
st.header("π Session List")
|
107 |
|
|
|
144 |
st.header(f"π¬ Percakapan: {st.session_state.current_session}")
|
145 |
|
146 |
for message in st.session_state.sessions[st.session_state.current_session]:
|
147 |
+
role = message["role"]
|
148 |
+
avatar = "π€" if role == "user" else "π€"
|
149 |
+
|
150 |
+
with st.chat_message(role):
|
151 |
+
st.markdown(
|
152 |
+
f'<div class="chat-message {role}">'
|
153 |
+
f'<span style="font-size:20px;">{avatar}</span> '
|
154 |
+
f'{message["content"]}</div>',
|
155 |
+
unsafe_allow_html=True
|
156 |
+
)
|
157 |
|
158 |
# Input pengguna
|
159 |
if prompt := st.chat_input("Ketik pesan Anda di sini..."):
|
160 |
st.session_state.sessions[st.session_state.current_session].append({"role": "user", "content": prompt})
|
161 |
+
|
162 |
with st.chat_message("user"):
|
163 |
+
st.markdown(
|
164 |
+
f'<div class="chat-message user">'
|
165 |
+
f'<span style="font-size:20px;">π€</span> '
|
166 |
+
f'{prompt}</div>',
|
167 |
+
unsafe_allow_html=True
|
168 |
+
)
|
169 |
|
170 |
# Loading Animation
|
171 |
loading_placeholder = st.empty()
|
|
|
180 |
provider=g4f.Provider.Blackbox
|
181 |
)
|
182 |
loading_placeholder.empty()
|
183 |
+
st.markdown(
|
184 |
+
f'<div class="chat-message assistant">'
|
185 |
+
f'<span style="font-size:20px;">π€</span> '
|
186 |
+
f'{response}</div>',
|
187 |
+
unsafe_allow_html=True
|
188 |
+
)
|
189 |
|
190 |
# Tambahkan respons ke riwayat percakapan
|
191 |
st.session_state.sessions[st.session_state.current_session].append({"role": "assistant", "content": response})
|