zhangyang-0123 commited on
Commit
ee69d30
·
1 Parent(s): de221d8

change layout

Browse files
Files changed (1) hide show
  1. app.py +54 -107
app.py CHANGED
@@ -2,7 +2,7 @@ import gradio as gr
2
  import numpy as np
3
  import random
4
 
5
- import spaces #[uncomment to use ZeroGPU]
6
  from diffusers import DiffusionPipeline
7
  import torch
8
 
@@ -21,34 +21,14 @@ MAX_SEED = np.iinfo(np.int32).max
21
  MAX_IMAGE_SIZE = 1024
22
 
23
 
24
- @spaces.GPU #[uncomment to use ZeroGPU]
25
- def infer(
26
- prompt,
27
- negative_prompt,
28
- seed,
29
- randomize_seed,
30
- width,
31
- height,
32
- guidance_scale,
33
- num_inference_steps,
34
- progress=gr.Progress(track_tqdm=True),
35
- ):
36
- if randomize_seed:
37
- seed = random.randint(0, MAX_SEED)
38
-
39
- generator = torch.Generator().manual_seed(seed)
40
-
41
- image = pipe(
42
- prompt=prompt,
43
- negative_prompt=negative_prompt,
44
- guidance_scale=guidance_scale,
45
- num_inference_steps=num_inference_steps,
46
- width=width,
47
- height=height,
48
- generator=generator,
49
- ).images[0]
50
-
51
- return image, seed
52
 
53
 
