Last active
December 10, 2023 22:15
-
-
Save devinschumacher/ff0dade572672607e9a6e740167b9873 to your computer and use it in GitHub Desktop.
create-ai-generated-images-in-bulk-via-csv-dale-3-api.ipynb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| { | |
| "nbformat": 4, | |
| "nbformat_minor": 0, | |
| "metadata": { | |
| "colab": { | |
| "provenance": [], | |
| "collapsed_sections": [ | |
| "3YvqDk2txsSo" | |
| ], | |
| "authorship_tag": "ABX9TyMV4AlCyLx9MCzRoJbpk1qG", | |
| "include_colab_link": true | |
| }, | |
| "kernelspec": { | |
| "name": "python3", | |
| "display_name": "Python 3" | |
| }, | |
| "language_info": { | |
| "name": "python" | |
| } | |
| }, | |
| "cells": [ | |
| { | |
| "cell_type": "markdown", | |
| "metadata": { | |
| "id": "view-in-github", | |
| "colab_type": "text" | |
| }, | |
| "source": [ | |
| "<a href=\"https://colab.research.google.com/gist/devinschumacher/ff0dade572672607e9a6e740167b9873/create-ai-generated-images-in-bulk-via-csv-dale-3-api.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "source": [ | |
| "# Instructions\n", | |
| "\n", | |
| "\n", | |
| "\n", | |
| "1. File > Save a copy in Drive\n", | |
| "2. Upload a CSV with 3 headers: `prompt,num_images,keyword` (name it input.csv)\n", | |
| "3. Add your API_KEY (from openAI) below in the code\n", | |
| "4. In the menu click `Runtime > Run all` & wait\n", | |
| "5. Look in the `generated_images` folder to download one by one\n", | |
| "6. Refresh the side area to download the .zip\n", | |
| "\n", | |
| "---\n", | |
| "\n", | |
| "\n", | |
| "📺 Video Tutorial: https://youtu.be/1nyDvfCoqLs\n", | |
| "\n", | |
| "\n", | |
| "---\n", | |
| "\n", | |
| "## Questions?\n", | |
| "\n", | |
| "- [Join Discord](https://serp.ly/@serp/discord)\n", | |
| "- [SERP](https://serp.co)\n", | |
| "- [SERP AI](https://serp.ai)\n", | |
| "- [DevinSchumacher.com](https://devinschumacher.com)\n", | |
| "- [YouTube Tutorials](https://serp.ly/@devin/youtube)" | |
| ], | |
| "metadata": { | |
| "id": "FnQ1C8DfiKKk" | |
| } | |
| }, | |
| { | |
| "cell_type": "code", | |
| "source": [ | |
| "# Add your OpenAI API key here\n", | |
| "\n", | |
| "API_KEY = \"sk-xxx\"\n" | |
| ], | |
| "metadata": { | |
| "id": "IDC6JciUi3EO" | |
| }, | |
| "execution_count": null, | |
| "outputs": [] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "source": [ | |
| "# Code\n", | |
| "\n", | |
| "👇 Click this to \"Run all\"" | |
| ], | |
| "metadata": { | |
| "id": "3YvqDk2txsSo" | |
| } | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": null, | |
| "metadata": { | |
| "id": "FBPr2NGUh4om" | |
| }, | |
| "outputs": [], | |
| "source": [ | |
| "# CSV\n", | |
| "\n", | |
| "csv_file_path = \"/content/input.csv\"\n", | |
| "\n", | |
| "!pip install openai tiktoken cohere\n", | |
| "\n", | |
| "import csv\n", | |
| "from openai import OpenAI\n", | |
| "import requests\n", | |
| "from IPython.display import display, Image\n", | |
| "from io import BytesIO\n", | |
| "import os\n", | |
| "\n", | |
| "\n", | |
| "def read_prompts_from_csv(file_path):\n", | |
| " prompts = []\n", | |
| " with open(file_path, newline=\"\", encoding=\"utf-8\") as csvfile:\n", | |
| " reader = csv.DictReader(csvfile)\n", | |
| " for row in reader:\n", | |
| " prompts.append(\n", | |
| " {\n", | |
| " \"prompt\": row[\"prompt\"],\n", | |
| " \"num_images\": int(row[\"num_images\"]),\n", | |
| " \"keyword\": row[\"keyword\"],\n", | |
| " }\n", | |
| " )\n", | |
| " return prompts\n", | |
| "\n", | |
| "\n", | |
| "def generate_dalle_images(prompts, save_path):\n", | |
| " headers = {\"Authorization\": f\"Bearer {API_KEY}\"}\n", | |
| "\n", | |
| " # Create the directory if it does not exist\n", | |
| " if not os.path.exists(save_path):\n", | |
| " os.makedirs(save_path)\n", | |
| "\n", | |
| " for prompt_details in prompts:\n", | |
| " prompt = prompt_details[\"prompt\"]\n", | |
| " num_images = prompt_details[\"num_images\"]\n", | |
| " keyword = prompt_details[\"keyword\"]\n", | |
| "\n", | |
| " for i in range(num_images):\n", | |
| " data = {\n", | |
| " \"model\": \"dall-e-3\",\n", | |
| " \"prompt\": prompt,\n", | |
| " \"n\": 1,\n", | |
| " \"size\": \"1792x1024\",\n", | |
| " \"quality\": \"hd\",\n", | |
| " }\n", | |
| "\n", | |
| " response = requests.post(\n", | |
| " \"https://api.openai.com/v1/images/generations\",\n", | |
| " headers=headers,\n", | |
| " json=data,\n", | |
| " )\n", | |
| "\n", | |
| " if response.status_code == 200:\n", | |
| " images = response.json()[\"data\"]\n", | |
| " for img in images:\n", | |
| " image_response = requests.get(img[\"url\"])\n", | |
| " if image_response.status_code == 200:\n", | |
| " image = Image(BytesIO(image_response.content))\n", | |
| " display(image)\n", | |
| "\n", | |
| " # Save the image with a formatted name\n", | |
| " image_file_name = f\"{keyword.replace(' ', '_')}_{i+1:02d}.png\"\n", | |
| " image_file_path = os.path.join(save_path, image_file_name)\n", | |
| " with open(image_file_path, \"wb\") as f:\n", | |
| " f.write(image_response.content)\n", | |
| " print(f\"Image saved to {image_file_path}\")\n", | |
| " else:\n", | |
| " print(\"Error loading image:\", image_response.status_code)\n", | |
| " else:\n", | |
| " print(\"Error:\", response.status_code, response.text)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "source": [ | |
| "# Example usage with CSV\n", | |
| "\n", | |
| "prompts_from_csv = read_prompts_from_csv(csv_file_path)\n", | |
| "\n", | |
| "generate_dalle_images(prompts_from_csv, \"./generated_images\")\n", | |
| "\n", | |
| "!zip -r /content/generated_images.zip /content/generated_images/" | |
| ], | |
| "metadata": { | |
| "id": "e6jgbef-jzp5" | |
| }, | |
| "execution_count": null, | |
| "outputs": [] | |
| } | |
| ] | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment