-
Notifications
You must be signed in to change notification settings - Fork 445
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature: use chatgpt to easily create imaginairy prompt.
- Loading branch information
jaydrennan
authored and
jaydrennan
committed
Oct 28, 2023
1 parent
6cd519c
commit 64a81b5
Showing
4 changed files
with
104 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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
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() |
This file contains 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
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() |
This file contains 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
This file contains 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