| import os
|
| import time
|
| import subprocess
|
| from huggingface_hub import HfApi
|
|
|
|
|
| api = HfApi()
|
|
|
|
|
| repo_id = "mikahniehaus/YelpPredictorDeploy"
|
|
|
|
|
| ALLOWED_EXTENSIONS = {".py", ".json", ".csv", ".pth", ".txt", ".md"}
|
|
|
|
|
| IGNORE_DIRS = {".git", "__pycache__", ".venv", "venv", "env", ".env", "hf_env", "node_modules"}
|
|
|
|
|
| def get_current_branch():
|
| try:
|
| branch = subprocess.check_output(["git", "rev-parse", "--abbrev-ref", "HEAD"], text=True).strip()
|
| return branch
|
| except subprocess.CalledProcessError:
|
| print("⚠️ Warning: Not in a Git repository.")
|
| return None
|
|
|
|
|
| def get_changed_files():
|
| try:
|
| changed_files = subprocess.check_output(["git", "diff", "--name-only", "origin/main"], text=True).splitlines()
|
| return set(changed_files)
|
| except subprocess.CalledProcessError:
|
| print("⚠️ Warning: Could not get changed files.")
|
| return set()
|
|
|
|
|
| def get_repo_files():
|
| try:
|
| return set(api.list_repo_files(repo_id=repo_id))
|
| except Exception as e:
|
| print(f"⚠️ Failed to list repo files: {e}")
|
| return set()
|
|
|
|
|
| def get_all_files(directory="."):
|
| file_list = []
|
| for root, dirs, files in os.walk(directory):
|
|
|
| if any(ignored in root.split(os.sep) for ignored in IGNORE_DIRS):
|
| continue
|
|
|
| for file in files:
|
| file_path = os.path.join(root, file)
|
| file_extension = os.path.splitext(file)[1]
|
|
|
| if file_extension in ALLOWED_EXTENSIONS:
|
| repo_path = os.path.relpath(file_path, directory).replace("\\", "/")
|
| file_list.append((file_path, repo_path))
|
|
|
| return file_list
|
|
|
|
|
| repo_files = get_repo_files()
|
|
|
|
|
| current_branch = get_current_branch()
|
| if current_branch:
|
| print(f"📂 Current Git branch: {current_branch}")
|
|
|
|
|
| changed_files = get_changed_files()
|
|
|
|
|
| files_to_upload = get_all_files()
|
|
|
|
|
| for file_path, repo_path in files_to_upload:
|
| if repo_path in repo_files and repo_path not in changed_files:
|
| print(f"⏭️ Skipping {file_path}, already uploaded and unchanged.")
|
| continue
|
|
|
| while True:
|
| try:
|
| print(f"🚀 Uploading {file_path} to {repo_path} in {repo_id} (branch: {current_branch})...")
|
| api.upload_file(
|
| path_or_fileobj=file_path,
|
| path_in_repo=repo_path,
|
| repo_id=repo_id,
|
| repo_type="model"
|
| )
|
| print(f"✅ Successfully uploaded {file_path}")
|
| time.sleep(5)
|
| break
|
|
|
| except Exception as e:
|
| print(f"❌ Error uploading {file_path}: {e}")
|
| print("⏳ Waiting 1 hour before retrying...")
|
| time.sleep(3600)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|