macrdel commited on
Commit
a2c9c1b
·
1 Parent(s): 4ebd26e

update app.py

Browse files
Files changed (2) hide show
  1. .gitignore +2 -1
  2. app.py +109 -66
.gitignore CHANGED
@@ -2,4 +2,5 @@ Include
2
  Lib
3
  Scripts
4
  share
5
- *env*
 
 
2
  Lib
3
  Scripts
4
  share
5
+ *env*
6
+ unico_*
app.py CHANGED
@@ -2,32 +2,68 @@ 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 #, FluxPipeline
7
  import torch
8
- # import os
9
 
10
- # device = "cuda" if torch.cuda.is_available() else "cpu"
11
- # torch_dtype = torch.bfloat16 if torch.cuda.is_available() else torch.float32
12
- # model_repo = "black-forest-labs/FLUX.1-dev" # Replace to the model you would like to use
 
 
 
 
 
 
 
13
 
14
- # pipe = FluxPipeline.from_pretrained(model_repo,
15
- # torch_dtype=torch_dtype,
16
- # token=f"{os.environ.get('tkn')}",
17
- # #force_download=True,
18
- # ).to(device)
19
- #pipe.enable_model_cpu_offload()
20
 
21
- device = "cuda" if torch.cuda.is_available() else "cpu"
22
- torch_dtype = torch.float16 if torch.cuda.is_available() else torch.float32
23
- model_repo = "stabilityai/sdxl-turbo"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
24
 
25
  MAX_SEED = np.iinfo(np.int32).max
26
  MAX_IMAGE_SIZE = 1024
27
 
