frogleo commited on
Commit
781a759
·
1 Parent(s): 975fe79

首次提交

Browse files
Files changed (4) hide show
  1. __pycache__/utils.cpython-310.pyc +0 -0
  2. app.py +166 -106
  3. requirements.txt +8 -6
  4. utils.py +35 -0
__pycache__/utils.cpython-310.pyc ADDED
Binary file (1.13 kB). View file
 
app.py CHANGED
@@ -1,128 +1,186 @@
 
1
  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
-
9
- device = "cuda" if torch.cuda.is_available() else "cpu"
10
- model_repo_id = "stabilityai/sdxl-turbo" # Replace to the model you would like to use
11
-
12
- if torch.cuda.is_available():
13
- torch_dtype = torch.float16
14
- else:
15
- torch_dtype = torch.float32
16
-
17
- pipe = DiffusionPipeline.from_pretrained(model_repo_id, torch_dtype=torch_dtype)
18
- pipe = pipe.to(device)
19
 
20
  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 = [
55
  "Astronaut in a jungle, cold color palette, muted colors, detailed, 8k",
56
  "An astronaut riding a green horse",
57
  "A delicious ceviche cheesecake slice",
58
  ]
59
 
60
- css = """
61
- #col-container {
62
- margin: 0 auto;
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(
@@ -130,25 +188,27 @@ with gr.Blocks(css=css) as demo:
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__":
154
- demo.launch()
 
1
+ import spaces
2
  import gradio as gr
3
  import numpy as np
 
 
 
 
4
  import torch
5
+ import random
6
+ import logging
7
+ import utils
8
+ from diffusers.models import AutoencoderKL
 
 
 
 
 
 
 
9
 
10
  MAX_SEED = np.iinfo(np.int32).max
11
+ MIN_IMAGE_SIZE = 512
12
+ MAX_IMAGE_SIZE = 2048
13
+
14
+ # Enhanced logging configuration
15
+ logging.basicConfig(
16
+ level=logging.INFO,
17
+ format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
18
+ datefmt='%Y-%m-%d %H:%M:%S'
19
+ )
20
+ logger = logging.getLogger(__name__)
21
+
22
+ # PyTorch settings for better performance and determinism
23
+ torch.backends.cudnn.deterministic = True
24
+ torch.backends.cudnn.benchmark = False
25
+ torch.backends.cuda.matmul.allow_tf32 = True
26
+
27
+ device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
28
+ logger.info(f"Using device: {device}")
29
+
30
+ # Model initialization
31
+ # if torch.cuda.is_available():
32
+ # try:
33
+ # logger.info("Loading VAE and pipeline...")
34
+ # vae = AutoencoderKL.from_pretrained(
35
+ # "madebyollin/sdxl-vae-fp16-fix",
36
+ # torch_dtype=torch.float16,
37
+ # )
38
+ # pipe = utils.load_pipeline("cagliostrolab/animagine-xl-4.0", device, vae=vae)
39
+ # logger.info("Pipeline loaded successfully on GPU!")
40
+ # except Exception as e:
41
+ # logger.error(f"Error loading VAE, falling back to default: {e}")
42
+ # pipe = utils.load_pipeline("cagliostrolab/animagine-xl-4.0", device)
43
+ # else:
44
+ # logger.warning("CUDA not available, running on CPU")
45
+ # pipe = None
46
+
47
+
48
+
49
+ @spaces.GPU
50
+ def generate(
51
+ prompt: str,
52
+ negative_prompt: str,
53
+ width: int,
54
+ height: int,
55
+ scheduler: str,
56
+ upscaler_strength:float,
57
+ upscale_by:float,
58
+ seed: int,
59
+ randomize_seed: bool,
60
+ guidance_scale: float,
61
+ num_inference_steps: int,
62
+ progress:gr.Progress=gr.Progress(track_tqdm=True),
63
  ):
64
  if randomize_seed:
65
  seed = random.randint(0, MAX_SEED)
66
 
67
+ # generator = torch.Generator().manual_seed(seed)
68
+
69
+ # image = pipe(
70
+ # prompt=prompt,
71
+ # negative_prompt=negative_prompt,
72
+ # guidance_scale=guidance_scale,
73
+ # num_inference_steps=num_inference_steps,
74
+ # width=width,
75
+ # height=height,
76
+ # generator=generator,
77
+ # ).images[0]
78
 
79
+ # return image, seed
 
 
 
 
 
 
 
 
80
 
81
+ return None, seed
82
 
83
 
84
+
85
+ scheduler_list = [
86
+ "DPM++ 2M Karras",
87
+ "DPM++ SDE Karras",
88
+ "DPM++ 2M SDE Karras",
89
+ "Euler",
90
+ "Euler a",
91
+ "DDIM"
92
+ ]
93
+
94
+
95
+ title = "# Animagine XL 4.0 Demo"
96
+
97
  examples = [
98
  "Astronaut in a jungle, cold color palette, muted colors, detailed, 8k",
99
  "An astronaut riding a green horse",
100
  "A delicious ceviche cheesecake slice",
101
  ]
102
 
103
+ custom_css = """
104
+ #row-container {
105
+ align-items: stretch;
106
+ }
107
+ #output-image{
108
+ flex-grow: 1;
109
  }
