Skip to content

Commit

Permalink
feature: use chatgpt to easily create imaginairy prompt.
Browse files Browse the repository at this point in the history
  • Loading branch information
jaydrennan authored and jaydrennan committed Oct 28, 2023
1 parent 6cd519c commit 64a81b5
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 1 deletion.
5 changes: 5 additions & 0 deletions imaginairy/bin/imaginai
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/env python
import imaginairy.cli.imaginai

if __name__ == "__main__":
imaginairy.cli.imaginai.imaginai_cmd()
96 changes: 96 additions & 0 deletions imaginairy/cli/imaginai.py
Original file line number Diff line number Diff line change
@@ -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()
2 changes: 2 additions & 0 deletions imaginairy/cli/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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")
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down

0 comments on commit 64a81b5

Please sign in to comment.