{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Setup " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Libraries to use:" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "from PIL import Image\n", "from transformers import AutoModel\n", "import torch\n", "from torch import autocast\n", "from diffusers import StableDiffusionPipeline\n", "import os\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Some vars to store settings:" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "model_id = \"CompVis/stable-diffusion-v1-4\"\n", "device = \"cpu\"\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Utility Functions" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "bea4f56468d24b2ab018be2cb18b656a", "version_major": 2, "version_minor": 0 }, "text/plain": [ "Downloading: 0%| | 0.00/543 [00:00. Note that this might lead to problems when using and is not recommended.\n", "You have disabled the safety checker for by passing `safety_checker=None`. Ensure that you abide to the conditions of the Stable Diffusion license and do not expose unfiltered results in services or applications open to the public. Both the diffusers team and Hugging Face strongly recommend to keep the safety filter enabled in all public facing circumstances, disabling it only for use-cases that involve analyzing network behavior or auditing its results. For more information, please have a look at https://github.com/huggingface/diffusers/pull/254 .\n" ] } ], "source": [ "pipe = StableDiffusionPipeline.from_pretrained(\n", " model_id, use_auth_token=os.getenv(\"HF_ACCESS_TOKEN\"), safety_checker=None\n", ")\n", "pipe = pipe.to(device)\n", "\n", "\n", "def predict(\n", " prompt=\"art nuveau style, woman pose study, flowers, 4k, high detail\",\n", " num_imgs=6,\n", " img_height=512,\n", " img_width=512,\n", "):\n", " \"\"\"\n", " Generate some images from the given prompt\n", " \"\"\"\n", "\n", " return [\n", " pipe(prompt, guidance_scale=7.5, height=img_height, width=img_width).images[0]\n", " for _ in range(num_imgs)\n", " ]\n" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "# based on https://stackoverflow.com/questions/42040747/more-idiomatic-way-to-display-images-in-a-grid-with-numpy\n", "\n", "\n", "def make_gallery(images, num_cols=3):\n", " \"\"\"\n", " Turn a list of Images into a grid with num_cols number of columns\n", " \"\"\"\n", " xs = [np.asarray(img) for img in results]\n", " xs = np.stack(xs)\n", "\n", " num_imgs, height, width, channel = xs.shape\n", "\n", " num_rows = num_imgs // num_cols\n", "\n", " gallery = (\n", " xs.reshape(num_rows, num_cols, height, width, channel)\n", " .swapaxes(1, 2)\n", " .reshape(height * num_rows, width * num_cols, channel)\n", " )\n", "\n", " return Image.fromarray(gallery)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Demo " ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "94566a61e07d4f9998a3b2d3cc5e43a4", "version_major": 2, "version_minor": 0 }, "text/plain": [ " 0%| | 0/51 [00:00" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "prompt = \"a home with a modern furniture and blue theme, 4k, high detail\"\n", "h = 512\n", "w = 512\n", "n = 6\n", "\n", "results = predict(prompt=prompt, img_height=h, img_width=w, num_imgs=n)\n", "\n", "make_gallery(results, num_cols=3)\n" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "d0084fef805b4f3996264c588cb271f7", "version_major": 2, "version_minor": 0 }, "text/plain": [ " 0%| | 0/51 [00:00" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "prompt = \"a living room with a fountain in the middle and with ice theme, 4k, high detail\"\n", "h = 512\n", "w = 512\n", "n = 6\n", "\n", "results = predict(prompt=prompt, img_height=h, img_width=w, num_imgs=n)\n", "\n", "make_gallery(results, num_cols=3)\n" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3.10.7 64-bit", "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.10.8" }, "orig_nbformat": 4, "vscode": { "interpreter": { "hash": "949777d72b0d2535278d3dc13498b2535136f6dfe0678499012e853ee9abcab1" } } }, "nbformat": 4, "nbformat_minor": 2 }