Brain-Tumor-AI / push_to_hf.py
StillWorking's picture
Upload folder using huggingface_hub (part 13)
1f2e262 verified
Raw History Blame Contribute Delete
3.36 kB
#!/usr/bin/env python3
"""
Script to push the entire Brain Tumor AI project to Hugging Face
Includes all source code, datasets, and models
"""
import os
import subprocess
from pathlib import Path
from huggingface_hub import Repository, login
# Configuration
HF_USERNAME = "Ujjwalathub" # Update this with your HF username
REPO_NAME = "Brain-Tumor-AI"
LOCAL_PATH = r"e:\Brain"
HF_REPO_ID = f"{HF_USERNAME}/{REPO_NAME}"
def main():
print("🚀 Starting push to Hugging Face...")
print(f"📦 Repository: {HF_REPO_ID}")
# Initialize or clone the HF repo
hf_repo_url = f"https://huggingface.co/datasets/{HF_REPO_ID}" if True else f"https://huggingface.co/{HF_REPO_ID}"
try:
print("\n📝 Creating/accessing Hugging Face repository...")
# Create a repo on HF using the API
from huggingface_hub import create_repo
try:
create_repo(
repo_id=REPO_NAME,
repo_type="dataset",
exist_ok=True,
private=False
)
print(f"✅ Repository created/accessed: {REPO_NAME}")
except Exception as e:
print(f"⚠️ Repository creation: {e}")
# Clone the HF repo
temp_dir = rf"{LOCAL_PATH}_hf_temp"
if os.path.exists(temp_dir):
import shutil
shutil.rmtree(temp_dir)
print(f"\n📥 Cloning HF repository to {temp_dir}...")
repo = Repository(
local_dir=temp_dir,
clone_from=HF_REPO_ID,
repo_type="dataset",
use_auth_token=True
)
# Copy all files from local Brain directory to temp HF directory
print(f"\n📋 Copying files from {LOCAL_PATH} to HF repository...")
import shutil
for item in os.listdir(LOCAL_PATH):
src_path = os.path.join(LOCAL_PATH, item)
dst_path = os.path.join(temp_dir, item)
# Skip .git and other HF-specific folders
if item in ['.git', '.gitignore', '.gitattributes', '.hf_temp', 'push_to_hf.py']:
continue
if os.path.isdir(src_path):
if os.path.exists(dst_path):
shutil.rmtree(dst_path)
print(f" 📁 Copying directory: {item}")
shutil.copytree(src_path, dst_path)
else:
print(f" 📄 Copying file: {item}")
shutil.copy2(src_path, dst_path)
# Add all files to the repo
print("\n➕ Adding files to repository...")
repo.git_add()
# Commit and push
print("\n💾 Committing changes...")
repo.git_commit("Initial commit: Brain Tumor MRI Classifier with full dataset and models")
print("\n⬆️ Pushing to Hugging Face...")
repo.git_push()
print("\n✅ Successfully pushed to Hugging Face!")
print(f"🔗 Repository URL: https://huggingface.co/datasets/{HF_REPO_ID}")
# Cleanup
print("\n🧹 Cleaning up temporary files...")
shutil.rmtree(temp_dir)
except Exception as e:
print(f"\n❌ Error: {e}")
import traceback
traceback.print_exc()
if __name__ == "__main__":
main()