kadirnar commited on
Commit
6723bf5
·
verified ·
1 Parent(s): 515bf21

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +175 -196
app.py CHANGED
@@ -1,205 +1,184 @@
1
-
2
- import os
3
- import random
4
- import uuid
5
- import json
6
-
7
  import gradio as gr
8
- import numpy as np
9
- from PIL import Image
10
- import spaces
11
  import torch
12
- from diffusers import StableDiffusionXLPipeline, DPMSolverSinglestepScheduler
13
-
14
- # Use environment variables for flexibility
15
- MODEL_ID = os.getenv("MODEL_ID", "sd-community/sdxl-flash")
16
- MAX_IMAGE_SIZE = int(os.getenv("MAX_IMAGE_SIZE", "4096"))
17
- USE_TORCH_COMPILE = os.getenv("USE_TORCH_COMPILE", "0") == "1"
18
- ENABLE_CPU_OFFLOAD = os.getenv("ENABLE_CPU_OFFLOAD", "0") == "1"
19
- BATCH_SIZE = int(os.getenv("BATCH_SIZE", "1")) # Allow generating multiple images at once
20
-
21
- # Determine device and load model outside of function for efficiency
22
- device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
23
- pipe = StableDiffusionXLPipeline.from_single_file(
24
- "https://huggingface.co/kadirnar/Black-Hole/blob/main/tachyon.safetensors",
25
- torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,
26
- use_safetensors=True,
27
- add_watermarker=False,
28
- ).to(device)
29
- pipe.scheduler = DPMSolverSinglestepScheduler(use_karras_sigmas=True).from_config(pipe.scheduler.config)
30
-
31
- # Torch compile for potential speedup (experimental)
32
- if USE_TORCH_COMPILE:
33
- pipe.compile()
34
-
35
- # CPU offloading for larger RAM capacity (experimental)
36
- if ENABLE_CPU_OFFLOAD:
37
- pipe.enable_model_cpu_offload()
38
-
39
- MAX_SEED = np.iinfo(np.int32).max
40
-
41
- def randomize_seed_fn(seed: int, randomize_seed: bool) -> int:
42
- if randomize_seed:
43
- seed = random.randint(0, MAX_SEED)
44
- return seed
45
-
46
- @spaces.GPU()
47
- def generate(
48
- prompt: str,
49
- negative_prompt: str = "",
50
- use_negative_prompt: bool = False,
51
- seed: int = 1,
52
- width: int = 1024,
53
- height: int = 1024,
54
- guidance_scale: float = 3,
55
- num_inference_steps: int = 30,
56
- randomize_seed: bool = False,
57
- use_resolution_binning: bool = True,
58
- num_images: int = 1, # Number of images to generate
59
- progress=gr.Progress(track_tqdm=True),
60
- ):
61
- seed = int(randomize_seed_fn(seed, randomize_seed))
62
- generator = torch.Generator(device=device).manual_seed(seed)
63
-
64
- # Improved options handling
65
- options = {
66
- "prompt": [prompt] * num_images,
67
- "negative_prompt": [negative_prompt] * num_images if use_negative_prompt else None,
68
- "width": width,
69
- "height": height,
70
- "guidance_scale": guidance_scale,
71
- "num_inference_steps": num_inference_steps,
72
- "generator": generator,
73
- "output_type": "pil",
74
- }
75
-
76
- output = pipe(prompt,negative_prompt, width, height,guidance_scale,num_inference_steps)
77
-
78
- return output
79
-
80
- examples = [
81
- "a cat eating a piece of cheese",
82
- "a ROBOT riding a BLUE horse on Mars, photorealistic, 4k",
83
- "Ironman VS Hulk, ultrarealistic",
84
- "Astronaut in a jungle, cold color palette, oil pastel, detailed, 8k",
85
- "An alien holding a sign board containing the word 'Flash', futuristic, neonpunk",
86
- "Kids going to school, Anime style"
87
- ]
88
-
89
- css = '''
90
- .gradio-container{max-width: 700px !important}
91
- h1{text-align:center}
92
- footer {
93
- visibility: hidden
94
- }
95
- '''
96
-
97
- with gr.Blocks(css=css) as demo:
98
- gr.Markdown("""# Black Hole SDXL-Lightning""")
99
- with gr.Group():
100
- with gr.Row():
101
- prompt = gr.Text(
102
- label="Prompt",
103
- show_label=False,
104
- max_lines=1,
105
- placeholder="Enter your prompt",
106
- container=False,
107
- )
108
- run_button = gr.Button("Run", scale=0)
109
- result = gr.Gallery(elem_id="gallery", label="Result", show_label=False)
110
-
111
- with gr.Accordion("Advanced options", open=False):
112
- num_images = gr.Slider(
113
- label="Number of Images",
114
- minimum=1,
115
- maximum=4,
116
- step=1,
117
- value=1,
118
- )
119
- with gr.Row():
120
- use_negative_prompt = gr.Checkbox(label="Use negative prompt", value=True)
121
- negative_prompt = gr.Text(
122
- label="Negative prompt",
123
- max_lines=5,
124
- lines=4,
125
- placeholder="Enter a negative prompt",
126
- value="(deformed, distorted, disfigured:1.3), poorly drawn, bad anatomy, wrong anatomy, extra limb, missing limb, floating limbs, (mutated hands and fingers:1.4), disconnected limbs, mutation, mutated, ugly, disgusting, blurry, amputation, NSFW",
127
- visible=True,
128
  )
