{ "cells": [ { "cell_type": "markdown", "id": "c4b02e9b-6a70-4e41-880b-81a694500dd0", "metadata": {}, "source": [ "# Text-to-Image Synthesis with Huggingface Stable Diffusion XL 1.0\n", "\n", "This notebook demonstrates the basic usage of the Huggingface API for text-to-image synthesis using the stable diffusion xl 1.0 model.\n", "\n", "Mostly though, it's just me having some fun with generating synthetic images." ] }, { "cell_type": "code", "execution_count": 1, "id": "f6e62064-0f5d-4cf2-926e-313f2c1a8725", "metadata": {}, "outputs": [], "source": [ "import torch as th\n", "import torchinfo\n", "import diffusers" ] }, { "cell_type": "code", "execution_count": 2, "id": "bb0004a8-81a0-4ccb-9c71-409bdbc73990", "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "82db72a4c7a54b7e99e17c856cf12672", "version_major": 2, "version_minor": 0 }, "text/plain": [ "Loading pipeline components...: 0%| | 0/7 [00:00" ] }, "execution_count": 605, "metadata": {}, "output_type": "execute_result" } ], "source": [ "result = pipeline(\"Water droplet high speed photograph.\")\n", "result.images[0]" ] }, { "cell_type": "code", "execution_count": 7, "id": "c92960b2-cf50-43b8-a52d-ae39bb5cca8c", "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "0b986ef296a04b6289a83e8b3769d851", "version_major": 2, "version_minor": 0 }, "text/plain": [ " 0%| | 0/50 [00:00" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "result = pipeline('A labradoodle puppy running through a sprinkler')\n", "result.images[0]" ] }, { "cell_type": "code", "execution_count": 514, "id": "597d5336-66e3-4668-a447-f196f09fd495", "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "79e3c881cf7a4ae0abe8f35f5782bf78", "version_major": 2, "version_minor": 0 }, "text/plain": [ " 0%| | 0/50 [00:00" ] }, "execution_count": 514, "metadata": {}, "output_type": "execute_result" } ], "source": [ "result = pipeline('A hand with fingers.')\n", "result.images[0]" ] }, { "cell_type": "code", "execution_count": 62, "id": "eb5d36b7-fb6d-4966-8286-d0f181a5eb6d", "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "654dca8890e34f47af7890ab5ffa091e", "version_major": 2, "version_minor": 0 }, "text/plain": [ " 0%| | 0/50 [00:00" ] }, "execution_count": 62, "metadata": {}, "output_type": "execute_result" } ], "source": [ "result = pipeline('Computer code on a VT100 terminal.')\n", "result.images[0]" ] }, { "cell_type": "code", "execution_count": 737, "id": "dcf61c25-cf80-4ead-acdb-7ccc95577d72", "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "17de328aa737409e8940e3fff6d9a85c", "version_major": 2, "version_minor": 0 }, "text/plain": [ " 0%| | 0/50 [00:00" ] }, "execution_count": 737, "metadata": {}, "output_type": "execute_result" } ], "source": [ "result = pipeline('A LAN party in the eighties.')\n", "result.images[0]" ] }, { "cell_type": "code", "execution_count": 583, "id": "abb440fa-f8ce-430c-b0cc-59f9a3b49b3f", "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "fae90e220c064931bf29656fad679bd2", "version_major": 2, "version_minor": 0 }, "text/plain": [ " 0%| | 0/50 [00:00" ] }, "execution_count": 583, "metadata": {}, "output_type": "execute_result" } ], "source": [ "result = pipeline('If stable diffusion had a face.')\n", "result.images[0]" ] }, { "cell_type": "code", "execution_count": 776, "id": "15ce564d-95fe-42aa-9122-c6d086ff9283", "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "c4b8c8db81bb4bb79d9fd3a97bbf0b09", "version_major": 2, "version_minor": 0 }, "text/plain": [ " 0%| | 0/50 [00:00" ] }, "execution_count": 776, "metadata": {}, "output_type": "execute_result" } ], "source": [ "result = pipeline('An evil dangerous skynet robot zombie terminator.')\n", "result.images[0]" ] }, { "cell_type": "code", "execution_count": 673, "id": "d768deb9-f48e-40ed-b81f-937a9e099bb4", "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "dc6eac694e6042b993d7c13091097e77", "version_major": 2, "version_minor": 0 }, "text/plain": [ " 0%| | 0/50 [00:00" ] }, "execution_count": 673, "metadata": {}, "output_type": "execute_result" } ], "source": [ "result = pipeline('A man rolling in the mud laughing.')\n", "result.images[0]" ] }, { "cell_type": "code", "execution_count": 706, "id": "0f2a176d-ab96-48bf-beef-e66d52bc86c8", "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "15c2d5ba07be43b988d5b04abceaf4b8", "version_major": 2, "version_minor": 0 }, "text/plain": [ " 0%| | 0/50 [00:00" ] }, "execution_count": 706, "metadata": {}, "output_type": "execute_result" } ], "source": [ "result = pipeline(\"A cow wearing a viking helmet.\")\n", "result.images[0]" ] }, { "cell_type": "code", "execution_count": 252, "id": "8cb777b0-364f-4255-9080-cd5f7bab30c9", "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "a5d019686f5a4f6aa251d3c3eed24331", "version_major": 2, "version_minor": 0 }, "text/plain": [ " 0%| | 0/50 [00:00" ] }, "execution_count": 252, "metadata": {}, "output_type": "execute_result" } ], "source": [ "result = pipeline(\"An alien octopus with two eyes.\")\n", "result.images[0]" ] }, { "cell_type": "code", "execution_count": 284, "id": "30565f20-5c70-4af4-a9f0-d044d25af396", "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "c38c0ed2eef14928acdb1368af950c11", "version_major": 2, "version_minor": 0 }, "text/plain": [ " 0%| | 0/50 [00:00" ] }, "execution_count": 284, "metadata": {}, "output_type": "execute_result" } ], "source": [ "result = pipeline(\"A selfie in space.\")\n", "result.images[0]" ] }, { "cell_type": "code", "execution_count": 675, "id": "8506cc8c-2b5c-4bcc-83e3-1d2cd81767c4", "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "0db9be93de8e48a8a31abd1ff495f469", "version_major": 2, "version_minor": 0 }, "text/plain": [ " 0%| | 0/50 [00:00" ] }, "execution_count": 675, "metadata": {}, "output_type": "execute_result" } ], "source": [ "result = pipeline(\"Drinking cofee at starbucks.\")\n", "result.images[0]" ] }, { "cell_type": "code", "execution_count": 678, "id": "7253df84-0e91-4a7c-a6da-6b35da9de4f3", "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "8b552556bf014e47bce1af78411b8877", "version_major": 2, "version_minor": 0 }, "text/plain": [ " 0%| | 0/50 [00:00" ] }, "execution_count": 678, "metadata": {}, "output_type": "execute_result" } ], "source": [ "result = pipeline(\"Van Gogh Painting of a water buffalo.\")\n", "result.images[0]" ] }, { "cell_type": "code", "execution_count": 127, "id": "ded42fee-4cb2-462c-9755-534bfed53547", "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "4ae0af1c5925412d8dfd5897ba299114", "version_major": 2, "version_minor": 0 }, "text/plain": [ " 0%| | 0/50 [00:00" ] }, "execution_count": 127, "metadata": {}, "output_type": "execute_result" } ], "source": [ "result = pipeline(\"A psychedelic cat.\")\n", "result.images[0]" ] }, { "cell_type": "code", "execution_count": 52, "id": "b65dd5a4-e1f8-472c-be0f-287352e709fc", "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "c3846a2a4e76407590fab2f709545c3f", "version_major": 2, "version_minor": 0 }, "text/plain": [ " 0%| | 0/50 [00:00" ] }, "execution_count": 52, "metadata": {}, "output_type": "execute_result" } ], "source": [ "result = pipeline(\"A unicorn on the moon.\", prompt_2=\"A unicorn with the earth in the sky.\")\n", "result.images[0]" ] }, { "cell_type": "code", "execution_count": 473, "id": "23222528-2a86-4b6e-990d-46a447fb2eef", "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "2a6115e58fd7443b9d039145ef2ec32d", "version_major": 2, "version_minor": 0 }, "text/plain": [ " 0%| | 0/50 [00:00" ] }, "execution_count": 473, "metadata": {}, "output_type": "execute_result" } ], "source": [ "result = pipeline(\"A mountain landscape with the sun rising.\")\n", "result.images[0]" ] }, { "cell_type": "code", "execution_count": 497, "id": "d25e65fc-cb5a-46ff-9c01-472f1d34954c", "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "6ca06d06bdb54a49ba285adaf1f54660", "version_major": 2, "version_minor": 0 }, "text/plain": [ " 0%| | 0/50 [00:00" ] }, "execution_count": 497, "metadata": {}, "output_type": "execute_result" } ], "source": [ "result = pipeline(\"Sunrise on the cold planet Neptune.\")\n", "result.images[0]" ] }, { "cell_type": "code", "execution_count": 751, "id": "ae7a446a-5a56-4e0d-acb3-a46038534d27", "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "c6d9c868c6bb4916996738a3ab5de07f", "version_major": 2, "version_minor": 0 }, "text/plain": [ " 0%| | 0/50 [00:00" ] }, "execution_count": 751, "metadata": {}, "output_type": "execute_result" } ], "source": [ "result = pipeline('A tornado in the Nebraska Sandhills.')\n", "result.images[0]" ] }, { "cell_type": "code", "execution_count": 215, "id": "4f4f7ae5-3e5c-4ac3-9769-65159f9bc654", "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "f1d80da472454fbe8917fc27d99fe107", "version_major": 2, "version_minor": 0 }, "text/plain": [ " 0%| | 0/50 [00:00" ] }, "execution_count": 215, "metadata": {}, "output_type": "execute_result" } ], "source": [ "result = pipeline(\"A hampster shoveling snow.\")\n", "result.images[0]" ] }, { "cell_type": "code", "execution_count": 57, "id": "a403a341-4997-45b0-a2fc-d913190b42db", "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "59cd8ee52c9e4aae8fb52c80da9c6dd2", "version_major": 2, "version_minor": 0 }, "text/plain": [ " 0%| | 0/50 [00:00" ] }, "execution_count": 57, "metadata": {}, "output_type": "execute_result" } ], "source": [ "result = pipeline(\"A cat as a soldier.\")\n", "result.images[0]" ] }, { "cell_type": "code", "execution_count": 668, "id": "b85068f3-e914-4473-b4ac-d93a8a6ec42d", "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "dc5d456ece6645a3b1e7767634769f5e", "version_major": 2, "version_minor": 0 }, "text/plain": [ " 0%| | 0/50 [00:00" ] }, "execution_count": 668, "metadata": {}, "output_type": "execute_result" } ], "source": [ "result = pipeline(\"An F16 fighter jet.\")\n", "result.images[0]" ] }, { "cell_type": "code", "execution_count": 499, "id": "26078fa7-f8d9-4c21-b686-290d82f1177a", "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "8dc0d20902a343f7a9d2242b0d4b636c", "version_major": 2, "version_minor": 0 }, "text/plain": [ " 0%| | 0/50 [00:00" ] }, "execution_count": 499, "metadata": {}, "output_type": "execute_result" } ], "source": [ "result = pipeline(\"A cutaway diagram of a Saturn V rocket.\")\n", "result.images[0]" ] }, { "cell_type": "code", "execution_count": 500, "id": "27611d3d-8991-4c81-a0d4-158b3a7ce597", "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "6d3fee1bb29b44cdbdebbf674e8b762f", "version_major": 2, "version_minor": 0 }, "text/plain": [ " 0%| | 0/50 [00:00" ] }, "execution_count": 500, "metadata": {}, "output_type": "execute_result" } ], "source": [ "result = pipeline(\"Schematic diagram of an electro-transcombobulator.\")\n", "result.images[0]" ] }, { "cell_type": "code", "execution_count": 241, "id": "f65c2876-9de4-4dee-bdcd-80b554b155e1", "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "6a0e380d13ec4b9e959ce26718ec3ff8", "version_major": 2, "version_minor": 0 }, "text/plain": [ " 0%| | 0/50 [00:00" ] }, "execution_count": 241, "metadata": {}, "output_type": "execute_result" } ], "source": [ "result = pipeline('Diagram of the human circulatory system.')\n", "result.images[0]" ] }, { "cell_type": "code", "execution_count": 299, "id": "f18edceb-502a-4ac7-9428-3cb59710d9d1", "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "35b93643bd8f4268ac73b9550a1fc4ba", "version_major": 2, "version_minor": 0 }, "text/plain": [ " 0%| | 0/50 [00:00" ] }, "execution_count": 299, "metadata": {}, "output_type": "execute_result" } ], "source": [ "result = pipeline(\"Superbowl half-time show.\")\n", "result.images[0]" ] }, { "cell_type": "code", "execution_count": 747, "id": "bd2f57a6-6d75-48ba-8284-8c3792d36a02", "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "9db1352570ad4a77be329e0f25afc969", "version_major": 2, "version_minor": 0 }, "text/plain": [ " 0%| | 0/50 [00:00" ] }, "execution_count": 747, "metadata": {}, "output_type": "execute_result" } ], "source": [ "result = pipeline('A huge firework show.')\n", "result.images[0]" ] }, { "cell_type": "code", "execution_count": null, "id": "901dbf43-c5e3-4544-82e4-a7d9de07c2bf", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.11.6" } }, "nbformat": 4, "nbformat_minor": 5 }