From 64a81b50eaae48d5a931dad92da358f31820d667 Mon Sep 17 00:00:00 2001 From: jaydrennan Date: Sat, 28 Oct 2023 10:02:28 -0700 Subject: [PATCH] feature: use chatgpt to easily create imaginairy prompt. --- imaginairy/bin/imaginai | 5 ++ imaginairy/cli/imaginai.py | 96 ++++++++++++++++++++++++++++++++++++++ imaginairy/cli/main.py | 2 + setup.py | 2 +- 4 files changed, 104 insertions(+), 1 deletion(-) create mode 100644 imaginairy/bin/imaginai create mode 100644 imaginairy/cli/imaginai.py diff --git a/imaginairy/bin/imaginai b/imaginairy/bin/imaginai new file mode 100644 index 00000000..e925c796 --- /dev/null +++ b/imaginairy/bin/imaginai @@ -0,0 +1,5 @@ +#!/bin/env python +import imaginairy.cli.imaginai + +if __name__ == "__main__": + imaginairy.cli.imaginai.imaginai_cmd() \ No newline at end of file diff --git a/imaginairy/cli/imaginai.py b/imaginairy/cli/imaginai.py new file mode 100644 index 00000000..3933c2a0 --- /dev/null +++ b/imaginairy/cli/imaginai.py @@ -0,0 +1,96 @@ +import json +import logging +import os +import subprocess + +import click +import openai + +logger = logging.getLogger(__name__) + +openai.api_key = os.environ.get("OPENAI_API_KEY") + +system_prompt = """ +# ChatGPT+ ImaginAIry System Prompt + +You are ChatGPT, a large language model trained by OpenAI. +Knowledge cutoff: 2022-01 +Current date: 2023-10-26 + +## Tools +### imaginAIry + +You'll be given a description of an image, use `imaginAIry` (a python package used to generate images with stable diffusion) to create the image. Your response should be returned as json of the prompt and terminal command, NOTHING ELSE. "prompt" and "command" MUST be the json keys. Summarize the prompt used to generate the image in plain text and create the correct ImaginAIry terminal command following these guidelines: + +- **Image Type**: Always mention the image type (e.g. photo, oil painting, watercolor painting, illustration, cartoon, drawing, vector, render) at the beginning of the caption. Default to making at least 1--2 of the 4 images as photos unless otherwise specified. + +- **Description**: The prompt must intricately describe every part of the image in concrete, objective detail. THINK about the end goal of the description and extrapolate that to what would produce satisfying images. If the USER prompt is too brief, generate more, DO NOT ASK THE USER FOR MORE. + +- **Length**: All descriptions sent to `imaginAIry` should be descriptive and detailed, but spanning less than 3 sentences. + +--- + +To generate an image using `imaginAIry`, follow the structure: + +```bash +aimg imagine [OPTIONS] [PROMPT_TEXT] +``` + +Here's a basic example given your description: + +```bash +aimg imagine --model-weights-path SD-1.5 --prompt-strength 7.5 --outdir ./outputs --output-file-extension jpg "Image Description Here" +``` + +### Options +The options allow fine-tuning of the image generation process. Here are some commonly used options: + +- `--negative-prompt TEXT`: Exclude certain elements from the generated images. +- `--prompt-strength FLOAT`: Strength of the prompt adherence. Default: 7.5 +- `--init-image PATH|URL`: Provide a starting image. +- `--outdir PATH`: Specify the output directory. Default: `./outputs` +- `--output-file-extension`: Specify the image format (jpg/png). Default: jpg +- `--height INTEGER` & `--width INTEGER`: Define the dimensions of the image. They should be multiples of 8. +- `--steps INTEGER`: Number of diffusion steps. More steps usually yield more detail. +- `--seed INTEGER`: Seed for randomness, enabling reproducible image renders. +- `--model-weights-path, --model TEXT`: Choose a specific model for image generation. Default: SD-1.5 + +For a comprehensive list of options, refer to the `--help` command from `imaginAIry`. + +---""" + + +@click.command(name="imaginai") +@click.argument("prompt", nargs=-1) +# @click.pass_context +def imaginai_cmd(prompt): + """ + Use chatgpt-3.5 to assist with Imaginairy prompts. + """ + prompt = prompt[0] + click.echo("ai generating prompt") + completion = openai.ChatCompletion.create( + model="gpt-3.5-turbo", + messages=[ + {"role": "system", "content": system_prompt}, + {"role": "user", "content": prompt}, + ], + temperature=0, + ) + # Extract the response and return + message = json.loads(completion.choices[0].message["content"]) + command = message["command"] + + click.echo("generated command: " + command) + + # command = command.split() + # print(command) + # print(type(command)) + if command[:4] == "aimg": + subprocess.run(command, shell=True, text=True) + else: + click.echo("invalid command") + + +if __name__ == "__main__": + imaginai_cmd() diff --git a/imaginairy/cli/main.py b/imaginairy/cli/main.py index 840e5baf..07ad42dd 100644 --- a/imaginairy/cli/main.py +++ b/imaginairy/cli/main.py @@ -7,6 +7,7 @@ from imaginairy.cli.describe import describe_cmd from imaginairy.cli.edit import edit_cmd from imaginairy.cli.edit_demo import edit_demo_cmd +from imaginairy.cli.imaginai import imaginai_cmd from imaginairy.cli.imagine import imagine_cmd from imaginairy.cli.run_api import run_server_cmd from imaginairy.cli.train import prep_images_cmd, prune_ckpt_cmd, train_concept_cmd @@ -46,6 +47,7 @@ def aimg(ctx): aimg.add_command(edit_demo_cmd, name="edit-demo") aimg.add_command(imagine_cmd, name="imagine") aimg.add_command(prep_images_cmd, name="prep-images") +aimg.add_command(imaginai_cmd, name="imaginai") aimg.add_command(prune_ckpt_cmd, name="prune-ckpt") aimg.add_command(train_concept_cmd, name="train-concept") aimg.add_command(upscale_cmd, name="upscale") diff --git a/setup.py b/setup.py index 3f2591a1..e6e3ba2a 100644 --- a/setup.py +++ b/setup.py @@ -15,7 +15,7 @@ ], } else: - scripts = ["imaginairy/bin/aimg", "imaginairy/bin/imagine"] + scripts = ["imaginairy/bin/aimg", "imaginairy/bin/imagine", "imaginairy/bin/imaginai"] entry_points = None