Created
November 27, 2024 12:45
-
-
Save araffin/081b186c59042c179ee9f8c60c61f0ec to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
A simple GUI to collect human feedback. | |
It writes the rating to a file "gui_value.txt" next to the script. | |
The rating can be reset by removing or emptying the text file. | |
Nicegui is the only dependency. | |
If you use `uv` you can do `uv run feedback_gui.py`. | |
Author: Antonin Raffin (2024) | |
MIT License | |
""" | |
# /// script | |
# dependencies = [ | |
# "nicegui>=2.0", | |
# ] | |
# /// | |
from pathlib import Path | |
from typing import Callable | |
from nicegui import ui | |
icons = [ | |
"sentiment_very_dissatisfied", | |
"sentiment_dissatisfied", | |
"sentiment_neutral", | |
"sentiment_satisfied", | |
"sentiment_very_satisfied", | |
] | |
colors = [ | |
"red-600", | |
"orange-600", | |
"slate-500", | |
"lime-500", | |
"green-600", | |
] | |
class HumanRating: | |
value: float = 0.0 | |
buttons: list[ui.button] | |
filename: Path = Path(__file__).parent / "gui_value.txt" | |
def init_buttons(self, buttons: list[ui.button]) -> None: | |
self.buttons = buttons | |
def update(self, event, rating: int) -> None: | |
for button in self.buttons: | |
button.props("color=slate-400") | |
event.sender.props("color=violet-600") | |
self.value = rating | |
self.filename.write_text(str(rating)) | |
def maybe_reset_buttons(self) -> None: | |
if not self.filename.is_file() or self.filename.read_text() == "": | |
for idx, button in enumerate(self.buttons): | |
button.props(f"color={colors[idx]}") | |
def buttons_row(callback: Callable) -> list[ui.button]: | |
buttons = [] | |
with ui.row(): | |
for icon_name, color in zip(icons, colors): | |
button = ui.button(icon=icon_name).classes("w-32 h-32 text-5xl") | |
button.props(f"color={color}") | |
buttons.append(button) | |
for idx, button in enumerate(buttons, start=-2): | |
button.on_click(lambda event, rating=idx: callback(event, rating)) | |
return buttons | |
if __name__ in {"__main__", "__mp_main__"}: | |
human_rating = HumanRating() | |
buttons = buttons_row(human_rating.update) | |
human_rating.init_buttons(buttons) | |
ui.timer(0.2, human_rating.maybe_reset_buttons) | |
ui.dark_mode().enable() | |
ui.run(reload=True, show=False, favicon="⚖️", port=8911, title="Human Rating") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment