Skip to content

Commit 4ed2305

Browse files
authored
Update cli.py
1 parent 28960f1 commit 4ed2305

File tree

1 file changed

+23
-14
lines changed

1 file changed

+23
-14
lines changed

dallecli/cli.py

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
import click
22
from os import path, makedirs, getenv
33
from io import BytesIO
4-
import openai
4+
from openai import OpenAI, AuthenticationError
55
from PIL import Image, ImageEnhance, ImageFilter, ImageOps
66
import requests
77
from rich.progress import Console
88
import json
99

1010
console = Console()
11+
client = OpenAI(api_key="")
1112

1213

1314
def configure_openai():
@@ -24,33 +25,35 @@ def configure_openai():
2425
api_key = input("🔑 Enter your OpenAI API key: ")
2526
with open(config_file, "w", encoding="UTF-8") as f:
2627
f.write(f'{{"api_key": "{api_key}"}}')
27-
openai.api_key = api_key or getenv("OPENAI_API_KEY")
28+
client.api_key = api_key or getenv("OPENAI_API_KEY")
2829

2930

30-
def generate_image(prompt, size, filter, iterations, hide, save_path=None):
31+
def generate_image(
32+
prompt, size, filter, iterations, hide, quality, model, save_path=None
33+
):
3134
try:
3235
with console.status("Generating image...", spinner="dots8Bit"):
3336
for _ in range(iterations):
34-
response = openai.Image.create(
37+
response = client.images.generate(
38+
model=model,
3539
prompt=prompt,
3640
size=size,
37-
response_format="url",
41+
quality=quality,
3842
**({"filter": filter} if filter else {}),
3943
)
40-
image_data = requests.get(
41-
response.get("data")[0]["url"], timeout=300
42-
).content
44+
image_data = requests.get(response.data[0].url, timeout=300).content
4345
image = Image.open(BytesIO(image_data))
4446
if not hide:
4547
image.show()
4648
if save_path is not None:
4749
if not path.exists(path.dirname(save_path)):
4850
makedirs(path.dirname(save_path))
4951
image.save(save_path)
50-
except openai.error.AuthenticationError:
52+
except AuthenticationError:
5153
print("🔒 Authentication Failed. Try with a fresh API key.")
52-
except Exception:
54+
except Exception as e:
5355
print("❌ Failed to generate image. Please try again with a different prompt.")
56+
print(f"Error: {e}")
5457

5558

5659
def edit_image(image, brightness=None, contrast=None, sharpness=None):
@@ -109,7 +112,7 @@ def apply_filter_choices(image, filter_name):
109112

110113

111114
@click.group()
112-
@click.version_option(version="1.3.0")
115+
@click.version_option(version="2.0.0")
113116
def cli():
114117
"""💠 Use the Dall.E 2 api to generate, edit & filter images from the cmd line."""
115118

@@ -121,7 +124,7 @@ def cli():
121124
prompt=True,
122125
help="💬 The prompt to generate the image from.",
123126
)
124-
@click.option("--size", default="512x512", help="📐 The size of the generated image.")
127+
@click.option("--size", default="1024x1024", help="📐 The size of the generated image.")
125128
@click.option(
126129
"--filter",
127130
type=click.Choice(
@@ -157,10 +160,16 @@ def cli():
157160
default="images/output.png",
158161
)
159162
@click.option("--hide", is_flag=True, help="🖱️ Do not open the image after generation")
160-
def generate(prompt, size, filter, iterations, save_path, hide):
163+
@click.option("--quality", default="standard", help="👌 The quality of the image")
164+
@click.option(
165+
"--model",
166+
default="dall-e-3",
167+
help="🦾 The OpenAI model to use when generating images",
168+
)
169+
def generate(prompt, size, filter, iterations, save_path, hide, quality, model):
161170
"""🌸 Generate an image from the OpenAI Dalle api"""
162171
configure_openai()
163-
generate_image(prompt, size, filter, iterations, hide, save_path)
172+
generate_image(prompt, size, filter, iterations, hide, quality, model, save_path)
164173

165174

166175
@cli.command("edit")

0 commit comments

Comments
 (0)