fffiloni commited on
Commit
8c12ff1
·
1 Parent(s): 7f07a26

added auto filled sfts + trigger word

Browse files
Files changed (1) hide show
  1. app.py +86 -19
app.py CHANGED
@@ -1,10 +1,14 @@
1
  import gradio as gr
2
- from huggingface_hub import login
3
  import os
 
4
  is_shared_ui = True if "fffiloni/sd-xl-custom-model" in os.environ['SPACE_ID'] else False
5
  hf_token = os.environ.get("HF_TOKEN")
6
  login(token=hf_token)
7
 
 
 
 
8
  import torch
9
  from diffusers import DiffusionPipeline, AutoencoderKL
10
 
@@ -13,25 +17,64 @@ vae = AutoencoderKL.from_pretrained("madebyollin/sdxl-vae-fp16-fix", torch_dtype
13
  pipe = DiffusionPipeline.from_pretrained(
14
  "stabilityai/stable-diffusion-xl-base-1.0",
15
  vae=vae, torch_dtype=torch.float16, variant="fp16",
16
- use_safetensors=True
17
  )
18
 
19
  device="cuda" if torch.cuda.is_available() else "cpu"
20
 
21
  pipe.to(device)
22
 
23
- def load_model(custom_model, weight_name):
24
-
25
  if custom_model == "":
26
  gr.Warning("If you want to use a private model, you need to duplicate this space on your personal account.")
27
  raise gr.Error("You forgot to define Model ID.")
28
-
29
- # This is where you load your trained weights
30
- pipe.load_lora_weights(custom_model, weight_name=weight_name, use_auth_token=True)
31
-
32
- return "Model loaded!"
33
 
34
- def infer (prompt, inf_steps, guidance_scale, seed, lora_weight, progress=gr.Progress(track_tqdm=True)):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
35
 
36
  generator = torch.Generator(device="cuda").manual_seed(seed)
37
 
@@ -97,11 +140,34 @@ with gr.Blocks(css=css) as demo:
97
  """)
98
  with gr.Row():
99
  with gr.Column():
100
- custom_model = gr.Textbox(label="Your custom model ID", placeholder="your_username/your_trained_model_name", info="Make sure your model is set to PUBLIC ")
101
- weight_name = gr.Textbox(label="Safetensors file", value="pytorch_lora_weights.safetensors", info="specify which one if model has several .safetensors files")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
102
  with gr.Column():
103
  load_model_btn = gr.Button("Load my model")
104
- model_status = gr.Textbox(label="model status", interactive=False)
105
 
106
  prompt_in = gr.Textbox(label="Prompt")
107
  with gr.Row():
@@ -121,10 +187,11 @@ with gr.Blocks(css=css) as demo:
121
  )
122
  seed = gr.Slider(
123
  label="Seed",
124
- minimum=0,
125
- maximum=500000,
 
126
  step=1,
127
- value=42
128
  )
129
  lora_weight = gr.Slider(
130
  label="LoRa weigth",
@@ -138,12 +205,12 @@ with gr.Blocks(css=css) as demo:
138
 
139
  load_model_btn.click(
140
  fn = load_model,
141
- inputs=[custom_model, weight_name],
142
- outputs = [model_status]
143
  )
144
  submit_btn.click(
145
  fn = infer,
146
- inputs = [prompt_in, inf_steps, guidance_scale, seed, lora_weight],
147
  outputs = [image_out]
148
  )
149
 
 
1
  import gradio as gr
2
+ from huggingface_hub import login, HfFileSystem, HfApi, ModelCard
3
  import os
4
+
5
  is_shared_ui = True if "fffiloni/sd-xl-custom-model" in os.environ['SPACE_ID'] else False
6
  hf_token = os.environ.get("HF_TOKEN")
7
  login(token=hf_token)
8
 
9
+ fs = HfFileSystem(token=hf_token)
10
+ api = HfApi()
11
+
12
  import torch
13
  from diffusers import DiffusionPipeline, AutoencoderKL
14
 
 
17
  pipe = DiffusionPipeline.from_pretrained(
18
  "stabilityai/stable-diffusion-xl-base-1.0",
19
  vae=vae, torch_dtype=torch.float16, variant="fp16",
20
+ #use_safetensors=True
21
  )
22
 
23
  device="cuda" if torch.cuda.is_available() else "cpu"
24
 
25
  pipe.to(device)
26
 
27
+ def load_model(custom_model):
28
+
29
  if custom_model == "":
30
  gr.Warning("If you want to use a private model, you need to duplicate this space on your personal account.")
31
  raise gr.Error("You forgot to define Model ID.")
 
 
 
 
 
32
 
33
+ # Get instance_prompt a.k.a trigger word
34
+ card = ModelCard.load(custom_model)
35
+ repo_data = card.data.to_dict()
36
+ instance_prompt = repo_data.get("instance_prompt")
37
+
38
+ if instance_prompt is not None:
39
+ print(f"Trigger word: {instance_prompt}")
40
+ else:
41
+ instance_prompt = "no trigger word needed"
42
+ print(f"Trigger word: no trigger word needed")
43
+
44
+ # List all ".safetensors" files in repo
45
+ sfts_available_files = fs.glob(f"{custom_model}/*safetensors")
46
+ sfts_available_files = get_files(sfts_available_files)
47
+
48
+ if sfts_available_files == []:
49
+ sfts_available_files = ["NO SAFETENSORS FILE"]
50
+
51
+ print(f"Safetensors available: {sfts_available_files}")
52
+
53
+ return gr.update(choices=sfts_available_files, value=sfts_available_files[0], visible=True), gr.update(value=instance_prompt, visible=True)
54
+
55
+
56
+
57
+ def infer (custom_model, weight_name, prompt, inf_steps, guidance_scale, seed, lora_weight, progress=gr.Progress(track_tqdm=True)):
58
+
59
+ if weight_name == "NO SAFETENSORS FILE":
60
+ pipe.load_lora_weights(
61
+ custom_model,
62
+ low_cpu_mem_usage = True,
63
+ use_auth_token = True
64
+ )
65
+
66
+ else:
67
+ pipe.load_lora_weights(
68
+ custom_model,
69
+ weight_name = weight_name,
70
+ low_cpu_mem_usage = True,
71
+ use_auth_token = True
72
+ )
73
+
74
+ pipe.fuse_lora(lora_weight)
75
+
76
+ if seed < 0 :
77
+ seed = random.randint(0, 423538377342)
78
 
79
  generator = torch.Generator(device="cuda").manual_seed(seed)
80
 
 
140
  """)
