mikahniehaus commited on
Commit
079e68a
·
verified ·
1 Parent(s): da5fc22

Upload upload_project.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. upload_project.py +107 -0
upload_project.py ADDED
@@ -0,0 +1,107 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import time
3
+ import subprocess
4
+ from huggingface_hub import HfApi
5
+
6
+ # Initialize Hugging Face API
7
+ api = HfApi()
8
+
9
+ # Define your Hugging Face repo ID
10
+ repo_id = "mikahniehaus/YelpPredictorDeploy" # Change this if needed
11
+
12
+ # Allowed file extensions
13
+ ALLOWED_EXTENSIONS = {".py", ".json", ".csv", ".pth", ".txt", ".md"}
14
+
15
+ # Directories to ignore (including `.env`, virtual environments, and `node_modules`)
16
+ IGNORE_DIRS = {".git", "__pycache__", ".venv", "venv", "env", ".env", "hf_env", "node_modules"}
17
+
18
+ # Get the current branch name
19
+ def get_current_branch():
20
+ try:
21
+ branch = subprocess.check_output(["git", "rev-parse", "--abbrev-ref", "HEAD"], text=True).strip()
22
+ return branch
23
+ except subprocess.CalledProcessError:
24
+ print("⚠️ Warning: Not in a Git repository.")
25
+ return None
26
+
27
+ # Get a list of changed files in the current branch
28
+ def get_changed_files():
29
+ try:
30
+ changed_files = subprocess.check_output(["git", "diff", "--name-only", "origin/main"], text=True).splitlines()
31
+ return set(changed_files)
32
+ except subprocess.CalledProcessError:
33
+ print("⚠️ Warning: Could not get changed files.")
34
+ return set()
35
+
36
+ # Get a list of files already in the repository
37
+ def get_repo_files():
38
+ try:
39
+ return set(api.list_repo_files(repo_id=repo_id))
40
+ except Exception as e:
41
+ print(f"⚠️ Failed to list repo files: {e}")
42
+ return set() # Return an empty set on failure
43
+
44
+ # Recursively find all valid files to upload
45
+ def get_all_files(directory="."):
46
+ file_list = []
47
+ for root, dirs, files in os.walk(directory):
48
+ # Skip any directory that matches IGNORE_DIRS
49
+ if any(ignored in root.split(os.sep) for ignored in IGNORE_DIRS):
50
+ continue # Skip the entire directory
51
+
52
+ for file in files:
53
+ file_path = os.path.join(root, file)
54
+ file_extension = os.path.splitext(file)[1]
55
+
56
+ if file_extension in ALLOWED_EXTENSIONS:
57
+ repo_path = os.path.relpath(file_path, directory).replace("\\", "/")
58
+ file_list.append((file_path, repo_path))
59
+
60
+ return file_list
61
+
62
+ # Get repo file list to skip already uploaded files
63
+ repo_files = get_repo_files()
64
+
65
+ # Get the current Git branch
66
+ current_branch = get_current_branch()
67
+ if current_branch:
68
+ print(f"📂 Current Git branch: {current_branch}")
69
+
70
+ # Get the list of changed files
71
+ changed_files = get_changed_files()
72
+
73
+ # Get local files to upload (excluding ignored directories)
74
+ files_to_upload = get_all_files()
75
+
76
+ # Upload only files that are changed or not yet in the repo
77
+ for file_path, repo_path in files_to_upload:
78
+ if repo_path in repo_files and repo_path not in changed_files:
79
+ print(f"⏭️ Skipping {file_path}, already uploaded and unchanged.")
80
+ continue # Skip if file is already uploaded and not changed
81
+
82
+ while True: # Infinite retries
83
+ try:
84
+ print(f"🚀 Uploading {file_path} to {repo_path} in {repo_id} (branch: {current_branch})...")
85
+ api.upload_file(
86
+ path_or_fileobj=file_path,
87
+ path_in_repo=repo_path,
88
+ repo_id=repo_id,
89
+ repo_type="model"
90
+ )
91
+ print(f"✅ Successfully uploaded {file_path}")
92
+ time.sleep(5) # Wait to avoid rate limits
93
+ break # Exit loop if upload is successful
94
+
95
+ except Exception as e:
96
+ print(f"❌ Error uploading {file_path}: {e}")
97
+ print("⏳ Waiting 1 hour before retrying...")
98
+ time.sleep(3600) # Wait 1 hour before retrying
99
+
100
+ #remove exesive history
101
+ #git checkout --orphan latest_branch
102
+ #git add -A
103
+ #git commit -m "Initial commit with latest changes"
104
+ #git branch -D main # Delete the old "main" branch (or replace with your branch name)
105
+ #git branch -m main # Rename the new branch to "main"
106
+ #git push --force origin main
107
+