Magnus Persson commited on
Commit ·
e0858f7
1
Parent(s): 4c81502
Add a before/after switch for the last edit
Browse filesFiles changed (8)
28 unmodified lines29303132333428 unmodified lines"ideas.example_note": "This is an example picture. Change the description and tap Create image to get your own.","style.heading": "Pick a style (optional)","style.hint": "The style is added to your description and changes how the picture looks.","chips.heading": "Change your picture","chips.hint": "Tap a button and AI changes the picture. Create a picture first.","style.photo": "Photo",28 unmodified lines2930313233343528 unmodified lines"ideas.example_note": "This is an example picture. Change the description and tap Create image to get your own.","style.heading": "Pick a style (optional)","style.hint": "The style is added to your description and changes how the picture looks.","chips.swap": "Switch before / after","chips.heading": "Change your picture","chips.hint": "Tap a button and AI changes the picture. Create a picture first.","style.photo": "Photo",
28 unmodified lines29303132333428 unmodified lines"ideas.example_note": "Det här är en exempelbild. Ändra beskrivningen och tryck på Skapa bild för att få din egen.","style.heading": "Välj en stil (valfritt)","style.hint": "Stilen läggs till i din beskrivning och ändrar hur bilden ser ut.","chips.heading": "Ändra din bild","chips.hint": "Tryck på en knapp så ändrar AI bilden. Skapa en bild först.","style.photo": "Foto",28 unmodified lines2930313233343528 unmodified lines"ideas.example_note": "Det här är en exempelbild. Ändra beskrivningen och tryck på Skapa bild för att få din egen.","style.heading": "Välj en stil (valfritt)","style.hint": "Stilen läggs till i din beskrivning och ändrar hur bilden ser ut.","chips.swap": "Växla före / efter","chips.heading": "Ändra din bild","chips.hint": "Tryck på en knapp så ändrar AI bilden. Skapa en bild först.","style.photo": "Foto",
20 unmodified lines2122232425269 unmodified lines36373839404142432 unmodified lines46474849505120 unmodified linesdirty: bool = Falsestyle: str | None = Nonecurrent: GeneratedImage | None = Nonehistory: list[GeneratedImage] = field(default_factory=list)undo_text: str | None = field(default=None, repr=False9 unmodified linesdef set_current(self, image: GeneratedImage, history_size: int) -> None:self.current = imageself.history = [*self.history, image][-history_size:]def is_idle(self, seconds: float, now: float | None = None) -> bool:now = time.monotonic() if now is None else nowreturn self.dirty and now - self.last_interaction >= seconds2 unmodified linesself.lang = langself.style = Noneself.current = Noneself.history = []self.undo_text = Noneself.epoch += 120 unmodified lines212223242526279 unmodified lines3738394041424344454647484950515253545556572 unmodified lines6061626364656620 unmodified linesdirty: bool = Falsestyle: str | None = Nonecurrent: GeneratedImage | None = Noneprevious: GeneratedImage | None = None # the image before the last edit, for before/afterhistory: list[GeneratedImage] = field(default_factory=list)undo_text: str | None = field(default=None, repr=False9 unmodified linesdef set_current(self, image: GeneratedImage, history_size: int) -> None:self.current = imageself.previous = Noneself.history = [*self.history, image][-history_size:]def set_edited(self, image: GeneratedImage, history_size: int) -> None:before = self.currentself.set_current(image, history_size)self.previous = beforedef swap(self) -> bool:"""Show the other of the two latest images; False if there is only one."""if self.current is None or self.previous is None:return Falseself.current, self.previous = self.previous, self.currentreturn Truedef is_idle(self, seconds: float, now: float | None = None) -> bool:now = time.monotonic() if now is None else nowreturn self.dirty and now - self.last_interaction >= seconds2 unmodified linesself.lang = langself.style = Noneself.current = Noneself.previous = Noneself.history = []self.undo_text = Noneself.epoch += 1
22 unmodified lines23242526272829303132333435363738396 unmodified lines4647484950515211 unmodified lines646566676869704 unmodified lines757677787980811 unmodified line83848586878822 unmodified lines1111121131141151169 unmodified lines1261271281291301311321331341351362 unmodified lines1391401411421431449 unmodified lines15415515622 unmodified linesclass ChipRow:row: gr.Rowbuttons: dict[str, gr.Button]@propertydef managed(self) -> list[Block]:return [self.row, *self.buttons.values()]def reset_props(self) -> Updates:return {button: {"interactive": False} for button in self.buttons.values()}def build_chip_row(*, settings: Settings, loc: Localizer) -> ChipRow:chips = settings.config.edit_chipsbuild_heading(loc, "chips.heading", "chips.hint")with gr.Row(elem_id="chip-row") as row:buttons = {6 unmodified lines)for key in settings.config.ui.edit_chips}return ChipRow(row, buttons)def wire_chips(11 unmodified lines) -> None:t = loc.thistory_size = settings.config.ui.history_sizeoutputs = [session, device, create_button, *chips.buttons.values(), status, result]count = len(chips.buttons)def handler(key: str):4 unmodified linessource = current.currentepoch = current.epochif source is None:yield (current, device_id, *[gr.skip()] * (count + 3))returnbusy = gr.update(interactive=False)yield (1 unmodified linedevice_id,busy,*[busy] * count,gr.update(value=t(lang, "edit.working"), visible=True),gr.skip(),)22 unmodified linesdevice_id,ready,*[off] * count,gr.update(value="", visible=False),gr.skip(),)9 unmodified linesdevice_id,ready,*[ready] * count,gr.update(value=message, visible=True),gr.skip(),)returncurrent.set_current(GeneratedImage(made.image, source.prompt, made.model_key, made.seconds),history_size,)2 unmodified linesdevice_id,ready,*[ready] * count,gr.update(value="", visible=False),gr.update(value=picture, visible=True),)9 unmodified linesjs=start_js("edit"),api_visibility=api_visibility,)22 unmodified lines232425262728293031323334353637383940414243444546476 unmodified lines5455565758596011 unmodified lines727374757677784 unmodified lines838485868788891 unmodified line9192939495969722 unmodified lines1201211221231241251269 unmodified lines1361371381391401411421431441451461472 unmodified lines1501511521531541551569 unmodified lines16616716816917017117217317417517617717817918018118218318422 unmodified linesclass ChipRow:row: gr.Rowbuttons: dict[str, gr.Button]swap: gr.Button@propertydef managed(self) -> list[Block]:return [self.row, self.swap, *self.buttons.values()]def reset_props(self) -> Updates:return {button: {"interactive": False} for button in [self.swap, *self.buttons.values()]}def build_chip_row(*, settings: Settings, loc: Localizer) -> ChipRow:chips = settings.config.edit_chipsswap = loc.make(gr.Button,lambda lang: {"value": loc.t(lang, "chips.swap")},variant="secondary",elem_classes=["swap-button"],interactive=False,)build_heading(loc, "chips.heading", "chips.hint")with gr.Row(elem_id="chip-row") as row:buttons = {6 unmodified lines)for key in settings.config.ui.edit_chips}return ChipRow(row, buttons, swap)def wire_chips(11 unmodified lines) -> None:t = loc.thistory_size = settings.config.ui.history_sizeoutputs = [session, device, create_button, *chips.buttons.values(), chips.swap, status, result]count = len(chips.buttons)def handler(key: str):4 unmodified linessource = current.currentepoch = current.epochif source is None:yield (current, device_id, *[gr.skip()] * (count + 4))returnbusy = gr.update(interactive=False)yield (1 unmodified linedevice_id,busy,*[busy] * count,busy,gr.update(value=t(lang, "edit.working"), visible=True),gr.skip(),)22 unmodified linesdevice_id,ready,*[off] * count,off,gr.update(value="", visible=False),gr.skip(),)9 unmodified linesdevice_id,ready,*[ready] * count,gr.update(interactive=current.previous is not None),gr.update(value=message, visible=True),gr.skip(),)returncurrent.set_edited(GeneratedImage(made.image, source.prompt, made.model_key, made.seconds),history_size,)2 unmodified linesdevice_id,ready,*[ready] * count,ready,gr.update(value="", visible=False),gr.update(value=picture, visible=True),)9 unmodified linesjs=start_js("edit"),api_visibility=api_visibility,)def on_swap(current: VisitorSession):current.touch()if not current.swap():return current, gr.skip()assert current.current is not Nonereturn current, gr.update(value=to_pil(current.current.image), visible=True)chips.swap.click(on_swap,[session],[session, result],queue=False,show_progress="hidden",api_visibility=api_visibility,)
275 unmodified lines276277278279280281282283284285286287275 unmodified linesif chips:def show_chips(current: VisitorSession):return [gr.update(interactive=current.current is not None)] * len(chips.buttons)created.then(show_chips,[session],list(chips.buttons.values()),queue=False,show_progress="hidden",**visibility,275 unmodified lines276277278279280281282283284285286287288289290275 unmodified linesif chips:def show_chips(current: VisitorSession):return [*[gr.update(interactive=current.current is not None)] * len(chips.buttons),gr.update(interactive=current.previous is not None),]created.then(show_chips,[session],[*chips.buttons.values(), chips.swap],queue=False,show_progress="hidden",**visibility,
171 unmodified lines172173174175176177171 unmodified linesdisplay: none;}.chip-button {min-height: 52px;flex: 1 1 120px;171 unmodified lines172173174175176177178179180181182171 unmodified linesdisplay: none;}.swap-button {min-height: 52px;width: 100%;}.chip-button {min-height: 52px;flex: 1 1 120px;
144 unmodified lines145146147144 unmodified linestime.sleep(1.2)assert client.predict(api_name="/on_idle")[2]["value"] == ""assert client.predict(api_name="/on_idle")[2] == NOOP144 unmodified lines145146147148149150151152153154155156157158159160144 unmodified linestime.sleep(1.2)assert client.predict(api_name="/on_idle")[2]["value"] == ""assert client.predict(api_name="/on_idle")[2] == NOOPdef test_swap_toggles_between_the_image_before_and_after_a_chip(client: Client):client.predict("A cat", None, api_name="/on_create")_, _, edited = client.predict(None, api_name="/on_chip_evening_light")original = client.predict(api_name="/on_swap")assert original["value"] != edited["value"]assert client.predict(api_name="/on_swap")["value"] != original["value"]def test_swap_without_an_edit_does_nothing(client: Client):client.predict("A cat", None, api_name="/on_create")assert client.predict(api_name="/on_swap") == NOOP
49 unmodified lines50515249 unmodified linesbefore = session.epochsession.reset("sv")assert session.epoch == before + 149 unmodified lines5051525354555657585960616263646566676869707172737449 unmodified linesbefore = session.epochsession.reset("sv")assert session.epoch == before + 1def test_an_edit_keeps_the_image_before_it_and_swap_toggles_between_them():session = VisitorSession(lang="sv")before, after = _image("before"), _image("after")session.set_current(before, 6)assert session.previous is None and not session.swap()session.set_edited(after, 6)assert session.current is after and session.previous is beforeassert session.swap() and session.current is beforeassert session.swap() and session.current is afterdef test_a_new_image_or_a_reset_drops_the_before_image():session = VisitorSession(lang="sv")session.set_current(_image("a"), 6)session.set_edited(_image("b"), 6)session.set_current(_image("c"), 6)assert session.previous is Nonesession.set_edited(_image("d"), 6)session.reset("sv")assert session.previous is None and session.current is None
locales/en.json+1
locales/sv.json+1
src/services/session.py+15
src/ui/chips.py+34-6
src/ui/create_tab.py+5-2
src/ui/style.css+5
tests/test_app_smoke.py+13
tests/test_session.py+22