openfree commited on
Commit
217860d
·
verified ·
1 Parent(s): 071e81f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -6
app.py CHANGED
@@ -38,6 +38,27 @@ if not is_spaces:
38
  MAX_IMAGES = 150
39
 
40
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
41
  def load_captioning(uploaded_files, concept_sentence):
42
  uploaded_images = [file for file in uploaded_files if not file.endswith('.txt')]
43
  txt_files = [file for file in uploaded_files if file.endswith('.txt')]
@@ -157,7 +178,6 @@ def recursive_update(d, u):
157
  d[k] = v
158
  return d
159
 
160
- # start_training 함수 수정 부분
161
  def start_training(
162
  lora_name,
163
  concept_sentence,
@@ -171,18 +191,19 @@ def start_training(
171
  sample_3,
172
  use_more_advanced_options,
173
  more_advanced_options,
174
- profile: Union[gr.OAuthProfile, None],
175
- oauth_token: Union[gr.OAuthToken, None],
176
  ):
177
  if not lora_name:
178
  raise gr.Error("You forgot to insert your LoRA name! This name has to be unique.")
179
 
 
 
 
 
 
180
  print("Started training")
181
  slugged_lora_name = slugify(lora_name)
182
 
183
- # Load the default config
184
- with open("train_lora_flux_24gb.yaml", "r") as f:
185
- config = yaml.safe_load(f)
186
 
187
  # Update the config with user inputs
188
  config["config"]["name"] = slugged_lora_name
@@ -202,6 +223,9 @@ def start_training(
202
 
203
  config["config"]["process"][0]["save"]["hf_repo_id"] = f"{username}/{slugged_lora_name}"
204
  config["config"]["process"][0]["save"]["hf_private"] = True
 
 
 
205
 
206
  if concept_sentence:
207
  config["config"]["process"][0]["trigger_word"] = concept_sentence
 
38
  MAX_IMAGES = 150
39
 
40
 
41
+ import subprocess
42
+ from typing import Union
43
+ from huggingface_hub import whoami, HfApi
44
+
45
+ # Hugging Face 토큰 설정
46
+ HF_TOKEN = os.getenv("HF_TOKEN")
47
+ if not HF_TOKEN:
48
+ raise ValueError("HF_TOKEN environment variable is not set")
49
+
50
+ is_spaces = True if os.environ.get("SPACE_ID") else False
51
+
52
+ if is_spaces:
53
+ subprocess.run('pip install flash-attn --no-build-isolation', env={'FLASH_ATTENTION_SKIP_CUDA_BUILD': "TRUE"}, shell=True)
54
+ import spaces
55
+
56
+ os.environ["HF_HUB_ENABLE_HF_TRANSFER"] = "1"
57
+ os.environ["HUGGING_FACE_HUB_TOKEN"] = HF_TOKEN
58
+
59
+ # HF API 초기화
60
+ api = HfApi(token=HF_TOKEN)
61
+
62
  def load_captioning(uploaded_files, concept_sentence):
63
  uploaded_images = [file for file in uploaded_files if not file.endswith('.txt')]
64
  txt_files = [file for file in uploaded_files if file.endswith('.txt')]
 
178
  d[k] = v
179
  return d
180
 
 
181
  def start_training(
182
  lora_name,
183
  concept_sentence,
 
191
  sample_3,
192
  use_more_advanced_options,
193
  more_advanced_options,
 
 
194
  ):
195
  if not lora_name:
196
  raise gr.Error("You forgot to insert your LoRA name! This name has to be unique.")
197
 
198
+ try:
199
+ username = whoami()["name"]
200
+ except:
201
+ raise gr.Error("Failed to get username. Please check your HF_TOKEN.")
202
+
203
  print("Started training")
204
  slugged_lora_name = slugify(lora_name)
205
 
206
+
 
 
207
 
208
  # Update the config with user inputs
209
  config["config"]["name"] = slugged_lora_name
 
223
 
224
  config["config"]["process"][0]["save"]["hf_repo_id"] = f"{username}/{slugged_lora_name}"
225
  config["config"]["process"][0]["save"]["hf_private"] = True
226
+ config["config"]["process"][0]["save"]["hf_token"] = HF_TOKEN
227
+
228
+
229
 
230
  if concept_sentence:
231
  config["config"]["process"][0]["trigger_word"] = concept_sentence