prithivMLmods commited on
Commit
fa1521a
1 Parent(s): 890fc52

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +220 -0
app.py ADDED
@@ -0,0 +1,220 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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": "B & W",
34
+ "prompt": "black and white collage of {prompt}. monochromatic, timeless, classic, dramatic contrast",
35
+ },
36
+ {
37
+ "name": "Polaroid",
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": "Cinematic",
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": "Scrapbook",
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: 520px;
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
+ else:
99
+ return 1024, 1024 # Default return if none of the conditions are met
100
+
101
+ @spaces.GPU(duration=60, enable_queue=True)
102
+ 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)):
103
+ if randomize_seed:
104
+ seed = random.randint(0, MAX_SEED)
105
+ generator = torch.Generator().manual_seed(seed)
106
+
107
+ width, height = set_wallpaper_size(wallpaper_size)
108
+
109
+ styled_prompt = apply_style(style_name, prompt)
110
+
111
+ options = {
112
+ "prompt": styled_prompt,
113
+ "width": width,
114
+ "height": height,
115
+ "guidance_scale": 0.0,
116
+ "num_inference_steps": num_inference_steps,
117
+ "generator": generator,
118
+ }
119
+
120
+ torch.cuda.empty_cache()
121
+ images = pipe(**options).images
122
+
123
+ grid_img = Image.new('RGB', (width, height))
124
+ grid_img.paste(images[0], (0, 0))
125
+
126
+ unique_name = str(uuid.uuid4()) + ".png"
127
+ grid_img.save(unique_name)
128
+ return unique_name, seed
129
+
130
+ examples = [
131
+
132
+ "3d image, cute girl, in the style of pixars --stylize 750",
133
+ "chocolate dripping from a donut a yellow background",
134
+ "cold coffee in a cup bokeh --ar 85:128 --style",
135
+ "an anime illustration of a wiener schnitzel",
136
+ "a delicious ceviche cheesecake slice, ultra-hd+",
137
+ "illustration starry night camp in the mountain",
138
+
139
+ ]
140
+
141
+ def load_predefined_images1():
142
+ predefined_images1 = [
143
+ "assets/ww.webp",
144
+ "assets/xx.webp",
145
+ "assets/yy.webp",
146
+ ]
147
+ return predefined_images1
148
+
149
+ with gr.Blocks(css=css, theme=gr.themes.Soft(primary_hue=gr.themes.colors.orange, secondary_hue=gr.themes.colors.blue)) as demo:
150
+
151
+ with gr.Column(elem_id="col-container"):
152
+ gr.Markdown(f"""# FLUX.1 SIM""")
153
+ with gr.Row():
154
+ prompt = gr.Text(
155
+ label="Prompt",
156
+ show_label=False,
157
+ max_lines=1,
158
+ placeholder="Enter your prompt",
159
+ container=False,
160
+ )
161
+ run_button = gr.Button("Run", scale=0)
162
+ result = gr.Image(label="Result", show_label=False)
163
+
164
+ with gr.Row(visible=True):
165
+ wallpaper_size = gr.Radio(
166
+ choices=["Mobile (1080x1920)", "Desktop (1920x1080)", "Extented (1920x512)", "Default (1024x1024)"],
167
+ label="Pixel Size(x*y)",
168
+ value="Default (1024x1024)"
169
+ )
170
+
171
+ with gr.Row(visible=True):
172
+ style_selection = gr.Radio(
173
+ show_label=True,
174
+ container=True,
175
+ interactive=True,
176
+ choices=STYLE_NAMES,
177
+ value=DEFAULT_STYLE_NAME,
178
+ label="Quality Style",
179
+ )
180
+ with gr.Accordion("Advanced Settings", open=True):
181
+ seed = gr.Slider(
182
+ label="Seed",
183
+ minimum=0,
184
+ maximum=MAX_SEED,
185
+ step=1,
186
+ value=0,
187
+ )
188
+ randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
189
+ with gr.Row():
190
+ num_inference_steps = gr.Slider(
191
+ label="Number of inference steps",
192
+ minimum=1,
193
+ maximum=50,
194
+ step=1,
195
+ value=4,
196
+ )
197
+
198
+ gr.Examples(
199
+ examples=examples,
200
+ fn=infer,
201
+ inputs=[prompt],
202
+ outputs=[result, seed],
203
+ cache_examples=False,
204
+ )
205
+
206
+ gr.on(
207
+ triggers=[prompt.submit, run_button.click],
208
+ fn=infer,
209
+ inputs=[prompt, seed, randomize_seed, wallpaper_size, num_inference_steps, style_selection],
210
+ outputs=[result, seed]
211
+ )
212
+
213
+ gr.Markdown("### Image Sample")
214
+ predefined_gallery = gr.Gallery(label="## Images Sample", columns=3, show_label=False, value=load_predefined_images1())
215
+
216
+ gr.Markdown("**Disclaimer/Note:**")
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()