-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #570 from realpython/update-dalle
Update code for new API and model versions
- Loading branch information
Showing
6 changed files
with
54 additions
and
42 deletions.
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
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 |
---|---|---|
@@ -1,24 +1,24 @@ | ||
import json | ||
import os | ||
from pathlib import Path | ||
|
||
import openai | ||
from openai import OpenAI | ||
|
||
client = OpenAI() | ||
|
||
PROMPT = "An eco-friendly computer from the 90s in the style of vaporwave" | ||
DATA_DIR = Path.cwd() / "responses" | ||
|
||
DATA_DIR.mkdir(exist_ok=True) | ||
|
||
openai.api_key = os.getenv("OPENAI_API_KEY") | ||
|
||
response = openai.Image.create( | ||
response = client.images.generate( | ||
model="dall-e-2", | ||
prompt=PROMPT, | ||
n=1, | ||
size="256x256", | ||
response_format="b64_json", | ||
) | ||
|
||
file_name = DATA_DIR / f"{PROMPT[:5]}-{response['created']}.json" | ||
file_name = DATA_DIR / f"{PROMPT[:5]}-{response.created}.json" | ||
|
||
with open(file_name, mode="w", encoding="utf-8") as file: | ||
json.dump(response, file) | ||
json.dump(response.to_dict(), file) |
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,14 @@ | ||
from openai import OpenAI | ||
|
||
client = OpenAI() | ||
|
||
PROMPT = "A vaporwave computer" | ||
|
||
|
||
response = client.images.generate( | ||
model="dall-e-3", | ||
prompt=PROMPT, | ||
) | ||
|
||
print(response.data[0].url) | ||
print(response.data[0].revised_prompt) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,15 @@ | ||
certifi==2022.9.24 | ||
charset-normalizer==2.1.1 | ||
et-xmlfile==1.1.0 | ||
idna==3.4 | ||
numpy==1.23.4 | ||
openai==0.25.0 | ||
openpyxl==3.0.10 | ||
pandas==1.5.1 | ||
pandas-stubs==1.2.0.62 | ||
python-dateutil==2.8.2 | ||
pytz==2022.6 | ||
requests==2.28.1 | ||
six==1.16.0 | ||
tqdm==4.64.1 | ||
typing_extensions==4.4.0 | ||
urllib3==1.26.12 | ||
annotated-types==0.7.0 | ||
anyio==4.4.0 | ||
certifi==2024.7.4 | ||
distro==1.9.0 | ||
h11==0.14.0 | ||
httpcore==1.0.5 | ||
httpx==0.27.0 | ||
idna==3.7 | ||
jiter==0.5.0 | ||
openai==1.40.6 | ||
pydantic==2.8.2 | ||
pydantic_core==2.20.1 | ||
sniffio==1.3.1 | ||
tqdm==4.66.5 | ||
typing_extensions==4.12.2 |
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 |
---|---|---|
@@ -1,27 +1,26 @@ | ||
import json | ||
import os | ||
from base64 import b64decode | ||
from pathlib import Path | ||
|
||
import openai | ||
from openai import OpenAI | ||
|
||
client = OpenAI() | ||
|
||
DATA_DIR = Path.cwd() / "responses" | ||
SOURCE_FILE = DATA_DIR / "An ec-1667994848.json" | ||
|
||
openai.api_key = os.getenv("OPENAI_API_KEY") | ||
|
||
with open(SOURCE_FILE, mode="r", encoding="utf-8") as json_file: | ||
saved_response = json.load(json_file) | ||
image_data = b64decode(saved_response["data"][0]["b64_json"]) | ||
|
||
response = openai.Image.create_variation( | ||
response = client.images.create_variation( | ||
image=image_data, | ||
n=3, | ||
size="256x256", | ||
response_format="b64_json", | ||
) | ||
|
||
new_file_name = f"vary-{SOURCE_FILE.stem[:5]}-{response['created']}.json" | ||
new_file_name = f"vary-{SOURCE_FILE.stem[:5]}-{response.created}.json" | ||
|
||
with open(DATA_DIR / new_file_name, mode="w", encoding="utf-8") as file: | ||
json.dump(response, file) | ||
json.dump(response.to_dict(), file) |