-
Notifications
You must be signed in to change notification settings - Fork 300
add example nodejs and image generation example #108
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
# AI Logo Design (Nodejs image generation example) | ||
|
||
## Overview | ||
|
||
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. | ||
|
||
## Features | ||
|
||
- Three built in AI artists. | ||
- Ability to generate three additional AI artists. | ||
- Provide a description of the logo to be generated. | ||
- View three rendered logo options. | ||
|
||
## Installation | ||
|
||
1. Clone the repository. | ||
|
||
```bash | ||
git clone https://github.com/gptscript-ai/gptscript.git | ||
``` | ||
|
||
1. Navigate to the 'examples/nodejs-imagegen' directory and install dependencies. | ||
|
||
Python: | ||
|
||
*note: You can use a virtual environment to install the python dependencies, just make sure it is activated before running the server.* | ||
|
||
```bash | ||
pip install -r requirements.txt | ||
``` | ||
|
||
this will install the python dependencies for the image generation tool. | ||
|
||
Node: | ||
|
||
```bash | ||
npm install | ||
``` | ||
|
||
This is for the application itself. | ||
|
||
1. Setup OPENAI_API_KEY environment variable. | ||
|
||
```bash | ||
export OPENAI_API_KEY=your-api-key | ||
``` | ||
|
||
1. Run the application. | ||
|
||
```bash | ||
npm run dev | ||
``` | ||
|
||
## Usage | ||
|
||
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. | ||
|
||
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. | ||
|
||
## Diving in | ||
|
||
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. | ||
|
||
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. |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
{ | ||
"artists": [ | ||
{ | ||
"name": "Elena Vasquez", | ||
"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." | ||
}, | ||
{ | ||
"name": "Taro Yamamoto", | ||
"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." | ||
}, | ||
{ | ||
"name": "Ava Johnson", | ||
"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." | ||
} | ||
] | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import os | ||
import sys | ||
import argparse | ||
import openai | ||
|
||
# Set up defaults and get API key from environment variable | ||
defaults = { | ||
"api_key": os.getenv('OPENAI_API_KEY'), | ||
"model": "dall-e-3", | ||
"size": "1024x1024", | ||
"quality": "standard", | ||
"number": "1", | ||
} | ||
|
||
# Function to validate and parse arguments | ||
def validate_and_parse_args(parser): | ||
args = parser.parse_args() | ||
|
||
for key, value in vars(args).items(): | ||
if not value: | ||
args.__dict__[key] = parser.get_default(key) | ||
|
||
if not args.api_key: | ||
parser.error('The --api-key argument is required if OPENAI_API_KEY environment variable is not set.') | ||
if not args.prompt: | ||
parser.error('The --prompt argument is required.') | ||
if not args.number.isdigit(): | ||
parser.error('The --number argument must be a number.') | ||
args.number = int(args.number) | ||
|
||
return args | ||
|
||
def main(): | ||
# Parse the command line arguments | ||
parser = argparse.ArgumentParser(description="CLI for image generation prompt using OpenAI's DALL-E model.") | ||
parser.add_argument('-k', '--api-key', type=str, default=defaults["api_key"], | ||
help='OpenAI API key. Can also be set with OPENAI_API_KEY environment variable.') | ||
parser.add_argument('-p', '--prompt', type=str, required=True, help='Prompt for image generation.') | ||
parser.add_argument('-m', '--model', type=str, default=defaults["model"], | ||
help=f'Model to use for image generation. Default is "{defaults["model"]}".') | ||
parser.add_argument('-s', '--size', type=str, default=defaults["size"], | ||
help=f'Size of the image to generate, format WxH (e.g. {defaults["size"]}). Default is {defaults["size"]}.') | ||
parser.add_argument('-q', '--quality', type=str, default=defaults["quality"], | ||
help=f'Quality of the generated image. Allowed values are "standard" or "hd". Default is "{defaults["quality"]}"') | ||
parser.add_argument('-n', '--number', type=str, default=defaults["number"], | ||
help='Number of images to generate. Default is 1.') | ||
args = validate_and_parse_args(parser) | ||
|
||
# Initialize OpenAI client | ||
client = openai.OpenAI(api_key=args.api_key) | ||
|
||
# Make request to the OpenAI API | ||
try: | ||
response = client.images.generate( | ||
model=args.model, | ||
prompt=args.prompt, | ||
size=args.size, | ||
quality=args.quality, | ||
n=args.number | ||
) | ||
print([image.url for image in response.data]) | ||
except openai.OpenAIError as e: | ||
print(f"Received an error code while generating images: {e}", file=sys.stderr) | ||
sys.exit(1) | ||
|
||
if __name__ == "__main__": | ||
main() | ||
Comment on lines
+1
to
+67
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We might be able use git subtrees to make maintaining these copies of our external tools a bit easier. IIUC, pulling and updating a tool subtree for an example would look something like this: > # Initialize the subtree, pulling main into a single commit
> git subtree add --prefix=examples/nodejs-imagegen image-generator [email protected]:gptscript-ai/image-generation.git main --squash
> tree -L 2 examples/nodejs-imagegen
examples/nodejs-imagegen
|- tool.gpt
|- server.js
|- ...
|_ image-generation
|- cli.py
|- ...
> # Pull any updates from the image-generation repo since the last commit for the subtree
> git subtree pull --prefix=examples/nodejs-imagegen image-generator [email protected]:gptscript-ai/image-generation.git main --squash @cloudnautique wdyt? too complex for a short-term quality-of-life hack? |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: