{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "1f417fcf-52d5-4e49-9e26-3834eba323cf", "metadata": { "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Collecting transformers\n", " Downloading transformers-4.45.0-py3-none-any.whl.metadata (44 kB)\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m44.4/44.4 kB\u001b[0m \u001b[31m1.3 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "\u001b[?25hCollecting diffusers\n", " Downloading diffusers-0.30.3-py3-none-any.whl.metadata (18 kB)\n", "Collecting accelerate\n", " Downloading accelerate-0.34.2-py3-none-any.whl.metadata (19 kB)\n", "Requirement already satisfied: ipywidgets in /opt/conda/lib/python3.11/site-packages (8.1.1)\n", "Requirement already satisfied: filelock in /opt/conda/lib/python3.11/site-packages (from transformers) (3.13.1)\n", "Collecting huggingface-hub<1.0,>=0.23.2 (from transformers)\n", " Downloading huggingface_hub-0.25.1-py3-none-any.whl.metadata (13 kB)\n", "Requirement already satisfied: numpy>=1.17 in /opt/conda/lib/python3.11/site-packages (from transformers) (1.26.3)\n", "Requirement already satisfied: packaging>=20.0 in /opt/conda/lib/python3.11/site-packages (from transformers) (23.2)\n", "Requirement already satisfied: pyyaml>=5.1 in /opt/conda/lib/python3.11/site-packages (from transformers) (6.0.1)\n", "Collecting regex!=2019.12.17 (from transformers)\n", " Downloading regex-2024.9.11-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.metadata (40 kB)\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m40.5/40.5 kB\u001b[0m \u001b[31m3.2 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "\u001b[?25hRequirement already satisfied: requests in /opt/conda/lib/python3.11/site-packages (from transformers) (2.31.0)\n", "Collecting tokenizers<0.21,>=0.20 (from transformers)\n", " Downloading tokenizers-0.20.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.metadata (6.7 kB)\n", "Collecting safetensors>=0.4.1 (from transformers)\n", " Downloading safetensors-0.4.5-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.metadata (3.8 kB)\n", "Requirement already satisfied: tqdm>=4.27 in /opt/conda/lib/python3.11/site-packages (from transformers) (4.66.1)\n", "Requirement already satisfied: importlib-metadata in /opt/conda/lib/python3.11/site-packages (from diffusers) (7.0.1)\n", "Requirement already satisfied: Pillow in /opt/conda/lib/python3.11/site-packages (from diffusers) (10.2.0)\n", "Requirement already satisfied: psutil in /opt/conda/lib/python3.11/site-packages (from accelerate) (5.9.8)\n", "Requirement already satisfied: torch>=1.10.0 in /opt/conda/lib/python3.11/site-packages (from accelerate) (2.3.0+cu118)\n", "Requirement already satisfied: comm>=0.1.3 in /opt/conda/lib/python3.11/site-packages (from ipywidgets) (0.2.1)\n", "Requirement already satisfied: ipython>=6.1.0 in /opt/conda/lib/python3.11/site-packages (from ipywidgets) (8.21.0)\n", "Requirement already satisfied: traitlets>=4.3.1 in /opt/conda/lib/python3.11/site-packages (from ipywidgets) (5.14.1)\n", "Requirement already satisfied: widgetsnbextension~=4.0.9 in /opt/conda/lib/python3.11/site-packages (from ipywidgets) (4.0.9)\n", "Requirement already satisfied: jupyterlab-widgets~=3.0.9 in /opt/conda/lib/python3.11/site-packages (from ipywidgets) (3.0.9)\n", "Requirement already satisfied: fsspec>=2023.5.0 in /opt/conda/lib/python3.11/site-packages (from huggingface-hub<1.0,>=0.23.2->transformers) (2023.12.2)\n", "Requirement already satisfied: typing-extensions>=3.7.4.3 in /opt/conda/lib/python3.11/site-packages (from huggingface-hub<1.0,>=0.23.2->transformers) (4.9.0)\n", "Requirement already satisfied: decorator in /opt/conda/lib/python3.11/site-packages (from ipython>=6.1.0->ipywidgets) (5.1.1)\n", "Requirement already satisfied: jedi>=0.16 in /opt/conda/lib/python3.11/site-packages (from ipython>=6.1.0->ipywidgets) (0.19.1)\n", "Requirement already satisfied: matplotlib-inline in /opt/conda/lib/python3.11/site-packages (from ipython>=6.1.0->ipywidgets) (0.1.6)\n", "Requirement already satisfied: prompt-toolkit<3.1.0,>=3.0.41 in /opt/conda/lib/python3.11/site-packages (from ipython>=6.1.0->ipywidgets) (3.0.42)\n", "Requirement already satisfied: pygments>=2.4.0 in /opt/conda/lib/python3.11/site-packages (from ipython>=6.1.0->ipywidgets) (2.17.2)\n", "Requirement already satisfied: stack-data in /opt/conda/lib/python3.11/site-packages (from ipython>=6.1.0->ipywidgets) (0.6.2)\n", "Requirement already satisfied: pexpect>4.3 in /opt/conda/lib/python3.11/site-packages (from ipython>=6.1.0->ipywidgets) (4.9.0)\n", "Requirement already satisfied: sympy in /opt/conda/lib/python3.11/site-packages (from torch>=1.10.0->accelerate) (1.12)\n", "Requirement already satisfied: networkx in /opt/conda/lib/python3.11/site-packages (from torch>=1.10.0->accelerate) (3.2.1)\n", "Requirement already satisfied: jinja2 in /opt/conda/lib/python3.11/site-packages (from torch>=1.10.0->accelerate) (3.1.3)\n", "Requirement already satisfied: nvidia-cuda-nvrtc-cu11==11.8.89 in /opt/conda/lib/python3.11/site-packages (from torch>=1.10.0->accelerate) (11.8.89)\n", "Requirement already satisfied: nvidia-cuda-runtime-cu11==11.8.89 in /opt/conda/lib/python3.11/site-packages (from torch>=1.10.0->accelerate) (11.8.89)\n", "Requirement already satisfied: nvidia-cuda-cupti-cu11==11.8.87 in /opt/conda/lib/python3.11/site-packages (from torch>=1.10.0->accelerate) (11.8.87)\n", "Requirement already satisfied: nvidia-cudnn-cu11==8.7.0.84 in /opt/conda/lib/python3.11/site-packages (from torch>=1.10.0->accelerate) (8.7.0.84)\n", "Requirement already satisfied: nvidia-cublas-cu11==11.11.3.6 in /opt/conda/lib/python3.11/site-packages (from torch>=1.10.0->accelerate) (11.11.3.6)\n", "Requirement already satisfied: nvidia-cufft-cu11==10.9.0.58 in /opt/conda/lib/python3.11/site-packages (from torch>=1.10.0->accelerate) (10.9.0.58)\n", "Requirement already satisfied: nvidia-curand-cu11==10.3.0.86 in /opt/conda/lib/python3.11/site-packages (from torch>=1.10.0->accelerate) (10.3.0.86)\n", "Requirement already satisfied: nvidia-cusolver-cu11==11.4.1.48 in /opt/conda/lib/python3.11/site-packages (from torch>=1.10.0->accelerate) (11.4.1.48)\n", "Requirement already satisfied: nvidia-cusparse-cu11==11.7.5.86 in /opt/conda/lib/python3.11/site-packages (from torch>=1.10.0->accelerate) (11.7.5.86)\n", "Requirement already satisfied: nvidia-nccl-cu11==2.20.5 in /opt/conda/lib/python3.11/site-packages (from torch>=1.10.0->accelerate) (2.20.5)\n", "Requirement already satisfied: nvidia-nvtx-cu11==11.8.86 in /opt/conda/lib/python3.11/site-packages (from torch>=1.10.0->accelerate) (11.8.86)\n", "Requirement already satisfied: triton==2.3.0 in /opt/conda/lib/python3.11/site-packages (from torch>=1.10.0->accelerate) (2.3.0)\n", "Requirement already satisfied: zipp>=0.5 in /opt/conda/lib/python3.11/site-packages (from importlib-metadata->diffusers) (3.17.0)\n", "Requirement already satisfied: charset-normalizer<4,>=2 in /opt/conda/lib/python3.11/site-packages (from requests->transformers) (3.3.2)\n", "Requirement already satisfied: idna<4,>=2.5 in /opt/conda/lib/python3.11/site-packages (from requests->transformers) (3.6)\n", "Requirement already satisfied: urllib3<3,>=1.21.1 in /opt/conda/lib/python3.11/site-packages (from requests->transformers) (2.2.0)\n", "Requirement already satisfied: certifi>=2017.4.17 in /opt/conda/lib/python3.11/site-packages (from requests->transformers) (2024.2.2)\n", "Requirement already satisfied: parso<0.9.0,>=0.8.3 in /opt/conda/lib/python3.11/site-packages (from jedi>=0.16->ipython>=6.1.0->ipywidgets) (0.8.3)\n", "Requirement already satisfied: ptyprocess>=0.5 in /opt/conda/lib/python3.11/site-packages (from pexpect>4.3->ipython>=6.1.0->ipywidgets) (0.7.0)\n", "Requirement already satisfied: wcwidth in /opt/conda/lib/python3.11/site-packages (from prompt-toolkit<3.1.0,>=3.0.41->ipython>=6.1.0->ipywidgets) (0.2.13)\n", "Requirement already satisfied: MarkupSafe>=2.0 in /opt/conda/lib/python3.11/site-packages (from jinja2->torch>=1.10.0->accelerate) (2.1.5)\n", "Requirement already satisfied: executing>=1.2.0 in /opt/conda/lib/python3.11/site-packages (from stack-data->ipython>=6.1.0->ipywidgets) (2.0.1)\n", "Requirement already satisfied: asttokens>=2.1.0 in /opt/conda/lib/python3.11/site-packages (from stack-data->ipython>=6.1.0->ipywidgets) (2.4.1)\n", "Requirement already satisfied: pure-eval in /opt/conda/lib/python3.11/site-packages (from stack-data->ipython>=6.1.0->ipywidgets) (0.2.2)\n", "Requirement already satisfied: mpmath>=0.19 in /opt/conda/lib/python3.11/site-packages (from sympy->torch>=1.10.0->accelerate) (1.3.0)\n", "Requirement already satisfied: six>=1.12.0 in /opt/conda/lib/python3.11/site-packages (from asttokens>=2.1.0->stack-data->ipython>=6.1.0->ipywidgets) (1.16.0)\n", "Downloading transformers-4.45.0-py3-none-any.whl (9.9 MB)\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m9.9/9.9 MB\u001b[0m \u001b[31m45.4 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m00:01\u001b[0m00:01\u001b[0m\n", "\u001b[?25hDownloading diffusers-0.30.3-py3-none-any.whl (2.7 MB)\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m2.7/2.7 MB\u001b[0m \u001b[31m44.9 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0ma \u001b[36m0:00:01\u001b[0m\n", "\u001b[?25hDownloading accelerate-0.34.2-py3-none-any.whl (324 kB)\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m324.4/324.4 kB\u001b[0m \u001b[31m31.0 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "\u001b[?25hDownloading huggingface_hub-0.25.1-py3-none-any.whl (436 kB)\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m436.4/436.4 kB\u001b[0m \u001b[31m37.6 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "\u001b[?25hDownloading regex-2024.9.11-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (792 kB)\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m792.8/792.8 kB\u001b[0m \u001b[31m41.9 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "\u001b[?25hDownloading safetensors-0.4.5-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (435 kB)\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m435.0/435.0 kB\u001b[0m \u001b[31m31.3 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "\u001b[?25hDownloading tokenizers-0.20.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.9 MB)\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m2.9/2.9 MB\u001b[0m \u001b[31m52.9 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m:00:01\u001b[0m\n", "\u001b[?25hInstalling collected packages: safetensors, regex, huggingface-hub, tokenizers, diffusers, accelerate, transformers\n", "Successfully installed accelerate-0.34.2 diffusers-0.30.3 huggingface-hub-0.25.1 regex-2024.9.11 safetensors-0.4.5 tokenizers-0.20.0 transformers-4.45.0\n" ] } ], "source": [ "!pip install transformers diffusers accelerate ipywidgets" ] }, { "cell_type": "code", "execution_count": 2, "id": "6814d202-9131-4170-a967-6527a504eff4", "metadata": { "tags": [] }, "outputs": [], "source": [ "import torch\n", "from diffusers import StableDiffusionPipeline\n", "from matplotlib import pyplot as plt\n", "import datetime" ] }, { "cell_type": "code", "execution_count": 3, "id": "9a48b8ed-6f07-4f27-95eb-7ec45c4a7d2f", "metadata": { "tags": [] }, "outputs": [], "source": [ "# diffusers docs: https://huggingface.co/docs/diffusers/api/pipelines/stable_diffusion/text2img#texttoimage-generation\n", "# model_id = \"dreamlike-art/dreamlike-diffusion-1.0\"\n", "# model_id = \"prompthero/openjourney\"\n", "# model_id = \"XpucT/Deliberate\"\n", "model_id = \"dreamlike-art/dreamlike-photoreal-2.0\"" ] }, { "cell_type": "code", "execution_count": 4, "id": "895b7852-6e82-4d74-bada-4e5b92ef90f4", "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "b635499e010a40d595430657db538a65", "version_major": 2, "version_minor": 0 }, "text/plain": [ "Loading pipeline components...: 0%| | 0/5 [00:00" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "import numpy as np\n", "import ipywidgets as widgets\n", "from IPython.display import clear_output, display\n", "\n", "import matplotlib.pyplot as plt\n", "from IPython.display import set_matplotlib_formats\n", "%matplotlib inline\n", "#set_matplotlib_formats('svg')\n", "\n", "np.random.seed(42)\n", "\n", "#prompt = \"Renaissance-style portrait of an astronaut in space, detailed starry background, reflective helmet, matte painting, hyperdetailed, CGSociety\"\n", "#negative_prompt = \"gold\"\n", "#textarea\n", "std_layout = widgets.Layout(width=\"auto\")\n", "\n", "prompt_box = widgets.Textarea(value='', placeholder='cats, sharks, ships, underwater, cinematic composition', description='', layout=widgets.Layout(height=\"50px\", width=\"auto\"))\n", "neg_prompt_box = widgets.Textarea(value='', placeholder='negative prompt', description='', layout=widgets.Layout(height=\"30px\", width=\"auto\"))\n", "#slider \n", "img2gen_lab = widgets.Label(value=\"Images to Generate:\", layout=std_layout)\n", "img2gen_var = widgets.IntSlider(value=1, min=1, max=10, layout=std_layout)\n", "#input\n", "size_x_lab = widgets.Label(value=\"Image Width:\", layout=std_layout)\n", "size_x_var = widgets.BoundedIntText(value=32, min=32, max=512, step=8, layout=std_layout)\n", "size_y_lab = widgets.Label(value=\"Image heigth:\", layout=std_layout)\n", "size_y_var = widgets.BoundedIntText(value=32, min=32, max=512, step=8, layout=std_layout)\n", "steps_lab = widgets.Label(value=\"Steps:\", layout=std_layout)\n", "steps_var = widgets.BoundedIntText(value=50, min=11, max=100, step=1, layout=std_layout)\n", "#Button\n", "btn = widgets.Button(description=\"Generate\", tooltip=\"Click me\",\n", " layout=widgets.Layout(width=\"auto\", height=\"30px\", margin=\"30px\"))\n", "#Canvas\n", "fig = plt.figure(figsize=(128/80, 128/80))\n", "box = widgets.VBox([prompt_box, neg_prompt_box, img2gen_lab, img2gen_var, steps_lab, steps_var, size_x_lab, size_x_var,size_y_lab, size_y_var, btn])\n", "box2 = widgets.VBox([prompt_box, neg_prompt_box, img2gen_lab, img2gen_var, steps_lab, steps_var, size_x_lab, size_x_var,size_y_lab, size_y_var, btn])\n", "\n", "box2" ] }, { "cell_type": "code", "execution_count": 6, "id": "71055e73-a072-41d7-84c6-602fa69ff46f", "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "4ada6ef0af1647a094013dbbc578e8ec", "version_major": 2, "version_minor": 0 }, "text/plain": [ "Output()" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "out = widgets.Output()\n", "out" ] }, { "cell_type": "code", "execution_count": 7, "id": "d2645fdf-4bed-43f1-b3d1-f06c174d45ad", "metadata": {}, "outputs": [], "source": [ "def on_value_change(change):\n", " prompt = prompt_box.value\n", " negative_prompt = neg_prompt_box.value\n", " images_to_generate = int(img2gen_var.value)\n", " steps = steps_var.value\n", " width = size_x_var.value\n", " height = size_y_var.value\n", " seed = None\n", " guidance = 7.5 # Higher guidance scale encourages to generate images that are closely linked to the text prompt, usually at the expense of lower image quality.\n", " with out:\n", " clear_output(wait=True)\n", " for _ in range(images_to_generate):\n", " steps = int(steps)\n", " width = int(width)\n", " height = int(height)\n", " current_seed = seed or torch.randint(0, int(1e5), size=(1, 1))[0].item()\n", " generator = torch.Generator().manual_seed(int(current_seed))\n", " img = pipe(prompt=prompt, negative_prompt=negative_prompt, width=width, height=height, num_inference_steps=steps, guidance_scale=guidance, generator=generator).images[0]\n", " time_now = datetime.datetime.now().strftime(\"%y.%m.%d_%H:%M:%S\")\n", " plt.imshow(img)\n", " plt.axis(\"off\")\n", " print(\"Current Seed:\", current_seed)\n", " plt.title(\"\")\n", " plt.show()\n", " # img.save(f\"./{time_now}_{current_seed}.jpg\")\n", "\n", "btn.on_click(on_value_change)\n", "\n" ] }, { "cell_type": "code", "execution_count": null, "id": "801c01c7-d364-411a-ba10-25161e47f65f", "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.7" } }, "nbformat": 4, "nbformat_minor": 5 }