fffiloni commited on
Commit
3d7e17e
Β·
verified Β·
1 Parent(s): 8889ff3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +60 -0
app.py CHANGED
@@ -11,6 +11,19 @@ api = HfApi()
11
 
12
  hf_token = os.environ.get("HF_TOKEN_WITH_WRITE_PERMISSION")
13
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14
  def train_dreambooth_blora_sdxl(instance_data_dir, b_lora_trained_folder, instance_prompt, max_train_steps, checkpoint_steps):
15
 
16
  script_filename = "train_dreambooth_b-lora_sdxl.py" # Assuming it's in the same folder
@@ -50,6 +63,12 @@ def train_dreambooth_blora_sdxl(instance_data_dir, b_lora_trained_folder, instan
50
  print(f"An error occurred: {e}")
51
 
52
  def main(image_path, b_lora_trained_folder, instance_prompt):
 
 
 
 
 
 
53
 
54
  local_dir = "image_to_train"
55
  # Check if the directory exists and create it if necessary
@@ -73,6 +92,47 @@ css = """
73
  """
74
  with gr.Blocks(css=css) as demo:
75
  with gr.Column(elem_id="col-container"):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
76
  image = gr.Image(sources=["upload"], type="filepath")
77
  b_lora_name = gr.Textbox(label="b_lora_name", placeholder="b_lora_trained_folder")
78
  instance_prompt = gr.Textbox(label="instance prompt", placeholder="[v42]")
 
11
 
12
  hf_token = os.environ.get("HF_TOKEN_WITH_WRITE_PERMISSION")
13
 
14
+ is_shared_ui = True if "fffiloni/train-dreambooth-lora-sdxl" in os.environ['SPACE_ID'] else False
15
+
16
+ is_gpu_associated = torch.cuda.is_available()
17
+
18
+ if is_gpu_associated:
19
+ gpu_info = getoutput('nvidia-smi')
20
+ if("A10G" in gpu_info):
21
+ which_gpu = "A10G"
22
+ elif("T4" in gpu_info):
23
+ which_gpu = "T4"
24
+ else:
25
+ which_gpu = "CPU"
26
+
27
  def train_dreambooth_blora_sdxl(instance_data_dir, b_lora_trained_folder, instance_prompt, max_train_steps, checkpoint_steps):
28
 
29
  script_filename = "train_dreambooth_b-lora_sdxl.py" # Assuming it's in the same folder
 
63
  print(f"An error occurred: {e}")
64
 
65
  def main(image_path, b_lora_trained_folder, instance_prompt):
66
+
67
+ if is_shared_ui:
68
+ raise gr.Error("This Space only works in duplicated instances")
69
+
70
+ if not is_gpu_associated:
71
+ raise gr.Error("Please associate a T4 or A10G GPU for this Space")
72
 
73
  local_dir = "image_to_train"
74
  # Check if the directory exists and create it if necessary
 
92
  """
93
  with gr.Blocks(css=css) as demo:
94
  with gr.Column(elem_id="col-container"):
95
+ if is_shared_ui:
96
+ top_description = gr.HTML(f'''
97
+ <div class="gr-prose">
98
+ <h2><svg xmlns="http://www.w3.org/2000/svg" width="18px" height="18px" style="margin-right: 0px;display: inline-block;"fill="none"><path fill="#fff" d="M7 13.2a6.3 6.3 0 0 0 4.4-10.7A6.3 6.3 0 0 0 .6 6.9 6.3 6.3 0 0 0 7 13.2Z"/><path fill="#fff" fill-rule="evenodd" d="M7 0a6.9 6.9 0 0 1 4.8 11.8A6.9 6.9 0 0 1 0 7 6.9 6.9 0 0 1 7 0Zm0 0v.7V0ZM0 7h.6H0Zm7 6.8v-.6.6ZM13.7 7h-.6.6ZM9.1 1.7c-.7-.3-1.4-.4-2.2-.4a5.6 5.6 0 0 0-4 1.6 5.6 5.6 0 0 0-1.6 4 5.6 5.6 0 0 0 1.6 4 5.6 5.6 0 0 0 4 1.7 5.6 5.6 0 0 0 4-1.7 5.6 5.6 0 0 0 1.7-4 5.6 5.6 0 0 0-1.7-4c-.5-.5-1.1-.9-1.8-1.2Z" clip-rule="evenodd"/><path fill="#000" fill-rule="evenodd" d="M7 2.9a.8.8 0 1 1 0 1.5A.8.8 0 0 1 7 3ZM5.8 5.7c0-.4.3-.6.6-.6h.7c.3 0 .6.2.6.6v3.7h.5a.6.6 0 0 1 0 1.3H6a.6.6 0 0 1 0-1.3h.4v-3a.6.6 0 0 1-.6-.7Z" clip-rule="evenodd"/></svg>
99
+ Attention: this Space need to be duplicated to work</h2>
100
+ <p class="main-message">
101
+ To make it work, <strong>duplicate the Space</strong> and run it on your own profile using a <strong>private</strong> GPU (T4-small or A10G-small).<br />
102
+ A T4 costs <strong>US$0.60/h</strong>, so it should cost < US$1 to train most models.
103
+ </p>
104
+ <p class="actions">
105
+ <a href="https://huggingface.co/spaces/{os.environ['SPACE_ID']}?duplicate=true">
106
+ <img src="https://huggingface.co/datasets/huggingface/badges/resolve/main/duplicate-this-space-lg-dark.svg" alt="Duplicate this Space" />
107
+ </a>
108
+ to start training your own B-LoRa model
109
+ </p>
110
+ </div>
111
+ ''', elem_id="warning-duplicate")
112
+ else:
113
+ if(is_gpu_associated):
114
+ top_description = gr.HTML(f'''
115
+ <div class="gr-prose">
116
+ <h2><svg xmlns="http://www.w3.org/2000/svg" width="18px" height="18px" style="margin-right: 0px;display: inline-block;"fill="none"><path fill="#fff" d="M7 13.2a6.3 6.3 0 0 0 4.4-10.7A6.3 6.3 0 0 0 .6 6.9 6.3 6.3 0 0 0 7 13.2Z"/><path fill="#fff" fill-rule="evenodd" d="M7 0a6.9 6.9 0 0 1 4.8 11.8A6.9 6.9 0 0 1 0 7 6.9 6.9 0 0 1 7 0Zm0 0v.7V0ZM0 7h.6H0Zm7 6.8v-.6.6ZM13.7 7h-.6.6ZM9.1 1.7c-.7-.3-1.4-.4-2.2-.4a5.6 5.6 0 0 0-4 1.6 5.6 5.6 0 0 0-1.6 4 5.6 5.6 0 0 0 1.6 4 5.6 5.6 0 0 0 4 1.7 5.6 5.6 0 0 0 4-1.7 5.6 5.6 0 0 0 1.7-4 5.6 5.6 0 0 0-1.7-4c-.5-.5-1.1-.9-1.8-1.2Z" clip-rule="evenodd"/><path fill="#000" fill-rule="evenodd" d="M7 2.9a.8.8 0 1 1 0 1.5A.8.8 0 0 1 7 3ZM5.8 5.7c0-.4.3-.6.6-.6h.7c.3 0 .6.2.6.6v3.7h.5a.6.6 0 0 1 0 1.3H6a.6.6 0 0 1 0-1.3h.4v-3a.6.6 0 0 1-.6-.7Z" clip-rule="evenodd"/></svg>
117
+ You have successfully associated a {which_gpu} GPU to the B-LoRa Training Space πŸŽ‰</h2>
118
+ <p>
119
+ You can now train your model! You will be billed by the minute from when you activated the GPU until when it is turned off.
120
+ </p>
121
+ </div>
122
+ ''', elem_id="warning-ready")
123
+ else:
124
+ top_description = gr.HTML(f'''
125
+ <div class="gr-prose">
126
+ <h2><svg xmlns="http://www.w3.org/2000/svg" width="18px" height="18px" style="margin-right: 0px;display: inline-block;"fill="none"><path fill="#fff" d="M7 13.2a6.3 6.3 0 0 0 4.4-10.7A6.3 6.3 0 0 0 .6 6.9 6.3 6.3 0 0 0 7 13.2Z"/><path fill="#fff" fill-rule="evenodd" d="M7 0a6.9 6.9 0 0 1 4.8 11.8A6.9 6.9 0 0 1 0 7 6.9 6.9 0 0 1 7 0Zm0 0v.7V0ZM0 7h.6H0Zm7 6.8v-.6.6ZM13.7 7h-.6.6ZM9.1 1.7c-.7-.3-1.4-.4-2.2-.4a5.6 5.6 0 0 0-4 1.6 5.6 5.6 0 0 0-1.6 4 5.6 5.6 0 0 0 1.6 4 5.6 5.6 0 0 0 4 1.7 5.6 5.6 0 0 0 4-1.7 5.6 5.6 0 0 0 1.7-4 5.6 5.6 0 0 0-1.7-4c-.5-.5-1.1-.9-1.8-1.2Z" clip-rule="evenodd"/><path fill="#000" fill-rule="evenodd" d="M7 2.9a.8.8 0 1 1 0 1.5A.8.8 0 0 1 7 3ZM5.8 5.7c0-.4.3-.6.6-.6h.7c.3 0 .6.2.6.6v3.7h.5a.6.6 0 0 1 0 1.3H6a.6.6 0 0 1 0-1.3h.4v-3a.6.6 0 0 1-.6-.7Z" clip-rule="evenodd"/></svg>
127
+ You have successfully duplicated the B-LoRa Training Space πŸŽ‰</h2>
128
+ <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.
129
+ You will be billed by the minute from when you activate the GPU until when it is turned off.</p>
130
+ <p class="actions">
131
+ <a href="https://huggingface.co/spaces/{os.environ['SPACE_ID']}/settings">πŸ”₯ &nbsp; Set recommended GPU</a>
132
+ </p>
133
+ </div>
134
+ ''', elem_id="warning-setgpu")
135
+
136
  image = gr.Image(sources=["upload"], type="filepath")
137
  b_lora_name = gr.Textbox(label="b_lora_name", placeholder="b_lora_trained_folder")
138
  instance_prompt = gr.Textbox(label="instance prompt", placeholder="[v42]")