#!/usr/bin/env python3 """ Final script to push entire project to Hugging Face using git commands Uses the correct HF username from the login """ import os import subprocess import shutil from pathlib import Path # Get the actual HF username from the API from huggingface_hub import whoami current_user = whoami() HF_USERNAME = current_user['name'] 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" print(f"Current HF user: {HF_USERNAME}") 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("\n" + "="*60) print("๐Ÿš€ Pushing to Hugging Face (Dataset)") print("="*60) 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, ignore_errors=True) 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") file_count = 0 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 scripts if item in ['.git', 'push_to_hf.py', 'push_to_hf_simple.py', 'push_to_hf_final.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')) file_count += len(list(Path(dst_path).rglob('*'))) else: print(f" ๐Ÿ“„ {item}") shutil.copy2(src_path, dst_path) file_count += 1 print(f"\nโœ… All files copied! ({file_count} items)") # Add all files print("\nโž• Adding files to git...") run_command("git add .", cwd=TEMP_HF_PATH) # Check what's being tracked print("\n๐Ÿ“Š Getting file statistics...") success, output, _ = run_command("git status --short | wc -l", 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") return # Add Hugging Face remote with authentication 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 several minutes 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:") print(f" {hf_url}") print("\nYou can access your dataset at:") print(f" https://huggingface.co/datasets/{HF_REPO_ID}") print("\nโœจ All code, data, and models are now on Hugging Face!") else: print("\nโŒ Push failed!") print(f"Error: {error}") # Cleanup print("\n๐Ÿงน Cleaning up temporary files...") try: shutil.rmtree(TEMP_HF_PATH, ignore_errors=True) print("โœ… Cleanup complete!") except Exception as e: print(f"โš ๏ธ Cleanup warning: {e}") if __name__ == "__main__": main()