Anurag181011 commited on
Commit
96d90f0
·
verified ·
1 Parent(s): a37a20c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +201 -111
app.py CHANGED
@@ -1,120 +1,210 @@
 
1
  import gradio as gr
2
  import torch
3
- import numpy as np
4
- from diffusers import DiffusionPipeline
5
- from diffusers.pipelines.stable_diffusion import StableDiffusionSafetyChecker
6
- from functools import lru_cache
7
  from PIL import Image
8
- from transformers import CLIPImageProcessor
9
-
10
- @lru_cache(maxsize=1)
11
- def load_pipeline():
12
- device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
13
- # Use FP16 when CUDA is available, along with a revision flag if supported.
14
- torch_dtype = torch.float16 if device.type == "cuda" else torch.float32
15
- revision = "fp16" if device.type == "cuda" else None
16
-
17
- base_model = "black-forest-labs/FLUX.1-dev"
18
- pipe = DiffusionPipeline.from_pretrained(
19
- base_model,
20
- torch_dtype=torch_dtype,
21
- low_cpu_mem_usage=True,
22
- revision=revision,
23
- )
24
-
25
- # Load LoRA weights
26
- lora_repo = "strangerzonehf/Flux-Super-Realism-LoRA"
27
- pipe.load_lora_weights(lora_repo)
28
-
29
- # Load safety checker and image processor.
30
- # If memory remains an issue, you can disable the safety checker below.
31
- safety_checker = StableDiffusionSafetyChecker.from_pretrained(
32
- "CompVis/stable-diffusion-safety-checker"
33
- )
34
- image_processor = CLIPImageProcessor.from_pretrained("openai/clip-vit-base-patch32")
35
-
36
- if device.type == "cuda":
37
- # Use attention slicing for further memory savings.
38
- pipe.enable_attention_slicing()
39
- # Offload layers to CPU when not in use.
40
- pipe.enable_sequential_cpu_offload()
41
-
42
- return pipe, safety_checker, image_processor
43
-
44
- pipe, safety_checker, image_processor = load_pipeline()
45
-
46
- def generate_image(
47
- prompt,
48
- seed=42,
49
- width=512, # Keep resolution low by default
50
- height=512,
51
- guidance_scale=6,
52
- steps=28,
53
- progress=gr.Progress()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
54
  ):
55
- try:
56
- progress(0, desc="Initializing...")
57
- device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
58
- generator = torch.Generator(device=device).manual_seed(seed)
59
-
60
- # Auto-add trigger words if not present
61
- if "super realism" not in prompt.lower():
62
- prompt = f"Super Realism, {prompt}"
63
-
64
- with torch.inference_mode():
65
- result = pipe(
66
- prompt=prompt,
67
- width=width,
68
- height=height,
69
- guidance_scale=guidance_scale,
70
- num_inference_steps=steps,
71
- generator=generator,
72
- )
73
- image = result.images[0]
74
-
75
- progress(1, desc="Safety checking...")
76
- # Process image for safety checking
77
- safety_input = image_processor(image, return_tensors="pt")
78
- np_image = np.array(image)
79
- _, nsfw_detected = safety_checker(
80
- images=[np_image],
81
- clip_input=safety_input.pixel_values
82
- )
83
-
84
- if nsfw_detected[0]:
85
- return Image.new("RGB", (width, height)), "NSFW content detected"
86
-
87
- # Clear CUDA cache
88
- if device.type == "cuda":
89
- torch.cuda.empty_cache()
90
- return image, "Generation successful"
91
-
92
- except Exception as e:
93
- return Image.new("RGB", (width, height)), f"Error: {str(e)}"
94
 
95
- with gr.Blocks() as app:
96
- gr.Markdown("# Flux Super Realism Generator")
97
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
98
  with gr.Row():
99
- with gr.Column():
100
- prompt_input = gr.Textbox(label="Prompt", value="A portrait of a person")
101
- seed_input = gr.Slider(0, 1000, value=42, label="Seed")
102
- # Limit the resolution sliders to help avoid memory overuse.
103
- width_input = gr.Slider(256, 1024, value=512, step=64, label="Width")
104
- height_input = gr.Slider(256, 1024, value=512, step=64, label="Height")
105
- guidance_input = gr.Slider(1, 20, value=6, label="Guidance Scale")
106
- steps_input = gr.Slider(10, 100, value=28, label="Steps")
107
- submit = gr.Button("Generate")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
108
 
