Spaces:
Sleeping
Sleeping
Commit
·
579c644
1
Parent(s):
9b59d5d
Update app.py
Browse files
app.py
CHANGED
|
@@ -13,7 +13,7 @@ def respond(message, history, system_message, max_tokens, temperature, top_p, se
|
|
| 13 |
# Create an InferenceClient for the selected model
|
| 14 |
client = InferenceClient(model_list.get(selected_model, "HuggingFaceH4/zephyr-7b-beta"))
|
| 15 |
|
| 16 |
-
# Build conversation messages for the client
|
| 17 |
messages = [{"role": "system", "content": system_message}]
|
| 18 |
for user_msg, assistant_msg in history:
|
| 19 |
if user_msg: # Only add non-empty messages
|
|
@@ -147,11 +147,35 @@ with gr.Blocks(css=css) as demo:
|
|
| 147 |
|
| 148 |
# Main area: Chat interface
|
| 149 |
with gr.Column(scale=3):
|
|
|
|
| 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...",
|
|
@@ -168,10 +192,9 @@ with gr.Blocks(css=css) as demo:
|
|
| 168 |
with gr.Row():
|
| 169 |
clear_button = gr.Button("Clear Chat", elem_id="clear-btn")
|
| 170 |
|
| 171 |
-
#
|
| 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,39 +220,8 @@ with gr.Blocks(css=css) as demo:
|
|
| 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 (only if it doesn't already have one)
|
| 210 |
-
if user_msg:
|
| 211 |
-
if not user_msg.startswith("👤 "):
|
| 212 |
-
user_msg_with_emoji = f"👤 {user_msg}"
|
| 213 |
-
else:
|
| 214 |
-
user_msg_with_emoji = user_msg
|
| 215 |
-
else:
|
| 216 |
-
user_msg_with_emoji = user_msg
|
| 217 |
-
|
| 218 |
-
# Add the assistant emoji to the assistant message (only if it doesn't already have one)
|
| 219 |
-
if assistant_msg:
|
| 220 |
-
if not assistant_msg.startswith("🛡️ "):
|
| 221 |
-
assistant_msg_with_emoji = f"🛡️ {assistant_msg}"
|
| 222 |
-
else:
|
| 223 |
-
assistant_msg_with_emoji = assistant_msg
|
| 224 |
-
else:
|
| 225 |
-
assistant_msg_with_emoji = assistant_msg
|
| 226 |
-
|
| 227 |
-
# Add the modified messages to the new history
|
| 228 |
-
history_with_emojis.append([user_msg_with_emoji, assistant_msg_with_emoji])
|
| 229 |
-
|
| 230 |
-
return history_with_emojis
|
| 231 |
|
| 232 |
-
# Wire up the event chain
|
| 233 |
user_input.submit(
|
| 234 |
user,
|
| 235 |
[user_input, chatbot],
|
|
@@ -240,10 +232,6 @@ with gr.Blocks(css=css) as demo:
|
|
| 240 |
[chatbot, system_message, max_tokens_slider, temperature_slider, top_p_slider, model_dropdown],
|
| 241 |
[chatbot],
|
| 242 |
queue=True
|
| 243 |
-
).then(
|
| 244 |
-
display_with_emojis, # Add the emoji processing step
|
| 245 |
-
[chatbot],
|
| 246 |
-
[chatbot]
|
| 247 |
)
|
| 248 |
|
| 249 |
send_button.click(
|
|
@@ -256,13 +244,9 @@ with gr.Blocks(css=css) as demo:
|
|
| 256 |
[chatbot, system_message, max_tokens_slider, temperature_slider, top_p_slider, model_dropdown],
|
| 257 |
[chatbot],
|
| 258 |
queue=True
|
| 259 |
-
).then(
|
| 260 |
-
display_with_emojis, # Add the emoji processing step
|
| 261 |
-
[chatbot],
|
| 262 |
-
[chatbot]
|
| 263 |
)
|
| 264 |
|
| 265 |
-
# Clear the chat history
|
| 266 |
def clear_history():
|
| 267 |
return []
|
| 268 |
|
|
|
|
| 13 |
# Create an InferenceClient for the selected model
|
| 14 |
client = InferenceClient(model_list.get(selected_model, "HuggingFaceH4/zephyr-7b-beta"))
|
| 15 |
|
| 16 |
+
# Build conversation messages for the client - without any emojis in the history
|
| 17 |
messages = [{"role": "system", "content": system_message}]
|
| 18 |
for user_msg, assistant_msg in history:
|
| 19 |
if user_msg: # Only add non-empty messages
|
|
|
|
| 147 |
|
| 148 |
# Main area: Chat interface
|
| 149 |
with gr.Column(scale=3):
|
| 150 |
+
# Using a custom chatbot renderer to add emojis
|
| 151 |
chatbot = gr.Chatbot(
|
| 152 |
label="Conversation",
|
| 153 |
show_label=True,
|
| 154 |
+
height=400,
|
| 155 |
+
render=False # We'll render the messages manually
|
| 156 |
)
|
| 157 |
+
|
| 158 |
+
# Custom renderer for chat messages
|
| 159 |
+
def render_chat(history):
|
| 160 |
+
result = []
|
| 161 |
+
for user_msg, bot_msg in history:
|
| 162 |
+
if user_msg is None:
|
| 163 |
+
user_msg = ""
|
| 164 |
+
if bot_msg is None:
|
| 165 |
+
bot_msg = ""
|
| 166 |
+
|
| 167 |
+
# Add emojis to non-empty messages
|
| 168 |
+
if user_msg and not user_msg.startswith("👤"):
|
| 169 |
+
user_msg = f"👤 {user_msg}"
|
| 170 |
+
if bot_msg and not bot_msg.startswith("🛡️"):
|
| 171 |
+
bot_msg = f"🛡️ {bot_msg}"
|
| 172 |
+
|
| 173 |
+
result.append((user_msg, bot_msg))
|
| 174 |
+
return result
|
| 175 |
+
|
| 176 |
+
# Custom chatbot with emoji rendering
|
| 177 |
+
chatbot.render = render_chat
|
| 178 |
+
|
| 179 |
with gr.Row():
|
| 180 |
user_input = gr.Textbox(
|
| 181 |
placeholder="Type your message here...",
|
|
|
|
| 192 |
with gr.Row():
|
| 193 |
clear_button = gr.Button("Clear Chat", elem_id="clear-btn")
|
| 194 |
|
| 195 |
+
# Define functions for chatbot interactions
|
| 196 |
def user(user_message, history):
|
| 197 |
# Return the user's message and add it to history
|
|
|
|
| 198 |
return "", history + [[user_message, None]]
|
| 199 |
|
| 200 |
def bot(history, system_message, max_tokens, temperature, top_p, selected_model):
|
|
|
|
| 220 |
for response in response_generator:
|
| 221 |
history[-1][1] = response
|
| 222 |
yield history
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 223 |
|
| 224 |
+
# Wire up the event chain - submit and send button work the same way
|
| 225 |
user_input.submit(
|
| 226 |
user,
|
| 227 |
[user_input, chatbot],
|
|
|
|
| 232 |
[chatbot, system_message, max_tokens_slider, temperature_slider, top_p_slider, model_dropdown],
|
| 233 |
[chatbot],
|
| 234 |
queue=True
|
|
|
|
|
|
|
|
|
|
|
|
|
| 235 |
)
|
| 236 |
|
| 237 |
send_button.click(
|
|
|
|
| 244 |
[chatbot, system_message, max_tokens_slider, temperature_slider, top_p_slider, model_dropdown],
|
| 245 |
[chatbot],
|
| 246 |
queue=True
|
|
|
|
|
|
|
|
|
|
|
|
|
| 247 |
)
|
| 248 |
|
| 249 |
+
# Clear the chat history
|
| 250 |
def clear_history():
|
| 251 |
return []
|
| 252 |
|