multimodalart HF staff commited on
Commit
c74a0ea
1 Parent(s): 95ea872

Fix locally and let it work with A10G

Browse files
Files changed (2) hide show
  1. app.py +34 -16
  2. requirements.txt +1 -2
app.py CHANGED
@@ -1,5 +1,17 @@
1
- import gradio as gr
2
  import os
 
 
 
 
 
 
 
 
 
 
 
 
3
  from pathlib import Path
4
  import argparse
5
  import shutil
@@ -16,7 +28,6 @@ import gc
16
  from diffusers import StableDiffusionPipeline
17
  from huggingface_hub import snapshot_download, update_repo_visibility, HfApi
18
 
19
-
20
  is_spaces = True if "SPACE_ID" in os.environ else False
21
  if(is_spaces):
22
  is_shared_ui = True if "multimodalart/dreambooth-training" in os.environ['SPACE_ID'] else False
@@ -40,8 +51,8 @@ if(is_gpu_associated):
40
  safety_checker = snapshot_download(repo_id="multimodalart/sd-sc")
41
  model_to_load = model_v1
42
 
43
- with zipfile.ZipFile("mix.zip", 'r') as zip_ref:
44
- zip_ref.extractall(".")
45
 
46
  def swap_text(option, base):
47
  resize_width = 768 if base == "v2-1-768" else 512
@@ -96,17 +107,19 @@ def count_files(*inputs):
96
  Training_Steps = 2400 #Avoid overfitting on person faces
97
  if(is_spaces):
98
  if(selected_model == "v1-5"):
99
- its = 1.1
100
  if(experimental_faces):
101
  its = 1
102
  elif(selected_model == "v2-1-512"):
103
- its = 0.8
104
  if(experimental_faces):
105
  its = 0.7
106
  elif(selected_model == "v2-1-768"):
107
- its = 0.5
 
 
108
  summary_sentence = f'''You are going to train {concept_counter} {type_of_thing}(s), with {file_counter} images for {Training_Steps} steps. The training should take around {round(Training_Steps/its, 2)} seconds, or {round((Training_Steps/its)/60, 2)} minutes.
109
- The setup, compression and uploading the model can take up to 20 minutes.<br>As the T4-Small GPU costs US$0.60 for 1h, <span style="font-size: 120%"><b>the estimated cost for this training is below US${round((((Training_Steps/its)/3600)+0.3+0.1)*0.60, 2)}.</b></span><br><br>
110
  If you check the box below the GPU attribution will automatically removed after training is done and the model is uploaded. If not, don't forget to come back here and swap the hardware back to CPU.<br><br>'''
111
  else:
112
  summary_sentence = f'''You are going to train {concept_counter} {type_of_thing}(s), with {file_counter} images for {Training_Steps} steps.<br><br>'''
@@ -141,7 +154,8 @@ def validate_model_upload(hf_token, model_name):
141
  except:
142
  raise gr.Error("You have inserted an invalid Hugging Face token")
143
  try:
144
- update_repo_visibility(repo_id=os.environ['SPACE_ID'], private=True, token=hf_token, repo_type="space")
 
145
  except:
146
  raise gr.Error("Oops, you created a Hugging Face token with read permissions only. You need one with write permissions")
147
  else:
@@ -153,10 +167,14 @@ def train(*inputs):
153
  if is_shared_ui:
154
  raise gr.Error("This Space only works in duplicated instances")
155
  if not is_gpu_associated:
156
- raise gr.Error("Please associate a T4 GPU for this Space")
157
  hf_token = inputs[-5]
158
  model_name = inputs[-7]
159
- remove_attribution_after = inputs[-6]
 
 
 
 
160
  if(remove_attribution_after):
161
  validate_model_upload(hf_token, model_name)
162
 
