Brain-Tumor-AI / push_to_hf_simple.py
StillWorking's picture
Upload folder using huggingface_hub (part 13)
1f2e262 verified
Raw History Blame Contribute Delete
4.73 kB
#!/usr/bin/env python3
"""
Simple script to push entire project to Hugging Face using git commands
"""
import os
import subprocess
import shutil
from pathlib import Path
HF_USERNAME = "Ujjwalathub"
REPO_NAME = "Brain-Tumor-AI"
HF_REPO_ID = f"{HF_USERNAME}/{REPO_NAME}"
LOCAL_PATH = r"e:\Brain"
TEMP_HF_PATH = r"e:\Brain_hf_push"
def run_command(cmd, cwd=None):
"""Run shell command and return output"""
print(f" $ {cmd}")
result = subprocess.run(cmd, shell=True, cwd=cwd, capture_output=True, text=True)
if result.stdout:
print(result.stdout)
if result.stderr and result.returncode != 0:
print(f" ❌ Error: {result.stderr}")
return result.returncode == 0, result.stdout, result.stderr
def main():
print("🚀 Starting push to Hugging Face (Dataset)...\n")
print(f"📦 Repository: {HF_REPO_ID}")
print(f"📁 Local Path: {LOCAL_PATH}\n")
# Create temp directory
if os.path.exists(TEMP_HF_PATH):
print(f"🧹 Removing existing temp directory...")
shutil.rmtree(TEMP_HF_PATH)
print(f"📁 Creating temp directory: {TEMP_HF_PATH}")
os.makedirs(TEMP_HF_PATH)
# Initialize git repo in temp directory
print("\n🔧 Initializing git repository...")
run_command("git init", cwd=TEMP_HF_PATH)
run_command("git config user.email 'ujjwalathub@gmail.com'", cwd=TEMP_HF_PATH)
run_command("git config user.name 'Ujjwal'", cwd=TEMP_HF_PATH)
# Configure Git LFS
print("\n📦 Configuring Git LFS for large files...")
run_command("git lfs install", cwd=TEMP_HF_PATH)
# Create .gitattributes for LFS
gitattributes_path = os.path.join(TEMP_HF_PATH, ".gitattributes")
with open(gitattributes_path, 'w') as f:
f.write("""# Binary files for LFS
*.pth filter=lfs diff=lfs merge=lfs -text
*.npy filter=lfs diff=lfs merge=lfs -text
*.onnx filter=lfs diff=lfs merge=lfs -text
*.csv filter=lfs diff=lfs merge=lfs -text
*.pkl filter=lfs diff=lfs merge=lfs -text
*.pickle filter=lfs diff=lfs merge=lfs -text
*.tar filter=lfs diff=lfs merge=lfs -text
*.gz filter=lfs diff=lfs merge=lfs -text
*.zip filter=lfs diff=lfs merge=lfs -text
""")
print(f"✅ Created .gitattributes with LFS config")
# Copy all files from Brain directory
print(f"\n📋 Copying files from {LOCAL_PATH}...\n")
for item in os.listdir(LOCAL_PATH):
src_path = os.path.join(LOCAL_PATH, item)
dst_path = os.path.join(TEMP_HF_PATH, item)
# Skip git and push script
if item in ['.git', 'push_to_hf.py', 'push_to_hf_simple.py', 'Brain_hf_push']:
continue
if os.path.isdir(src_path):
print(f" 📁 {item}/")
shutil.copytree(src_path, dst_path, ignore=shutil.ignore_patterns('.git'))
else:
print(f" 📄 {item}")
shutil.copy2(src_path, dst_path)
print("\n✅ All files copied!")
# Add all files
print("\n➕ Adding files to git...")
run_command("git add .", cwd=TEMP_HF_PATH)
# Check what's being tracked
print("\n📊 Files to be committed:")
success, output, _ = run_command("git status --short", cwd=TEMP_HF_PATH)
# Commit
print("\n💾 Creating commit...")
success, output, _ = run_command(
'git commit -m "Initial commit: Brain Tumor MRI Classifier with complete dataset and models"',
cwd=TEMP_HF_PATH
)
if not success:
print("⚠️ Commit failed (might be empty or already exists)")
return
# Add Hugging Face remote
print("\n🔗 Adding Hugging Face remote...")
hf_url = f"https://huggingface.co/datasets/{HF_REPO_ID}"
run_command(f'git remote add origin "{hf_url}"', cwd=TEMP_HF_PATH)
# Rename branch to main
print("\n🌿 Renaming branch to main...")
run_command("git branch -M main", cwd=TEMP_HF_PATH)
# Push to Hugging Face
print("\n⬆️ Pushing to Hugging Face...")
print("⏳ This may take a while depending on file sizes...\n")
success, output, error = run_command(
"git push -u origin main --force",
cwd=TEMP_HF_PATH
)
if success:
print("\n" + "="*60)
print("✅ SUCCESS! Project pushed to Hugging Face!")
print("="*60)
print(f"🔗 Repository URL: {hf_url}")
print("\nYou can now access your dataset at:")
print(f" https://huggingface.co/datasets/{HF_REPO_ID}")
else:
print("\n❌ Push failed!")
print(f"Error: {error}")
# Cleanup
print("\n🧹 Cleaning up temporary files...")
shutil.rmtree(TEMP_HF_PATH)
print("✅ Cleanup complete!")
if __name__ == "__main__":
main()