Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Colab: added the ability to download models from civitai #1758

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 95 additions & 0 deletions fooocus_colab.ipynb
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Run Fooocus"
]
},
{
"cell_type": "code",
"execution_count": null,
Expand All @@ -14,6 +21,94 @@
"%cd /content/Fooocus\n",
"!python entry_with_update.py --share\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Download checkpoints and LoRAs from civitai\n",
"to download checkpoints and LoRAs from civitai you have to run Fooocus first\n",
"\n",
"https://civitai.com/models"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# put the link of the model you want to download (checkpoint or LoRA)\n",
"# link can specify a version id and if not specified the latest version will be downloaded\n",
"# url example without version id: https://civitai.com/models/133005/juggernaut-xl\n",
"# url example with version id: https://civitai.com/models/133005?modelVersionId=252902\n",
"\n",
"civitai_url = \"https://civitai.com/models/133005/juggernaut-xl\"\n",
"\n",
"# -----------------------------\n",
"import requests\n",
"from urllib.parse import urlparse\n",
"from urllib.parse import parse_qs\n",
"\n",
"allowed_types = ['Checkpoint', 'LORA']\n",
"save_locations = {\n",
" 'Checkpoint': '/content/Fooocus/models/checkpoints',\n",
" 'LORA': '/content/Fooocus/models/loras'\n",
"}\n",
"\n",
"parsed_url = urlparse(civitai_url)\n",
"\n",
"model_id = parsed_url.path.split('/')[parsed_url.path.split('/').index('models') + 1]\n",
"model_version_id = parse_qs(parsed_url.query).get('modelVersionId')\n",
"\n",
"url = \"https://civitai.com/api/v1/models/\" + model_id\n",
"response = requests.get(url)\n",
"\n",
"if response.status_code != 200:\n",
" raise RuntimeError('model not found')\n",
"\n",
"data = response.json()\n",
"model_type = data.get('type')\n",
"\n",
"if model_type not in allowed_types:\n",
" raise RuntimeError('model is not a checkpoint or LoRA')\n",
"\n",
"model_versions = data.get('modelVersions')\n",
"\n",
"selected_version = None\n",
"\n",
"if model_version_id:\n",
" for model_version in model_versions:\n",
" if str(model_version.get('id')) == model_version_id[0]:\n",
" selected_version = model_version\n",
"else:\n",
" selected_version = model_versions[0]\n",
"\n",
"if selected_version is None:\n",
" raise RuntimeError(\"this version doesn't exist\")\n",
"\n",
"if \"SDXL\" not in selected_version.get('baseModel'):\n",
" raise RuntimeError(\"this model is not SDXL\")\n",
"\n",
"files = selected_version.get('files')\n",
"primary_file = None\n",
"\n",
"for f in files:\n",
" if f.get('primary'):\n",
" primary_file = f\n",
"\n",
"download_url = primary_file.get('downloadUrl')\n",
"file_name = primary_file.get('name')\n",
"\n",
"LOCATION = save_locations[model_type]\n",
"%cd $LOCATION\n",
"\n",
"model_name = data.get('name')\n",
"selected_version_name = selected_version.get('name')\n",
"print(f'downloading {model_name} ({model_type} version {selected_version_name})')\n",
"\n",
"get_ipython().system(f'wget -O \"{file_name}\" \"{download_url}\"')"
]
}
],
"metadata": {
Expand Down