Spaces:
Sleeping
Sleeping
Commit
·
a98205d
1
Parent(s):
c169c98
Update app.py
Browse files
app.py
CHANGED
|
@@ -92,13 +92,6 @@ body {
|
|
| 92 |
#clear-btn:hover {
|
| 93 |
background-color: #fff9e6 !important;
|
| 94 |
}
|
| 95 |
-
/* User/assistant emoji in chat */
|
| 96 |
-
.user-msg::before {
|
| 97 |
-
content: "👤 ";
|
| 98 |
-
}
|
| 99 |
-
.bot-msg::before {
|
| 100 |
-
content: "🛡️ ";
|
| 101 |
-
}
|
| 102 |
/* Hide elements */
|
| 103 |
footer {
|
| 104 |
display: none !important;
|
|
@@ -157,53 +150,8 @@ with gr.Blocks(css=css) as demo:
|
|
| 157 |
chatbot = gr.Chatbot(
|
| 158 |
label="Conversation",
|
| 159 |
show_label=True,
|
| 160 |
-
height=400
|
| 161 |
-
elem_classes=["chat-box"]
|
| 162 |
)
|
| 163 |
-
|
| 164 |
-
# Add JavaScript to inject user/assistant emojis
|
| 165 |
-
gr.HTML("""
|
| 166 |
-
<script>
|
| 167 |
-
// Function to add emojis to chat messages
|
| 168 |
-
function addEmojisToChat() {
|
| 169 |
-
setTimeout(function() {
|
| 170 |
-
// Find all user messages and add emoji
|
| 171 |
-
document.querySelectorAll('.user').forEach(function(el) {
|
| 172 |
-
if (!el.querySelector('.emoji-added')) {
|
| 173 |
-
let textEl = el.querySelector('p');
|
| 174 |
-
if (textEl) {
|
| 175 |
-
textEl.innerHTML = '👤 ' + textEl.innerHTML;
|
| 176 |
-
let span = document.createElement('span');
|
| 177 |
-
span.className = 'emoji-added';
|
| 178 |
-
span.style.display = 'none';
|
| 179 |
-
el.appendChild(span);
|
| 180 |
-
}
|
| 181 |
-
}
|
| 182 |
-
});
|
| 183 |
-
|
| 184 |
-
// Find all bot messages and add emoji
|
| 185 |
-
document.querySelectorAll('.bot').forEach(function(el) {
|
| 186 |
-
if (!el.querySelector('.emoji-added')) {
|
| 187 |
-
let textEl = el.querySelector('p');
|
| 188 |
-
if (textEl) {
|
| 189 |
-
textEl.innerHTML = '🛡️ ' + textEl.innerHTML;
|
| 190 |
-
let span = document.createElement('span');
|
| 191 |
-
span.className = 'emoji-added';
|
| 192 |
-
span.style.display = 'none';
|
| 193 |
-
el.appendChild(span);
|
| 194 |
-
}
|
| 195 |
-
}
|
| 196 |
-
});
|
| 197 |
-
|
| 198 |
-
// Call this function again to catch new messages
|
| 199 |
-
addEmojisToChat();
|
| 200 |
-
}, 1000);
|
| 201 |
-
}
|
| 202 |
-
|
| 203 |
-
// Start the process
|
| 204 |
-
window.addEventListener('load', addEmojisToChat);
|
| 205 |
-
</script>
|
| 206 |
-
""")
|
| 207 |
with gr.Row():
|
| 208 |
user_input = gr.Textbox(
|
| 209 |
placeholder="Type your message here...",
|
|
@@ -223,6 +171,7 @@ with gr.Blocks(css=css) as demo:
|
|
| 223 |
# Fix 1: Correct event handling for the chatbot interface
|
| 224 |
def user(user_message, history):
|
| 225 |
# Return the user's message and add it to history
|
|
|
|
| 226 |
return "", history + [[user_message, None]]
|
| 227 |
|
| 228 |
def bot(history, system_message, max_tokens, temperature, top_p, selected_model):
|
|
@@ -248,8 +197,33 @@ with gr.Blocks(css=css) as demo:
|
|
| 248 |
for response in response_generator:
|
| 249 |
history[-1][1] = response
|
| 250 |
yield history
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 251 |
|
| 252 |
-
# Wire up the event chain
|
| 253 |
user_input.submit(
|
| 254 |
user,
|
| 255 |
[user_input, chatbot],
|
|
@@ -260,6 +234,10 @@ with gr.Blocks(css=css) as demo:
|
|
| 260 |
[chatbot, system_message, max_tokens_slider, temperature_slider, top_p_slider, model_dropdown],
|
| 261 |
[chatbot],
|
| 262 |
queue=True
|
|
|
|
|
|
|
|
|
|
|
|
|
| 263 |
)
|
| 264 |
|
| 265 |
send_button.click(
|
|
@@ -272,6 +250,10 @@ with gr.Blocks(css=css) as demo:
|
|
| 272 |
[chatbot, system_message, max_tokens_slider, temperature_slider, top_p_slider, model_dropdown],
|
| 273 |
[chatbot],
|
| 274 |
queue=True
|
|
|
|
|
|
|
|
|
|
|
|
|
| 275 |
)
|
| 276 |
|
| 277 |
# Clear the chat history - using a proper function
|
|
|
|
| 92 |
#clear-btn:hover {
|
| 93 |
background-color: #fff9e6 !important;
|
| 94 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 95 |
/* Hide elements */
|
| 96 |
footer {
|
| 97 |
display: none !important;
|
|
|
|
| 150 |
chatbot = gr.Chatbot(
|
| 151 |
label="Conversation",
|
| 152 |
show_label=True,
|
| 153 |
+
height=400
|
|
|
|
| 154 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 155 |
with gr.Row():
|
| 156 |
user_input = gr.Textbox(
|
| 157 |
placeholder="Type your message here...",
|
|
|
|
| 171 |
# Fix 1: Correct event handling for the chatbot interface
|
| 172 |
def user(user_message, history):
|
| 173 |
# Return the user's message and add it to history
|
| 174 |
+
# No emoji here since we'll add it during display
|
| 175 |
return "", history + [[user_message, None]]
|
| 176 |
|
| 177 |
def bot(history, system_message, max_tokens, temperature, top_p, selected_model):
|
|
|
|
| 197 |
for response in response_generator:
|
| 198 |
history[-1][1] = response
|
| 199 |
yield history
|
| 200 |
+
|
| 201 |
+
# Add a function to display history with emojis
|
| 202 |
+
def display_with_emojis(history):
|
| 203 |
+
if history is None or len(history) == 0:
|
| 204 |
+
return []
|
| 205 |
+
|
| 206 |
+
# Create a new history with emojis added
|
| 207 |
+
history_with_emojis = []
|
| 208 |
+
for user_msg, assistant_msg in history:
|
| 209 |
+
# Add the user emoji to the user message
|
| 210 |
+
if user_msg:
|
| 211 |
+
user_msg_with_emoji = f"👤 {user_msg}"
|
| 212 |
+
else:
|
| 213 |
+
user_msg_with_emoji = user_msg
|
| 214 |
+
|
| 215 |
+
# Add the assistant emoji to the assistant message
|
| 216 |
+
if assistant_msg:
|
| 217 |
+
assistant_msg_with_emoji = f"🛡️ {assistant_msg}"
|
| 218 |
+
else:
|
| 219 |
+
assistant_msg_with_emoji = assistant_msg
|
| 220 |
+
|
| 221 |
+
# Add the modified messages to the new history
|
| 222 |
+
history_with_emojis.append([user_msg_with_emoji, assistant_msg_with_emoji])
|
| 223 |
+
|
| 224 |
+
return history_with_emojis
|
| 225 |
|
| 226 |
+
# Wire up the event chain with emoji display
|
| 227 |
user_input.submit(
|
| 228 |
user,
|
| 229 |
[user_input, chatbot],
|
|
|
|
| 234 |
[chatbot, system_message, max_tokens_slider, temperature_slider, top_p_slider, model_dropdown],
|
| 235 |
[chatbot],
|
| 236 |
queue=True
|
| 237 |
+
).then(
|
| 238 |
+
display_with_emojis, # Add the emoji processing step
|
| 239 |
+
[chatbot],
|
| 240 |
+
[chatbot]
|
| 241 |
)
|
| 242 |
|
| 243 |
send_button.click(
|
|
|
|
| 250 |
[chatbot, system_message, max_tokens_slider, temperature_slider, top_p_slider, model_dropdown],
|
| 251 |
[chatbot],
|
| 252 |
queue=True
|
| 253 |
+
).then(
|
| 254 |
+
display_with_emojis, # Add the emoji processing step
|
| 255 |
+
[chatbot],
|
| 256 |
+
[chatbot]
|
| 257 |
)
|
| 258 |
|
| 259 |
# Clear the chat history - using a proper function
|