Spaces:
Sleeping
Sleeping
Upload app_i2i.py
Browse files- app_i2i.py +119 -0
app_i2i.py
ADDED
@@ -0,0 +1,119 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import numpy as np
|
3 |
+
import gradio as gr
|
4 |
+
from utils.i2i import i2i_gen
|
5 |
+
|
6 |
+
MAX_SEED = np.iinfo(np.int32).max
|
7 |
+
MIN_IMAGE_SIZE = int(os.getenv("MIN_IMAGE_SIZE", "512"))
|
8 |
+
MAX_IMAGE_SIZE = int(os.getenv("MAX_IMAGE_SIZE", "2048"))
|
9 |
+
|
10 |
+
with gr.Blocks(
|
11 |
+
title="🪄 LayerDiffuse - Flux version (Image to Image)",
|
12 |
+
theme="CultriX/gradio-theme"
|
13 |
+
) as demo:
|
14 |
+
gr.Markdown(
|
15 |
+
"""
|
16 |
+
# 🪄 LayerDiffuse - Flux version (Image to Image)
|
17 |
+
|
18 |
+
A Flux version implementation of LayerDiffuse for image-to-image generation.
|
19 |
+
|
20 |
+
Upload an image with transparency (PNG with alpha channel) and transform it with a text prompt.
|
21 |
+
"""
|
22 |
+
)
|
23 |
+
|
24 |
+
with gr.Row():
|
25 |
+
with gr.Column(scale=1):
|
26 |
+
input_image = gr.Image(
|
27 |
+
label="Input Image",
|
28 |
+
type="pil",
|
29 |
+
tool="editor",
|
30 |
+
sources=["upload", "webcam", "clipboard"],
|
31 |
+
image_mode="RGBA",
|
32 |
+
height=512
|
33 |
+
)
|
34 |
+
|
35 |
+
prompt = gr.Text(
|
36 |
+
label="Prompt",
|
37 |
+
info="Describe what you want to generate",
|
38 |
+
placeholder="E.g: a beautiful landscape with mountains, high quality"
|
39 |
+
)
|
40 |
+
|
41 |
+
with gr.Row():
|
42 |
+
seed = gr.Slider(
|
43 |
+
label="Seed",
|
44 |
+
minimum=0,
|
45 |
+
maximum=MAX_SEED,
|
46 |
+
step=1,
|
47 |
+
value=0,
|
48 |
+
randomize=True
|
49 |
+
)
|
50 |
+
|
51 |
+
strength = gr.Slider(
|
52 |
+
label="Strength",
|
53 |
+
minimum=0.0,
|
54 |
+
maximum=1.0,
|
55 |
+
step=0.01,
|
56 |
+
value=0.8,
|
57 |
+
info="How much to transform the image (0 = no change, 1 = complete change)"
|
58 |
+
)
|
59 |
+
|
60 |
+
with gr.Row():
|
61 |
+
guidance_scale = gr.Slider(
|
62 |
+
label="Guidance scale",
|
63 |
+
minimum=1,
|
64 |
+
maximum=20,
|
65 |
+
step=0.1,
|
66 |
+
value=7.0,
|
67 |
+
)
|
68 |
+
|
69 |
+
num_inference_steps = gr.Slider(
|
70 |
+
label="Steps",
|
71 |
+
minimum=10,
|
72 |
+
maximum=100,
|
73 |
+
step=1,
|
74 |
+
value=50,
|
75 |
+
)
|
76 |
+
|
77 |
+
generate_btn = gr.Button("Generate", variant="primary")
|
78 |
+
|
79 |
+
with gr.Column(scale=1):
|
80 |
+
output_image = gr.Image(
|
81 |
+
label="Result",
|
82 |
+
type="pil",
|
83 |
+
height=512
|
84 |
+
)
|
85 |
+
|
86 |
+
# Set up the generation flow
|
87 |
+
gr.on(
|
88 |
+
triggers=[generate_btn.click],
|
89 |
+
fn=lambda: gr.update(interactive=False, value="Generating..."),
|
90 |
+
outputs=generate_btn,
|
91 |
+
api_name=False
|
92 |
+
).then(
|
93 |
+
fn=i2i_gen,
|
94 |
+
inputs=[
|
95 |
+
input_image,
|
96 |
+
prompt,
|
97 |
+
seed,
|
98 |
+
guidance_scale,
|
99 |
+
num_inference_steps,
|
100 |
+
strength
|
101 |
+
],
|
102 |
+
outputs=output_image
|
103 |
+
).then(
|
104 |
+
fn=lambda: gr.update(interactive=True, value="Generate"),
|
105 |
+
outputs=generate_btn,
|
106 |
+
api_name=False
|
107 |
+
)
|
108 |
+
|
109 |
+
# Add examples
|
110 |
+
gr.Examples(
|
111 |
+
examples=[
|
112 |
+
["./imgs/causal_cut.png", "a beautiful landscape with mountains, high quality"],
|
113 |
+
["./imgs/causal_cut.png", "a futuristic cityscape, cyberpunk style, detailed"],
|
114 |
+
],
|
115 |
+
inputs=[input_image, prompt],
|
116 |
+
)
|
117 |
+
|
118 |
+
if __name__ == "__main__":
|
119 |
+
demo.queue(max_size=20).launch(show_error=True)
|