๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ’ป IT·๊ธฐ์ˆ ·ํ†ต๊ณ„

[API] DALL·E 2 Python์œผ๋กœ ์‚ฌ์šฉํ•ด๋ณด๊ธฐ

by nowgeun 2023. 1. 25.
728x90

ChatGPT๋ฅผ ๊ฐœ๋ฐœํ•œ OpenAI๋Š” ์ž‘๋…„ 4์›”์— Text to Image ๋ชจ๋ธ์ธ DALL·E 2๋„ ๊ณต๊ฐœํ•œ ์ ์ด ์žˆ์Šต๋‹ˆ๋‹ค.

์ตœ๊ทผ์— ๊ทธ๋ฆผ์„ ๊ทธ๋ ค์ฃผ๋Š” AI์™€ ์‚ฌ์ง„์„ ์•„๋‹ˆ๋ฉ” (Anime) ํ’์œผ๋กœ ๋ณ€ํ™˜์„ ํ•ด์ฃผ๋Š” ๊ฐ์ข… ํ•„ํ„ฐ๊ฐ€ ์œ ํ–‰์„ ํ•˜๊ณ  ์žˆ์–ด์„œ ์ด๋ฏธ์ง€์™€ ๊ด€๋ จ๋œ AI๋Š” ๋†€๋ž๊ฒŒ ๋Š๊ปด์ง€์ง„ ์•Š์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ๋‹จ์ˆœํ•œ ์„ค๋ช…๋งŒ์œผ๋กœ ์ €์ž‘๊ถŒ ์—†๋Š” ๊ณ ํ€„๋ฆฌํ‹ฐ ์ด๋ฏธ์ง€๋ฅผ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์•„ํŠธ ์—…๊ณ„ ์ชฝ์—์„œ๋Š” ํฐ ์œ„ํ˜‘์ด ๋  ์ˆ˜ ๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ฐœ์ธ์ ์œผ๋กœ AI๊ฐ€ ๋ถˆ๋Ÿฌ์˜ค๋Š” disruptive innovation์˜ ๊ฐ€์žฅ ์ฒซ ๋ฒˆ์งธ ๋ถ„์•ผ๋Š” ์˜ˆ์ˆ ์ด ์•„๋‹๊นŒ ์‹ถ์Šต๋‹ˆ๋‹ค. OpenAI ํ™ˆํŽ˜์ด์ง€์—์„œ ์ฒดํ—˜์„ ํ•˜์‹œ๋ ค๋ฉด ์•„๋ž˜์˜ ๋งํฌ๋ฅผ ์ฐธ๊ณ ํ•˜์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค

 

DALL·E

Experiment with DALL·E, an AI system by OpenAI

labs.openai.com

๊ทธ๋Ÿผ ์ด์ œ Python์œผ๋กœ DALL·E 2๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.


Python์œผ๋กœ ๊ตฌํ˜„

OpenAI์—์„œ ๋ฐœ๊ธ‰๋ฐ›์€ API Key๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์•„๋ž˜์™€ ๊ฐ™์ด ์ฝ”๋“œ๋ฅผ ์งœ์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค.

(API Key ๋ฐœ๊ธ‰ ๋ฐฉ๋ฒ•์„ ๋ชจ๋ฅด์‹œ๊ฒ ๋‹ค๋ฉด ์ด ํฌ์ŠคํŠธ๋ฅผ ์ฐธ๊ณ ํ•ด ์ฃผ์„ธ์š”)

 

import os
import openai
import json
from pathlib import Path
from base64 import b64decode

description = 'a flying hamburger in space, digital art'    # Ai๊ฐ€ ๊ทธ๋ ค์ค„ ์ด๋ฏธ์ง€์— ๋Œ€ํ•œ ๋ฌ˜์‚ฌ
response = openai.Image.create(prompt=description            # ํ…์ŠคํŠธ ์ž…๋ ฅ
                               , n=1                    # ์ƒ์„ฑํ•  ์ด๋ฏธ์ง€ ๊ฐœ์ˆ˜
                               , size= "1024x1024"      # ์ด๋ฏธ์ง€ ํฌ๊ธฐ
                               , response_format= "b64_json"
                               , api_key = 'API KEY 16์ž๋ฆฌ ์ž…๋ ฅ') # openai APIํ‚ค

 
DATA_DIR = Path.cwd() # os.getcwd()
file_name = DATA_DIR / f"{PROMPT[:5]}_{response['created']}.json"  # ์ƒ์„ฑ๋œ ์ด๋ฏธ์ง€ํŒŒ์ผ ์ €์žฅ ๊ฒฝ๋กœ

# json ํŒŒ์ผ๋กœ ์ €์žฅํ•˜๊ธฐ 
with open(file_name, mode="w", encoding="utf-8") as file:
    json.dump(response, file)
    
# png ํŒŒ์ผ๋กœ ์ €์žฅ
for index, image_dict in enumerate(response["data"]):
    image_data = b64decode(image_dict["b64_json"])
    image_file = DATA_DIR / f"{file_name.stem}-{index}.png"
    with open(image_file, mode="wb") as png:
        png.write(image_data)

 

 

๊ฒฐ๊ณผ๋ฌผ

์œ„์˜ ์ฝ”๋“œ๋ฅผ ์‹คํ–‰ํ•˜๊ฒŒ ๋˜๋ฉด ์•„๋ž˜์™€ ๊ฐ™์€ ์ด๋ฏธ์ง€์™€ json ํŒŒ์ผ์ด ์ƒ์„ฑ๋ฉ๋‹ˆ๋‹ค.

 

a flying hamburger in space

 

๋ญ”๊ฐ€ ์‚ฌ์‹ค์ ์ด๊ณ  ๊ทธ๋Ÿด๋“ฏํ•ฉ๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ์•„์ง ์‚ฌ๋žŒ์ด๋‚˜ ๋™๋ฌผ์˜ ์‚ฌ์‹ค์ ์ธ ๋ฌ˜์‚ฌ๋Š” ํ€„๋ฆฌํ‹ฐ๊ฐ€ ์กฐ๊ธˆ ์•„์‰ฌ์šด ๋ถ€๋ถ„๋„ ์žˆ์Šต๋‹ˆ๋‹ค.

์•„๋ž˜์—๋Š” ์ œ๊ฐ€ ์ƒ์„ฑํ•œ ์ด๋ฏธ์ง€์˜ ๋ชจ์Œ์ž…๋‹ˆ๋‹ค. ์ค‘๊ฐ„์ค‘๊ฐ„ ์ด์ƒํ•˜๊ณ  ์–ด์ƒ‰ํ•œ ๋ถ€๋ถ„๋„ ์žˆ์–ด์„œ ์•„์ง๊นŒ์ง€๋Š” ๋ถˆ์พŒํ•œ ๊ณจ์งœ๊ธฐ์— ๋†“์ธ ๋ถ„์•ผ๋„ ์žˆ๋Š” ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค. AI๊ฐ€ ์„ธ์ƒ์„ ์ง€๋ฐฐํ•˜๋Š” ๊ฑด ๋จผ ํ›—๋‚ ์˜ ์ด์•ผ๊ธฐ์ผ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค ใ…Žใ…Ž

 

์ƒ์„ฑ ์˜ˆ์‹œ ๋ชจ์Œ

- high-teen north korean leader kim jeong eun, anime style

- a dog and a cat fighting with light saber, digital art

- teddy bears shopping for groceries, one-line drawing

 

- a man riding a tiger, pixel art

- a spacious futuristic library filled with starfishes reading books

- a house in the shape of a pineapple

๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€