DevWild commited on
Commit
f020510
·
verified ·
1 Parent(s): 83128e5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -8
app.py CHANGED
@@ -2,15 +2,9 @@ import os
2
  import subprocess
3
  from typing import Union
4
  from huggingface_hub import whoami
 
5
  is_spaces = True if os.environ.get("SPACE_ID") else False
6
 
7
- #from huggingface_hub import login
8
- #hf_token = os.getenv("HF_TOKEN")
9
- #if hf_token:
10
- # login(token=hf_token)
11
- #else:
12
- # raise ValueError("HF_TOKEN environment variable not found!")
13
-
14
  if is_spaces:
15
  subprocess.run('pip install flash-attn --no-build-isolation', env={'FLASH_ATTENTION_SKIP_CUDA_BUILD': "TRUE"}, shell=True)
16
  import spaces
@@ -260,8 +254,27 @@ def start_training(
260
  # copy requirements.autotrain to dataset_folder as requirements.txt
261
  shutil.copy(script_location + "/requirements.autotrain", dataset_folder + "/requirements.txt")
262
  # command to run autotrain spacerunner
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
263
  cmd = f"autotrain spacerunner --project-name {slugged_lora_name} --script-path {dataset_folder}"
264
- cmd += f" --username {profile.username} --token {oauth_token.token} --backend spaces-l4x1"
265
  outcome = subprocess.run(cmd.split())
266
  if outcome.returncode == 0:
267
  return f"""# Your training has started.
 
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:
9
  subprocess.run('pip install flash-attn --no-build-isolation', env={'FLASH_ATTENTION_SKIP_CUDA_BUILD': "TRUE"}, shell=True)
10
  import spaces
 
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
259
+ api = HfApi(token=hf_token)
260
+ new_space_id = f"{profile.username}/autotrain-{slugged_lora_name}"
261
+
262
+ try:
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
271
+ api.add_space_secret(repo_id=new_space_id, key="HF_TOKEN", value=hf_token)
272
+ print(f"Added HF_TOKEN secret to {new_space_id}")
273
+ except Exception as e:
274
+ print(f"Failed to add HF_TOKEN secret to new space: {e}")
275
+
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.