JacobLinCool commited on
Commit
b05b11d
·
verified ·
1 Parent(s): 23ecf7a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +118 -104
app.py CHANGED
@@ -1,49 +1,66 @@
 
 
 
1
  import gradio as gr
2
  import numpy as np
3
- import random
4
- from diffusers import DiffusionPipeline
5
  import torch
 
6
 
7
- device = "cuda" if torch.cuda.is_available() else "cpu"
 
 
 
 
 
 
8
 
9
- if torch.cuda.is_available():
10
- torch.cuda.max_memory_allocated(device=device)
11
- pipe = DiffusionPipeline.from_pretrained("stabilityai/stable-diffusion-xl-base-1.0", torch_dtype=torch.float16, variant="fp16", use_safetensors=True)
12
- pipe.load_lora_weights("JacobLinCool/sdxl-lora-gdsc-1")
13
- # pipe.enable_xformers_memory_efficient_attention()
14
- pipe = pipe.to(device)
15
- else:
16
- pipe = DiffusionPipeline.from_pretrained("stabilityai/stable-diffusion-xl-base-1.0", use_safetensors=True)
17
- pipe.load_lora_weights("JacobLinCool/sdxl-lora-gdsc-1")
18
- pipe = pipe.to(device)
19
 
20
  MAX_SEED = np.iinfo(np.int32).max
21
  MAX_IMAGE_SIZE = 1024
22
 
23
- def infer(prompt, negative_prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps):
24
-
 
 
 
 
 
 
 
 
 
25
  if randomize_seed:
26
  seed = random.randint(0, MAX_SEED)
27
-
28
  generator = torch.Generator().manual_seed(seed)
29
-
 
 
 
30
  image = pipe(
31
- prompt = prompt,
32
- negative_prompt = negative_prompt,
33
- guidance_scale = guidance_scale,
34
- num_inference_steps = num_inference_steps,
35
- width = width,
36
- height = height,
37
- generator = generator
38
- ).images[0]
39
-
40
  return image
41
 
42
- css="""
43
- #col-container {
44
- margin: 0 auto;
45
- max-width: 520px;
 
 
46
  }
 
 
 
 
 
 
47
  """
48
 
49
  if torch.cuda.is_available():
@@ -52,87 +69,84 @@ else:
52
  power_device = "CPU"
53
 
54
  with gr.Blocks(css=css) as demo:
55
-
56
- with gr.Column(elem_id="col-container"):
57
- gr.Markdown(f"""
58
- # Text-to-Image Gradio Template
59
- Currently running on {power_device}.
60
- """)
61
-
62
- with gr.Row():
63
-
64
- prompt = gr.Text(
65
- label="Prompt",
66
- show_label=False,
67
- max_lines=1,
68
- placeholder="Enter your prompt",
69
- container=False,
70
- )
71
-
72
- run_button = gr.Button("Run", scale=0)
73
-
74
- result = gr.Image(label="Result", show_label=False)
75
-
76
- with gr.Accordion("Advanced Settings", open=False):
77
-
78
- negative_prompt = gr.Text(
79
- label="Negative prompt",
80
- max_lines=1,
81
- placeholder="Enter a negative prompt",
82
- visible=False,
83
- value="",
84
- )
85
-
86
- seed = gr.Slider(
87
- label="Seed",
88
- minimum=0,
89
- maximum=MAX_SEED,
90
- step=1,
91
- value=0,
92
- )
93
-
94
- randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
95
-
96
  with gr.Row():
97
-
98
- width = gr.Slider(
99
- label="Width",
100
- minimum=256,
101
- maximum=MAX_IMAGE_SIZE,
102
- step=32,
103
- value=512,
104
  )
105
-
106
- height = gr.Slider(
107
- label="Height",
108
- minimum=256,
109
- maximum=MAX_IMAGE_SIZE,
110
- step=32,
111
- value=512,
112
- )
113
-
114
- with gr.Row():
115
-
116
- guidance_scale = gr.Slider(
117
- label="Guidance scale",
118
- minimum=0.0,
119
- maximum=10.0,
120
- step=0.1,
121
- value=0.0,
122
  )
123
-
124
- num_inference_steps = gr.Slider(
125
- label="Number of inference steps",
126
- minimum=1,
127
- maximum=12,
128
  step=1,
129
- value=2,
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
130
  )
131
 
