poulpy5 commited on
Commit
49442a5
·
verified ·
1 Parent(s): a30e43f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +136 -18
app.py CHANGED
@@ -1,18 +1,30 @@
1
  #!/usr/bin/env python
2
 
3
- import os
4
- import uuid
5
  import gradio as gr
6
- import spaces
7
  import torch
8
  from diffusers import DiffusionPipeline
 
 
 
 
 
 
9
 
 
10
  DESCRIPTION = """# Playground v2.5"""
11
  if not torch.cuda.is_available():
12
  DESCRIPTION += "\n<p>Running on CPU 🥶 This demo may not work on CPU.</p>"
13
 
 
 
 
 
 
 
 
14
  device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
15
 
 
16
  pipe = DiffusionPipeline.from_pretrained(
17
  "playgroundai/playground-v2.5-1024px-aesthetic",
18
  torch_dtype=torch.float16,
@@ -21,12 +33,24 @@ pipe = DiffusionPipeline.from_pretrained(
21
  variant="fp16"
22
  )
23
  pipe.to(device)
 
 
 
 
24
 
 
25
  def save_image(img):
26
  unique_name = str(uuid.uuid4()) + ".png"
27
  img.save(unique_name)
28
  return unique_name
29
 
 
 
 
 
 
 
 
30
  @spaces.GPU(enable_queue=True)
31
  def generate(
32
  prompt: str,
@@ -37,48 +61,142 @@ def generate(
37
  height: int = 1024,
38
  guidance_scale: float = 3,
39
  randomize_seed: bool = False,
 
 
40
  ):
41
  pipe.to(device)
42
- seed = random.randint(0, np.iinfo(np.int32).max) if randomize_seed else seed
43
  generator = torch.Generator().manual_seed(seed)
44
 
 
 
 
45
  images = pipe(
46
  prompt=prompt,
47
- negative_prompt=negative_prompt if use_negative_prompt else None,
48
  width=width,
49
  height=height,
50
  guidance_scale=guidance_scale,
51
  num_inference_steps=25,
52
  generator=generator,
 
 
 
53
  ).images
54
 
55
  image_paths = [save_image(img) for img in images]
56
  return image_paths, seed
57
 
58
- with gr.Blocks() as demo:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
59
  gr.Markdown(DESCRIPTION)
 
 
 
 
 
60
  with gr.Group():
61
  with gr.Row():
62
- prompt = gr.Textbox(label="Prompt")
63
- run_button = gr.Button("Run")
64
- result = gr.Gallery(label="Result")
 
 
 
 
 
 
65
  with gr.Accordion("Advanced options", open=False):
66
- use_negative_prompt = gr.Checkbox(label="Use negative prompt", value=False)
67
- negative_prompt = gr.Textbox(label="Negative prompt")
68
- seed = gr.Slider(label="Seed", minimum=0, maximum=np.iinfo(np.int32).max, step=1, value=0)
 
 
 
 
 
 
 
 
 
 
 
 
69
  randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
70
- width = gr.Slider(label="Width", minimum=256, maximum=1536, step=32, value=1024)
71
- height = gr.Slider(label="Height", minimum=256, maximum=1536, step=32, value=1024)
72
- guidance_scale = gr.Slider(label="Guidance Scale", minimum=0.1, maximum=20, step=0.1, value=3.0)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
73
 
74
  gr.on(
75
- triggers=[prompt.submit, negative_prompt.submit, run_button.click],
 
 
 
 
76
  fn=generate,
77
- inputs=[prompt, negative_prompt, use_negative_prompt, seed, width, height, guidance_scale, randomize_seed],
 
 
 
 
 
 
 
 
 
78
  outputs=[result, seed],
79
  api_name="run",
80
  )
81
 
82
  if __name__ == "__main__":
83
- demo.launch()
84
 
 
1
  #!/usr/bin/env python
2
 
 
 
3
  import gradio as gr
 
4
  import torch
5
  from diffusers import DiffusionPipeline
6
+ import spaces
7
+ import os
8
+ import random
9
+ import numpy as np
10
+ from PIL import Image
11
+ import uuid
12
 
13
+ # Description et Configuration
14
  DESCRIPTION = """# Playground v2.5"""
15
  if not torch.cuda.is_available():
16
  DESCRIPTION += "\n<p>Running on CPU 🥶 This demo may not work on CPU.</p>"
17
 
18
+ MAX_SEED = np.iinfo(np.int32).max
19
+ CACHE_EXAMPLES = torch.cuda.is_available() and os.getenv("CACHE_EXAMPLES", "1") == "1"
20
+ MAX_IMAGE_SIZE = int(os.getenv("MAX_IMAGE_SIZE", "1536"))
21
+ USE_TORCH_COMPILE = os.getenv("USE_TORCH_COMPILE", "0") == "1"
22
+ ENABLE_CPU_OFFLOAD = os.getenv("ENABLE_CPU_OFFLOAD", "0") == "1"
23
+
24
+ # Définir l'appareil
25
  device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
26
 
27
+ # Charger le pipeline de diffusion
28
  pipe = DiffusionPipeline.from_pretrained(
29
  "playgroundai/playground-v2.5-1024px-aesthetic",
30
  torch_dtype=torch.float16,
 
33
  variant="fp16"
34
  )
35
  pipe.to(device)
36
+ if ENABLE_CPU_OFFLOAD:
37
+ pipe.enable_model_cpu_offload()
38
+ if USE_TORCH_COMPILE:
39
+ pipe.unet = torch.compile(pipe.unet, mode="reduce-overhead", fullgraph=True)
40
 
41
+ # Fonction pour enregistrer l'image
42
  def save_image(img):
43
  unique_name = str(uuid.uuid4()) + ".png"
44
  img.save(unique_name)
45
  return unique_name
46
 
47
+ # Fonction pour randomiser le seed
48
+ def randomize_seed_fn(seed: int, randomize_seed: bool) -> int:
49
+ if randomize_seed:
50
+ seed = random.randint(0, MAX_SEED)
51
+ return seed
52
+
53
+ # Fonction de génération avec ZeroGPU
54
  @spaces.GPU(enable_queue=True)
55
  def generate(
56
  prompt: str,
 
61
  height: int = 1024,
62
  guidance_scale: float = 3,
63
  randomize_seed: bool = False,
64
+ use_resolution_binning: bool = True,
65
+ progress=gr.Progress(track_tqdm=True),
66
  ):
67
  pipe.to(device)
68
+ seed = int(randomize_seed_fn(seed, randomize_seed))
69
  generator = torch.Generator().manual_seed(seed)
70
 
71
+ if not use_negative_prompt:
72
+ negative_prompt = None
73
+
74
  images = pipe(
75
  prompt=prompt,
76
+ negative_prompt=negative_prompt,
77
  width=width,
78
  height=height,
79
  guidance_scale=guidance_scale,
80
  num_inference_steps=25,
81
  generator=generator,
82
+ num_images_per_prompt=1,
83
+ use_resolution_binning=use_resolution_binning,
84
+ output_type="pil",
85
  ).images
86
 
87
  image_paths = [save_image(img) for img in images]
88
  return image_paths, seed
89
 
90
+ # Exemples de prompts
91
+ examples = [
92
+ "neon holography crystal cat",
93
+ "a cat eating a piece of cheese",
94
+ "an astronaut riding a horse in space",
95
+ "a cartoon of a boy playing with a tiger",
96
+ "a cute robot artist painting on an easel, concept art",
97
+ "a close up of a woman wearing a transparent, prismatic, elaborate nemeses headdress, over the should pose, brown skin-tone"
98
+ ]
99
+
100
+ # CSS et Interface Gradio
101
+ css = '''
102
+ .gradio-container{max-width: 560px !important}
103
+ h1{text-align:center}
104
+ '''
105
+ with gr.Blocks(css=css) as demo:
106
  gr.Markdown(DESCRIPTION)
107
+ gr.DuplicateButton(
108
+ value="Duplicate Space for private use",
109
+ elem_id="duplicate-button",
110
+ visible=os.getenv("SHOW_DUPLICATE_BUTTON") == "1",
111
+ )
112
  with gr.Group():
113
  with gr.Row():
114
+ prompt = gr.Textbox(
115
+ label="Prompt",
116
+ show_label=False,
117
+ max_lines=1,
118
+ placeholder="Enter your prompt",
119
+ container=False,
120
+ )
121
+ run_button = gr.Button("Run", scale=0)
122
+ result = gr.Gallery(label="Result", columns=1, show_label=False)
123
  with gr.Accordion("Advanced options", open=False):
124
+ with gr.Row():
125
+ use_negative_prompt = gr.Checkbox(label="Use negative prompt", value=False)
126
+ negative_prompt = gr.Textbox(
127
+ label="Negative prompt",
128
+ max_lines=1,
129
+ placeholder="Enter a negative prompt",
130
+ visible=True,
131
+ )
132
+ seed = gr.Slider(
133
+ label="Seed",
134
+ minimum=0,
135
+ maximum=MAX_SEED,
136
+ step=1,
137
+ value=0,
138
+ )
139
  randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
140
+ with gr.Row(visible=True):
141
+ width = gr.Slider(
142
+ label="Width",
143
+ minimum=256,
144
+ maximum=MAX_IMAGE_SIZE,
145
+ step=32,
146
+ value=1024,
147
+ )
148
+ height = gr.Slider(
149
+ label="Height",
150
+ minimum=256,
151
+ maximum=MAX_IMAGE_SIZE,
152
+ step=32,
153
+ value=1024,
154
+ )
155
+ with gr.Row():
156
+ guidance_scale = gr.Slider(
157
+ label="Guidance Scale",
158
+ minimum=0.1,
159
+ maximum=20,
160
+ step=0.1,
161
+ value=3.0,
162
+ )
163
+
164
+ gr.Examples(
165
+ examples=examples,
166
+ inputs=prompt,
167
+ outputs=[result, seed],
168
+ fn=generate,
169
+ cache_examples=CACHE_EXAMPLES,
170
+ )
171
+
172
+ use_negative_prompt.change(
173
+ fn=lambda x: gr.update(visible=x),
174
+ inputs=use_negative_prompt,
175
+ outputs=negative_prompt,
176
+ api_name=False,
177
+ )
178
 
179
  gr.on(
180
+ triggers=[
181
+ prompt.submit,
182
+ negative_prompt.submit,
183
+ run_button.click,
184
+ ],
185
  fn=generate,
186
+ inputs=[
187
+ prompt,
188
+ negative_prompt,
189
+ use_negative_prompt,
190
+ seed,
191
+ width,
192
+ height,
193
+ guidance_scale,
194
+ randomize_seed,
195
+ ],
196
  outputs=[result, seed],
197
  api_name="run",
198
  )
199
 
200
  if __name__ == "__main__":
201
+ demo.queue(max_size=20).launch()
202