Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -30,61 +30,42 @@ def start_async_upload(local_dir, hf_repo, output_log):
|
|
| 30 |
threading.Thread(target=runner, daemon=True).start()
|
| 31 |
|
| 32 |
|
|
|
|
|
|
|
| 33 |
async def async_upload_model(local_dir, hf_repo, output_log, max_retries=3):
|
| 34 |
-
"""
|
|
|
|
|
|
|
| 35 |
try:
|
| 36 |
token = HfFolder.get_token()
|
| 37 |
-
api = HfApi()
|
| 38 |
-
api.create_repo(repo_id=hf_repo, exist_ok=True)
|
| 39 |
output_log.append(f"[INFO] βοΈ Preparing to upload to repo: {hf_repo}")
|
| 40 |
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
os.makedirs(os.path.dirname(dst_path), exist_ok=True)
|
| 64 |
-
shutil.copy2(src_path, dst_path)
|
| 65 |
-
output_log.append(f"[INFO] Copied {rel_path} to temp repo folder ({size / 1024:.2f} KB)")
|
| 66 |
-
|
| 67 |
-
# Push to hub with retries
|
| 68 |
-
attempt = 0
|
| 69 |
-
while attempt < max_retries:
|
| 70 |
-
try:
|
| 71 |
-
output_log.append(f"[INFO] π Attempt {attempt+1} to push to HF Hub...")
|
| 72 |
-
repo.push_to_hub(commit_message="Upload fine-tuned model")
|
| 73 |
-
output_log.append("[SUCCESS] β
Model successfully uploaded to HF Hub!")
|
| 74 |
-
break
|
| 75 |
-
except Exception as e:
|
| 76 |
-
attempt += 1
|
| 77 |
-
output_log.append(f"[ERROR] Upload attempt {attempt} failed: {e}")
|
| 78 |
-
if attempt >= max_retries:
|
| 79 |
-
output_log.append("[ERROR] β Max retries reached. Upload failed.")
|
| 80 |
-
else:
|
| 81 |
-
output_log.append("[INFO] Retrying upload in 5 seconds...")
|
| 82 |
-
await asyncio.sleep(5)
|
| 83 |
|
| 84 |
except Exception as e:
|
| 85 |
output_log.append(f"[ERROR] β Unexpected error during upload: {e}")
|
| 86 |
|
| 87 |
-
|
| 88 |
# ==== GPU check ====
|
| 89 |
def check_gpu_status():
|
| 90 |
return "π Zero GPU Ready - GPU will be allocated when training starts"
|
|
|
|
| 30 |
threading.Thread(target=runner, daemon=True).start()
|
| 31 |
|
| 32 |
|
| 33 |
+
from huggingface_hub import upload_folder, HfFolder
|
| 34 |
+
|
| 35 |
async def async_upload_model(local_dir, hf_repo, output_log, max_retries=3):
|
| 36 |
+
"""
|
| 37 |
+
Uploads a local model directory to HF Hub asynchronously using HTTP API.
|
| 38 |
+
"""
|
| 39 |
try:
|
| 40 |
token = HfFolder.get_token()
|
|
|
|
|
|
|
| 41 |
output_log.append(f"[INFO] βοΈ Preparing to upload to repo: {hf_repo}")
|
| 42 |
|
| 43 |
+
attempt = 0
|
| 44 |
+
while attempt < max_retries:
|
| 45 |
+
try:
|
| 46 |
+
output_log.append(f"[INFO] π Attempt {attempt+1} to upload folder via HTTP API...")
|
| 47 |
+
upload_folder(
|
| 48 |
+
folder_path=local_dir,
|
| 49 |
+
repo_id=hf_repo,
|
| 50 |
+
repo_type="model",
|
| 51 |
+
token=token,
|
| 52 |
+
ignore_patterns=["*.lock", "*.tmp"], # ignore temp files
|
| 53 |
+
create_pr=False,
|
| 54 |
+
)
|
| 55 |
+
output_log.append("[SUCCESS] β
Model successfully uploaded to HF Hub!")
|
| 56 |
+
break
|
| 57 |
+
except Exception as e:
|
| 58 |
+
attempt += 1
|
| 59 |
+
output_log.append(f"[ERROR] Upload attempt {attempt} failed: {e}")
|
| 60 |
+
if attempt >= max_retries:
|
| 61 |
+
output_log.append("[ERROR] β Max retries reached. Upload failed.")
|
| 62 |
+
else:
|
| 63 |
+
output_log.append("[INFO] Retrying upload in 5 seconds...")
|
| 64 |
+
await asyncio.sleep(5)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
|
| 66 |
except Exception as e:
|
| 67 |
output_log.append(f"[ERROR] β Unexpected error during upload: {e}")
|
| 68 |
|
|
|
|
| 69 |
# ==== GPU check ====
|
| 70 |
def check_gpu_status():
|
| 71 |
return "π Zero GPU Ready - GPU will be allocated when training starts"
|