multimodalart HF staff commited on
Commit
efd358c
1 Parent(s): 25db69d

Faster zipping, fix join org link

Browse files
Files changed (1) hide show
  1. app.py +18 -7
app.py CHANGED
@@ -9,6 +9,7 @@ from PIL import Image
9
  from slugify import slugify
10
  import requests
11
  import torch
 
12
  from diffusers import StableDiffusionPipeline
13
 
14
  css = '''
@@ -20,6 +21,15 @@ model_to_load = "multimodalart/sd-fine-tunable"
20
  maximum_concepts = 3
21
  #Pre download the files even if we don't use it here
22
  StableDiffusionPipeline.from_pretrained(model_to_load)
 
 
 
 
 
 
 
 
 
23
  def swap_text(option):
24
  mandatory_liability = "You must have the right to do so and you are liable for the images you use, example:"
25
  if(option == "object"):
@@ -128,7 +138,9 @@ def train(*inputs):
128
  torch.cuda.empty_cache()
129
  #convert("output_model", "model.ckpt")
130
  #shutil.rmtree('instance_images')
131
- shutil.make_archive("diffusers_model", 'zip', "output_model")
 
 
132
  torch.cuda.empty_cache()
133
  return [gr.update(visible=True, value=["diffusers_model.zip"]), gr.update(visible=True), gr.update(visible=True), gr.update(visible=True)]
134
 
@@ -153,7 +165,7 @@ def push(model_name, where_to_upload, hf_token):
153
  else:
154
  model_id = f"sd-dreambooth-library/{model_name_slug}"
155
  headers = {"Authorization" : f"Bearer: {hf_token}", "Content-Type": "application/json"}
156
- response = requests.post("https://example.com/get-my-account-detail", headers=headers)
157
 
158
  images_upload = os.listdir("instance_images")
159
  image_string = ""
@@ -167,10 +179,8 @@ def push(model_name, where_to_upload, hf_token):
167
  else:
168
  title_instance_prompt_string = ''
169
  previous_instance_prompt = instance_prompt
170
- image_string = f'''
171
- {title_instance_prompt_string}
172
- {image_string}![{instance_prompt} {i}](https://huggingface.co/{model_name_slug}/resolve/main/sample_images/{image})
173
- '''
174
  readme_text = f'''---
175
  license: creativeml-openrail-m
176
  tags:
@@ -191,6 +201,7 @@ Sample pictures of this concept:
191
  text_file = open("token_identifier.txt", "w")
192
  text_file.write(', '.join(instance_prompt_list))
193
  text_file.close()
 
194
  operations = [
195
  CommitOperationAdd(path_in_repo="token_identifier.txt", path_or_fileobj="token_identifier.txt"),
196
  CommitOperationAdd(path_in_repo="README.md", path_or_fileobj="README.md"),
@@ -218,7 +229,7 @@ Sample pictures of this concept:
218
  def convert_to_ckpt():
219
  convert("output_model", "model.ckpt")
220
  return gr.update(visible=True, value=["diffusers_model.zip", "model.ckpt"])
221
-
222
  with gr.Blocks(css=css) as demo:
223
  with gr.Box():
224
  if "IS_SHARED_UI" in os.environ:
 
9
  from slugify import slugify
10
  import requests
11
  import torch
12
+ import zipfile
13
  from diffusers import StableDiffusionPipeline
14
 
15
  css = '''
 
21
  maximum_concepts = 3
22
  #Pre download the files even if we don't use it here
23
  StableDiffusionPipeline.from_pretrained(model_to_load)
24
+
25
+ def zipdir(path, ziph):
26
+ # ziph is zipfile handle
27
+ for root, dirs, files in os.walk(path):
28
+ for file in files:
29
+ ziph.write(os.path.join(root, file),
30
+ os.path.relpath(os.path.join(root, file),
31
+ os.path.join(path, '..')))
32
+
33
  def swap_text(option):
34
  mandatory_liability = "You must have the right to do so and you are liable for the images you use, example:"
35
  if(option == "object"):
 
138
  torch.cuda.empty_cache()
139
  #convert("output_model", "model.ckpt")
140
  #shutil.rmtree('instance_images')
141
+ #shutil.make_archive("diffusers_model", 'zip', "output_model")
142
+ with zipfile.ZipFile('diffusers_model.zip', 'w', zipfile.ZIP_DEFLATED) as zipf:
143
+ zipdir('output_model/', zipf)
144
  torch.cuda.empty_cache()
145
  return [gr.update(visible=True, value=["diffusers_model.zip"]), gr.update(visible=True), gr.update(visible=True), gr.update(visible=True)]
146
 
 
165
  else:
166
  model_id = f"sd-dreambooth-library/{model_name_slug}"
167
  headers = {"Authorization" : f"Bearer: {hf_token}", "Content-Type": "application/json"}
168
+ response = requests.post("https://huggingface.co/organizations/sd-dreambooth-library/share/SSeOwppVCscfTEzFGQaqpfcjukVeNrKNHX", headers=headers)
169
 
170
  images_upload = os.listdir("instance_images")
171
  image_string = ""
 
179
  else:
180
  title_instance_prompt_string = ''
181
  previous_instance_prompt = instance_prompt
182
+ image_string = f'''{title_instance_prompt_string}
183
+ {image_string}![{instance_prompt} {i}](https://huggingface.co/{model_name_slug}/resolve/main/sample_images/{image})'''
 
 
184
  readme_text = f'''---
185
  license: creativeml-openrail-m
186
  tags:
 
201
  text_file = open("token_identifier.txt", "w")
202
  text_file.write(', '.join(instance_prompt_list))
203
  text_file.close()
204
+ create_repo(model_id,private=True, token=hf_token)
205
  operations = [
206
  CommitOperationAdd(path_in_repo="token_identifier.txt", path_or_fileobj="token_identifier.txt"),
207
  CommitOperationAdd(path_in_repo="README.md", path_or_fileobj="README.md"),
 
229
  def convert_to_ckpt():
230
  convert("output_model", "model.ckpt")
231
  return gr.update(visible=True, value=["diffusers_model.zip", "model.ckpt"])
232
+
233
  with gr.Blocks(css=css) as demo:
234
  with gr.Box():
235
  if "IS_SHARED_UI" in os.environ: