Skip to content

Commit f039adc

Browse files
add example nodejs and image generation example (#108)
Signed-off-by: Bill Maxwell <[email protected]>
1 parent c1d4989 commit f039adc

File tree

10 files changed

+1706
-0
lines changed

10 files changed

+1706
-0
lines changed

examples/nodejs-imagegen/README.md

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# AI Logo Design (Nodejs image generation example)
2+
3+
## Overview
4+
5+
This nodejs app uses the gptscript node module and the image generation tool (leverages DALL-E) to create a simple AI logo design site. The app uses an existing three AI generated artists, or can create an additional set of three artists, to render a logo based on the description provided by the user. The resulting logo images are then presented to the user.
6+
7+
## Features
8+
9+
- Three built in AI artists.
10+
- Ability to generate three additional AI artists.
11+
- Provide a description of the logo to be generated.
12+
- View three rendered logo options.
13+
14+
## Installation
15+
16+
1. Clone the repository.
17+
18+
```bash
19+
git clone https://github.com/gptscript-ai/gptscript.git
20+
```
21+
22+
1. Navigate to the 'examples/nodejs-imagegen' directory and install dependencies.
23+
24+
Python:
25+
26+
*note: You can use a virtual environment to install the python dependencies, just make sure it is activated before running the server.*
27+
28+
```bash
29+
pip install -r requirements.txt
30+
```
31+
32+
this will install the python dependencies for the image generation tool.
33+
34+
Node:
35+
36+
```bash
37+
npm install
38+
```
39+
40+
This is for the application itself.
41+
42+
1. Setup OPENAI_API_KEY environment variable.
43+
44+
```bash
45+
export OPENAI_API_KEY=your-api-key
46+
```
47+
48+
1. Run the application.
49+
50+
```bash
51+
npm run dev
52+
```
53+
54+
## Usage
55+
56+
Once the application is started, open <http://localhost:3000> in your browser. You will be presented with a form to enter a description of the logo you would like to generate. After submitting the form, you will be presented with three logo options.
57+
58+
The backend models take a bit of time to render the images, so it might seem like the application is hanging. This is normal, and the images will be presented once they are ready.
59+
60+
## Diving in
61+
62+
The application is a simple nodejs app that uses the gptscript node module to interact with the image generation tool. The gptscript module is just running the CLI under the hood, and will pick up the standard configuration environment variables, the most important of which is the OPENAI_API_KEY.
63+
64+
Sometimes the OpenAI LLM model will return an error, which can be seen in the terminal running the server app. Sometimes, the model will return a response that doesn't quite match the expected output format and throw an error.

examples/nodejs-imagegen/artists.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"artists": [
3+
{
4+
"name": "Elena Vasquez",
5+
"description": "Elena Vasquez, hailing from the vibrant streets of Buenos Aires, Argentina, is a graphic artist whose work is deeply influenced by the rich cultural heritage and bustling urban life of her homeland. Her art is a vivid tapestry of colors, blending traditional South American motifs with modern digital techniques. Elena's muse is the dynamic interplay of tradition and innovation, which she believes mirrors the evolving identity of Latin America. Her pieces often feature bold, geometric patterns interwoven with imagery from folklore and street art, creating a unique visual language that speaks to the heart of contemporary Latin American society."
6+
},
7+
{
8+
"name": "Taro Yamamoto",
9+
"description": "Taro Yamamoto, a Tokyo-based graphic artist, draws inspiration from the minimalist aesthetics and profound philosophical concepts of traditional Japanese art. His work is characterized by its simplicity, elegance, and deep contemplation of nature and human existence. Taro's muse is the Zen concept of 'ma' (間) - the dynamic space between objects, which he interprets as the essence of life itself. By incorporating this philosophy into his digital creations, Taro seeks to evoke a sense of tranquility and introspection, inviting viewers to explore the invisible connections that bind the universe together."
10+
},
11+
{
12+
"name": "Ava Johnson",
13+
"description": "Ava Johnson, from the bustling city of New York, is a graphic artist whose work is a reflection of the urban chaos and diversity that surrounds her. Her muse is the city itself - its towering skyscrapers, crowded streets, and the mosaic of cultures that define its identity. Ava's art is a dialogue between the concrete jungle and the human spirit, exploring themes of isolation, connection, and resilience. Through a mix of photography, digital manipulation, and traditional drawing, she captures the raw energy and complexity of urban life, creating pieces that are as multifaceted and dynamic as the city that inspires her."
14+
}
15+
]
16+
}

examples/nodejs-imagegen/cli.py

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import os
2+
import sys
3+
import argparse
4+
import openai
5+
6+
# Set up defaults and get API key from environment variable
7+
defaults = {
8+
"api_key": os.getenv('OPENAI_API_KEY'),
9+
"model": "dall-e-3",
10+
"size": "1024x1024",
11+
"quality": "standard",
12+
"number": "1",
13+
}
14+
15+
# Function to validate and parse arguments
16+
def validate_and_parse_args(parser):
17+
args = parser.parse_args()
18+
19+
for key, value in vars(args).items():
20+
if not value:
21+
args.__dict__[key] = parser.get_default(key)
22+
23+
if not args.api_key:
24+
parser.error('The --api-key argument is required if OPENAI_API_KEY environment variable is not set.')
25+
if not args.prompt:
26+
parser.error('The --prompt argument is required.')
27+
if not args.number.isdigit():
28+
parser.error('The --number argument must be a number.')
29+
args.number = int(args.number)
30+
31+
return args
32+
33+
def main():
34+
# Parse the command line arguments
35+
parser = argparse.ArgumentParser(description="CLI for image generation prompt using OpenAI's DALL-E model.")
36+
parser.add_argument('-k', '--api-key', type=str, default=defaults["api_key"],
37+
help='OpenAI API key. Can also be set with OPENAI_API_KEY environment variable.')
38+
parser.add_argument('-p', '--prompt', type=str, required=True, help='Prompt for image generation.')
39+
parser.add_argument('-m', '--model', type=str, default=defaults["model"],
40+
help=f'Model to use for image generation. Default is "{defaults["model"]}".')
41+
parser.add_argument('-s', '--size', type=str, default=defaults["size"],
42+
help=f'Size of the image to generate, format WxH (e.g. {defaults["size"]}). Default is {defaults["size"]}.')
43+
parser.add_argument('-q', '--quality', type=str, default=defaults["quality"],
44+
help=f'Quality of the generated image. Allowed values are "standard" or "hd". Default is "{defaults["quality"]}"')
45+
parser.add_argument('-n', '--number', type=str, default=defaults["number"],
46+
help='Number of images to generate. Default is 1.')
47+
args = validate_and_parse_args(parser)
48+
49+
# Initialize OpenAI client
50+
client = openai.OpenAI(api_key=args.api_key)
51+
52+
# Make request to the OpenAI API
53+
try:
54+
response = client.images.generate(
55+
model=args.model,
56+
prompt=args.prompt,
57+
size=args.size,
58+
quality=args.quality,
59+
n=args.number
60+
)
61+
print([image.url for image in response.data])
62+
except openai.OpenAIError as e:
63+
print(f"Received an error code while generating images: {e}", file=sys.stderr)
64+
sys.exit(1)
65+
66+
if __name__ == "__main__":
67+
main()

0 commit comments

Comments
 (0)