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|
|
|
|
1 |
+
import spaces
|
2 |
+
import gradio as gr
|
3 |
+
import torch
|
4 |
+
from PIL import Image
|
5 |
+
from diffusers import DiffusionPipeline
|
6 |
+
import random
|
7 |
+
import uuid
|
8 |
+
from typing import Tuple
|
9 |
+
import numpy as np
|
10 |
+
|
11 |
+
DESCRIPTIONz = """## FLUX REALPIX 🔥"""
|
12 |
+
|
13 |
+
def save_image(img):
|
14 |
+
unique_name = str(uuid.uuid4()) + ".png"
|
15 |
+
img.save(unique_name)
|
16 |
+
return unique_name
|
17 |
+
|
18 |
+
def randomize_seed_fn(seed: int, randomize_seed: bool) -> int:
|
19 |
+
if randomize_seed:
|
20 |
+
seed = random.randint(0, MAX_SEED)
|
21 |
+
return seed
|
22 |
+
|
23 |
+
MAX_SEED = np.iinfo(np.int32).max
|
24 |
+
|
25 |
+
if not torch.cuda.is_available():
|
26 |
+
DESCRIPTIONz += "\n<p>⚠️Running on CPU, This may not work on CPU.</p>"
|
27 |
+
|
28 |
+
base_model = "black-forest-labs/FLUX.1-dev"
|
29 |
+
pipe = DiffusionPipeline.from_pretrained(base_model, torch_dtype=torch.bfloat16)
|
30 |
+
|
31 |
+
lora_repo = "prithivMLmods/Canopus-LoRA-Flux-FaceRealism"
|
32 |
+
trigger_word = "realism" # Leave trigger_word blank if not used.
|
33 |
+
pipe.load_lora_weights(lora_repo)
|
34 |
+
|
35 |
+
pipe.to("cuda")
|
36 |
+
|
37 |
+
style_list = [
|
38 |
+
{
|
39 |
+
"name": "3840 x 2160",
|
40 |
+
"prompt": "hyper-realistic 8K image of {prompt}. ultra-detailed, lifelike, high-resolution, sharp, vibrant colors, photorealistic",
|
41 |
+
},
|
42 |
+
{
|
43 |
+
"name": "2560 x 1440",
|
44 |
+
"prompt": "hyper-realistic 4K image of {prompt}. ultra-detailed, lifelike, high-resolution, sharp, vibrant colors, photorealistic",
|
45 |
+
},
|
46 |
+
{
|
47 |
+
"name": "HD+",
|
48 |
+
"prompt": "hyper-realistic 2K image of {prompt}. ultra-detailed, lifelike, high-resolution, sharp, vibrant colors, photorealistic",
|
49 |
+
},
|
50 |
+
{
|
51 |
+
"name": "Style Zero",
|
52 |
+
"prompt": "{prompt}",
|
53 |
+
},
|
54 |
+
]
|
55 |
+
|
56 |
+
styles = {k["name"]: k["prompt"] for k in style_list}
|
57 |
+
|
58 |
+
DEFAULT_STYLE_NAME = "3840 x 2160"
|
59 |
+
STYLE_NAMES = list(styles.keys())
|
60 |
+
|
61 |
+
def apply_style(style_name: str, positive: str) -> str:
|
62 |
+
return styles.get(style_name, styles[DEFAULT_STYLE_NAME]).replace("{prompt}", positive)
|
63 |
+
|
64 |
+
@spaces.GPU(duration=60, enable_queue=True)
|
65 |
+
def generate(
|
66 |
+
prompt: str,
|
67 |
+
seed: int = 0,
|
68 |
+
width: int = 1024,
|
69 |
+
height: int = 1024,
|
70 |
+
guidance_scale: float = 3,
|
71 |
+
randomize_seed: bool = False,
|
72 |
+
style_name: str = DEFAULT_STYLE_NAME,
|
73 |
+
progress=gr.Progress(track_tqdm=True),
|
74 |
+
):
|
75 |
+
seed = int(randomize_seed_fn(seed, randomize_seed))
|
76 |
+
|
77 |
+
positive_prompt = apply_style(style_name, prompt)
|
78 |
+
|
79 |
+
if trigger_word:
|
80 |
+
positive_prompt = f"{trigger_word} {positive_prompt}"
|
81 |
+
|
82 |
+
images = pipe(
|
83 |
+
prompt=positive_prompt,
|
84 |
+
width=width,
|
85 |
+
height=height,
|
86 |
+
guidance_scale=guidance_scale,
|
87 |
+
num_inference_steps=16,
|
88 |
+
num_images_per_prompt=1,
|
89 |
+
output_type="pil",
|
90 |
+
).images
|
91 |
+
image_paths = [save_image(img) for img in images]
|
92 |
+
print(image_paths)
|
93 |
+
return image_paths, seed
|
94 |
+
|
95 |
+
|
96 |
+
def load_predefined_images():
|
97 |
+
predefined_images = [
|
98 |
+
"assets/11.png",
|
99 |
+
"assets/22.png",
|
100 |
+
"assets/33.png",
|
101 |
+
"assets/44.png",
|
102 |
+
"assets/55.webp",
|
103 |
+
"assets/66.png",
|
104 |
+
"assets/77.png",
|
105 |
+
"assets/88.png",
|
106 |
+
"assets/99.png",
|
107 |
+
]
|
108 |
+
return predefined_images
|
109 |
+
|
110 |
+
|
111 |
+
|
112 |
+
examples = [
|
113 |
+
"A portrait of an attractive woman in her late twenties with light brown hair and purple, wearing large a a yellow sweater. She is looking directly at the camera, standing outdoors near trees.. --ar 128:85 --v 6.0 --style raw",
|
114 |
+
"A photo of the model wearing a white bodysuit and beige trench coat, posing in front of a train station with hands on head, soft light, sunset, fashion photography, high resolution, 35mm lens, f/22, natural lighting, global illumination. --ar 85:128 --v 6.0 --style raw",
|
115 |
+
]
|
116 |
+
|
117 |
+
|
118 |
+
css = '''
|
119 |
+
.gradio-container{max-width: 575px !important}
|
120 |
+
h1{text-align:center}
|
121 |
+
footer {
|
122 |
+
visibility: hidden
|
123 |
+
}
|
124 |
+
'''
|
125 |
+
|
126 |
+
with gr.Blocks(css=css, theme="bethecloud/storj_theme") as demo:
|
127 |
+
gr.Markdown(DESCRIPTIONz)
|
128 |
+
with gr.Group():
|
129 |
+
with gr.Row():
|
130 |
+
prompt = gr.Text(
|
131 |
+
label="Prompt",
|
132 |
+
show_label=False,
|
133 |
+
max_lines=1,
|
134 |
+
placeholder="Enter your prompt with realism tag!",
|
135 |
+
container=False,
|
136 |
+
)
|
137 |
+
run_button = gr.Button("Run", scale=0)
|
138 |
+
result = gr.Gallery(label="Result", columns=1, preview=True, show_label=False)
|
139 |
+
|
140 |
+
with gr.Accordion("Advanced options", open=False, visible=True):
|
141 |
+
seed = gr.Slider(
|
142 |
+
label="Seed",
|
143 |
+
minimum=0,
|
144 |
+
maximum=MAX_SEED,
|
145 |
+
step=1,
|
146 |
+
value=0,
|
147 |
+
visible=True
|
148 |
+
)
|
149 |
+
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
|
150 |
+
|
151 |
+
with gr.Row(visible=True):
|
152 |
+
width = gr.Slider(
|
153 |
+
label="Width",
|
154 |
+
minimum=512,
|
155 |
+
maximum=2048,
|
156 |
+
step=64,
|
157 |
+
value=1024,
|
158 |
+
)
|
159 |
+
height = gr.Slider(
|
160 |
+
label="Height",
|
161 |
+
minimum=512,
|
162 |
+
maximum=2048,
|
163 |
+
step=64,
|
164 |
+
value=1024,
|
165 |
+
)
|
166 |
+
|
167 |
+
with gr.Row():
|
168 |
+
guidance_scale = gr.Slider(
|
169 |
+
label="Guidance Scale",
|
170 |
+
minimum=0.1,
|
171 |
+
maximum=20.0,
|
172 |
+
step=0.1,
|
173 |
+
value=3.0,
|
174 |
+
)
|
175 |
+
num_inference_steps = gr.Slider(
|
176 |
+
label="Number of inference steps",
|
177 |
+
minimum=1,
|
178 |
+
maximum=40,
|
179 |
+
step=1,
|
180 |
+
value=16,
|
181 |
+
)
|
182 |
+
|
183 |
+
style_selection = gr.Radio(
|
184 |
+
show_label=True,
|
185 |
+
container=True,
|
186 |
+
interactive=True,
|
187 |
+
choices=STYLE_NAMES,
|
188 |
+
value=DEFAULT_STYLE_NAME,
|
189 |
+
label="Quality Style",
|
190 |
+
)
|
191 |
+
|
192 |
+
|
193 |
+
|
194 |
+
gr.Examples(
|
195 |
+
examples=examples,
|
196 |
+
inputs=prompt,
|
197 |
+
outputs=[result, seed],
|
198 |
+
fn=generate,
|
199 |
+
cache_examples=False,
|
200 |
+
)
|
201 |
+
|
202 |
+
gr.on(
|
203 |
+
triggers=[
|
204 |
+
prompt.submit,
|
205 |
+
run_button.click,
|
206 |
+
],
|
207 |
+
fn=generate,
|
208 |
+
inputs=[
|
209 |
+
prompt,
|
210 |
+
seed,
|
211 |
+
width,
|
212 |
+
height,
|
213 |
+
guidance_scale,
|
214 |
+
randomize_seed,
|
215 |
+
style_selection,
|
216 |
+
],
|
217 |
+
outputs=[result, seed],
|
218 |
+
api_name="run",
|
219 |
+
)
|
220 |
+
|
221 |
+
gr.Markdown("### Generated Images")
|
222 |
+
predefined_gallery = gr.Gallery(label="Generated Images", columns=3, show_label=False, value=load_predefined_images())
|
223 |
+
gr.Markdown("**Disclaimer/Note:**")
|
224 |
+
|
225 |
+
gr.Markdown("🔥This space provides realistic image generation, which works better for human faces and portraits. Realistic trigger works properly, better for photorealistic trigger words, close-up shots, face diffusion, male, female characters.")
|
226 |
+
|
227 |
+
gr.Markdown("🔥users are accountable for the content they generate and are responsible for ensuring it meets appropriate ethical standards.")
|
228 |
+
|
229 |
+
if __name__ == "__main__":
|
230 |
+
demo.queue(max_size=40).launch()
|