Clone SD Models: Any Hugging Face Repos to Your Account

clone stable diffusion models

Imagine you are a business owner or professional creator managing a project based on Stable Diffusion, and due to some government policies any AI based startup instantly remove their models from the Hugging Face repository. Your business will collapse. 

The AI community is so rapidly growing and all those government interventions impacted a lot in the past days.

runwayml deleted models

A similar incident recently happened. There was the dubious situation in the Reddit community when RunwayML deleted their officially released Stable Diffusion 1.5 model from their Hugging Face account. 

After doing quite in-depth research, we found a quick solution to deal with these types of uncertain situations. To do this, you can remotely clone any public Hugging Face repository to your personal repository. All the steps are described in an easy flow that a non-techy savvy can also grasp.

Here, we are using Google Colab so that you don't need to manage huge AI models locally and spend all your network bandwidth. This process can be followed in any cloud server as well. 


Table of Contents (Jump to section):


Step-By-Step Process:

This process assumes you have access to both the source and destination repositories on Hugging Face and they are not listed as the private one. 


1: Set Up Your Environment in Google Colab:

First, you need to open google colab and create a new notebook. Use this command to install the hugging face library to do the initial setup.

!pip install huggingface_hub



install hugging face library


2. Creating Hugging Face Access Token

Now, login to Hugging Face and create new access token.


create new access token

Click on the "Create new token" button to create it.

Add permissions to access token

Check the box to grant relevant permission to your access token. There are multiple options you can choose from- read , write, delete, etc. Add a relevant name for your access token. Then, just click "Create" button available at the bottom. 

Copy and save your access token

Next, you will be prompted with your new access token. Click "Copy" to save it. Make sure you save the access token safely as it will not be accessed in the future. To use the Hugging Face, you need to create a new one again.

3. Login hugging face using Access token:

from huggingface_hub import login

login(token="<<your-access-token>>")


Here, replace <<your-access-token>> with your new access token.


Grant access for operation

After the code execution, a popup message will appear. You have to grant access by hitting the "Grant access" option.


Login successful message

After that, you will get a confirmation message "Login successful".


4. Cloning/Downloading the whole hugging face repo:

!git clone <<public-repository-link>>

clone entire repository

This is useful if you want to download the entire repository. Make sure it's in the public domain and not set to private which means it can be accessed by anyone. For illustration, we are using the public "pt-sk/stable-diffusion-1.5" Hugging face repository.


copy hugging face repo id

To get the public Hugging face id, just copy the id from its repository like we shown above.

Coning entire repository is a cumbersome task and required enough disk space. Make sure you have enough disk space on Google Colab account.


You can use these function to download only specific file:

from huggingface_hub import hf_hub_download

hf_hub_download(repo_id="put-public-repo-id", filename="name-of-file", local_dir="file-location-path")


copy hugging face repo id


Now, copy the public Hugging Face repository ID (whatever you have chosen to clone).

download single file from public repository

This block of code will help you to download a single file from any public repository.
 
Parameters:
repo_id="put-your-public-repo-id"
filename="name-of-specific-file"
local_dir="path-location-of-file"

copy the path using right click

To copy the relevant path of any file select using right click option available on the left panel. Then, put the path for the "filename" and "local_dir" respective parameters as we shown above.

5. Create a new hugging face repository:

from huggingface_hub import create_repo

create_repo("<<your-HF-profile-name>>/<<your-HF-repo-name>>")


Replace <<your-HF-profile-name>> and <<your-HF-repo-name>> with your Hugging face profile name and repository name. In our case, "stablediffusiontutorials" is our profile name, and "stable-diffusion-v1.5" is the new repository name we want to create. Skip this step if you already created a new repository.

Create new repository

new repository created

You can see we have the new created repository as "stable-diffusion-v1.5" in our Hugging face account.


6. Uploading the whole repository to the personal repository:

from huggingface_hub import HfApi

api = HfApi()

api.upload_folder( folder_path="<<path-of-folder-location>>", repo_id="your-new-created-repo-id", repo_type="model" )


Upload single file to our repository


copy path of folder

To copy the relevant path of any file select using right click option. Then, put the path and repository id beside the relevant parameters as we show above.

Parameters:

folder_path="<<path-of-folder-location>>"

repo_id="your-new-created-repo-id"

repo_type= "your-repository-type"


Repository cloned

Now, you can see the whole repository has been cloned to our new Hugging Face repository.

Upload a single file to your personal account:

from huggingface_hub import HfApi

api = HfApi()

api.upload_file(path_or_fileobj="<<file-path>>", path_in_repo="README.md", repo_id="<<your-new-created-repo-id>>", repo_type="model")

Upload single file to our hugging face repository

This will help if you want to upload only the single file and not the whole repository. 

Parameters:
path_or_fileobj="<<file-path>>"
path_in_repo="README.md"
repo_id="<<your-new-created-repo-id>>"

Here, Readme.md is the filename to identify the location your new repository where you want to upload the file. 
Here, we want to upload a file where "Readme.md" file located. So, we put this file name. If you want to upload inside a specific folder then your path will be like "folder-name/any-file-name".



uploading single file

We are using these files to upload to our repository.

uploaded single file

Here are the results after uploading relevant files.


Important points:


disk is full error on colab

1. You should have enough space in Google Colab while cloning any specific repository otherwise you will get warnings "Disk is almost full" message. Using the free tier will provide some restrictions. With free tier, you get the limited RAM/GPU/Disk resources. 

You can face multiple disconnects if you use session very extensively. To fix these, you can switch to their pro plan if required. 


check real-time status

2. To get the real-time status of your Colab disk you can hover on the top corner of the RAM/Disk status bar. 

3. The time takes to upload/download the files will depend on your repository size and what Colab plan you have chosen. Here, we have chosen the free tier and the entire repository is around 100GB. The total download time took 14 minutes 28seconds.


Conclusion:

Cloning any public hugging face is the most efficient way to store your specific models remotely so that any future conflicts will not hamper the work in long run.

As described, you can use the above process in any remote servers as well.