109
- with gr.Column():
110
- output_image = gr.Image(label="Result", type="pil")
111
- status = gr.Textbox(label="Status")
112
-
113
- submit.click(
114
- generate_image,
115
- inputs=[prompt_input, seed_input, width_input, height_input, guidance_input, steps_input],
116
- outputs=[output_image, status]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
117
  )
118
-
119
- # Queue settings to limit concurrent requests
120
- app.queue(max_size=3).launch()
 
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
+ def save_image(img):
12
+ unique_name = str(uuid.uuid4()) + ".png"
13
+ img.save(unique_name)
14
+ return unique_name
15
+
16
+ def randomize_seed_fn(seed: int, randomize_seed: bool) -> int:
17
+ if randomize_seed:
18
+ seed = random.randint(0, MAX_SEED)
19
+ return seed
20
+
21
+ MAX_SEED = np.iinfo(np.int32).max
22
+
23
+ if not torch.cuda.is_available():
24
+ DESCRIPTIONz += "\n<p>⚠️Running on CPU, This may not work on CPU.</p>"
25
+
26
+ base_model = "black-forest-labs/FLUX.1-dev"
27
+ pipe = DiffusionPipeline.from_pretrained(base_model, torch_dtype=torch.bfloat16)
28
+
29
+ lora_repo = "strangerzonehf/Flux-Super-Realism-LoRA"
30
+ trigger_word = "Super Realism" # Leave trigger_word blank if not used.
31
+
32
+ pipe.load_lora_weights(lora_repo)
33
+ pipe.to("cuda")
34
+
35
+ style_list = [
36
+ {
37
+ "name": "3840 x 2160",
38
+ "prompt": "hyper-realistic 8K image of {prompt}. ultra-detailed, lifelike, high-resolution, sharp, vibrant colors, photorealistic",
39
+ },
40
+ {
41
+ "name": "2560 x 1440",
42
+ "prompt": "hyper-realistic 4K image of {prompt}. ultra-detailed, lifelike, high-resolution, sharp, vibrant colors, photorealistic",
43
+ },
44
+ {
45
+ "name": "HD+",
46
+ "prompt": "hyper-realistic 2K image of {prompt}. ultra-detailed, lifelike, high-resolution, sharp, vibrant colors, photorealistic",
47
+ },
48
+ {
49
+ "name": "Style Zero",
50
+ "prompt": "{prompt}",
51
+ },
52
+ ]
53
+
54
+ styles = {k["name"]: k["prompt"] for k in style_list}
55
+
56
+ DEFAULT_STYLE_NAME = "3840 x 2160"
57
+ STYLE_NAMES = list(styles.keys())
58
+
59
+ def apply_style(style_name: str, positive: str) -> str:
60
+ return styles.get(style_name, styles[DEFAULT_STYLE_NAME]).replace("{prompt}", positive)
61
+
62
+ @spaces.GPU(duration=60, enable_queue=True)
63
+ def generate(
64
+ prompt: str,
65
+ seed: int = 0,
66
+ width: int = 1024,
67
+ height: int = 1024,
68
+ guidance_scale: float = 3,
69
+ randomize_seed: bool = False,
70
+ style_name: str = DEFAULT_STYLE_NAME,
71
+ progress=gr.Progress(track_tqdm=True),
72
  ):
73
+ seed = int(randomize_seed_fn(seed, randomize_seed))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
74
 
75
+ positive_prompt = apply_style(style_name, prompt)
 
76
 
