File size: 3,076 Bytes
86fc953
ced082c
9c9e714
b5594eb
cfd74ee
3d78d34
cfd74ee
b5594eb
cfd74ee
 
 
 
0894424
 
cfd74ee
0894424
 
 
cfd74ee
20654e3
cfd74ee
fb1a543
 
 
 
 
cfd74ee
 
 
 
 
 
ced082c
fb1a543
20654e3
86fc953
cfd74ee
86fc953
cfd74ee
20654e3
86fc953
 
 
 
cfd74ee
fb1a543
86fc953
ff5b825
 
fb1a543
cfd74ee
fb1a543
ced082c
cfd74ee
3d78d34
86fc953
 
 
 
 
 
 
 
 
9c9e714
b5594eb
3d78d34
9c9e714
cfd74ee
 
9c9e714
 
 
b5594eb
86fc953
 
cfd74ee
3d78d34
9c9e714
3d78d34
fb1a543
 
 
 
 
 
 
 
 
 
 
 
 
 
86fc953
 
 
 
 
 
 
fb1a543
86fc953
 
 
 
 
 
 
fb1a543
 
 
3d78d34
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import re
from functools import partial
from io import BytesIO
from typing import Any

import gradio as gr
import segno
from gradio.components import Component
from huggingface_hub import InferenceClient
from PIL import Image
from qrcode_artistic import write_artistic

try:
    import dotenv

    dotenv.load_dotenv()
except ImportError:
    pass

client = InferenceClient(model="black-forest-labs/FLUX.1-schnell")

MODELS = [
    "stabilityai/stable-diffusion-3.5-large",
    "black-forest-labs/FLUX.1-schnell",
]

with gr.Blocks() as demo:
    with gr.Row():
        with gr.Column():
            text = gr.Textbox(
                "https://wheelingvultures.bandcamp.com/album/ep", label="Text"
            )
            prompt = gr.TextArea("A psychedelic vulture", label="Prompt")
            model = gr.Radio(MODELS, value=MODELS[0], label="Model")
            button = gr.Button("Generate")

        with gr.Column():
            output = gr.Image()
            background = gr.Image(visible=False, type="filepath")
            scale = gr.Slider(3, 15, 9, step=1, label="Scale")

            with gr.Row():
                color_dark = gr.ColorPicker("#000000", label="Dark")
                color_light = gr.ColorPicker("#FFFFFF", label="Light")

    def generate_background(data: dict[Component, Any]):
        if not data.get(prompt):
            return gr.skip(), gr.skip()

        return client.text_to_image(data[prompt], model=data[model]), None

    def generate_output(data: dict[Component, Any]):
        if data.get(background) is None:
            return None

        def to_hex_format(value: str):
            if value.startswith("#"):
                return value

            matches = re.findall(r"\d+(?:\.\d+)?", value)
            r, g, b = map(int, map(float, matches[:3]))

            return f"#{r:x}{g:x}{b:x}".upper()

        image = Image.open(data[background])
        qr_code = segno.make(data[text], error="h")

        with BytesIO() as buffer:
            write_artistic(
                qr_code,
                target=buffer,
                background=image.fp,
                kind=image.format,
                scale=data[scale],
                light=to_hex_format(data[color_light]),
                dark=to_hex_format(data[color_dark]),
            )

            return Image.open(buffer)

    gr.on(
        [button.click, prompt.submit],
        partial(gr.update, interactive=False),
        outputs=button,
    ).then(
        generate_background,
        inputs={prompt, model},
        outputs=[background, output],
    ).then(
        partial(gr.update, interactive=True),
        outputs=button,
    )

    gr.on(
        [
            text.submit,
            background.change,
            scale.change,
            color_light.change,
            color_dark.change,
        ],
        generate_output,
        inputs={
            text,
            background,
            scale,
            color_light,
            color_dark,
        },
        outputs=output,
    )


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