Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow canceling the like/dislike status in gr.Chatbot #10286

Closed
1 task
HenryBao91 opened this issue Jan 5, 2025 · 1 comment · Fixed by #10292
Closed
1 task

Allow canceling the like/dislike status in gr.Chatbot #10286

HenryBao91 opened this issue Jan 5, 2025 · 1 comment · Fixed by #10292
Labels
enhancement New feature or request

Comments

@HenryBao91
Copy link

  • I have searched to see if a similar issue already exists.
    I installed the newest version:
pip install https://gradio-pypi-previews.s3.amazonaws.com/936988dbbfd3cb1431ecd59fd7061e0217fe66f1/gradio-5.9.1-py3-none-any.whl

My Code is here:

import gradio as gr

disliked_messages = {}

def greet(history, user_input):
    return history + [{"role": "user", "content": user_input}, {"role": "assistant", "content": "Hello, " + user_input}], gr.update(value="")  

def vote(data: gr.LikeData, history):
    print(data.index, data.liked)
    
    if isinstance(data.index, int):
        message = history[data.index]["content"]  
    else:
        message = ""
    
    if disliked_messages.get(message, False):  # dislike -> cancel dislike
        disliked_messages[message] = False
        print("You canceled the dislike for this response: " + message)
    else:  
        disliked_messages[message] = True
        print("You disliked this response: " + message)
    
    updated_history = [
        {"role": item["role"], "content": item["content"]} if disliked_messages.get(item["content"], False) else item
        for item in history
    ]

    return gr.update(value=updated_history)

with gr.Blocks() as demo:
    chatbot = gr.Chatbot(feedback_options=["Dislike"], type="messages") 
    textbox = gr.Textbox()
    textbox.submit(greet, [chatbot, textbox], [chatbot, textbox])  
    chatbot.like(vote, [chatbot], chatbot) 

if __name__ == "__main__":
    demo.launch()

No matter how I modify the vote function, when I click on "thumb-down" again, "Dislike" cannot be canceled.

When I click “thumb-down” ,it turn to
image

and if I click “thumb-down” again, it turn to
image

but now:
image

@abidlabs abidlabs added the enhancement New feature or request label Jan 5, 2025
@abidlabs abidlabs changed the title How can I cancel like/dislike status? Allow canceling the like/dislike status in gr.Chatbot Jan 5, 2025
@HenryBao91
Copy link
Author

@abidlabs Thank you , That's really cool

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants