DevWild commited on
Commit
16ec943
·
verified ·
1 Parent(s): f020510

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -2
app.py CHANGED
@@ -2,7 +2,7 @@ import os
2
  import subprocess
3
  from typing import Union
4
  from huggingface_hub import whoami
5
- from huggingface_hub import HfApi
6
  is_spaces = True if os.environ.get("SPACE_ID") else False
7
 
8
  if is_spaces:
@@ -253,6 +253,7 @@ def start_training(
253
  shutil.copy(script_location + "/script.py", dataset_folder)
254
  # copy requirements.autotrain to dataset_folder as requirements.txt
255
  shutil.copy(script_location + "/requirements.autotrain", dataset_folder + "/requirements.txt")
 
256
  # command to run autotrain spacerunner
257
  # --- Pre-create subspace and inject token ---
258
  hf_token = oauth_token.token
@@ -263,8 +264,15 @@ def start_training(
263
  # Try to create the space (if it doesn't exist)
264
  api.create_repo(repo_id=new_space_id, repo_type="space", space_sdk="docker", private=True)
265
  print(f"Created space {new_space_id}")
 
 
 
 
 
 
266
  except Exception as e:
267
- print(f"Space creation may have failed or already exists: {e}")
 
268
 
269
  try:
270
  # Inject HF_TOKEN into the new space
@@ -276,6 +284,7 @@ def start_training(
276
  cmd = f"autotrain spacerunner --project-name {slugged_lora_name} --script-path {dataset_folder}"
277
  cmd += f" --username {profile.username} --token {hf_token} --backend spaces-l4x1"
278
  outcome = subprocess.run(cmd.split())
 
279
  if outcome.returncode == 0:
280
  return f"""# Your training has started.
281
  ## - Training Status: <a href='https://huggingface.co/spaces/{profile.username}/autotrain-{slugged_lora_name}?logs=container'>{profile.username}/autotrain-{slugged_lora_name}</a> <small>(in the logs tab)</small>
 
2
  import subprocess
3
  from typing import Union
4
  from huggingface_hub import whoami
5
+ from huggingface_hub import HfApi, HfHubHTTPError
6
  is_spaces = True if os.environ.get("SPACE_ID") else False
7
 
8
  if is_spaces:
 
253
  shutil.copy(script_location + "/script.py", dataset_folder)
254
  # copy requirements.autotrain to dataset_folder as requirements.txt
255
  shutil.copy(script_location + "/requirements.autotrain", dataset_folder + "/requirements.txt")
256
+
257
  # command to run autotrain spacerunner
258
  # --- Pre-create subspace and inject token ---
259
  hf_token = oauth_token.token
 
264
  # Try to create the space (if it doesn't exist)
265
  api.create_repo(repo_id=new_space_id, repo_type="space", space_sdk="docker", private=True)
266
  print(f"Created space {new_space_id}")
267
+ except HfHubHTTPError as e:
268
+ if e.response is not None and e.response.status_code == 409:
269
+ print(f"Space {new_space_id} already exists — continuing.")
270
+ else:
271
+ print(f"Error creating space: {e}")
272
+ raise e
273
  except Exception as e:
274
+ print(f"General space creation error: {e}")
275
+ raise e
276
 
277
  try:
278
  # Inject HF_TOKEN into the new space
 
284
  cmd = f"autotrain spacerunner --project-name {slugged_lora_name} --script-path {dataset_folder}"
285
  cmd += f" --username {profile.username} --token {hf_token} --backend spaces-l4x1"
286
  outcome = subprocess.run(cmd.split())
287
+
288
  if outcome.returncode == 0:
289
  return f"""# Your training has started.
290
  ## - Training Status: <a href='https://huggingface.co/spaces/{profile.username}/autotrain-{slugged_lora_name}?logs=container'>{profile.username}/autotrain-{slugged_lora_name}</a> <small>(in the logs tab)</small>