Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -20,27 +20,82 @@ import os, tempfile, shutil, asyncio, threading, time
|
|
| 20 |
from datetime import datetime
|
| 21 |
|
| 22 |
# ==== Async upload wrapper ====
|
|
|
|
|
|
|
|
|
|
| 23 |
def start_async_upload(local_dir, hf_repo, output_log):
|
|
|
|
|
|
|
|
|
|
| 24 |
def runner():
|
|
|
|
| 25 |
asyncio.run(async_upload_model(local_dir, hf_repo, output_log))
|
|
|
|
|
|
|
| 26 |
threading.Thread(target=runner, daemon=True).start()
|
| 27 |
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
try:
|
| 30 |
token = HfFolder.get_token()
|
| 31 |
api = HfApi()
|
| 32 |
api.create_repo(repo_id=hf_repo, exist_ok=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
-
output_log.append(f"
|
|
|
|
|
|
|
| 35 |
|
|
|
|
| 36 |
with tempfile.TemporaryDirectory() as tmpdir:
|
| 37 |
repo = Repository(local_dir=tmpdir, clone_from=hf_repo, use_auth_token=token)
|
| 38 |
-
shutil.copytree(local_dir, tmpdir, dirs_exist_ok=True)
|
| 39 |
-
repo.push_to_hub(commit_message="Upload fine-tuned model")
|
| 40 |
|
| 41 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
except Exception as e:
|
| 43 |
-
output_log.append(f"
|
| 44 |
|
| 45 |
# ==== GPU check ====
|
| 46 |
def check_gpu_status():
|
|
|
|
| 20 |
from datetime import datetime
|
| 21 |
|
| 22 |
# ==== Async upload wrapper ====
|
| 23 |
+
import threading
|
| 24 |
+
import asyncio
|
| 25 |
+
|
| 26 |
def start_async_upload(local_dir, hf_repo, output_log):
|
| 27 |
+
"""
|
| 28 |
+
Starts async model upload in a background thread.
|
| 29 |
+
"""
|
| 30 |
def runner():
|
| 31 |
+
output_log.append(f"[INFO] π Async upload thread started for repo: {hf_repo}")
|
| 32 |
asyncio.run(async_upload_model(local_dir, hf_repo, output_log))
|
| 33 |
+
output_log.append(f"[INFO] π Async upload thread finished for repo: {hf_repo}")
|
| 34 |
+
|
| 35 |
threading.Thread(target=runner, daemon=True).start()
|
| 36 |
|
| 37 |
+
|
| 38 |
+
import os
|
| 39 |
+
import shutil
|
| 40 |
+
import tempfile
|
| 41 |
+
import asyncio
|
| 42 |
+
from huggingface_hub import HfApi, HfFolder, Repository
|
| 43 |
+
|
| 44 |
+
async def async_upload_model(local_dir, hf_repo, output_log, max_retries=3):
|
| 45 |
+
"""
|
| 46 |
+
Uploads a local model directory to HF Hub asynchronously with detailed logging.
|
| 47 |
+
"""
|
| 48 |
try:
|
| 49 |
token = HfFolder.get_token()
|
| 50 |
api = HfApi()
|
| 51 |
api.create_repo(repo_id=hf_repo, exist_ok=True)
|
| 52 |
+
output_log.append(f"[INFO] βοΈ Preparing to upload to repo: {hf_repo}")
|
| 53 |
+
|
| 54 |
+
# List files to upload
|
| 55 |
+
all_files = []
|
| 56 |
+
total_size = 0
|
| 57 |
+
for root, dirs, files in os.walk(local_dir):
|
| 58 |
+
for file in files:
|
| 59 |
+
filepath = os.path.join(root, file)
|
| 60 |
+
filesize = os.path.getsize(filepath)
|
| 61 |
+
total_size += filesize
|
| 62 |
+
rel_path = os.path.relpath(filepath, local_dir)
|
| 63 |
+
all_files.append((rel_path, filepath, filesize))
|
| 64 |
|
| 65 |
+
output_log.append(f"[INFO] π Total {len(all_files)} files to upload, total size: {total_size / 1024:.2f} KB")
|
| 66 |
+
for f, _, size in all_files:
|
| 67 |
+
output_log.append(f" - {f} ({size / 1024:.2f} KB)")
|
| 68 |
|
| 69 |
+
# Upload files individually with retries
|
| 70 |
with tempfile.TemporaryDirectory() as tmpdir:
|
| 71 |
repo = Repository(local_dir=tmpdir, clone_from=hf_repo, use_auth_token=token)
|
|
|
|
|
|
|
| 72 |
|
| 73 |
+
# Copy files to temp repo folder
|
| 74 |
+
for rel_path, src_path, size in all_files:
|
| 75 |
+
dst_path = os.path.join(tmpdir, rel_path)
|
| 76 |
+
os.makedirs(os.path.dirname(dst_path), exist_ok=True)
|
| 77 |
+
shutil.copy2(src_path, dst_path)
|
| 78 |
+
output_log.append(f"[INFO] Copied {rel_path} to temp repo folder ({size / 1024:.2f} KB)")
|
| 79 |
+
|
| 80 |
+
# Push to hub with retries
|
| 81 |
+
attempt = 0
|
| 82 |
+
while attempt < max_retries:
|
| 83 |
+
try:
|
| 84 |
+
output_log.append(f"[INFO] π Attempt {attempt+1} to push to HF Hub...")
|
| 85 |
+
repo.push_to_hub(commit_message="Upload fine-tuned model")
|
| 86 |
+
output_log.append("[SUCCESS] β
Model successfully uploaded to HF Hub!")
|
| 87 |
+
break
|
| 88 |
+
except Exception as e:
|
| 89 |
+
attempt += 1
|
| 90 |
+
output_log.append(f"[ERROR] Upload attempt {attempt} failed: {e}")
|
| 91 |
+
if attempt >= max_retries:
|
| 92 |
+
output_log.append("[ERROR] β Max retries reached. Upload failed.")
|
| 93 |
+
else:
|
| 94 |
+
output_log.append("[INFO] Retrying upload in 5 seconds...")
|
| 95 |
+
await asyncio.sleep(5)
|
| 96 |
+
|
| 97 |
except Exception as e:
|
| 98 |
+
output_log.append(f"[ERROR] β Unexpected error during upload: {e}")
|
| 99 |
|
| 100 |
# ==== GPU check ====
|
| 101 |
def check_gpu_status():
|