Skip to content

Commit 667a3c3

Browse files
authored
Add a text to image IO function. (#99)
Also adds an example usage of the function.
1 parent 0d60e4a commit 667a3c3

File tree

6 files changed

+151
-0
lines changed

6 files changed

+151
-0
lines changed

docs/components/text_to_image.md

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
## Overview
2+
3+
Text To Image component is a quick and simple way of getting started with Mesop. Text To Image is part of [Mesop Labs](../guides/labs.md).
4+
5+
## Examples
6+
7+
```python
8+
--8<-- "mesop/examples/text_to_image.py"
9+
```
10+
11+
## API
12+
13+
::: mesop.labs.text_to_image.text_to_image

mesop/examples/__init__.py

+1
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,6 @@
1919
from mesop.examples import sxs as sxs
2020
from mesop.examples import testing as testing
2121
from mesop.examples import text_io as text_io
22+
from mesop.examples import text_to_image as text_to_image
2223

2324
# Do not import error_state_missing_init_prop because it cause all examples to fail.

mesop/examples/text_to_image.py

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import mesop as me
2+
import mesop.labs as mel
3+
4+
5+
@me.page(path="/text_to_image", title="Text to Image Example")
6+
def app():
7+
mel.text_to_image(
8+
generate_image,
9+
title="Text to Image Example",
10+
)
11+
12+
13+
def generate_image(prompt: str):
14+
return "https://google.github.io/mesop/assets/editor-v1.png"

mesop/labs/__init__.py

+1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
from mesop.labs.io import text_io as text_io
2+
from mesop.labs.text_to_image import text_to_image as text_to_image

mesop/labs/text_to_image.py

+121
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
from typing import Callable
2+
3+
import mesop as me
4+
5+
6+
@me.stateclass
7+
class State:
8+
input: str
9+
output: str
10+
textarea_key: int
11+
12+
13+
def text_to_image(
14+
transform: Callable[[str], str],
15+
*,
16+
title: str | None = None,
17+
):
18+
"""Creates a simple UI which takes in a text input and returns an image output.
19+
20+
This function creates event handlers for text input and output operations
21+
using the provided function `transform` to process the input and generate the image
22+
output.
23+
24+
Args:
25+
transform: Function that takes in a string input and returns a URL to an image or a base64 encoded image.
26+
title: Headline text to display at the top of the UI.
27+
"""
28+
29+
def on_input(e: me.InputEvent):
30+
state = me.state(State)
31+
state.input = e.value
32+
33+
def on_click_generate(e: me.ClickEvent):
34+
state = me.state(State)
35+
state.output = transform(state.input)
36+
37+
def on_click_clear(e: me.ClickEvent):
38+
state = me.state(State)
39+
state.input = ""
40+
state.output = ""
41+
state.textarea_key += 1
42+
43+
with me.box(
44+
style=me.Style(
45+
background="#f0f4f8",
46+
height="100%",
47+
)
48+
):
49+
with me.box(
50+
style=me.Style(
51+
background="#f0f4f8",
52+
padding=me.Padding(top=24, left=24, right=24, bottom=24),
53+
display="flex",
54+
flex_direction="column",
55+
)
56+
):
57+
if title:
58+
me.text(title, type="headline-5")
59+
with me.box(
60+
style=me.Style(
61+
margin=me.Margin(left="auto", right="auto"),
62+
width="min(1024px, 100%)",
63+
gap="24px",
64+
flex_grow=1,
65+
display="flex",
66+
flex_wrap="wrap",
67+
)
68+
):
69+
box_style = me.Style(
70+
flex_basis="max(480px, calc(50% - 48px))",
71+
background="#fff",
72+
border_radius=12,
73+
box_shadow=(
74+
"0 3px 1px -2px #0003, 0 2px 2px #00000024, 0 1px 5px #0000001f"
75+
),
76+
padding=me.Padding(top=16, left=16, right=16, bottom=16),
77+
display="flex",
78+
flex_direction="column",
79+
)
80+
81+
with me.box(style=box_style):
82+
me.text("Input", style=me.Style(font_weight=500))
83+
me.box(style=me.Style(height=16))
84+
me.textarea(
85+
key=str(me.state(State).textarea_key),
86+
on_input=on_input,
87+
rows=5,
88+
autosize=True,
89+
max_rows=15,
90+
style=me.Style(width="100%"),
91+
)
92+
me.box(style=me.Style(height=12))
93+
with me.box(
94+
style=me.Style(display="flex", justify_content="space-between")
95+
):
96+
me.button(
97+
"Clear",
98+
color="primary",
99+
type="stroked",
100+
on_click=on_click_clear,
101+
)
102+
me.button(
103+
"Generate",
104+
color="primary",
105+
type="flat",
106+
on_click=on_click_generate,
107+
)
108+
with me.box(style=box_style):
109+
me.text("Output", style=me.Style(font_weight=500))
110+
if me.state(State).output:
111+
with me.box(
112+
style=me.Style(
113+
display="grid",
114+
justify_content="center",
115+
justify_items="center",
116+
)
117+
):
118+
me.image(
119+
src=me.state(State).output,
120+
style=me.Style(width="100%", margin=me.Margin(top=10)),
121+
)

mkdocs.yml

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ nav:
1515
- Components API:
1616
- I/O:
1717
- Text I/O: components/text_io.md
18+
- Text To Image: components/text_to_image.md
1819
- Layout:
1920
- Box: components/box.md
2021
- Sidenav: components/sidenav.md

0 commit comments

Comments
 (0)