File size: 4,008 Bytes
079e68a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import os
import time
import subprocess
from huggingface_hub import HfApi

# Initialize Hugging Face API
api = HfApi()

# Define your Hugging Face repo ID
repo_id = "mikahniehaus/YelpPredictorDeploy"  # Change this if needed

# Allowed file extensions
ALLOWED_EXTENSIONS = {".py", ".json", ".csv", ".pth", ".txt", ".md"}

# Directories to ignore (including `.env`, virtual environments, and `node_modules`)
IGNORE_DIRS = {".git", "__pycache__", ".venv", "venv", "env", ".env", "hf_env", "node_modules"}

# Get the current branch name
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

# Get a list of changed files in the current branch
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()

# Get a list of files already in the repository
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()  # Return an empty set on failure

# Recursively find all valid files to upload
def get_all_files(directory="."):
    file_list = []
    for root, dirs, files in os.walk(directory):
        # Skip any directory that matches IGNORE_DIRS
        if any(ignored in root.split(os.sep) for ignored in IGNORE_DIRS):
            continue  # Skip the entire directory

        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

# Get repo file list to skip already uploaded files
repo_files = get_repo_files()

# Get the current Git branch
current_branch = get_current_branch()
if current_branch:
    print(f"📂 Current Git branch: {current_branch}")

# Get the list of changed files
changed_files = get_changed_files()

# Get local files to upload (excluding ignored directories)
files_to_upload = get_all_files()

# Upload only files that are changed or not yet in the repo
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  # Skip if file is already uploaded and not changed

    while True:  # Infinite retries
        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)  # Wait to avoid rate limits
            break  # Exit loop if upload is successful

        except Exception as e:
            print(f"❌ Error uploading {file_path}: {e}")
            print("⏳ Waiting 1 hour before retrying...")
            time.sleep(3600)  # Wait 1 hour before retrying
            
            #remove exesive history
#git checkout --orphan latest_branch
#git add -A
#git commit -m "Initial commit with latest changes"
#git branch -D main  # Delete the old "main" branch (or replace with your branch name)
#git branch -m main  # Rename the new branch to "main"
#git push --force origin main