Spaces:
Paused

File size: 8,019 Bytes
043c42b
 
 
 
 
 
 
 
 
 
 
 
 
f3e252e
043c42b
 
f3e252e
043c42b
 
 
 
 
 
 
 
 
 
 
 
 
b76b1bc
11414d2
043c42b
 
3858a34
043c42b
f566db9
 
3858a34
91e0285
3858a34
 
 
91e0285
3858a34
 
 
f566db9
f3e252e
 
 
 
 
3858a34
 
 
19ca402
 
cae1504
19ca402
f672729
57c7a39
19ca402
 
cae1504
 
 
57c7a39
cae1504
 
 
f672729
cae1504
f672729
57c7a39
 
 
 
 
19ca402
 
cae1504
 
58911a9
 
 
 
f566db9
a923c60
 
 
 
7e28cb5
 
 
 
f566db9
a923c60
 
f566db9
a923c60
 
 
 
 
d6798ec
7e28cb5
a923c60
7e28cb5
 
 
 
 
 
57c7a39
cae1504
 
57c7a39
12c182c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
043c42b
f3e252e
043c42b
 
7faeb03
57c7a39
f672729
 
57c7a39
f3e252e
a923c60
 
 
043c42b
7faeb03
 
 
 
 
 
 
12c182c
 
 
 
 
 
57c7a39
043c42b
 
 
 
 
a923c60
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
043c42b
 
f672729
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12c182c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
57c7a39
 
 
 
 
 
12c182c
57c7a39
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12c182c
57c7a39
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a923c60
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
import gradio as gr
import numpy as np
import random
import spaces
import torch
from diffusers import DiffusionPipeline

dtype = torch.bfloat16
device = "cuda" if torch.cuda.is_available() else "cpu"

pipe = DiffusionPipeline.from_pretrained("black-forest-labs/FLUX.1-schnell", torch_dtype=dtype).to(device)

MAX_SEED = np.iinfo(np.int32).max
WIDTH = 700
HEIGHT = 900


@spaces.GPU()
def infer(prompt, progress=gr.Progress(track_tqdm=True)):
    seed = random.randint(0, MAX_SEED)
    generator = torch.Generator().manual_seed(seed)
    image = pipe(
        prompt=prompt,
        width=WIDTH,
        height=HEIGHT,
        num_inference_steps=4,
        generator=generator,
        guidance_scale=0.0,
    ).images[0]

    out_path = f"/tmp/generated_{seed}.png"
    image.save(out_path)
    return image, gr.update(value=out_path, visible=True)


css = """
.gradio-container {
    max-width: 700px !important;
    padding: 2px !important;
    padding-top: 0 !important;
}
#col-container {
    margin: 0 auto;
    margin-top: 0 !important;
    max-width: 700px;
    width: 100%;
    gap: 0 !important;
}
#sticky-controls {
    position: sticky;
    top: 0;
    z-index: 100;
    background: var(--body-background-fill);
    padding-top: 2px;
    padding-bottom: 2px;
    margin-bottom: 0 !important;
    flex-wrap: nowrap !important;
    flex-direction: row !important;
    gap: 8px !important;
}
#generate-btn, #save-btn {
    flex: 1 1 30% !important;
    min-width: 0 !important;
}
#generate-btn, #save-btn {
    font-size: 15px !important;
}
#combo-btn, #autoloop-btn {
    flex: 0 0 52px !important;
    min-width: 52px !important;
    max-width: 52px !important;
    padding: 0 !important;
    font-size: 18px !important;
}
#autoloop-btn {
    background-color: #e74c3c !important;
    border-color: #e74c3c !important;
    color: white !important;
}
#sticky-controls button {
    width: 100% !important;
    height: 44px !important;
    min-height: 44px !important;
    transition: filter 0.05s ease !important;
}
#sticky-controls button:active {
    filter: brightness(0.65) !important;
}
#result-hidden {
    display: none !important;
}
#display-wrap {
    width: 100vw;
    max-width: 100vw;
    margin-left: calc(50% - 50vw);
    margin-right: calc(50% - 50vw);
    aspect-ratio: 700 / 900;
    background: #111;
    overflow: hidden;
}
#display-img {
    width: 100%;
    height: 100%;
    object-fit: cover;
    display: block;
}
@media (min-width: 700px) {
    #display-wrap {
        width: 700px;
        max-width: 700px;
        margin-left: auto;
        margin-right: auto;
    }
}
#interval-row {
    margin-top: 16px;
    padding: 0 8px;
}
#interval-panel {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 16px;
    width: 100%;
    padding: 10px 0;
}
#interval-minus, #interval-plus {
    flex: 0 0 56px !important;
    max-width: 56px !important;
    height: 56px !important;
    min-height: 56px !important;
    font-size: 26px !important;
    font-weight: 700 !important;
    border-radius: 50% !important;
}
#interval-display {
    flex: 0 0 140px;
    text-align: center;
    font-size: 20px;
    font-weight: 600;
}
#interval-label {
    text-align: center;
    font-size: 13px;
    opacity: 0.7;
    margin-top: 2px;
}
"""

