Skip to content

Instantly share code, notes, and snippets.

@karim23657
Created September 14, 2022 12:15
Show Gist options
  • Save karim23657/3fd3b75b7679cb2b734b04f9c14e5273 to your computer and use it in GitHub Desktop.
Save karim23657/3fd3b75b7679cb2b734b04f9c14e5273 to your computer and use it in GitHub Desktop.
A simpel demo of Stable Diffusion on colab gpu:
Display the source blob
Display the rendered blob
Raw
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"provenance": [],
"include_colab_link": true
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"language_info": {
"name": "python"
},
"accelerator": "GPU",
"gpuClass": "standard"
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"<a href=\"https://colab.research.google.com/gist/karim23657/9296048c437f2abeda4c5138fe23c053/stable-diffusion-on-colab.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "J8BCzk8jw-sk"
},
"outputs": [],
"source": [
"!wget https://pixeldrain.com/api/file/2AeUBpSH?download -O \"stable-diffusion-v1-4.zip\"\n",
"# https://tg.jmdkh.eu.org/2:/stable-diffusion-v1-4.zip\n",
"!unzip -qq \"stable-diffusion-v1-4.zip\""
]
},
{
"cell_type": "code",
"source": [
"%pip install --quiet --upgrade diffusers transformers scipy mediapy ftfy"
],
"metadata": {
"id": "3jKyes4m1wBZ"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"import mediapy as media\n",
"import torch\n",
"from torch import autocast\n",
"from diffusers import StableDiffusionPipeline\n",
"\n",
"model_id = \"/content/content/model\"\n",
"device = \"cuda\"\n",
"\n",
"\n",
"pipe = StableDiffusionPipeline.from_pretrained(model_id, torch_dtype=torch.float16, revision=\"fp16\")\n",
"pipe = pipe.to(device)\n",
"def dummy(images, **kwargs): return images, False \n",
"pipe.safety_checker = dummy\n"
],
"metadata": {
"id": "Rd-l1zbU1znx"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"#@title generate\n",
"prompt = \"Macdonald in Edo Period Japan\" #@param {type:\"string\"}\n",
"num_images = 2 #@param {type:\"slider\", min:1, max:10, step:1}\n",
"height = 512 #@param {type:\"slider\", min:256, max:1024, step:64}\n",
"width = 512 #@param {type:\"slider\", min:256, max:1024, step:64}\n",
"num_inference_steps=50 #@param {type:\"slider\", min:1, max:200, step:1}\n",
"guidance_scale=7.5 #@param {type:\"slider\", min:1, max:30, step:0.1}\n",
"prompts = [ prompt ] * num_images\n",
"with autocast(\"cuda\"):\n",
" images = pipe(prompts, guidance_scale=guidance_scale, num_inference_steps=num_inference_steps,\n",
" height=height,width=width\n",
" )[\"sample\"] \n",
" \n",
"media.show_images(images)\n",
"images[0].save(\"output.jpg\")"
],
"metadata": {
"cellView": "form",
"id": "5TuYxXYX2ESg"
},
"execution_count": null,
"outputs": []
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment