{ "nbformat": 4, "nbformat_minor": 0, "metadata": { "colab": { "provenance": [] }, "kernelspec": { "name": "python3", "display_name": "Python 3" }, "language_info": { "name": "python" }, "accelerator": "GPU", "gpuClass": "standard" }, "cells": [ { "cell_type": "markdown", "source": [ "# Introduction to Stable Diffusion and Google Colab Workshop for Capricon 2023 \n", "Jim Plaxco, www.artsnova.com\n", "\n", "\n", "---\n", "\n", "#SECTION 1: OPENAI GPT-3 and DALL-E\n", "\n", "---\n" ], "metadata": { "id": "KeQDzLbLmhET" } }, { "cell_type": "markdown", "source": [ "**Step 1**: Make sure that the runtime type is GPU \n", " ( Runtime -> Change Runtime type -> Hardware Accelerator = GPU )\n", " Click Save after selecting GPU\n", "Verify GPU availability\n", "\n", "Documentation for the nvidia command is at https://developer.nvidia.com/nvidia-system-management-interface\n", "\n" ], "metadata": { "id": "_W0Ai6zdKTwU" } }, { "cell_type": "code", "source": [ "!nvidia-smi" ], "metadata": { "id": "mUNxBmgYnOQN" }, "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "source": [ "**Step 2**: Install the openai library" ], "metadata": { "id": "dSDiDjCOnUE8" } }, { "cell_type": "code", "source": [ "!pip install openai" ], "metadata": { "id": "uerbDU33Uc60" }, "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "source": [ "**Step 3**: Import the getpass library and use it to enter your private openai API key. Using getpass() your key will not be written to the screen." ], "metadata": { "id": "EpIYEM-ZKaTo" } }, { "cell_type": "code", "source": [ "import getpass\n", "mykey = getpass.getpass(\"Enter your openAI API Key: \")\n", "#print(password)" ], "metadata": { "id": "WC6Z7QNi08kX", "colab": { "base_uri": "https://localhost:8080/" }, "outputId": "eaab41d3-6b20-4fb5-8ab9-a3ba2f8e043f" }, "execution_count": null, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Enter your openAI API Key: ··········\n" ] } ] }, { "cell_type": "markdown", "source": [ "**Step 4**: import the libraries you will use and set the openai api key\n", "\n", "This only needs to be done once per session." ], "metadata": { "id": "HeNc6C2WSNyE" } }, { "cell_type": "code", "source": [ "import openai\n", "import random\n", "openai.api_key = mykey" ], "metadata": { "id": "udO1YrpPSYOk" }, "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "source": [ "**Step 5**: Use GPT-3 model to generate text responses.\n", "\n", "The number of responses to generate is controlled by the variable gty.\n", "\n", "The temperature for each response is set to a random value based on range input. 0 is for well defined answers and 1 is for more creative answers" ], "metadata": { "id": "EJ-Z_fO7LM0R" } }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "IcCYpJwmTcID" }, "outputs": [], "source": [ "# use the GPT-3 model to generate text \n", "engineToUse= \"text-davinci-002\"\n", "qty = 2 #@param {type:\"number\"}\n", "minTemp=0.44 #@param {type:\"slider\", min:0, max:1, step:0.01}\n", "maxTemp=0.53 #@param {type:\"slider\", min:0, max:1, step:0.01}\n", "thePrompt = \"Tell me about artificial intelligence as depicted in science fiction.\" #@param {type:\"string\"}\n", "\n", "for x in range(qty):\n", " thetemp = random.uniform(minTemp,maxTemp)\n", " completions = openai.Completion.create(engine=engineToUse, prompt=thePrompt, max_tokens=3048,n=1,stop=None,temperature=thetemp,)\n", " theResponse = completions.choices[0].text # \n", " print(\"\\n\",thetemp, theResponse, \"\\n-----------------------------------------------------\")\n", "\n", "print(\"*** DONE ***\") " ] }, { "cell_type": "markdown", "source": [ "**Step5B**: Or you can submit a list of prompts" ], "metadata": { "id": "uhbxg3pGpBHD" } }, { "cell_type": "code", "source": [ "# import openai only need to do this once\n", "# openai.api_key = mykey # only need to do this once\n", "# openai.api_key = \"key string\"\n", "# use the GPT-3 model to generate responses\n", "promptlist = [f\"make up a name for a high tech product\", f\"Write a limerick about science fiction\", f\"what is CSS?\"]\n", "qty=2 # this is the number of responses to generate for each prompt\n", "for x in promptlist:\n", " print(\"Response to the prompt: \", x)\n", " for i in range(qty):\n", " completions = openai.Completion.create(engine=\"text-davinci-002\",prompt=x,max_tokens=3024,n=1,stop=None,temperature=0.5,)\n", " message = completions.choices[0].text\n", " print(message)\n", " print(\"----------------------------------\")\n", "print(\"*** END ***\") " ], "metadata": { "id": "wPJeVXS1iCTN" }, "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "source": [ "# **Use Dall-E to Generate Images**\n", "\n", "Generate a single image based on the Prompt" ], "metadata": { "id": "jWBv4ltYq77h" } }, { "cell_type": "code", "source": [ "# NOTE: Image objects are tied to the session and are deleted when the session is disconnected\n", "# available sizes are is not one of ['256x256', '512x512', '1024x1024']\n", "# Parameters for openai.Image.create()\n", "# n: integer, Optional, Defaults to 1, The number of images to generate. Must be between 1 and 10.\n", "# size: string, Optional, Defaults to 1024x1024, The size of the generated images. Must be one of 256x256, 512x512, or 1024x1024.\n", "# See https://beta.openai.com/docs/api-reference/images\n", "#\n", "dallePrompt = \"An image of an artificial intelligence as depicted in science fiction.\" #@param {type:\"string\"}\n", "response = openai.Image.create(\n", " prompt = dallePrompt,\n", " n = 1,\n", " size = \"512x512\"\n", ")\n", "image_url = response['data'][0]['url'] # response is in the form of \n", "print(image_url)" ], "metadata": { "id": "2xmzqMmOmRCJ", "colab": { "base_uri": "https://localhost:8080/" }, "outputId": "5c3bcf38-f525-44eb-fa13-cd66929538c2" }, "execution_count": null, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "https://oaidalleapiprodscus.blob.core.windows.net/private/org-7bpCzjBfiJRYpOW55skcuhEx/user-dU4knlZxj16rUFWZAJqCQxMR/img-WPvM2tcAisLVxG1kdbNPzUui.png?st=2023-01-30T20%3A58%3A20Z&se=2023-01-30T22%3A58%3A20Z&sp=r&sv=2021-08-06&sr=b&rscd=inline&rsct=image/png&skoid=6aaadede-4fb3-4698-a8f6-684d7786b067&sktid=a48cca56-e6da-484e-a814-9c849652bcb3&skt=2023-01-30T13%3A24%3A06Z&ske=2023-01-31T13%3A24%3A06Z&sks=b&skv=2021-08-06&sig=QK2LeKCp2TiMEBjwyqrxDDZUZIbReCpwumjhsah18eI%3D\n" ] } ] }, { "cell_type": "markdown", "source": [ "# **Use Dall-E to Generate Images**\n", "\n", "Generate multiple images based on the Prompt\n" ], "metadata": { "id": "ufLH0CQ8s13l" } }, { "cell_type": "code", "source": [ "# NOTE: Image objects are tied to the session and are deleted when the session is disconnected\n", "# available sizes are is not one of ['256x256', '512x512', '1024x1024']\n", "# Parameters for openai.Image.create()\n", "# n: integer, Optional, Defaults to 1, The number of images to generate. Must be between 1 and 10.\n", "# size: string, Optional, Defaults to 1024x1024, The size of the generated images. Must be one of 256x256, 512x512, or 1024x1024.\n", "# See https://beta.openai.com/docs/api-reference/images\n", "\n", "dallePrompt = \"What is your prompt?\" #@param {type:\"string\"}\n", "numberOfImages = 2 #@param {type:\"number\"}\n", "\n", "# response variable is an openai.openai_object.OpenAIObject\n", "response = openai.Image.create(\n", " prompt = dallePrompt,\n", " n = numberOfImages,\n", " size = \"1024x1024\"\n", ")\n", "# get the url of where the generated image is and print it out\n", "for ndx in range(numberOfImages):\n", " image_url = response['data'][ndx]['url']\n", " print(\"Image {} URL: {}\\n\".format(ndx,image_url))\n", "\n", "print(\"*** DONE ***\") " ], "metadata": { "id": "1frAGY6_mb7K" }, "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "source": [ "# **The End**" ], "metadata": { "id": "sHzLWHGWzPwC" } } ] }