{ "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 OpenAI GPT-3 and DALL-E Using Google Colab Workshop \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" }, "execution_count": null, "outputs": [] }, { "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 Qty.\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.25 #@param {type:\"slider\", min:0, max:1, step:0.01}\n", "maxTemp=0.78 #@param {type:\"slider\", min:0, max:1, step:0.01}\n", "thePrompt = \"write a limerick about 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", "VERSION 1: Generate a single image based on the Prompt\n", "\n", "The output will be a link to where the image file is stored" ], "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 = \"A susrral painting 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" }, "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "source": [ "# **Use Dall-E to Generate Images**\n", "\n", "VERSION 2: Generate multiple images based on the Prompt while also specifying the number of images to create.\n", "\n", "The output will be a link to where the image file is stored\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 = \"chicago skyline photograph with cats and a giant parakeet\" #@param {type:\"string\"}\n", "numberOfImages = 3 #@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" } } ] }