28
-
29
- # @spaces.GPU #[uncomment to use ZeroGPU]
30
  def infer(
 
31
  prompt,
32
  negative_prompt,
33
  seed,
@@ -36,18 +72,24 @@ def infer(
36
  height,
37
  guidance_scale,
38
  num_inference_steps,
39
- model_repo=model_repo,
40
  progress=gr.Progress(track_tqdm=True),
41
  ):
 
 
 
42
  if randomize_seed:
43
  seed = random.randint(0, MAX_SEED)
44
 
45
- generator = torch.Generator().manual_seed(seed)
46
 
47
- pipe = DiffusionPipeline.from_pretrained(
48
- model_repo,
49
- torch_dtype=torch_dtype,
50
- ).to(device)
 
 
 
51
 
52
  image = pipe(
53
  prompt=prompt,
@@ -61,7 +103,6 @@ def infer(
61
 
62
  return image, seed
63
 
64
-
65
  examples = [
66
  "Astronaut in a jungle, cold color palette, muted colors, detailed, 8k",
67
  "An astronaut riding a green horse",
@@ -77,20 +118,15 @@ css = """
77
 
78
  with gr.Blocks(css=css) as demo:
79
  with gr.Column(elem_id="col-container"):
80
- gr.Markdown(" # Text2Img Gradio")
81
- # gr.Markdown(f" ## Model '{model_repo}'")
82
-
83
- model_dropdown = gr.Dropdown(
84
- label="Select Model",
85
- choices=[
86
- "stabilityai/sdxl-turbo",
87
- "stabilityai/stable-diffusion-xl-base-1.0",
88
- #"runwayml/stable-diffusion-v1-5",
89
- #"SG161222/Realistic_Vision_V5.1_noVAE",
90
- "stabilityai/stable-diffusion-2-1",
91
- ],
92
- value="stabilityai/sdxl-turbo",
93
- )
94
 
95
  with gr.Row():
96
  prompt = gr.Text(
@@ -105,25 +141,22 @@ with gr.Blocks(css=css) as demo:
105
 
106
  result = gr.Image(label="Result", show_label=False)
107
 
108
- with gr.Accordion("Advanced Settings", open=True):
109
- with gr.Row():
110
- negative_prompt = gr.Text(
111
- label="Negative prompt",
112
- max_lines=1,
113
- placeholder="Enter a negative prompt",
114
- visible=True,
115
- )
116
-
117
- with gr.Row():
118
- seed = gr.Slider(
119
- label="Seed",
120
- minimum=0,
121
- maximum=MAX_SEED,
122
- step=1,
123
- value=777,
124
- )
125
 
126
- randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
127
 
128
  with gr.Row():
129
  width = gr.Slider(
@@ -131,7 +164,7 @@ with gr.Blocks(css=css) as demo:
131
  minimum=256,
132
  maximum=MAX_IMAGE_SIZE,
133
  step=32,
134
- value=1024, # Replace with defaults that work for your model
135
  )
136
 
137
  height = gr.Slider(
@@ -139,32 +172,42 @@ with gr.Blocks(css=css) as demo:
139
  minimum=256,
140
  maximum=MAX_IMAGE_SIZE,
141
  step=32,
142
- value=1024, # Replace with defaults that work for your model
143
  )
144
 
145
  with gr.Row():
146
  guidance_scale = gr.Slider(
147
- label="Guidance scale (CFG)",
148
- minimum=1.0,
149
- maximum=10.0,
150
  step=0.5,
151
- value=3.5, # Replace with defaults that work for your model
152
  )
153
 
154
  num_inference_steps = gr.Slider(
155
  label="Number of inference steps",
156
  minimum=1,
157
- maximum=50,
158
  step=1,
159
- value=1, # Replace with defaults that work for your model
160
  )
161
 
 
 
 
 
 
 
 
 
 
 
162
  gr.Examples(examples=examples, inputs=[prompt])
163
-
164
  gr.on(
165
  triggers=[run_button.click, prompt.submit],
166
  fn=infer,
167
  inputs=[
 
168
  prompt,
169
  negative_prompt,
170
  seed,
@@ -173,7 +216,7 @@ with gr.Blocks(css=css) as demo:
173
  height,
174
  guidance_scale,
175
  num_inference_steps,
176
- model_dropdown,
177
  ],
178
  outputs=[result, seed],
179
  )
 
2
  import numpy as np
3
  import random
4
 
5
+ from diffusers import DiffusionPipeline
6
+ from peft import PeftModel, PeftConfig
7
  import torch
 
8
 
9
+ device = "cuda" if torch.cuda.is_available() else "cpu"
10
+
11
+ # Model list including your LoRA model
12
+ MODEL_LIST = [
13
+ "CompVis/stable-diffusion-v1-4",
14
+ "stabilityai/sdxl-turbo",
15
+ "runwayml/stable-diffusion-v1-5",
16
+ "stabilityai/stable-diffusion-2-1",
17
+ "macrdel/unico_proj",
18
+ ]
19
 
20
+ if torch.cuda.is_available():
21
+ torch_dtype = torch.float16
22
+ else:
23
+ torch_dtype = torch.float32
 
 
24
 
25
+ # Cache to avoid re-initializing pipelines repeatedly
26
+ model_cache = {}
27
+
28
+ def load_pipeline(model_id: str):
29
+ """
30
+ Loads or retrieves a cached DiffusionPipeline.
31
+
32
+ If the chosen model is your LoRA adapter, then load the base model
33
+ (CompVis/stable-diffusion-v1-4) and apply the LoRA weights.
34
+ """
35
+ if model_id in model_cache:
36
+ return model_cache[model_id]
37
+
38
+ if model_id == "macrdel/unico_proj":
39
+ # Use the specified base model for your LoRA adapter.
40
+ base_model = "CompVis/stable-diffusion-v1-4"
41
+ pipe = DiffusionPipeline.from_pretrained(base_model, torch_dtype=torch_dtype)
42
+ # Load the LoRA weights
43
+ pipe.unet = PeftModel.from_pretrained(
44
+ pipe.unet,
45
+ model_id,
46
+ subfolder="unet",
47
+ torch_dtype=torch_dtype
48
+ )
49
+ pipe.text_encoder = PeftModel.from_pretrained(
50
+ pipe.text_encoder,
51
+ model_id,
52
+ subfolder="text_encoder",
53
+ torch_dtype=torch_dtype
54
+ )
55
+ else:
56
+ pipe = DiffusionPipeline.from_pretrained(model_id, torch_dtype=torch_dtype)
57
+
58
+ pipe.to(device)
59
+ model_cache[model_id] = pipe
60
+ return pipe
61
 
62
  MAX_SEED = np.iinfo(np.int32).max
63
  MAX_IMAGE_SIZE = 1024
64
 
 
 
65
  def infer(
66
+ model_id,
67
  prompt,
68
  negative_prompt,
69
  seed,
 
72
  height,
73
  guidance_scale,
74
  num_inference_steps,
75
+ lora_scale, # New parameter for adjusting LoRA scale
76
  progress=gr.Progress(track_tqdm=True),
77
  ):
78
+ # Load the pipeline for the chosen model
79
+ pipe = load_pipeline(model_id)
80
+
81
  if randomize_seed:
82
  seed = random.randint(0, MAX_SEED)
83
 
84
+ generator = torch.Generator(device=device).manual_seed(seed)
85
 
86
+ # If using the LoRA model, update the LoRA scale if supported.
87
+ if model_id == "macrdel/unico_proj":
88
+ # This assumes your pipeline's unet has a method to update the LoRA scale.
89
+ if hasattr(pipe.unet, "set_lora_scale"):
90
+ pipe.unet.set_lora_scale(lora_scale)
91
+ else:
92
+ print("Warning: LoRA scale adjustment method not found on UNet.")
93
 
94
  image = pipe(
95
  prompt=prompt,
 
103
 
104
  return image, seed
105
 
 
106
  examples = [
107
  "Astronaut in a jungle, cold color palette, muted colors, detailed, 8k",
108
  "An astronaut riding a green horse",
 
118
 
119
  with gr.Blocks(css=css) as demo:
120
  with gr.Column(elem_id="col-container"):
121
+ gr.Markdown(" # Text-to-Image Gradio Template")
122
+
123
+ with gr.Row():
124
+ # Dropdown to select the model from Hugging Face
125
+ model_id = gr.Dropdown(
126
+ label="Model",
127
+ choices=MODEL_LIST,
128
+ value=MODEL_LIST[0], # Default model
129
+ )
 
 
 
 
 
130
 
131
  with gr.Row():
132
  prompt = gr.Text(
 
141
 
142
  result = gr.Image(label="Result", show_label=False)
143
 
144
+ with gr.Accordion("Advanced Settings", open=False):
145
+ negative_prompt = gr.Text(
146
+ label="Negative prompt",
147
+ max_lines=1,
148
+ placeholder="Enter a negative prompt",
149
+ )
150
+
151
+ seed = gr.Slider(
152
+ label="Seed",
153
+ minimum=0,
154
+ maximum=MAX_SEED,
155
+ step=1,
156
+ value=42, # Default seed
157
+ )
 
 
 
158
 
159
+ randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
160
 
161
  with gr.Row():
162
  width = gr.Slider(
 
164
  minimum=256,
165
  maximum=MAX_IMAGE_SIZE,
166
  step=32,
167
+ value=1024,
168
  )
169
 
170
  height = gr.Slider(
 
172
  minimum=256,
173
  maximum=MAX_IMAGE_SIZE,
174
  step=32,
175
+ value=1024,
176
  )
177
 
178
  with gr.Row():
179
  guidance_scale = gr.Slider(
180
+ label="Guidance scale",
181
+ minimum=0.0,
182
+ maximum=20.0,
183
  step=0.5,
184
+ value=7.0,
185
  )
186
 
187
  num_inference_steps = gr.Slider(
188
  label="Number of inference steps",
189
  minimum=1,
190
+ maximum=100,
191
  step=1,
192
+ value=20,
193
  )
194
 
195
+ # New slider for LoRA scale.
196
+ lora_scale = gr.Slider(
197
+ label="LoRA Scale",
198
+ minimum=0.0,
199
+ maximum=2.0,
200
+ step=0.1,
201
+ value=1.0,
202
+ info="Adjust the influence of the LoRA weights",
203
+ )
204
+
205
  gr.Examples(examples=examples, inputs=[prompt])
 
206
  gr.on(
207
  triggers=[run_button.click, prompt.submit],
208
  fn=infer,
209
  inputs=[
210
+ model_id,
211
  prompt,
212
  negative_prompt,
213
  seed,
 
216
  height,
217
  guidance_scale,
218
  num_inference_steps,
219
+ lora_scale, # Pass the new slider value
220
  ],
221
  outputs=[result, seed],
222
  )