129
- seed = gr.Slider(
130
- label="Seed",
131
- minimum=0,
132
- maximum=MAX_SEED,
133
- step=1,
134
- value=0,
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
135
  )
136
- randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
137
- with gr.Row(visible=True):
138
- width = gr.Slider(
139
- label="Width",
140
- minimum=512,
141
- maximum=MAX_IMAGE_SIZE,
142
- step=64,
143
- value=1024,
144
- )
145
- height = gr.Slider(
146
- label="Height",
147
- minimum=512,
148
- maximum=MAX_IMAGE_SIZE,
149
- step=64,
150
- value=1024,
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
151
  )
 
 
 
 
 
 
 
152
  with gr.Row():
153
- guidance_scale = gr.Slider(
154
- label="Guidance Scale",
155
- minimum=0.1,
156
- maximum=6,
157
- step=0.1,
158
- value=3.0,
159
- )
160
- num_inference_steps = gr.Slider(
161
- label="Number of inference steps",
162
- minimum=1,
163
- maximum=15,
164
- step=1,
165
- value=4,
166
- )
167
 
168
- gr.Examples(
169
- examples=examples,
170
- inputs=prompt,
171
- cache_examples=False
172
- )
173
-
174
- use_negative_prompt.change(
175
- fn=lambda x: gr.update(visible=x),
176
- inputs=use_negative_prompt,
177
- outputs=negative_prompt,
178
- api_name=False,
179
- )
180
-
181
- gr.on(
182
- triggers=[
183
- prompt.submit,
184
- negative_prompt.submit,
185
- run_button.click,
186
- ],
187
- fn=generate,
188
- inputs=[
189
- prompt,
190
- negative_prompt,
191
- use_negative_prompt,
192
- seed,
193
- width,
194
- height,
195
- guidance_scale,
196
- num_inference_steps,
197
- randomize_seed,
198
- num_images
199
- ],
200
- outputs=[result, seed],
201
- api_name="run",
202
- )
203
 
204
  if __name__ == "__main__":
205
- demo.queue().launch()
 
 
 
 
 
 
 
1
  import gradio as gr
 
 
 
2
  import torch
3
+ from diffusers import StableDiffusionPipeline
4
+
5
+ from diffusion_webui.utils.model_list import stable_model_list
6
+ from diffusion_webui.utils.scheduler_list import (
7
+ SCHEDULER_MAPPING,
8
+ get_scheduler,
9
+ )
10
+ import spaces
11
+
12
+
13
+ class StableDiffusionText2ImageGenerator:
14
+ def __init__(self):
15
+ self.pipe = None
16
+
17
+ def load_model(
18
+ self,
19
+ model_path,
20
+ scheduler,
21
+ ):
22
+ if self.pipe is None:
23
+ self.pipe = StableDiffusionPipeline.from_pretrained(
24
+ model_path, safety_checker=None, torch_dtype=torch.float16
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25
  )
26
+
27
+ self.pipe = get_scheduler(pipe=self.pipe, scheduler=scheduler)
28
+ self.pipe.to("cuda")
29
+ self.pipe.enable_xformers_memory_efficient_attention()
30
+
31
+ return self.pipe
32
+
33
+ @spaces.GPU()
34
+ def generate_image(
35
+ self,
36
+ model_path: str,
37
+ prompt: str,
38
+ negative_prompt: str,
39
+ num_images_per_prompt: int,
40
+ scheduler: str,
41
+ guidance_scale: int,
42
+ num_inference_step: int,
43
+ height: int,
44
+ width: int,
45
+ seed_generator=0,
46
+ ):
47
+ pipe = self.load_model(
48
+ model_path=model_path,
49
+ scheduler=scheduler,
50
  )