110
  """
111
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
112
 
113
+ with gr.Blocks(css=custom_css).queue() as demo:
114
+ gr.Markdown(title)
115
+ with gr.Row(
116
+ elem_id="row-container"
117
+ ):
118
+ with gr.Column():
119
+ gr.Markdown("### Input")
120
+ with gr.Column():
121
+ prompt = gr.Text(
122
+ label="Prompt",
123
+ max_lines=1,
124
+ placeholder="Enter your prompt",
125
+ )
126
+ negative_prompt = gr.Text(
127
+ label="Negative prompt",
128
+ max_lines=1,
129
+ placeholder="Enter a negative prompt",
130
+ )
131
  with gr.Row():
132
  width = gr.Slider(
133
  label="Width",
134
+ minimum=MIN_IMAGE_SIZE,
135
  maximum=MAX_IMAGE_SIZE,
136
+ step=8,
137
+ value=832,
138
  )
 
139
  height = gr.Slider(
140
  label="Height",
141
+ minimum=MIN_IMAGE_SIZE,
142
  maximum=MAX_IMAGE_SIZE,
143
+ step=8,
144
+ value=1216,
145
  )
146
+ with gr.Row():
147
+ upscaler_strength = gr.Slider(
148
+ label="Upscaler strength",
149
+ minimum=0,
150
+ maximum=1,
151
+ step=0.05,
152
+ value=0.55,
153
+ )
154
+ upscale_by = gr.Slider(
155
+ label="Upscale",
156
+ minimum=1,
157
+ maximum=1.5,
158
+ step=0.1,
159
+ value=1.5,
160
+ )
161
+ with gr.Column():
162
+ scheduler = gr.Dropdown(
163
+ label="scheduler",
164
+ choices=scheduler_list,
165
+ interactive=True,
166
+ value="Euler a",
167
+ )
168
+ with gr.Column():
169
+ seed = gr.Slider(
170
+ label="Seed",
171
+ minimum=0,
172
+ maximum=MAX_SEED,
173
+ step=1,
174
+ value=0,
175
+ )
176
+ randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
177
  with gr.Row():
178
  guidance_scale = gr.Slider(
179
  label="Guidance scale",
180
+ minimum=1.0,
181
+ maximum=12.0,
182
  step=0.1,
183
+ value=6.0,
184
  )
185
 
186
  num_inference_steps = gr.Slider(
 
188
  minimum=1,
189
  maximum=50,
190
  step=1,
191
+ value=25,
192
  )
193
+ run_button = gr.Button("Run", variant="primary")
194
 
195
+ with gr.Column():
196
+ gr.Markdown("### Output")
197
+ result = gr.Image(
198
+ label="Generated Image",
199
+ elem_id="output-image"
200
+ )
201
+ run_button.click(
202
+ fn=generate,
203
  inputs=[
204
+ prompt, negative_prompt,
205
+ width, height,
206
+ scheduler,
207
+ upscaler_strength,upscale_by,
208
+ seed,randomize_seed,
209
+ guidance_scale,num_inference_steps
210
+ ],
 
 
211
  outputs=[result, seed],
212
+ )
213
 
214
+ demo.launch()
 
requirements.txt CHANGED
@@ -1,6 +1,8 @@
1
- accelerate
2
- diffusers
3
- invisible_watermark
4
- torch
5
- transformers
6
- xformers
 
 
 
1
+ accelerate>=1.2.1
2
+ diffusers>=0.32.1
3
+ gradio==4.44.1
4
+ hf-transfer>=0.1.9
5
+ spaces>=0.32.0
6
+ torch>=2.4.0
7
+ transformers>=4.48.0
8
+ tomli>=2.0.1
utils.py ADDED
@@ -0,0 +1,35 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ from typing import Optional, Any
3
+ from diffusers import (
4
+ DDIMScheduler,
5
+ DPMSolverMultistepScheduler,
6
+ DPMSolverSinglestepScheduler,
7
+ EulerAncestralDiscreteScheduler,
8
+ EulerDiscreteScheduler,
9
+ AutoencoderKL,
10
+ StableDiffusionXLPipeline,
11
+ )
12
+ import logging
13
+
14
+ def load_pipeline(model_name: str, device: torch.device, hf_token: Optional[str] = None, vae: Optional[AutoencoderKL] = None) -> Any:
15
+ """Load the Stable Diffusion pipeline."""
16
+ try:
17
+ pipeline = (
18
+ StableDiffusionXLPipeline.from_single_file
19
+ if model_name.endswith(".safetensors")
20
+ else StableDiffusionXLPipeline.from_pretrained
21
+ )
22
+
23
+ pipe = pipeline(
24
+ model_name,
25
+ vae=vae,
26
+ torch_dtype=torch.float16,
27
+ custom_pipeline="lpw_stable_diffusion_xl",
28
+ use_safetensors=True,
29
+ add_watermarker=False
30
+ )
31
+ pipe.to(device)
32
+ return pipe
33
+ except Exception as e:
34
+ logging.error(f"Failed to load pipeline: {str(e)}", exc_info=True)
35
+ raise