@@ -469,7 +487,7 @@ with gr.Blocks(css=css) as demo:
469
  top_description = gr.HTML(f'''
470
  <div class="gr-prose" style="max-width: 80%">
471
  <h2>Attention - This Space doesn't work in this shared UI</h2>
472
- <p>For it to work, you can either run locally or duplicate the Space and run it on your own profile using a (paid) private T4 GPU for training. As each T4 costs US$0.60/h, it should cost < US$1 to train most models using default settings!&nbsp;&nbsp;<a class="duplicate-button" style="display:inline-block" target="_blank" href="https://huggingface.co/spaces/{os.environ['SPACE_ID']}?duplicate=true"><img src="https://img.shields.io/badge/-Duplicate%20Space-blue?labelColor=white&style=flat&logo=data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAAXNSR0IArs4c6QAAAP5JREFUOE+lk7FqAkEURY+ltunEgFXS2sZGIbXfEPdLlnxJyDdYB62sbbUKpLbVNhyYFzbrrA74YJlh9r079973psed0cvUD4A+4HoCjsA85X0Dfn/RBLBgBDxnQPfAEJgBY+A9gALA4tcbamSzS4xq4FOQAJgCDwV2CPKV8tZAJcAjMMkUe1vX+U+SMhfAJEHasQIWmXNN3abzDwHUrgcRGmYcgKe0bxrblHEB4E/pndMazNpSZGcsZdBlYJcEL9Afo75molJyM2FxmPgmgPqlWNLGfwZGG6UiyEvLzHYDmoPkDDiNm9JR9uboiONcBXrpY1qmgs21x1QwyZcpvxt9NS09PlsPAAAAAElFTkSuQmCC&logoWidth=14" alt="Duplicate Space"></a></p>
473
  <img class="instruction" src="file/duplicate.png">
474
  <img class="arrow" src="file/arrow.png" />
475
  </div>
@@ -478,15 +496,15 @@ with gr.Blocks(css=css) as demo:
478
  if(is_gpu_associated):
479
  top_description = gr.HTML(f'''
480
  <div class="gr-prose" style="max-width: 80%">
481
- <h2>You have successfully associated a GPU to the Dreambooth Training Space 🎉</h2>
482
- <p>Certify that you got a T4. You can now train your model! You will be billed by the minute from when you activated the GPU until when it is turned it off.</p>
483
  </div>
484
  ''')
485
  else:
486
  top_description = gr.HTML(f'''
487
  <div class="gr-prose" style="max-width: 80%">
488
  <h2>You have successfully duplicated the Dreambooth Training Space 🎉</h2>
489
- <p>There's only one step left before you can train your model: <a href="https://huggingface.co/spaces/{os.environ['SPACE_ID']}/settings" style="text-decoration: underline" target="_blank">attribute a <b>T4 GPU</b> to it (via the Settings tab)</a> and run the training below. Other GPUs are not compatible for now. You will be billed by the minute from when you activate the GPU until when it is turned it off.</p>
490
  </div>
491
  ''')
492
  else:
@@ -573,7 +591,7 @@ with gr.Blocks(css=css) as demo:
573
  if(is_shared_ui):
574
  training_ongoing = gr.Markdown("## This Space only works in duplicated instances. Please duplicate it and try again!", visible=False)
575
  elif(not is_gpu_associated):
576
- training_ongoing = gr.Markdown("## Oops, you haven't associated your T4 GPU to this Space. Visit the Settings tab, associate and try again.", visible=False)
577
  else:
578
  training_ongoing = gr.Markdown("## Training is ongoing ⌛... You can close this tab if you like or just wait. If you did not check the `Remove GPU After training`, you can come back here to try your model and upload it after training. Don't forget to remove the GPU attribution after you are done. ", visible=False)
579
 
 
1
+ from subprocess import getoutput
2
  import os
3
+
4
+ gpu_info = getoutput('nvidia-smi')
5
+ if("A10G" in gpu_info):
6
+ which_gpu = "A10G"
7
+ os.system(f"pip install -q https://github.com/camenduru/stable-diffusion-webui-colab/releases/download/0.0.15/xformers-0.0.15.dev0+4c06c79.d20221205-cp38-cp38-linux_x86_64.whl")
8
+ elif("T4" in gpu_info):
9
+ which_gpu = "T4"
10
+ os.system(f"pip install -q https://github.com/camenduru/stable-diffusion-webui-colab/releases/download/0.0.15/xformers-0.0.15.dev0+1515f77.d20221130-cp38-cp38-linux_x86_64.whl")
11
+ else:
12
+ which_gpu = "CPU"
13
+
14
+ import gradio as gr
15
  from pathlib import Path
16
  import argparse
17
  import shutil
 
28
  from diffusers import StableDiffusionPipeline
29
  from huggingface_hub import snapshot_download, update_repo_visibility, HfApi
30
 
 
31
  is_spaces = True if "SPACE_ID" in os.environ else False
32
  if(is_spaces):
33
  is_shared_ui = True if "multimodalart/dreambooth-training" in os.environ['SPACE_ID'] else False
 
51
  safety_checker = snapshot_download(repo_id="multimodalart/sd-sc")
52
  model_to_load = model_v1
53
 
54
+ #with zipfile.ZipFile("mix.zip", 'r') as zip_ref:
55
+ # zip_ref.extractall(".")
56
 
57
  def swap_text(option, base):
58
  resize_width = 768 if base == "v2-1-768" else 512
 
107
  Training_Steps = 2400 #Avoid overfitting on person faces
108
  if(is_spaces):
109
  if(selected_model == "v1-5"):
110
+ its = 1.1 if which_gpu == "T4" else 1.8
111
  if(experimental_faces):
112
  its = 1
113
  elif(selected_model == "v2-1-512"):
114
+ its = 0.8 if which_gpu == "T4" else 1.5
115
  if(experimental_faces):
116
  its = 0.7
117
  elif(selected_model == "v2-1-768"):
118
+ its = 0.48 if which_gpu == "T4" else 0.85
119
+
120
+ gpu_price = 0.60 if which_gpu == "T4" else 1.10
121
  summary_sentence = f'''You are going to train {concept_counter} {type_of_thing}(s), with {file_counter} images for {Training_Steps} steps. The training should take around {round(Training_Steps/its, 2)} seconds, or {round((Training_Steps/its)/60, 2)} minutes.
122
+ The setup, compression and uploading the model can take up to 20 minutes.<br>As the {which_gpu}-Small GPU costs US${gpu_price} for 1h, <span style="font-size: 120%"><b>the estimated cost for this training is below US${round((((Training_Steps/its)/3600)+0.3+0.1)*gpu_price, 2)}.</b></span><br><br>
123
  If you check the box below the GPU attribution will automatically removed after training is done and the model is uploaded. If not, don't forget to come back here and swap the hardware back to CPU.<br><br>'''
124
  else:
125
  summary_sentence = f'''You are going to train {concept_counter} {type_of_thing}(s), with {file_counter} images for {Training_Steps} steps.<br><br>'''
 
154
  except:
155
  raise gr.Error("You have inserted an invalid Hugging Face token")
156
  try:
157
+ if(is_spaces):
158
+ update_repo_visibility(repo_id=os.environ['SPACE_ID'], private=True, token=hf_token, repo_type="space")
159
  except:
160
  raise gr.Error("Oops, you created a Hugging Face token with read permissions only. You need one with write permissions")
161
  else:
 
167
  if is_shared_ui:
168
  raise gr.Error("This Space only works in duplicated instances")
169
  if not is_gpu_associated:
170
+ raise gr.Error("Please associate a T4 or A10G GPU for this Space")
171
  hf_token = inputs[-5]
172
  model_name = inputs[-7]
173
+ if(is_spaces):
174
+ remove_attribution_after = inputs[-6]
175
+ else:
176
+ remove_attribution_after = False
177
+
178
  if(remove_attribution_after):
179
  validate_model_upload(hf_token, model_name)
180
 
 
487
  top_description = gr.HTML(f'''
488
  <div class="gr-prose" style="max-width: 80%">
489
  <h2>Attention - This Space doesn't work in this shared UI</h2>
490
+ <p>For it to work, you can either run locally or duplicate the Space and run it on your own profile using a (paid) private T4-small or A10G-small GPU for training. A T4 costs US$0.60/h, so it should cost < US$1 to train most models using default settings with it!&nbsp;&nbsp;<a class="duplicate-button" style="display:inline-block" target="_blank" href="https://huggingface.co/spaces/{os.environ['SPACE_ID']}?duplicate=true"><img src="https://img.shields.io/badge/-Duplicate%20Space-blue?labelColor=white&style=flat&logo=data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAAXNSR0IArs4c6QAAAP5JREFUOE+lk7FqAkEURY+ltunEgFXS2sZGIbXfEPdLlnxJyDdYB62sbbUKpLbVNhyYFzbrrA74YJlh9r079973psed0cvUD4A+4HoCjsA85X0Dfn/RBLBgBDxnQPfAEJgBY+A9gALA4tcbamSzS4xq4FOQAJgCDwV2CPKV8tZAJcAjMMkUe1vX+U+SMhfAJEHasQIWmXNN3abzDwHUrgcRGmYcgKe0bxrblHEB4E/pndMazNpSZGcsZdBlYJcEL9Afo75molJyM2FxmPgmgPqlWNLGfwZGG6UiyEvLzHYDmoPkDDiNm9JR9uboiONcBXrpY1qmgs21x1QwyZcpvxt9NS09PlsPAAAAAElFTkSuQmCC&logoWidth=14" alt="Duplicate Space"></a></p>
491
  <img class="instruction" src="file/duplicate.png">
492
  <img class="arrow" src="file/arrow.png" />
493
  </div>
 
496
  if(is_gpu_associated):
497
  top_description = gr.HTML(f'''
498
  <div class="gr-prose" style="max-width: 80%">
499
+ <h2>You have successfully associated a {which_gpu} GPU to the Dreambooth Training Space 🎉</h2>
500
+ <p>You can now train your model! You will be billed by the minute from when you activated the GPU until when it is turned it off.</p>
501
  </div>
502
  ''')
503
  else:
504
  top_description = gr.HTML(f'''
505
  <div class="gr-prose" style="max-width: 80%">
506
  <h2>You have successfully duplicated the Dreambooth Training Space 🎉</h2>
507
+ <p>There's only one step left before you can train your model: <a href="https://huggingface.co/spaces/{os.environ['SPACE_ID']}/settings" style="text-decoration: underline" target="_blank">attribute a <b>T4-small or A10G-small GPU</b> to it (via the Settings tab)</a> and run the training below. You will be billed by the minute from when you activate the GPU until when it is turned it off.</p>
508
  </div>
509
  ''')
510
  else:
 
591
  if(is_shared_ui):
592
  training_ongoing = gr.Markdown("## This Space only works in duplicated instances. Please duplicate it and try again!", visible=False)
593
  elif(not is_gpu_associated):
594
+ training_ongoing = gr.Markdown("## Oops, you haven't associated your T4 or A10G GPU to this Space. Visit the Settings tab, associate and try again.", visible=False)
595
  else:
596
  training_ongoing = gr.Markdown("## Training is ongoing ⌛... You can close this tab if you like or just wait. If you did not check the `Remove GPU After training`, you can come back here to try your model and upload it after training. Don't forget to remove the GPU attribution after you are done. ", visible=False)
597
 
requirements.txt CHANGED
@@ -14,5 +14,4 @@ triton==2.0.0.dev20220701
14
  bitsandbytes
15
  python-slugify
16
  requests
17
- tensorboard
18
- https://github.com/apolinario/xformers/releases/download/0.0.2/xformers-0.0.14.dev0-cp38-cp38-linux_x86_64.whl
 
14
  bitsandbytes
15
  python-slugify
16
  requests
17
+ tensorboard