|
|
|
"""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 |
|
[![ko-fi](https://ko-fi.com/img/githubbutton_sm.svg)](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://dcbadge.vercel.app/api/shield/442099748669751297?style=flat)](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])) |