rahul7star commited on
Commit
817ccbc
Β·
verified Β·
1 Parent(s): 8c913fc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -46
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
- """Uploads a local model directory to HF Hub asynchronously with detailed logging."""
 
 
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
- # List files to upload
42
- all_files = []
43
- total_size = 0
44
- for root, dirs, files in os.walk(local_dir):
45
- for file in files:
46
- filepath = os.path.join(root, file)
47
- filesize = os.path.getsize(filepath)
48
- total_size += filesize
49
- rel_path = os.path.relpath(filepath, local_dir)
50
- all_files.append((rel_path, filepath, filesize))
51
-
52
- output_log.append(f"[INFO] πŸ“‚ Total {len(all_files)} files to upload, total size: {total_size / 1024:.2f} KB")
53
- for f, _, size in all_files:
54
- output_log.append(f" - {f} ({size / 1024:.2f} KB)")
55
-
56
- # Upload files individually with retries
57
- with tempfile.TemporaryDirectory() as tmpdir:
58
- repo = Repository(local_dir=tmpdir, clone_from=hf_repo, use_auth_token=token)
59
-
60
- # Copy files to temp repo folder
61
- for rel_path, src_path, size in all_files:
62
- dst_path = os.path.join(tmpdir, rel_path)
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"