|
1239 | 1239 | " !python /content/gdrive/MyDrive/sd/stable-diffusion-$blasphemy/webui.py $share --ckpt-dir \"$path_to_trained_model\" $auth $configf"
|
1240 | 1240 | ]
|
1241 | 1241 | },
|
1242 |
| - { |
1243 |
| - "cell_type": "markdown", |
1244 |
| - "metadata": { |
1245 |
| - "id": "d_mQ23XsOc5R" |
1246 |
| - }, |
1247 |
| - "source": [ |
1248 |
| - "# Upload The Trained Model to Hugging Face " |
1249 |
| - ] |
1250 |
| - }, |
1251 |
| - { |
1252 |
| - "cell_type": "code", |
1253 |
| - "execution_count": null, |
1254 |
| - "metadata": { |
1255 |
| - "cellView": "form", |
1256 |
| - "id": "NTqUIuhROdH4" |
1257 |
| - }, |
1258 |
| - "outputs": [], |
1259 |
| - "source": [ |
1260 |
| - "from slugify import slugify\n", |
1261 |
| - "from huggingface_hub import HfApi, HfFolder, CommitOperationAdd\n", |
1262 |
| - "from huggingface_hub import create_repo\n", |
1263 |
| - "from IPython.display import display_markdown\n", |
1264 |
| - "from IPython.display import clear_output\n", |
1265 |
| - "from IPython.utils import capture\n", |
1266 |
| - "from google.colab import files\n", |
1267 |
| - "import shutil\n", |
1268 |
| - "import time\n", |
1269 |
| - "import os\n", |
1270 |
| - "\n", |
1271 |
| - "Upload_sample_images = False #@param {type:\"boolean\"}\n", |
1272 |
| - "#@markdown - Upload showcase images of your trained model\n", |
1273 |
| - "\n", |
1274 |
| - "Name_of_your_concept = \"\" #@param {type:\"string\"}\n", |
1275 |
| - "if(Name_of_your_concept == \"\"):\n", |
1276 |
| - " Name_of_your_concept = Session_Name\n", |
1277 |
| - "Name_of_your_concept=Name_of_your_concept.replace(\" \",\"-\") \n", |
1278 |
| - " \n", |
1279 |
| - "#@markdown - [Create a write access token](https://huggingface.co/settings/tokens) , go to \"New token\" -> Role : Write. A regular read token won't work here.\n", |
1280 |
| - "hf_token_write = \"\" #@param {type:\"string\"}\n", |
1281 |
| - "if hf_token_write ==\"\":\n", |
1282 |
| - " print('\u001b[1;32mYour Hugging Face write access token : ')\n", |
1283 |
| - " hf_token_write=input()\n", |
1284 |
| - "\n", |
1285 |
| - "hf_token = hf_token_write\n", |
1286 |
| - "\n", |
1287 |
| - "api = HfApi()\n", |
1288 |
| - "your_username = api.whoami(token=hf_token)[\"name\"]\n", |
1289 |
| - "\n", |
1290 |
| - "repo_id = f\"{your_username}/{slugify(Name_of_your_concept)}\"\n", |
1291 |
| - "output_dir = f'/content/models/'+INSTANCE_NAME\n", |
1292 |
| - "\n", |
1293 |
| - "def bar(prg):\n", |
1294 |
| - " br=\"\u001b[1;33mUploading to HuggingFace : \" '\u001b[0m|'+'█' * prg + ' ' * (25-prg)+'| ' +str(prg*4)+ \"%\"\n", |
1295 |
| - " return br\n", |
1296 |
| - "\n", |
1297 |
| - "print(\"\u001b[1;32mLoading...\")\n", |
1298 |
| - "\n", |
1299 |
| - "NM=\"False\"\n", |
1300 |
| - "if os.path.getsize(OUTPUT_DIR+\"/text_encoder/pytorch_model.bin\") > 670901463:\n", |
1301 |
| - " NM=\"True\"\n", |
1302 |
| - "\n", |
1303 |
| - "with capture.capture_output() as cap:\n", |
1304 |
| - " if NM==\"False\":\n", |
1305 |
| - " %cd $OUTPUT_DIR\n", |
1306 |
| - " !rm -r safety_checker feature_extractor .git\n", |
1307 |
| - " !rm model_index.json\n", |
1308 |
| - " !git init\n", |
1309 |
| - " !git lfs install --system --skip-repo\n", |
1310 |
| - " !git remote add -f origin \"https://USER:{hf_token}@huggingface.co/runwayml/stable-diffusion-v1-5\"\n", |
1311 |
| - " !git config core.sparsecheckout true\n", |
1312 |
| - " !echo -e \"feature_extractor\\nsafety_checker\\nmodel_index.json\" > .git/info/sparse-checkout\n", |
1313 |
| - " !git pull origin main\n", |
1314 |
| - " !rm -r .git\n", |
1315 |
| - " %cd /content\n", |
1316 |
| - " else:\n", |
1317 |
| - " %cd $OUTPUT_DIR\n", |
1318 |
| - " !rm -r feature_extractor .git\n", |
1319 |
| - " !git init\n", |
1320 |
| - " !git lfs install --system --skip-repo\n", |
1321 |
| - " !git remote add -f origin \"https://USER:{hf_token}@huggingface.co/stabilityai/stable-diffusion-2-1\"\n", |
1322 |
| - " !git config core.sparsecheckout true\n", |
1323 |
| - " !echo -e \"feature_extractor\" > .git/info/sparse-checkout\n", |
1324 |
| - " !git pull origin main\n", |
1325 |
| - " !rm -r .git\n", |
1326 |
| - " %cd /content\n", |
1327 |
| - "\n", |
1328 |
| - "\n", |
1329 |
| - "image_string = \"\"\n", |
1330 |
| - "\n", |
1331 |
| - "if os.path.exists('/content/sample_images'):\n", |
1332 |
| - " !rm -r /content/sample_images\n", |
1333 |
| - "Samples=\"/content/sample_images\"\n", |
1334 |
| - "!mkdir $Samples\n", |
1335 |
| - "clear_output()\n", |
1336 |
| - "\n", |
1337 |
| - "if Upload_sample_images:\n", |
1338 |
| - "\n", |
1339 |
| - " print(\"\u001b[1;32mUpload Sample images of the model\")\n", |
1340 |
| - " uploaded = files.upload()\n", |
1341 |
| - " for filename in uploaded.keys():\n", |
1342 |
| - " shutil.move(filename, Samples)\n", |
1343 |
| - " %cd $Samples\n", |
1344 |
| - " !find . -name \"* *\" -type f | rename 's/ /_/g'\n", |
1345 |
| - " %cd /content\n", |
1346 |
| - " clear_output()\n", |
1347 |
| - "\n", |
1348 |
| - " print(bar(1))\n", |
1349 |
| - "\n", |
1350 |
| - " images_upload = os.listdir(Samples)\n", |
1351 |
| - " instance_prompt_list = []\n", |
1352 |
| - " for i, image in enumerate(images_upload):\n", |
1353 |
| - " image_string = f'''\n", |
1354 |
| - " {image_string}\n", |
1355 |
| - " '''\n", |
1356 |
| - " \n", |
1357 |
| - "readme_text = f'''---\n", |
1358 |
| - "license: creativeml-openrail-m\n", |
1359 |
| - "tags:\n", |
1360 |
| - "- text-to-image\n", |
1361 |
| - "- stable-diffusion\n", |
1362 |
| - "---\n", |
1363 |
| - "### {Name_of_your_concept} Dreambooth model trained by {api.whoami(token=hf_token)[\"name\"]} with [TheLastBen's fast-DreamBooth](https://colab.research.google.com/github/TheLastBen/fast-stable-diffusion/blob/main/fast-DreamBooth.ipynb) notebook\n", |
1364 |
| - "\n", |
1365 |
| - "\n", |
1366 |
| - "Test the concept via A1111 Colab [fast-Colab-A1111](https://colab.research.google.com/github/TheLastBen/fast-stable-diffusion/blob/main/fast_stable_diffusion_AUTOMATIC1111.ipynb)\n", |
1367 |
| - "\n", |
1368 |
| - "Sample pictures of this concept:\n", |
1369 |
| - "{image_string}\n", |
1370 |
| - "'''\n", |
1371 |
| - "#Save the readme to a file\n", |
1372 |
| - "readme_file = open(\"README.md\", \"w\")\n", |
1373 |
| - "readme_file.write(readme_text)\n", |
1374 |
| - "readme_file.close()\n", |
1375 |
| - "\n", |
1376 |
| - "operations = [\n", |
1377 |
| - " CommitOperationAdd(path_in_repo=\"README.md\", path_or_fileobj=\"README.md\"),\n", |
1378 |
| - " CommitOperationAdd(path_in_repo=f\"{Session_Name}.ckpt\",path_or_fileobj=MDLPTH)\n", |
1379 |
| - "\n", |
1380 |
| - "]\n", |
1381 |
| - "create_repo(repo_id,private=True, token=hf_token)\n", |
1382 |
| - "\n", |
1383 |
| - "api.create_commit(\n", |
1384 |
| - " repo_id=repo_id,\n", |
1385 |
| - " operations=operations,\n", |
1386 |
| - " commit_message=f\"Upload the concept {Name_of_your_concept} embeds and token\",\n", |
1387 |
| - " token=hf_token\n", |
1388 |
| - ")\n", |
1389 |
| - "\n", |
1390 |
| - "api.upload_folder(\n", |
1391 |
| - " folder_path=OUTPUT_DIR+\"/feature_extractor\",\n", |
1392 |
| - " path_in_repo=\"feature_extractor\",\n", |
1393 |
| - " repo_id=repo_id,\n", |
1394 |
| - " token=hf_token\n", |
1395 |
| - ")\n", |
1396 |
| - "\n", |
1397 |
| - "clear_output()\n", |
1398 |
| - "print(bar(4))\n", |
1399 |
| - "\n", |
1400 |
| - "if NM==\"False\":\n", |
1401 |
| - " api.upload_folder(\n", |
1402 |
| - " folder_path=OUTPUT_DIR+\"/safety_checker\",\n", |
1403 |
| - " path_in_repo=\"safety_checker\",\n", |
1404 |
| - " repo_id=repo_id,\n", |
1405 |
| - " token=hf_token\n", |
1406 |
| - " )\n", |
1407 |
| - "\n", |
1408 |
| - "clear_output()\n", |
1409 |
| - "print(bar(8))\n", |
1410 |
| - "\n", |
1411 |
| - "\n", |
1412 |
| - "api.upload_folder(\n", |
1413 |
| - " folder_path=OUTPUT_DIR+\"/scheduler\",\n", |
1414 |
| - " path_in_repo=\"scheduler\",\n", |
1415 |
| - " repo_id=repo_id,\n", |
1416 |
| - " token=hf_token\n", |
1417 |
| - ")\n", |
1418 |
| - "\n", |
1419 |
| - "clear_output()\n", |
1420 |
| - "print(bar(9))\n", |
1421 |
| - "\n", |
1422 |
| - "api.upload_folder(\n", |
1423 |
| - " folder_path=OUTPUT_DIR+\"/text_encoder\",\n", |
1424 |
| - " path_in_repo=\"text_encoder\",\n", |
1425 |
| - " repo_id=repo_id,\n", |
1426 |
| - " token=hf_token\n", |
1427 |
| - ")\n", |
1428 |
| - "\n", |
1429 |
| - "clear_output()\n", |
1430 |
| - "print(bar(12))\n", |
1431 |
| - "\n", |
1432 |
| - "api.upload_folder(\n", |
1433 |
| - " folder_path=OUTPUT_DIR+\"/tokenizer\",\n", |
1434 |
| - " path_in_repo=\"tokenizer\",\n", |
1435 |
| - " repo_id=repo_id,\n", |
1436 |
| - " token=hf_token\n", |
1437 |
| - ")\n", |
1438 |
| - "\n", |
1439 |
| - "clear_output()\n", |
1440 |
| - "print(bar(13))\n", |
1441 |
| - "\n", |
1442 |
| - "api.upload_folder(\n", |
1443 |
| - " folder_path=OUTPUT_DIR+\"/unet\",\n", |
1444 |
| - " path_in_repo=\"unet\",\n", |
1445 |
| - " repo_id=repo_id,\n", |
1446 |
| - " token=hf_token\n", |
1447 |
| - ")\n", |
1448 |
| - "\n", |
1449 |
| - "clear_output()\n", |
1450 |
| - "print(bar(21))\n", |
1451 |
| - "\n", |
1452 |
| - "api.upload_folder(\n", |
1453 |
| - " folder_path=OUTPUT_DIR+\"/vae\",\n", |
1454 |
| - " path_in_repo=\"vae\",\n", |
1455 |
| - " repo_id=repo_id,\n", |
1456 |
| - " token=hf_token\n", |
1457 |
| - ")\n", |
1458 |
| - "\n", |
1459 |
| - "clear_output()\n", |
1460 |
| - "print(bar(23))\n", |
1461 |
| - "\n", |
1462 |
| - "api.upload_file(\n", |
1463 |
| - " path_or_fileobj=OUTPUT_DIR+\"/model_index.json\",\n", |
1464 |
| - " path_in_repo=\"model_index.json\",\n", |
1465 |
| - " repo_id=repo_id,\n", |
1466 |
| - " token=hf_token\n", |
1467 |
| - ")\n", |
1468 |
| - "\n", |
1469 |
| - "clear_output()\n", |
1470 |
| - "print(bar(24))\n", |
1471 |
| - "\n", |
1472 |
| - "api.upload_folder(\n", |
1473 |
| - " folder_path=Samples,\n", |
1474 |
| - " path_in_repo=\"sample_images\",\n", |
1475 |
| - " repo_id=repo_id,\n", |
1476 |
| - " token=hf_token\n", |
1477 |
| - ")\n", |
1478 |
| - "\n", |
1479 |
| - "clear_output()\n", |
1480 |
| - "print(bar(25))\n", |
1481 |
| - "\n", |
1482 |
| - "display_markdown(f'''## Your concept was saved successfully. [Click here to access it](https://huggingface.co/{repo_id})\n", |
1483 |
| - "''', raw=True)" |
1484 |
| - ] |
1485 |
| - }, |
1486 | 1242 | {
|
1487 | 1243 | "cell_type": "code",
|
1488 | 1244 | "execution_count": null,
|
|
0 commit comments