141
  with gr.Row():
142
  with gr.Column():
143
+ if not is_shared_ui:
144
+ your_username = api.whoami()["name"]
145
+ my_models = api.list_models(author=your_username, filter=["diffusers", "stable-diffusion-xl", 'lora'])
146
+ model_names = [item.modelId for item in my_models]
147
+
148
+ if not is_shared_ui:
149
+ custom_model = gr.Dropdown(
150
+ label = "Your custom model ID",
151
+ choices = model_names,
152
+ allow_custom_value = True
153
+ #placeholder = "username/model_id"
154
+ )
155
+ else:
156
+ custom_model = gr.Textbox(
157
+ label="Your custom model ID",
158
+ placeholder="your_username/your_trained_model_name",
159
+ info="Make sure your model is set to PUBLIC"
160
+ )
161
+
162
+ weight_name = gr.Dropdown(
163
+ label="Safetensors file",
164
+ #value="pytorch_lora_weights.safetensors",
165
+ info="specify which one if model has several .safetensors files",
166
+ visible = False
167
+ )
168
  with gr.Column():
169
  load_model_btn = gr.Button("Load my model")
170
+ trigger_word = gr.Textbox(label="Trigger word", interactive=False, visible=False)
171
 
172
  prompt_in = gr.Textbox(label="Prompt")
173
  with gr.Row():
 
187
  )
188
  seed = gr.Slider(
189
  label="Seed",
190
+ info = "-1 denotes a random seed",
191
+ minimum=-1,
192
+ maximum=423538377342,
193
  step=1,
194
+ value=-1
195
  )
196
  lora_weight = gr.Slider(
197
  label="LoRa weigth",
 
205
 
206
  load_model_btn.click(
207
  fn = load_model,
208
+ inputs=[custom_model],
209
+ outputs = [model_status, weight_name, trigger_word]
210
  )
211
  submit_btn.click(
212
  fn = infer,
213
+ inputs = [custom_model, weight_name, prompt_in, inf_steps, guidance_scale, seed, lora_weight],
214
  outputs = [image_out]
215
  )
216