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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import numpy as np
|
| 3 |
+
import random
|
| 4 |
+
import spaces
|
| 5 |
+
import torch
|
| 6 |
+
from diffusers import DiffusionPipeline
|
| 7 |
+
from PIL import Image
|
| 8 |
+
import uuid
|
| 9 |
+
from typing import Tuple
|
| 10 |
+
|
| 11 |
+
dtype = torch.bfloat16
|
| 12 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 13 |
+
|
| 14 |
+
pipe = DiffusionPipeline.from_pretrained("black-forest-labs/FLUX.1-schnell", torch_dtype=dtype).to(device)
|
| 15 |
+
|
| 16 |
+
MAX_SEED = np.iinfo(np.int32).max
|
| 17 |
+
MAX_IMAGE_SIZE = 2048
|
| 18 |
+
|
| 19 |
+
style_list = [
|
| 20 |
+
{
|
| 21 |
+
"name": "8K",
|
| 22 |
+
"prompt": "hyper-realistic 8K image of {prompt}. ultra-detailed, lifelike, high-resolution, sharp, vibrant colors, photorealistic",
|
| 23 |
+
},
|
| 24 |
+
{
|
| 25 |
+
"name": "4K",
|
| 26 |
+
"prompt": "hyper-realistic 4K image of {prompt}. ultra-detailed, lifelike, high-resolution, sharp, vibrant colors, photorealistic",
|
| 27 |
+
},
|
| 28 |
+
{
|
| 29 |
+
"name": "HD",
|
| 30 |
+
"prompt": "hyper-realistic 2K image of {prompt}. ultra-detailed, lifelike, high-resolution, sharp, vibrant colors, photorealistic",
|
| 31 |
+
},
|
| 32 |
+
{
|
| 33 |
+
"name": "BW",
|
| 34 |
+
"prompt": "black and white collage of {prompt}. monochromatic, timeless, classic, dramatic contrast",
|
| 35 |
+
},
|
| 36 |
+
{
|
| 37 |
+
"name": "Polar",
|
| 38 |
+
"prompt": "collage of polaroid photos featuring {prompt}. vintage style, high contrast, nostalgic, instant film aesthetic",
|
| 39 |
+
},
|
| 40 |
+
{
|
| 41 |
+
"name": "Mustard",
|
| 42 |
+
"prompt": "Duotone style Mustard applied to {prompt}",
|
| 43 |
+
},
|
| 44 |
+
{
|
| 45 |
+
"name": "Cinema",
|
| 46 |
+
"prompt": "cinematic collage of {prompt}. film stills, movie posters, dramatic lighting",
|
| 47 |
+
},
|
| 48 |
+
{
|
| 49 |
+
"name": "Coral",
|
| 50 |
+
"prompt": "Duotone style Coral applied to {prompt}",
|
| 51 |
+
},
|
| 52 |
+
{
|
| 53 |
+
"name": "Scrap",
|
| 54 |
+
"prompt": "scrapbook style collage of {prompt}. mixed media, hand-cut elements, textures, paper, stickers, doodles",
|
| 55 |
+
},
|
| 56 |
+
{
|
| 57 |
+
"name": "Fuchsia",
|
| 58 |
+
"prompt": "Duotone style Fuchsia tone applied to {prompt}",
|
| 59 |
+
},
|
| 60 |
+
{
|
| 61 |
+
"name": "Violet",
|
| 62 |
+
"prompt": "Duotone style Violet applied to {prompt}",
|
| 63 |
+
},
|
| 64 |
+
{
|
| 65 |
+
"name": "Pastel",
|
| 66 |
+
"prompt": "Duotone style Pastel applied to {prompt}",
|
| 67 |
+
},
|
| 68 |
+
{
|
| 69 |
+
"name": "Style Zero",
|
| 70 |
+
"prompt": "{prompt}",
|
| 71 |
+
},
|
| 72 |
+
]
|
| 73 |
+
|
| 74 |
+
css="""
|
| 75 |
+
#col-container {
|
| 76 |
+
margin: 0 auto;
|
| 77 |
+
max-width: 530px;
|
| 78 |
+
}
|
| 79 |
+
"""
|
| 80 |
+
|
| 81 |
+
styles = {k["name"]: k["prompt"] for k in style_list}
|
| 82 |
+
DEFAULT_STYLE_NAME = "Style Zero"
|
| 83 |
+
STYLE_NAMES = list(styles.keys())
|
| 84 |
+
|
| 85 |
+
def apply_style(style_name: str, positive: str) -> str:
|
| 86 |
+
if style_name in styles:
|
| 87 |
+
p = styles[style_name]
|
| 88 |
+
positive = p.format(prompt=positive)
|
| 89 |
+
return positive
|
| 90 |
+
|
| 91 |
+
def set_wallpaper_size(size):
|
| 92 |
+
if size == "Mobile (1080x1920)":
|
| 93 |
+
return 1080, 1920
|
| 94 |
+
elif size == "Desktop (1920x1080)":
|
| 95 |
+
return 1920, 1080
|
| 96 |
+
elif size == "Extented (1920x512)":
|
| 97 |
+
return 1920, 512
|
| 98 |
+
elif size == "Headers (1080x512)":
|
| 99 |
+
return 1080, 512
|
| 100 |
+
else:
|
| 101 |
+
return 1024, 1024 # Default return if none of the conditions are met
|
| 102 |
+
|
| 103 |
+
@spaces.GPU(duration=60, enable_queue=True)
|
| 104 |
+
def infer(prompt, seed=42, randomize_seed=False, wallpaper_size="Desktop(1920x1080)", num_inference_steps=4, style_name=DEFAULT_STYLE_NAME, progress=gr.Progress(track_tqdm=True)):
|
| 105 |
+
if randomize_seed:
|
| 106 |
+
seed = random.randint(0, MAX_SEED)
|
| 107 |
+
generator = torch.Generator().manual_seed(seed)
|
| 108 |
+
|
| 109 |
+
width, height = set_wallpaper_size(wallpaper_size)
|
| 110 |
+
|
| 111 |
+
styled_prompt = apply_style(style_name, prompt)
|
| 112 |
+
|
| 113 |
+
options = {
|
| 114 |
+
"prompt": styled_prompt,
|
| 115 |
+
"width": width,
|
| 116 |
+
"height": height,
|
| 117 |
+
"guidance_scale": 0.0,
|
| 118 |
+
"num_inference_steps": num_inference_steps,
|
| 119 |
+
"generator": generator,
|
| 120 |
+
}
|
| 121 |
+
|
| 122 |
+
torch.cuda.empty_cache()
|
| 123 |
+
images = pipe(**options).images
|
| 124 |
+
|
| 125 |
+
grid_img = Image.new('RGB', (width, height))
|
| 126 |
+
grid_img.paste(images[0], (0, 0))
|
| 127 |
+
|
| 128 |
+
unique_name = str(uuid.uuid4()) + ".png"
|
| 129 |
+
grid_img.save(unique_name)
|
| 130 |
+
return unique_name, seed
|
| 131 |
+
|
| 132 |
+
examples = [
|
| 133 |
+
|
| 134 |
+
"chocolate dripping from a donut a yellow background",
|
| 135 |
+
"cold coffee in a cup bokeh --ar 85:128 --style",
|
| 136 |
+
"an anime illustration of a wiener schnitzel",
|
| 137 |
+
"a delicious ceviche cheesecake slice, ultra-hd+",
|
| 138 |
+
]
|
| 139 |
+
|
| 140 |
+
def load_predefined_images1():
|
| 141 |
+
predefined_images1 = [
|
| 142 |
+
"assets/ww.webp",
|
| 143 |
+
"assets/xx.webp",
|
| 144 |
+
"assets/yy.webp",
|
| 145 |
+
]
|
| 146 |
+
return predefined_images1
|
| 147 |
+
|
| 148 |
+
with gr.Blocks(css=css, theme="bethecloud/storj_theme") as demo:
|
| 149 |
+
|
| 150 |
+
with gr.Column(elem_id="col-container"):
|
| 151 |
+
gr.Markdown(f"""# FLUX.1 SIM""")
|
| 152 |
+
with gr.Row():
|
| 153 |
+
prompt = gr.Text(
|
| 154 |
+
label="Prompt",
|
| 155 |
+
show_label=False,
|
| 156 |
+
max_lines=1,
|
| 157 |
+
placeholder="Enter your prompt",
|
| 158 |
+
container=False,
|
| 159 |
+
)
|
| 160 |
+
run_button = gr.Button("Run", scale=0)
|
| 161 |
+
result = gr.Image(label="Result", show_label=False)
|
| 162 |
+
|
| 163 |
+
with gr.Row(visible=True):
|
| 164 |
+
wallpaper_size = gr.Radio(
|
| 165 |
+
choices=["Mobile (1080x1920)", "Desktop (1920x1080)", "Extented (1920x512)", "Headers (1080x512)", "Default (1024x1024)"],
|
| 166 |
+
label="Pixel Size(x*y)",
|
| 167 |
+
value="Default (1024x1024)"
|
| 168 |
+
)
|
| 169 |
+
|
| 170 |
+
with gr.Row(visible=True):
|
| 171 |
+
style_selection = gr.Radio(
|
| 172 |
+
show_label=True,
|
| 173 |
+
container=True,
|
| 174 |
+
interactive=True,
|
| 175 |
+
choices=STYLE_NAMES,
|
| 176 |
+
value=DEFAULT_STYLE_NAME,
|
| 177 |
+
label="Quality Style",
|
| 178 |
+
)
|
| 179 |
+
with gr.Accordion("Advanced Settings", open=True):
|
| 180 |
+
seed = gr.Slider(
|
| 181 |
+
label="Seed",
|
| 182 |
+
minimum=0,
|
| 183 |
+
maximum=MAX_SEED,
|
| 184 |
+
step=1,
|
| 185 |
+
value=0,
|
| 186 |
+
)
|
| 187 |
+
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
|
| 188 |
+
with gr.Row():
|
| 189 |
+
num_inference_steps = gr.Slider(
|
| 190 |
+
label="Number of inference steps",
|
| 191 |
+
minimum=1,
|
| 192 |
+
maximum=50,
|
| 193 |
+
step=1,
|
| 194 |
+
value=4,
|
| 195 |
+
)
|
| 196 |
+
|
| 197 |
+
gr.Examples(
|
| 198 |
+
examples=examples,
|
| 199 |
+
fn=infer,
|
| 200 |
+
inputs=[prompt],
|
| 201 |
+
outputs=[result, seed],
|
| 202 |
+
cache_examples=False,
|
| 203 |
+
)
|
| 204 |
+
|
| 205 |
+
gr.on(
|
| 206 |
+
triggers=[prompt.submit, run_button.click],
|
| 207 |
+
fn=infer,
|
| 208 |
+
inputs=[prompt, seed, randomize_seed, wallpaper_size, num_inference_steps, style_selection],
|
| 209 |
+
outputs=[result, seed]
|
| 210 |
+
)
|
| 211 |
+
|
| 212 |
+
gr.Markdown("### Image Sample")
|
| 213 |
+
predefined_gallery = gr.Gallery(label="## Images Sample", columns=3, show_label=False, value=load_predefined_images1())
|
| 214 |
+
|
| 215 |
+
gr.Markdown("**Disclaimer/Note:**")
|
| 216 |
+
|
| 217 |
+
gr.Markdown("πModel used in the space <a href='https://huggingface.co/black-forest-labs/FLUX.1-schnell' target='_blank'>black-forest-labs/FLUX.1-schnell</a>. More: 12B param rectified flow transformer distilled from [FLUX.1 [pro]](https://blackforestlabs.ai/) for 4 step generation[[blog](https://blackforestlabs.ai/announcing-black-forest-labs/)] [[model](https://huggingface.co/black-forest-labs/FLUX.1-schnell)]")
|
| 218 |
+
gr.Markdown("β οΈ users are accountable for the content they generate and are responsible for ensuring it meets appropriate ethical standards.")
|
| 219 |
+
|
| 220 |
+
demo.launch()
|