77
+ if trigger_word:
78
+ positive_prompt = f"{trigger_word} {positive_prompt}"
79
+
80
+ images = pipe(
81
+ prompt=positive_prompt,
82
+ width=width,
83
+ height=height,
84
+ guidance_scale=guidance_scale,
85
+ num_inference_steps=28,
86
+ num_images_per_prompt=1,
87
+ output_type="pil",
88
+ ).images
89
+ image_paths = [save_image(img) for img in images]
90
+ print(image_paths)
91
+ return image_paths, seed
92
+
93
+ examples = [
94
+ "Woman in a red jacket, snowy, in the style of hyper-realistic portraiture, caninecore, mountainous vistas, timeless beauty, palewave, iconic, distinctive noses --ar 72:101 --stylize 750 --v 6",
95
+ "Super Realism, Headshot of handsome young man, wearing dark gray sweater with buttons and big shawl collar, brown hair and short beard, serious look on his face, black background, soft studio lighting, portrait photography --ar 85:128 --v 6.0 --style",
96
+ "Super Realism, High-resolution photograph, woman, UHD, photorealistic, shot on a Sony A7III --chaos 20 --ar 1:2 --style raw --stylize 250",
97
+ "Super-realism, Purple Dreamy, a medium-angle shot of a young woman with long brown hair, wearing a pair of eye-level glasses, stands in front of a backdrop of purple and white lights. The womans eyes are closed, her lips are slightly parted, as if she is looking up at the sky. Her hair is cascading over her shoulders, framing her face. She is wearing a sleeveless top, adorned with tiny white dots, and a gold chain necklace around her neck. Her left earrings are dangling from her ears, adding a pop of color to the scene."
98
+ ]
99
+
100
+ css = '''
101
+ .gradio-container{max-width: 888px !important}
102
+ h1{text-align:center}
103
+ footer {
104
+ visibility: hidden
105
+ }
106
+ .submit-btn {
107
+ background-color: #e34949 !important;
108
+ color: white !important;
109
+ }
110
+ .submit-btn:hover {
111
+ background-color: #ff3b3b !important;
112
+ }
113
+ '''
114
+
115
+ with gr.Blocks(css=css, theme="bethecloud/storj_theme") as demo:
116
  with gr.Row():
117
+ with gr.Column(scale=1):
118
+ prompt = gr.Text(
119
+ label="Prompt",
120
+ show_label=False,
121
+ max_lines=1,
122
+ placeholder="Enter your prompt",
123
+ container=False,
124
+ )
125
+ run_button = gr.Button("Generate as ( 768 x 1024 )🤗", scale=0, elem_classes="submit-btn")
126
+
127
+ with gr.Accordion("Advanced options", open=True, visible=True):
128
+ seed = gr.Slider(
129
+ label="Seed",
130
+ minimum=0,
131
+ maximum=MAX_SEED,
132
+ step=1,
133
+ value=0,
134
+ visible=True
135
+ )
136
+ randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
137
+
138
+ with gr.Row(visible=True):
139
+ width = gr.Slider(
140
+ label="Width",
141
+ minimum=512,
142
+ maximum=2048,
143
+ step=64,
144
+ value=768,
145
+ )
146
+ height = gr.Slider(
147
+ label="Height",
148
+ minimum=512,
149
+ maximum=2048,
150
+ step=64,
151
+ value=1024,
152
+ )
153
+
154
+ with gr.Row():
155
+ guidance_scale = gr.Slider(
156
+ label="Guidance Scale",
157
+ minimum=0.1,
158
+ maximum=20.0,
159
+ step=0.1,
160
+ value=3.0,
161
+ )
162
+ num_inference_steps = gr.Slider(
163
+ label="Number of inference steps",
164
+ minimum=1,
165
+ maximum=40,
166
+ step=1,
167
+ value=28,
168
+ )
169
+
170
+ style_selection = gr.Radio(
171
+ show_label=True,
172
+ container=True,
173
+ interactive=True,
174
+ choices=STYLE_NAMES,
175
+ value=DEFAULT_STYLE_NAME,
176
+ label="Quality Style",
177
+ )
178
 
179
+ with gr.Column(scale=2):
180
+ result = gr.Gallery(label="Result", columns=1, show_label=False)
181
+
182
+ gr.Examples(
183
+ examples=examples,
184
+ inputs=prompt,
185
+ outputs=[result, seed],
186
+ fn=generate,
187
+ cache_examples=False,
188
+ )
189
+
190
+ gr.on(
191
+ triggers=[
192
+ prompt.submit,
193
+ run_button.click,
194
+ ],
195
+ fn=generate,
196
+ inputs=[
197
+ prompt,
198
+ seed,
199
+ width,
200
+ height,
201
+ guidance_scale,
202
+ randomize_seed,
203
+ style_selection,
204
+ ],
205
+ outputs=[result, seed],
206
+ api_name="run",
207
  )
208
+
209
+ if __name__ == "__main__":
210
+ demo.queue(max_size=40).launch()