Update app.py
Browse files
app.py
CHANGED
|
@@ -22,15 +22,15 @@ def get_bot_response(message):
|
|
| 22 |
def chat(message, history):
|
| 23 |
history = history or []
|
| 24 |
# Add user message to chat history
|
| 25 |
-
history.append(
|
| 26 |
|
| 27 |
# Get bot response
|
| 28 |
bot_message = get_bot_response(message)
|
| 29 |
|
| 30 |
# Add bot message to chat history
|
| 31 |
-
history
|
| 32 |
|
| 33 |
-
return history
|
| 34 |
|
| 35 |
# Function to add selected emoji to input
|
| 36 |
def add_emoji(emoji, current_input):
|
|
@@ -38,14 +38,14 @@ def add_emoji(emoji, current_input):
|
|
| 38 |
|
| 39 |
# Function to copy the last bot message
|
| 40 |
def copy_last_message(history):
|
| 41 |
-
if history and history[-1][
|
| 42 |
-
return history[-1][
|
| 43 |
return "No message to copy."
|
| 44 |
|
| 45 |
# Function to handle feedback
|
| 46 |
def handle_feedback(feedback, history):
|
| 47 |
if history:
|
| 48 |
-
last_message = history[-1][
|
| 49 |
print(f"Feedback for '{last_message}': {feedback}")
|
| 50 |
return history
|
| 51 |
|
|
@@ -54,9 +54,9 @@ with gr.Blocks() as demo:
|
|
| 54 |
chatbot = gr.Chatbot()
|
| 55 |
|
| 56 |
with gr.Row():
|
| 57 |
-
with gr.Column(scale=
|
| 58 |
msg = gr.Textbox(label="Type your message here", placeholder="Enter your message...")
|
| 59 |
-
with gr.Column(scale=
|
| 60 |
emoji_dropdown = gr.Dropdown(choices=emojis, label="", interactive=True)
|
| 61 |
|
| 62 |
with gr.Row():
|
|
@@ -71,9 +71,9 @@ with gr.Blocks() as demo:
|
|
| 71 |
thumbs_down = gr.Button("π")
|
| 72 |
|
| 73 |
# Set up event handlers
|
| 74 |
-
submit.click(chat, inputs=[msg, chatbot], outputs=[chatbot
|
| 75 |
-
msg.submit(chat, inputs=[msg, chatbot], outputs=[chatbot
|
| 76 |
-
clear.click(lambda:
|
| 77 |
emoji_dropdown.change(add_emoji, inputs=[emoji_dropdown, msg], outputs=msg)
|
| 78 |
copy_button.click(copy_last_message, inputs=[chatbot], outputs=[copied_text])
|
| 79 |
thumbs_up.click(handle_feedback, inputs=["thumbs up", chatbot], outputs=[chatbot])
|
|
|
|
| 22 |
def chat(message, history):
|
| 23 |
history = history or []
|
| 24 |
# Add user message to chat history
|
| 25 |
+
history.append((message, ""))
|
| 26 |
|
| 27 |
# Get bot response
|
| 28 |
bot_message = get_bot_response(message)
|
| 29 |
|
| 30 |
# Add bot message to chat history
|
| 31 |
+
history[-1] = (message, bot_message)
|
| 32 |
|
| 33 |
+
return history
|
| 34 |
|
| 35 |
# Function to add selected emoji to input
|
| 36 |
def add_emoji(emoji, current_input):
|
|
|
|
| 38 |
|
| 39 |
# Function to copy the last bot message
|
| 40 |
def copy_last_message(history):
|
| 41 |
+
if history and history[-1][1]:
|
| 42 |
+
return history[-1][1]
|
| 43 |
return "No message to copy."
|
| 44 |
|
| 45 |
# Function to handle feedback
|
| 46 |
def handle_feedback(feedback, history):
|
| 47 |
if history:
|
| 48 |
+
last_message = history[-1][1]
|
| 49 |
print(f"Feedback for '{last_message}': {feedback}")
|
| 50 |
return history
|
| 51 |
|
|
|
|
| 54 |
chatbot = gr.Chatbot()
|
| 55 |
|
| 56 |
with gr.Row():
|
| 57 |
+
with gr.Column(scale=9):
|
| 58 |
msg = gr.Textbox(label="Type your message here", placeholder="Enter your message...")
|
| 59 |
+
with gr.Column(scale=1):
|
| 60 |
emoji_dropdown = gr.Dropdown(choices=emojis, label="", interactive=True)
|
| 61 |
|
| 62 |
with gr.Row():
|
|
|
|
| 71 |
thumbs_down = gr.Button("π")
|
| 72 |
|
| 73 |
# Set up event handlers
|
| 74 |
+
submit.click(chat, inputs=[msg, chatbot], outputs=[chatbot])
|
| 75 |
+
msg.submit(chat, inputs=[msg, chatbot], outputs=[chatbot])
|
| 76 |
+
clear.click(lambda: None, None, chatbot, queue=False)
|
| 77 |
emoji_dropdown.change(add_emoji, inputs=[emoji_dropdown, msg], outputs=msg)
|
| 78 |
copy_button.click(copy_last_message, inputs=[chatbot], outputs=[copied_text])
|
| 79 |
thumbs_up.click(handle_feedback, inputs=["thumbs up", chatbot], outputs=[chatbot])
|