132
  run_button.click(
133
- fn = infer,
134
- inputs = [prompt, negative_prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps],
135
- outputs = [result]
 
 
 
 
 
 
 
 
136
  )
137
 
 
138
  demo.queue().launch()
 
1
+ import json
2
+ import random
3
+
4
  import gradio as gr
5
  import numpy as np
6
+ import spaces
 
7
  import torch
8
+ from diffusers import DiffusionPipeline, LCMScheduler
9
 
10
+ DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
11
+ model_id = "stabilityai/stable-diffusion-xl-base-1.0"
12
+
13
+ pipe = DiffusionPipeline.from_pretrained(model_id, variant="fp16")
14
+ pipe.scheduler = LCMScheduler.from_config(pipe.scheduler.config)
15
+ pipe.load_lora_weights("JacobLinCool/sdxl-lora-gdsc-1")
16
+ pipe.to(device=DEVICE, dtype=torch.float16)
17
 
 
 
 
 
 
 
 
 
 
 
18
 
19
  MAX_SEED = np.iinfo(np.int32).max
20
  MAX_IMAGE_SIZE = 1024
21
 
22
+ @spaces.GPU
23
+ def infer(
24
+ pre_prompt,
25
+ prompt,
26
+ seed,
27
+ randomize_seed,
28
+ num_inference_steps,
29
+ negative_prompt,
30
+ guidance_scale,
31
+ progress=gr.Progress(track_tqdm=True),
32
+ ):
33
  if randomize_seed:
34
  seed = random.randint(0, MAX_SEED)
35
+
36
  generator = torch.Generator().manual_seed(seed)
37
+
38
+ if pre_prompt != "":
39
+ prompt = f"{pre_prompt} {prompt}"
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
+ generator=generator,
47
+ ).images[0]
48
+
 
 
49
  return image
50
 
51
+
52
+ css = """
53
+
54
+ h1 {
55
+ text-align: center;
56
+ display:block;
57
  }
58
+
59
+ p {
60
+ text-align: justify;
61
+ display:block;
62
+ }
63
+
64
  """
65
 
66
  if torch.cuda.is_available():
 
69
  power_device = "CPU"
70
 
71
  with gr.Blocks(css=css) as demo:
72
+ with gr.Row():
73
+ with gr.Column():
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
74
  with gr.Row():
75
+ prompt = gr.Text(
76
+ label="Prompt",
77
+ show_label=False,
78
+ max_lines=1,
79
+ placeholder="Enter your prompt",
80
+ container=False,
81
+ scale=5,
82
  )
83
+
84
+ run_button = gr.Button("Run", scale=1)
85
+
86
+ result = gr.Image(label="Result", show_label=False)
87
+
88
+ with gr.Accordion("Advanced Settings", open=False):
89
+
90
+ pre_prompt = gr.Text(
91
+ label="Pre-Prompt",
92
+ show_label=True,
93
+ max_lines=1,
94
+ placeholder="Pre Prompt from the LoRA config",
95
+ container=True,
96
+ scale=5,
 
 
 
97
  )
98
+
99
+ seed = gr.Slider(
100
+ label="Seed",
101
+ minimum=0,
102
+ maximum=MAX_SEED,
103
  step=1,
104
+ value=0,
105
+ )
106
+
107
+ randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
108
+
109
+ with gr.Row():
110
+
111
+ num_inference_steps = gr.Slider(
112
+ label="Number of inference steps",
113
+ minimum=4,
114
+ maximum=8,
115
+ step=1,
116
+ value=4,
117
+ )
118
+
119
+ with gr.Row():
120
+
121
+ guidance_scale = gr.Slider(
122
+ label="Guidance Scale",
123
+ minimum=1,
124
+ maximum=6,
125
+ step=0.5,
126
+ value=1,
127
+ )
128
+
129
+ negative_prompt = gr.Text(
130
+ label="Negative Prompt",
131
+ show_label=False,
132
+ max_lines=1,
133
+ placeholder="Enter a negative Prompt",
134
+ container=False,
135
  )
136
 
137
  run_button.click(
138
+ fn=infer,
139
+ inputs=[
140
+ pre_prompt,
141
+ prompt,
142
+ seed,
143
+ randomize_seed,
144
+ num_inference_steps,
145
+ negative_prompt,
146
+ guidance_scale,
147
+ ],
148
+ outputs=[result],
149
  )
150
 
151
+
152
  demo.queue().launch()