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 REALISM 🔥"""
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 .Row ():
129
+ prompt = gr .Text (
130
+ label = "Prompt" ,
131
+ show_label = False ,
132
+ max_lines = 1 ,
133
+ placeholder = "Enter your prompt" ,
134
+ container = False ,
135
+ )
136
+ run_button = gr .Button ("Run" , scale = 0 )
137
+ result = gr .Gallery (label = "Result" , columns = 1 , show_label = False )
138
+
139
+ with gr .Accordion ("Advanced options" , open = False , visible = True ):
140
+ seed = gr .Slider (
141
+ label = "Seed" ,
142
+ minimum = 0 ,
143
+ maximum = MAX_SEED ,
144
+ step = 1 ,
145
+ value = 0 ,
146
+ visible = True
147
+ )
148
+ randomize_seed = gr .Checkbox (label = "Randomize seed" , value = True )
149
+
150
+ with gr .Row (visible = True ):
151
+ width = gr .Slider (
152
+ label = "Width" ,
153
+ minimum = 512 ,
154
+ maximum = 2048 ,
155
+ step = 64 ,
156
+ value = 1024 ,
157
+ )
158
+ height = gr .Slider (
159
+ label = "Height" ,
160
+ minimum = 512 ,
161
+ maximum = 2048 ,
162
+ step = 64 ,
163
+ value = 1024 ,
164
+ )
165
+
166
+ with gr .Row ():
167
+ guidance_scale = gr .Slider (
168
+ label = "Guidance Scale" ,
169
+ minimum = 0.1 ,
170
+ maximum = 20.0 ,
171
+ step = 0.1 ,
172
+ value = 3.0 ,
173
+ )
174
+ num_inference_steps = gr .Slider (
175
+ label = "Number of inference steps" ,
176
+ minimum = 1 ,
177
+ maximum = 40 ,
178
+ step = 1 ,
179
+ value = 16 ,
180
+ )
181
+
182
+ style_selection = gr .Radio (
183
+ show_label = True ,
184
+ container = True ,
185
+ interactive = True ,
186
+ choices = STYLE_NAMES ,
187
+ value = DEFAULT_STYLE_NAME ,
188
+ label = "Quality Style" ,
189
+ )
190
+
191
+
192
+
193
+ gr .Examples (
194
+ examples = examples ,
195
+ inputs = prompt ,
196
+ outputs = [result , seed ],
197
+ fn = generate ,
198
+ cache_examples = False ,
199
+ )
200
+
201
+ gr .on (
202
+ triggers = [
203
+ prompt .submit ,
204
+ run_button .click ,
205
+ ],
206
+ fn = generate ,
207
+ inputs = [
208
+ prompt ,
209
+ seed ,
210
+ width ,
211
+ height ,
212
+ guidance_scale ,
213
+ randomize_seed ,
214
+ style_selection ,
215
+ ],
216
+ outputs = [result , seed ],
217
+ api_name = "run" ,
218
+ )
219
+
220
+ gr .Markdown ("### Generated Images" )
221
+ predefined_gallery = gr .Gallery (label = "Generated Images" , columns = 3 , show_label = False , value = load_predefined_images ())
222
+ gr .Markdown ("**Disclaimer/Note:**" )
223
+
224
+ 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." )
225
+
226
+ gr .Markdown ("🔥users are accountable for the content they generate and are responsible for ensuring it meets appropriate ethical standards." )
227
+
228
+ if __name__ == "__main__" :
229
+ demo .queue (max_size = 40 ).launch ()
0 commit comments