stable-diffusion/text-to-image.ipynb

125 lines
2.0 MiB
Plaintext
Raw Normal View History

2023-11-08 17:50:10 +00:00
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Text-to-Image\n",
"\n",
"Using SDXL 1.0 base model only."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Load Model"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"c:\\Users\\micro\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages\\tqdm\\auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html\n",
" from .autonotebook import tqdm as notebook_tqdm\n"
]
}
],
"source": [
"import torch\n",
"\n",
"from diffusers import DiffusionPipeline, StableDiffusionXLPipeline\n",
"from IPython.display import display"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"Loading pipeline components...: 100%|██████████| 7/7 [00:00<00:00, 9.05it/s]\n"
]
}
],
"source": [
"pipe = StableDiffusionXLPipeline.from_pretrained(\n",
" 'stabilityai/stable-diffusion-xl-base-1.0', \n",
" torch_dtype=torch.float16, \n",
" use_safetensors=True, \n",
" variant='fp16')\n",
"pipe.to('cuda')\n",
"\n",
"def generate_image(prompt: str):\n",
" return pipe(prompt=prompt, num_inference_steps=20).images[0]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Generate Image"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"100%|██████████| 20/20 [00:06<00:00, 2.94it/s]\n"
]
},
{
"data": {
"image/jpeg": "/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAgGBgcGBQgHBwcJCQgKDBQNDAsLDBkSEw8UHRofHh0aHBwgJC4nICIsIxwcKDcpLDAxNDQ0Hyc5PTgyP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5i/OuNHzXeNpajqe0RI6+zpIT1j4N6eQEdWlr3ltgXempafkOQA34D67Urzz9XcxOfuBpc4hzfVXDzLLcMEozn7MHCqNK8lNWoviCOMjhmQku9HPOapRMrS65Zm7Z8mJlry4o7citjmoFdqoqf3v7CjyRQKUWkvc+rJjy9zOPEKmcSg1AbYi+Lc9WltCaTdcFRKPqKEeTGodUx0M7xevcDwEH397+8PXlj+6w1IiXzlTXHJ637z99ffvpyylgfMQNKpnu5rnbeJJMTT8vWYSo7Q9f3v7UYknrafSOgURq8C/fXv/wq/3+Hv2OnBbGWyWI7HcCp9VrZbr8fxaHJEn2+F9qLVWGZA6n9BuZ5TlbZ5FysGv3lx9e/vxpXCG/wKCbrrzWWQhlI/1/+PL9p/rVbP0hfjq3W7a
"text/plain": [
"<PIL.Image.Image image mode=RGB size=1024x1024>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"image = generate_image('cthulhu, paper craft art, minimal detail')\n",
"display(image)\n",
"# image.save('image.png')"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"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.5"
}
},
"nbformat": 4,
"nbformat_minor": 2
}