| |
| """HuggingFace_Backup_2024_Colab.ipynb |
| |
| Automatically generated by Colab. |
| |
| Original file is located at |
| https://colab.research.google.com/github/duskfallcrew/HuggingFace_Backup/blob/Huggingface_Backup/HuggingFace_Backup_2024_Colab.ipynb |
| |
| # 🌈 HuggingFace Backup - Colab Edition |
| |
| Hey there, fellow adventurers in AI! Welcome to our delightfully unique corner of the universe! ✨ |
| |
| ## 💫 What's This All About? |
| This is our cozy mashup of Nocrypt's notebook and everyday AI tools, but with our own special twist! Think of it as the Colab-flavored version of our Jupyter notebook - because sometimes you need your AI magic on the go! |
| |
| ### 🎯 Quick Features: |
| - Fast uploads to HuggingFace (because waiting is overrated!) |
| - Focus on checkpoints (keeping it simple and sweet) |
| - Built for Google Colab (our cloud-powered playground) |
| - All the same magic as our Jupyter version, just with cloud sprinkles! ✨ |
| |
| ## 🔍 Lost & Found |
| - Repo doing a disappearing act? [Find us on GitHub!](https://github.com/duskfallcrew/HuggingFace_Backup) |
| - CivitAI friends: Updates drop faster than a cat chasing a laser! |
| |
| ## ☕ Support the Magic |
| [](https://ko-fi.com/Z8Z8L4EO) |
| |
| ## 🎮 Join Our Adventure! |
| We're a system of 300+ alters, rocking life with DID, ADHD, Autism, and CPTSD. We believe AI can be a game-changer for mental health and creativity. Come explore with us! |
| |
| ### 🌟 Find Us Here: |
| - 🏠 [End Media](https://www.end-media.org/) |
| - 🎮 [Discord](https://discord.gg/5t2kYxt7An) |
| - 🤗 [HuggingFace](https://huggingface.co/EarthnDusk) |
| - 🎵 [YouTube](https://www.youtube.com/channel/UCk7MGP7nrJz5awBSP75xmVw) |
| - 🎪 [Reddit](https://www.reddit.com/r/earthndusk/) |
| - 🎨 [DeviantArt](https://www.deviantart.com/diffusionai) |
| |
| ### 🏴☠️ Special Thanks To: |
| - [Pirate Diffusion](https://www.piratediffusion.com/) |
| - [Yodayo](https://yodayo.com/) |
| |
| ## 💝 Credits & Cool People |
| Huge thanks to our code ancestors: |
| - EVERYDREAM2 TRAINER |
| - LINAQRUF |
| - NOCRYPT [](https://lookup.guru/442099748669751297) |
| |
| Want more AI adventures? Check out the original SD Colab: |
| <a target="_blank" href="https://colab.research.google.com/drive/1wEa-tS10h4LlDykd87TF5zzpXIIQoCmq"> |
| <img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open In Colab"/> |
| </a> |
| |
| ## 🛠️ Need Help? |
| - Found a bug? Don't panic! Drop by our GitHub or Discord |
| - Questions? We're here for you! |
| - Want to help? Pull requests are like hugs - always welcome! |
| |
| ## 🎯 Recent Updates: |
| 1. 🌟 Enhanced Colab compatibility |
| 2. 🔧 Streamlined file handling |
| 3. 🎨 Made everything prettier (because we can!) |
| 4. 📝 Clearer instructions (no more confusion!) |
| 5. ✨ Added extra sprinkles of magic |
| |
| Remember: We're not pro coders, we're creative problem solvers! Sometimes the best solutions come with a side of chaos and a sprinkle of adventure! 🌈✨ |
| |
| Let's make some AI magic happen! Ready to dive in? 🚀 |
| |
| # Google Drive Mount |
| """ |
|
|
| |
| |
|
|
| |
| |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| from google.colab import drive |
| from google.colab import output |
| import os |
|
|
| def mount_drive(): |
| if not os.path.exists('/content/drive'): |
| print("🔮 Summoning your Google Drive...") |
| drive.mount('/content/drive') |
| print("\n✨ Drive mounted successfully! You're ready to rock!") |
| else: |
| print("🎉 Your Drive is already connected and ready to go!") |
|
|
| |
| output.enable_custom_widget_manager() |
|
|
| |
| mount_drive() |
|
|
| print(""" |
| 📚 Quick Navigation Guide: |
| - Your Drive lives at: /content/drive |
| - Main workspace: /content/drive/MyDrive |
| - Look for the folder icon in the left sidebar |
| |
| Need to upload files? |
| 1. Find the folder icon on the left |
| 2. Navigate to your drive |
| 3. Drag and drop your files |
| 4. Magic happens! ✨ |
| |
| Having troubles? No worries! You can: |
| 1. Try reconnecting (run this cell again) |
| 2. Use terminal commands if you're feeling brave |
| 3. Direct upload to Colab (look for the folder icon!) |
| |
| Remember: Your creative journey is valid, no matter which path you choose! 🌈 |
| """) |
|
|
| """# Installation & Requirements |
| ----------------- |
| |
| |
| """ |
|
|
| |
| |
|
|
| |
| |
| |
| |
|
|
| print("🔍 Starting the setup process...") |
|
|
| def check_and_install(package, install_name=None, verbose=False): |
| """Checks if a package is installed and installs it if needed.""" |
| import importlib |
| if install_name is None: |
| install_name = package |
| try: |
| importlib.import_module(package) |
| print(f"✅ {package} is already installed.") |
| except ImportError: |
| print(f"📦 Installing {package}...") |
| if verbose: |
| !pip install {install_name} |
| else: |
| !pip install -q {install_name} |
| print(f"✨ {package} installed successfully.") |
| except Exception as e: |
| print(f"❌ An error occurred during install of {package}: {e}") |
|
|
| |
| required_packages = { |
| 'huggingface_hub': '--force-reinstall huggingface_hub', |
| 'ipywidgets': 'ipywidgets', |
| 'glob': 'glob', |
| 'tqdm': 'tqdm' |
| } |
|
|
| |
| for package, install_cmd in required_packages.items(): |
| check_and_install(package, install_cmd, verbose=True) |
|
|
| |
| from IPython.display import clear_output |
| clear_output() |
|
|
| print(""" |
| 🎉 All set! Here's a summary of what was installed: |
| |
| 🤗 Hugging Face Hub - Your portal to model magic |
| 🎨 IPython Widgets - To make things pretty |
| 📁 File Handling - Tools to keep things organized |
| 📊 Progress Bars - For better tracking of uploads |
| |
| Having issues? Here's a checklist: |
| 1. Try running this cell again |
| 2. Ensure your Colab runtime is not encountering errors |
| 3. If still wonky, drop by our Discord for help! |
| |
| Let's make some AI magic! 🚀 |
| """) |
|
|
| |
| |
|
|
| |
| |
| |
| |
| |
|
|
| print("✨ Checking Hugging Face connection...") |
|
|
| |
| try: |
| hub_ok |
| except NameError: |
| print("📦 Installing huggingface_hub (if needed)...") |
| !pip install --force-reinstall -q huggingface_hub |
| hub_ok = True |
|
|
| from IPython.display import clear_output |
| from huggingface_hub import login |
| clear_output() |
|
|
| |
| write_token = "" |
|
|
| try: |
| if write_token: |
| print("🔑 Logging you in with your Hugging Face token...") |
| login(write_token, add_to_git_credential=True) |
| print("🎉 Successfully logged in to Hugging Face! Your powers are activated!") |
| print("💫 You're ready to upload models and files.") |
| else: |
| print("⚠️ No token was provided. Please paste your Hugging Face token above.") |
| except Exception as e: |
| print("❌ Login failed. Please check the following:") |
| print(f" 1. Ensure your token is correct and has 'Write' access.") |
| print(f" 2. The error is: {e}") |
| print("🤔 If you're still having problems, visit our Discord for help!") |
| raise |
|
|
| |
| |
|
|
| from huggingface_hub.utils import validate_repo_id, HfHubHTTPError |
| from huggingface_hub import HfApi |
| import os |
|
|
| |
| repo_name = "" |
| make_private = False |
| clone_repo = True |
|
|
| print("🛠️ Setting up your Hugging Face repository...") |
|
|
| try: |
| |
| api = HfApi() |
| user = api.whoami() |
| model_repo = f"{user['name']}/{repo_name.strip()}" |
|
|
| |
| print(f"🔍 Validating repository name: '{model_repo}'") |
| validate_repo_id(model_repo) |
|
|
| |
| print("📦 Creating repository if it doesn't exist...") |
| api.create_repo( |
| repo_id=model_repo, |
| private=make_private, |
| exist_ok=True |
| ) |
| print(f"🎉 Repository '{model_repo}' is ready for action!") |
|
|
| |
| if clone_repo: |
| print("\n📦 Cloning your repository (with LFS)...") |
| clone_path = f"/content/{repo_name.strip()}" |
|
|
| if os.path.exists(clone_path): |
| print(f"⚠️ Repository already exists at '{clone_path}'. Skipping clone step.") |
| else: |
| !git lfs install --skip-smudge |
| !export GIT_LFS_SKIP_SMUDGE=1 |
| !git clone https://huggingface.co/{model_repo} {clone_path} |
| print("✨ Repository cloned successfully!") |
|
|
| except Exception as e: |
| print(f"❌ An error occurred during repository setup: {e}") |
| raise |
|
|
| print("✅ Repository setup complete!") |
|
|
| |
| |
|
|
| |
| |
| |
| |
| |
|
|
| |
| |
| |
| |
|
|
| import os |
| import glob |
| import time |
| from pathlib import Path |
| from huggingface_hub import HfApi |
| from IPython.display import display, HTML, clear_output, Image |
| from tqdm.notebook import tqdm |
| from ipywidgets import * |
|
|
| |
| hfuser = "" |
| hfrepo = "" |
|
|
| |
| file_type = "mp3" |
| |
|
|
| |
| file_location = "" |
|
|
| |
| commit_message = "Upload with Earth & Dusk Huggingface 🤗 Backup" |
| create_pr = False |
| clear_after = True |
|
|
|
|
| def format_size(size): |
| """Formats a file size into a human-readable string.""" |
| for unit in ['B', 'KB', 'MB', 'GB']: |
| if size < 1024: |
| return f"{size:.2f} {unit}" |
| size /= 1024 |
| return f"{size:.2f} TB" |
|
|
| def find_files(file_location, file_type): |
| """Finds files matching the selected file type in the given directory.""" |
| try: |
| files = sorted( |
| glob.glob(os.path.join(file_location, f"*.{file_type}")), |
| key=os.path.getmtime if sort_by == 'date' else str |
| ) |
| return files |
| except Exception as e: |
| print(f"❌ Error finding files: {e}") |
| return [] |
|
|
| |
| all_ckpts = find_files(file_location, file_type) |
| ckpt_picker = SelectMultiple(options=all_ckpts, layout=Layout(width="600px"), description="Select File(s)") |
| upload_btn = Button(description='Upload') |
| out = Output() |
|
|
| def upload_ckpts(_): |
| repo_id = f"{hfuser}/{hfrepo}" |
| with out: |
| if not ckpt_picker.value: |
| print("Nothing selected for upload, make sure to click one of the files in the list, or verify there are files in the specified directory.") |
| return |
| for ckpt in ckpt_picker.value: |
| print(f"Uploading to HF: huggingface.co/{repo_id}/{os.path.basename(ckpt)}") |
| size = os.path.getsize(ckpt) |
| print(f"📦 Uploading file: {ckpt} ({format_size(size)})") |
|
|
| try: |
| start_time = time.time() |
| response = api.upload_file( |
| path_or_fileobj=ckpt, |
| path_in_repo=os.path.basename(ckpt), |
| repo_id=repo_id, |
| repo_type=None, |
| create_pr=create_pr, |
| commit_message=commit_message |
| ) |
| duration = time.time() - start_time |
| print(f"✅ Upload completed in {duration:.1f} seconds") |
| except Exception as e: |
| print(f"❌ Error uploading {ckpt}: {e}") |
| print("\n✨ All uploads complete!") |
| if create_pr: |
| print("🎉 Check your repository for the new Pull Request!") |
| else: |
| print("🎉 Files have been uploaded directly to your repository!") |
| if clear_after: |
| time.sleep(3) |
| clear_output() |
|
|
|
|
| upload_btn.on_click(upload_ckpts) |
|
|
| |
| box = VBox([ |
| ckpt_picker, |
| HBox([ |
| Label("HF User:"), |
| Text(value = hfuser, placeholder='YourUSERHERE', disabled = True), |
| Label("HF Repo:"), |
| Text(value = hfrepo, placeholder='ModelNameHere', disabled = True), |
| ]), |
| HBox([ |
| Label("File Directory:"), |
| Text(value = file_location, placeholder='/content', disabled = True), |
| ]), |
| upload_btn, |
| out, |
|
|
| ]) |
|
|
| display(box) |
|
|
| |
| def update_file_list(event): |
| global all_ckpts |
| all_ckpts = find_files(file_location, file_type) |
| ckpt_picker.options = all_ckpts |
|
|
| def on_file_type_change(change): |
| global all_ckpts |
| all_ckpts = find_files(file_location, change.new) |
| ckpt_picker.options = all_ckpts |
|
|
| |
| observe_file_path = Text(value="/content") |
| observe_file_path.observe(update_file_list, names='value') |
|
|
| |
| observe_file_type = Dropdown(options=["safetensors", "pt", "pth", "mp3", "onnx", "pb", "h5", "ckpt", "bin", "json", "yaml", "yml", "txt", "csv", "pkl", "png", "jpg", "jpeg", "webp", "gif", "zip", "tar", "gz", "mp3", "wav", "ogg", "mp4", "mov", "avi", "mkv"], value = file_type) |
| observe_file_type.observe(on_file_type_change, names='value') |
| display(HBox([Label("File Type:"), observe_file_type])) |