with gr.Blocks(css=css) as demo:
    with gr.Column(elem_id="col-container"):
        with gr.Row(elem_id="sticky-controls"):
            autoloop_button = gr.Button("\u27F3", elem_id="autoloop-btn")
            run_button = gr.Button("Generate", variant="primary", elem_id="generate-btn")
            download_button = gr.DownloadButton("Save", visible=False, elem_id="save-btn")
            combo_button = gr.Button("\u26A1", elem_id="combo-btn")

        result = gr.Image(show_label=False, elem_id="result-hidden")

        gr.HTML('<div id="display-wrap"><img id="display-img" src=""></div>')

        prompt = gr.Textbox(
            label=None,
            show_label=False,
            placeholder="Describe the image...",
            lines=2,
        )

        with gr.Column(elem_id="interval-row"):
            gr.HTML('<div id="interval-label">Auto-loop interval</div>')
            with gr.Row(elem_id="interval-panel"):
                interval_minus = gr.Button("\u2212", elem_id="interval-minus")
                interval_display = gr.HTML('<div id="interval-display">10 sec</div>')
                interval_plus = gr.Button("+", elem_id="interval-plus")

    gr.on(
        triggers=[run_button.click, prompt.submit],
        fn=infer,
        inputs=[prompt],
        outputs=[result, download_button],
        concurrency_limit=1,
    )

    result.change(
        fn=None,
        inputs=None,
        outputs=None,
        js="""
        () => {
            const hiddenImg = document.querySelector('#result-hidden img');
            const displayImg = document.querySelector('#display-img');
            if (hiddenImg && displayImg && hiddenImg.src) {
                displayImg.src = hiddenImg.src;
            }
        }
        """,
    )

    combo_button.click(
        fn=None,
        inputs=None,
        outputs=None,
        js="""
        () => {
            const saveBtn = document.querySelector('#save-btn');
            if (saveBtn) { saveBtn.click(); }
            setTimeout(() => {
                const genBtn = document.querySelector('#generate-btn');
                if (genBtn) { genBtn.click(); }
            }, 1000);
        }
        """,
    )

    interval_minus.click(
        fn=None,
        inputs=None,
        outputs=None,
        js="""
        () => {
            if (!window.__fluxLoop) { window.__fluxLoop = { running: false, timer: null, interval: 10 }; }
            window.__fluxLoop.interval = Math.max(1, window.__fluxLoop.interval - 1);
            const disp = document.querySelector('#interval-display');
            if (disp) { disp.textContent = window.__fluxLoop.interval + ' sec'; }
        }
        """,
    )

    interval_plus.click(
        fn=None,
        inputs=None,
        outputs=None,
        js="""
        () => {
            if (!window.__fluxLoop) { window.__fluxLoop = { running: false, timer: null, interval: 10 }; }
            window.__fluxLoop.interval = Math.min(60, window.__fluxLoop.interval + 1);
            const disp = document.querySelector('#interval-display');
            if (disp) { disp.textContent = window.__fluxLoop.interval + ' sec'; }
        }
        """,
    )

    autoloop_button.click(
        fn=None,
        inputs=None,
        outputs=None,
        js="""
        () => {
            if (!window.__fluxLoop) { window.__fluxLoop = { running: false, timer: null, interval: 10 }; }
            const state = window.__fluxLoop;
            const btn = document.querySelector('#autoloop-btn');

            function setColor(running) {
                if (btn) {
                    btn.style.setProperty('background-color', running ? '#2ecc71' : '#e74c3c', 'important');
                    btn.style.setProperty('border-color', running ? '#2ecc71' : '#e74c3c', 'important');
                }
            }
            function cycle() {
                if (!state.running) return;
                const genBtn = document.querySelector('#generate-btn');
                if (genBtn) { genBtn.click(); }
                state.timer = setTimeout(() => {
                    if (!state.running) return;
                    const saveBtn = document.querySelector('#save-btn');
                    if (saveBtn) { saveBtn.click(); }
                    state.timer = setTimeout(() => { cycle(); }, 1000);
                }, (state.interval || 10) * 1000);
            }

            if (state.running) {
                state.running = false;
                clearTimeout(state.timer);
                setColor(false);
            } else {
                state.running = true;
                setColor(true);
                cycle();
            }
        }
        """,
    )

demo.queue(max_size=10).launch()