51
+ if seed_generator == 0:
52
+ random_seed = torch.randint(0, 1000000, (1,))
53
+ generator = torch.manual_seed(random_seed)
54
+ else:
55
+ generator = torch.manual_seed(seed_generator)
56
+
57
+ images = pipe(
58
+ prompt=prompt,
59
+ height=height,
60
+ width=width,
61
+ negative_prompt=negative_prompt,
62
+ num_images_per_prompt=num_images_per_prompt,
63
+ num_inference_steps=num_inference_step,
64
+ guidance_scale=guidance_scale,
65
+ generator=generator,
66
+ ).images
67
+
68
+ return images
69
+
70
+ def app():
71
+ with gr.Blocks():
72
+ with gr.Row():
73
+ with gr.Column():
74
+ text2image_prompt = gr.Textbox(
75
+ lines=1,
76
+ placeholder="Prompt",
77
+ show_label=False,
78
+ )
79
+
80
+ text2image_negative_prompt = gr.Textbox(
81
+ lines=1,
82
+ placeholder="Negative Prompt",
83
+ show_label=False,
84
+ )
85
+ with gr.Row():
86
+ with gr.Column():
87
+ text2image_model_path = gr.Dropdown(
88
+ choices=stable_model_list,
89
+ value=stable_model_list[0],
90
+ label="Text-Image Model Id",
91
+ )
92
+
93
+ text2image_guidance_scale = gr.Slider(
94
+ minimum=0.1,
95
+ maximum=15,
96
+ step=0.1,
97
+ value=7.5,
98
+ label="Guidance Scale",
99
+ )
100
+
101
+ text2image_num_inference_step = gr.Slider(
102
+ minimum=1,
103
+ maximum=100,
104
+ step=1,
105
+ value=50,
106
+ label="Num Inference Step",
107
+ )
108
+ text2image_num_images_per_prompt = gr.Slider(
109
+ minimum=1,
110
+ maximum=30,
111
+ step=1,
112
+ value=1,
113
+ label="Number Of Images",
114
+ )
115
+ with gr.Row():
116
+ with gr.Column():
117
+ text2image_scheduler = gr.Dropdown(
118
+ choices=list(SCHEDULER_MAPPING.keys()),
119
+ value=list(SCHEDULER_MAPPING.keys())[0],
120
+ label="Scheduler",
121
+ )
122
+
123
+ text2image_height = gr.Slider(
124
+ minimum=128,
125
+ maximum=1280,
126
+ step=32,
127
+ value=512,
128
+ label="Image Height",
129
+ )
130
+
131
+ text2image_width = gr.Slider(
132
+ minimum=128,
133
+ maximum=1280,
134
+ step=32,
135
+ value=512,
136
+ label="Image Width",
137
+ )
138
+ text2image_seed_generator = gr.Slider(
139
+ label="Seed(0 for random)",
140
+ minimum=0,
141
+ maximum=1000000,
142
+ value=0,
143
+ )
144
+ text2image_predict = gr.Button(value="Generator")
145
+
146
+ with gr.Column():
147
+ output_image = gr.Gallery(
148
+ label="Generated images",
149
+ show_label=False,
150
+ elem_id="gallery",
151
+ ).style(grid=(1, 2), height=200)
152
+
153
+ text2image_predict.click(
154
+ fn=StableDiffusionText2ImageGenerator().generate_image,
155
+ inputs=[
156
+ text2image_model_path,
157
+ text2image_prompt,
158
+ text2image_negative_prompt,
159
+ text2image_num_images_per_prompt,
160
+ text2image_scheduler,
161
+ text2image_guidance_scale,
162
+ text2image_num_inference_step,
163
+ text2image_height,
164
+ text2image_width,
165
+ text2image_seed_generator,
166
+ ],
167
+ outputs=output_image,
168
  )
169
+
170
+ import gradio as gr
171
+
172
+
173
+ def diffusion_app():
174
+ app = gr.Blocks()
175
+ with app:
176
  with gr.Row():
177
+ with gr.Column():
178
+ StableDiffusionText2ImageGenerator.app()
179
+
180
+ app.launch(debug=True)
 
 
 
 
 
 
 
 
 
 
181
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
182
 
183
  if __name__ == "__main__":
184
+ diffusion_app()