BHARGAV REDDY commited on
Commit
3c9f3fe
·
verified ·
1 Parent(s): 9cc94a1

Upload upload_lora_to_hf.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. upload_lora_to_hf.py +63 -0
upload_lora_to_hf.py ADDED
@@ -0,0 +1,63 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import argparse
2
+ import os
3
+ from pathlib import Path
4
+
5
+ from huggingface_hub import HfApi
6
+
7
+
8
+ def parse_args():
9
+ parser = argparse.ArgumentParser(description="Upload a LoRA adapter folder to a Hugging Face model repo")
10
+ parser.add_argument("--repo-id", default="ASTERIZER/LUNA-100M")
11
+ parser.add_argument("--folder", default="Base/out/sft/rag_mcp_lora")
12
+ parser.add_argument("--path-in-repo", default="rag_mcp_lora")
13
+ parser.add_argument("--private", action="store_true")
14
+ return parser.parse_args()
15
+
16
+
17
+ def main():
18
+ args = parse_args()
19
+ token = os.environ.get("HF_TOKEN")
20
+ if not token:
21
+ raise RuntimeError("HF_TOKEN is required in the environment to upload the LoRA adapter")
22
+
23
+ folder = Path(args.folder)
24
+ if not folder.exists():
25
+ raise FileNotFoundError(f"LoRA output folder not found: {folder}")
26
+ if not folder.is_dir():
27
+ raise NotADirectoryError(f"Expected a folder, got: {folder}")
28
+
29
+ candidate_files = [
30
+ folder / "adapter_model.pt",
31
+ folder / "adapter_bundle.pt",
32
+ folder / "final" / "adapter_model.pt",
33
+ folder / "final" / "adapter_bundle.pt",
34
+ ]
35
+ existing = [path for path in candidate_files if path.exists()]
36
+ missing = [str(path) for path in candidate_files if not path.exists()]
37
+ if not existing:
38
+ raise FileNotFoundError(f"Missing expected adapter files: {', '.join(missing)}")
39
+
40
+ api = HfApi(token=token)
41
+ api.create_repo(
42
+ repo_id=args.repo_id,
43
+ repo_type="model",
44
+ private=args.private,
45
+ exist_ok=True,
46
+ )
47
+ api.upload_folder(
48
+ repo_id=args.repo_id,
49
+ repo_type="model",
50
+ folder_path=str(folder),
51
+ path_in_repo=args.path_in_repo,
52
+ )
53
+ print(
54
+ "uploaded_lora "
55
+ f"repo_id={args.repo_id} "
56
+ f"folder={folder} "
57
+ f"path_in_repo={args.path_in_repo} "
58
+ f"url=https://huggingface.co/{args.repo_id}/tree/main/{args.path_in_repo}"
59
+ )
60
+
61
+
62
+ if __name__ == "__main__":
63
+ main()