#!/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()