54
  examples = [
@@ -63,91 +43,58 @@ css = """
63
  max-width: 640px;
64
  }
65
  """
 
 
 
 
 
 
 
 
 
66
 
67
  with gr.Blocks(css=css) as demo:
68
- with gr.Column(elem_id="col-container"):
69
- gr.Markdown(" # Text-to-Image Gradio Template")
70
-
71
- with gr.Row():
72
- prompt = gr.Text(
73
- label="Prompt",
74
- show_label=False,
75
- max_lines=1,
76
- placeholder="Enter your prompt",
77
- container=False,
78
- )
79
-
80
- run_button = gr.Button("Run", scale=0, variant="primary")
81
-
82
- result = gr.Image(label="Result", show_label=False)
83
-
84
- with gr.Accordion("Advanced Settings", open=False):
85
- negative_prompt = gr.Text(
86
- label="Negative prompt",
87
- max_lines=1,
88
- placeholder="Enter a negative prompt",
89
- visible=False,
90
- )
91
-
92
- seed = gr.Slider(
93
- label="Seed",
94
- minimum=0,
95
- maximum=MAX_SEED,
96
- step=1,
97
- value=0,
98
- )
99
-
100
- randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
101
-
102
- with gr.Row():
103
- width = gr.Slider(
104
- label="Width",
105
- minimum=256,
106
- maximum=MAX_IMAGE_SIZE,
107
- step=32,
108
- value=1024, # Replace with defaults that work for your model
109
- )
110
-
111
- height = gr.Slider(
112
- label="Height",
113
- minimum=256,
114
- maximum=MAX_IMAGE_SIZE,
115
- step=32,
116
- value=1024, # Replace with defaults that work for your model
117
- )
118
-
119
- with gr.Row():
120
- guidance_scale = gr.Slider(
121
- label="Guidance scale",
122
- minimum=0.0,
123
- maximum=10.0,
124
- step=0.1,
125
- value=0.0, # Replace with defaults that work for your model
126
- )
127
-
128
- num_inference_steps = gr.Slider(
129
- label="Number of inference steps",
130
- minimum=1,
131
- maximum=50,
132
- step=1,
133
- value=2, # Replace with defaults that work for your model
134
- )
135
-
136
- gr.Examples(examples=examples, inputs=[prompt])
137
  gr.on(
138
- triggers=[run_button.click, prompt.submit],
139
- fn=infer,
140
  inputs=[
141
  prompt,
142
- negative_prompt,
143
  seed,
144
- randomize_seed,
145
- width,
146
- height,
147
- guidance_scale,
148
- num_inference_steps,
149
  ],
150
- outputs=[result, seed],
151
  )
152
 
153
  if __name__ == "__main__":
 
2
  import numpy as np
3
  import random
4
 
5
+ import spaces # [uncomment to use ZeroGPU]
6
  from diffusers import DiffusionPipeline
7
  import torch
8
 
 
21
  MAX_IMAGE_SIZE = 1024
22
 
23
 
24
+ @spaces.GPU # [uncomment to use ZeroGPU]
25
+ def generate_images(prompt, seed, steps, pipe, pruned_pipe):
26
+ # Run the model and return images directly
27
+ g_cpu = torch.Generator("cuda").manual_seed(seed)
28
+ original_image = pipe(prompt=prompt, generator=g_cpu, num_inference_steps=steps).images[0]
29
+ g_cpu = torch.Generator("cuda").manual_seed(seed)
30
+ ecodiff_image = pruned_pipe(prompt=prompt, generator=g_cpu, num_inference_steps=steps).images[0]
31
+ return original_image, ecodiff_image
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
32
 
33
 
34
  examples = [
 
43
  max-width: 640px;
44
  }
45
  """
46
+ header = """
47
+ # 🌱 Text-to-Image Generation with EcoDiff Pruned SD-XL (20% Pruning Ratio)
48
+ # Under Construction!!!
49
+ <div style="text-align: center; display: flex; justify-content: left; gap: 5px;">
50
+ <a href="https://arxiv.org/abs/2412.02852"><img src="https://img.shields.io/badge/ariXv-Paper-A42C25.svg" alt="arXiv"></a>
51
+ <a href="https://huggingface.co/zhangyang-0123/EcoDiffPrunedModels"><img src="https://img.shields.io/badge/🤗-Model-ffbd45.svg" alt="HuggingFace"></a>
52
+ <a href="https://github.com/YaNgZhAnG-V5/EcoDiff"><img src="https://img.shields.io/badge/GitHub-Code-blue.svg?logo=github&" alt="GitHub"></a>
53
+ </div>
54
+ """
55
 
56
  with gr.Blocks(css=css) as demo:
57
+ gr.Markdown(header)
58
+ with gr.Row():
59
+ prompt = gr.Textbox(
60
+ label="Prompt",
61
+ value="A clock tower floating in a sea of clouds",
62
+ scale=3,
63
+ )
64
+ seed = gr.Number(label="Seed", value=44, precision=0, scale=1)
65
+ steps = gr.Slider(
66
+ label="Number of Steps",
67
+ minimum=1,
68
+ maximum=100,
69
+ value=50,
70
+ step=1,
71
+ scale=1,
72
+ )
73
+ generate_btn = gr.Button("Generate Images")
74
+ gr.Examples(
75
+ examples=[
76
+ "A clock tower floating in a sea of clouds",
77
+ "Astronaut in a jungle, cold color palette, muted colors, detailed, 8k",
78
+ "An astronaut riding a green horse",
79
+ "A delicious ceviche cheesecake slice",
80
+ "A sprawling cyberpunk metropolis at night, with towering skyscrapers emitting neon lights of every color, holographic billboards advertising alien languages",
81
+ ],
82
+ inputs=[prompt],
83
+ )
84
+ with gr.Row():
85
+ original_output = gr.Image(label="Original Output")
86
+ ecodiff_output = gr.Image(label="EcoDiff Output")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
87
  gr.on(
88
+ triggers=[generate_btn.click, prompt.submit],
89
+ fn=generate_images,
90
  inputs=[
91
  prompt,
 
92
  seed,
93
+ steps,
94
+ pipe,
95
+ pipe,
 
 
96
  ],
97
+ outputs=[original_output, ecodiff_output],
98
  )
99
 
100
  if __name__ == "__main__":