TTI(Text-to-image)는 자연어 텍스트를 입력으로 사용하여 이미지를 생성하는 프로세스다. 개발자의 목표는 이미지의 텍스트 설명을 이해 및 해석하고 설명과 일치하는 해당 이미지를 생성할 수 있는 알고리즘을 개발하는 것이고, 사용자는 TTI기능을 잘 구현한 엔진을 기반으로 원하는 이미지를 신속히 고품질로 저렴하게 제작하는 것이다. 개발자는 일반적으로 GAN(Generative Adversarial Network) 또는 VAE(Variational Autoencoder)와 같은 머신 러닝 및 딥 러닝 기술을 기반으로 텍스트 및 이미지가 쌍으로 이뤄진 대규모 데이터 셋을 훈련하여 고성능 AI 모형을 개발한다.
library(tidyverse)library(openai)# usethis::edit_r_environ(scope = "project")response<-create_image( prompt ="Create R programming language logo for Korean R user group in a kandinsky and Gustav Klimt style embracing Python programming language supported by many contributors around the world, which must include R logo from R consortium and wikipedia", n =1, size ="256x256", response_format ="url", openai_api_key =Sys.getenv("OPEN_AI_KEY"))library(magick)R_logo<-image_read(response$data$url)print(R_logo)magick::image_write(R_logo, "images/R_logo.png")
2 무병장수 사례
역사상 가장 유명한 화풍을 chatGPT로부터 추천받아 이를 다양한 방식으로 시각화해보자. 무병장수(無病長壽)를 주제로 그림을 그려보자.
무병장수 세상을 르네상스 화풍으로 그려줘 : ‘draw good health and long life world in a Renaissance style’
2.1 chatGPT 프롬프트
recommend the most famous painting style in history
2.2 completion
Here are some of the most famous painting styles in history:
Renaissance: The Renaissance was a cultural movement in Italy from the 14th to the 17th century, characterized by a renewed interest in classical art, philosophy, and science. Renaissance paintings are known for their realism, balance, and harmony.
Baroque: The Baroque style was popular in Europe from the late 16th to the early 18th century. Baroque paintings are known for their dramatic lighting, intense emotions, and dynamic compositions.
Impressionism: Impressionism was a modernist movement in French painting in the late 19th century. Impressionist paintings are known for their loose brushstrokes, light-filled compositions, and focus on capturing fleeting moments.
Surrealism: Surrealism was a cultural movement in the 20th century that aimed to liberate the imagination and explore the subconscious. Surrealist paintings are known for their dreamlike and fantastical imagery.
Abstract Expressionism: Abstract Expressionism was a modernist movement in American painting in the mid-20th century. Abstract Expressionist paintings are known for their large canvases, gestural brushstrokes, and emphasis on the process of creation.
These are just a few examples of famous painting styles in history. There are many other styles that have influenced and shaped the course of art history.
library(tidyverse)library(openai)# usethis::edit_r_environ(scope = "project")response<-create_image( prompt ="draw good health and long life world in a Renaissance style", n =1, size ="1024x1024", response_format ="url", openai_api_key =Sys.getenv('OPEN_AI_KEY'))Renaissance<-image_read(response$data$url)print(Renaissance)image_write(Renaissance, path ="images/styles/Renaissance.png", format ="png")
코드
response<-create_image( prompt ="draw good health and long life world in a Baroque style", n =1, size ="1024x1024", response_format ="url", openai_api_key =Sys.getenv('OPEN_AI_KEY'))Baroque<-image_read(response$data$url)print(Baroque)image_write(Baroque, path ="images/styles/Baroque.png", format ="png")
코드
response<-create_image( prompt ="draw good health and long life world in a Impressionism style", n =1, size ="1024x1024", response_format ="url", openai_api_key =Sys.getenv('OPEN_AI_KEY'))Impressionism<-image_read(response$data$url)print(Impressionism)image_write(Impressionism, path ="images/styles/Impressionism.png", format ="png")
코드
response<-create_image( prompt ="draw good health and long life world in a Surrealism style", n =1, size ="1024x1024", response_format ="url", openai_api_key =Sys.getenv('OPEN_AI_KEY'))Surrealism<-image_read(response$data$url)print(Surrealism)image_write(Surrealism, path ="images/styles/Surrealism.png", format ="png")
코드
response<-create_image( prompt ="draw good health and long life world in a Abstract Expressionism style", n =1, size ="1024x1024", response_format ="url", openai_api_key =Sys.getenv('OPEN_AI_KEY'))expressionism<-image_read(response$data$url)print(expressionism)image_write(expressionism, path ="images/styles/expressionism.png", format ="png")
3BitGPT
bitGPT는 chatGPT를 보다 쉽게 사용할 수 있는, 그리고 한국어를 지원하는 openai, gpttools와 같지만 한국 R 사용자회에서 개발하여 공개한 패키지로 차이가 있습니다.
한글을 파파고 엔진을 사용하여 영어로 번역한다.
코드
# devtools::install_github("bit2r/bitGPT")library(bitGPT)text<-"무병장수 세상을 르네상스 화풍으로 그려줘"eng_prompt<-translate(text, source ="ko", target ="en", client_id =Sys.getenv("NAVER_CLIENT_ID"), client_secret =Sys.getenv("NAVER_CLIENT_SECRET"))eng_prompt#> [1] "Draw a world of health and longevity in a Renaissance style"
chatGPT text-to-image 기능을 사용하여 앞서 한글을 영어로 번역한 지시명령어(Prompt)를 사용하여 이미지를 제작한다.
코드
draw_img(eng_prompt, ko2en =FALSE, n =1L, size ="512x512", type ="file", format ="png", path ="./images", fname ="bitGPT_life", openai_api_key =Sys.getenv("OPEN_AI_KEY"))
마지막으로 bitGPT의 draw_img() 함수에 한글 넣어 바로 이미지로 뽑아내는 실험을 수행해보자.
코드
draw_img("인공지능과 사람이 공존하는 통일된 대한민국을 검사를 포함하여 그려줘", ko2en =TRUE, n =1L, size ="512x512", type ="file", format ="png", path ="./images", fname ="bitGPT_korean", openai_api_key =Sys.getenv("OPEN_AI_KEY"))
소스 코드
---title: "chatGPT"subtitle: "`bitGPT` Text-to-Image"description: 텍스트에서 원하는 이미지를 생성하는 방법을 살펴보자.author: - name: 이광춘 url: https://www.linkedin.com/in/kwangchunlee/ affiliation: 한국 R 사용자회 affiliation-url: https://github.com/bit2rtitle-block-banner: true#title-block-banner: "#562457"format: html: css: css/quarto.css theme: flatly code-fold: true toc: true toc-depth: 3 toc-title: 목차 number-sections: true highlight-style: github self-contained: falsefilters: - lightbox - custom-callout.lua lightbox: autolink-citations: trueknitr: opts_chunk: message: false warning: false collapse: true comment: "#>" R.options: knitr.graphics.auto_pdf: trueeditor_options: chunk_output_type: console---![](images/text2image_workflow.jpg)# 이미지 생성**TTI(Text-to-image)**는 자연어 텍스트를 입력으로 사용하여 이미지를 생성하는 프로세스다. 개발자의 목표는 이미지의 텍스트 설명을 이해 및 해석하고 설명과 일치하는 해당 이미지를 생성할 수 있는 알고리즘을 개발하는 것이고, 사용자는 TTI기능을 잘 구현한 엔진을 기반으로 원하는 이미지를 신속히 고품질로 저렴하게 제작하는 것이다.개발자는 일반적으로 GAN(Generative Adversarial Network) 또는 VAE(Variational Autoencoder)와 같은 머신 러닝 및 딥 러닝 기술을 기반으로 텍스트 및 이미지가 쌍으로 이뤄진 대규모 데이터 셋을 훈련하여 고성능 AI 모형을 개발한다.## `openai` 패키지[`openai`](https://cran.r-project.org/web/packages/openai/index.html) 패키지`create_image()` 함수를 사용하여 이미지를 제작할 수 있다.```{r}#| eval: falselibrary(tidyverse)library(openai)# usethis::edit_r_environ(scope = "project")response <-create_image(prompt ="Create R programming language logo for Korean R user group in a kandinsky and Gustav Klimt style embracing Python programming language supported by many contributors around the world, which must include R logo from R consortium and wikipedia",n =1,size ="256x256",response_format ="url",openai_api_key =Sys.getenv("OPEN_AI_KEY"))library(magick)R_logo <-image_read(response$data$url)print(R_logo)magick::image_write(R_logo, "images/R_logo.png")```![](images/R_logo.png)# 무병장수 사례역사상 가장 유명한 화풍을 chatGPT로부터 추천받아 이를 다양한 방식으로 시각화해보자. 무병장수(無病長壽)를 주제로 그림을 그려보자.> 무병장수 세상을 르네상스 화풍으로 그려줘 : > 'draw good health and long life world in a Renaissance style'## chatGPT 프롬프트recommend the most famous painting style in history::: {.custom-callout-tip collapse="true"}Here are some of the most famous painting styles in history:1. Renaissance: The Renaissance was a cultural movement in Italy from the 14th to the 17th century, characterized by a renewed interest in classical art, philosophy, and science. Renaissance paintings are known for their realism, balance, and harmony.1. Baroque: The Baroque style was popular in Europe from the late 16th to the early 18th century. Baroque paintings are known for their dramatic lighting, intense emotions, and dynamic compositions.1. Impressionism: Impressionism was a modernist movement in French painting in the late 19th century. Impressionist paintings are known for their loose brushstrokes, light-filled compositions, and focus on capturing fleeting moments.1. Surrealism: Surrealism was a cultural movement in the 20th century that aimed to liberate the imagination and explore the subconscious. Surrealist paintings are known for their dreamlike and fantastical imagery.1. Abstract Expressionism: Abstract Expressionism was a modernist movement in American painting in the mid-20th century. Abstract Expressionist paintings are known for their large canvases, gestural brushstrokes, and emphasis on the process of creation.These are just a few examples of famous painting styles in history. There are many other styles that have influenced and shaped the course of art history.:::[[Open AI - Create image API](https://platform.openai.com/docs/api-reference/images/create)]{.aside}## 화풍을 달리한 그림::: {.panel-tabset}### 르네상스(Renaissance)```{r}#| eval: falselibrary(tidyverse)library(openai)# usethis::edit_r_environ(scope = "project")response <-create_image(prompt ="draw good health and long life world in a Renaissance style",n =1,size ="1024x1024",response_format ="url",openai_api_key =Sys.getenv('OPEN_AI_KEY'))Renaissance <-image_read(response$data$url)print(Renaissance)image_write(Renaissance, path ="images/styles/Renaissance.png", format ="png")```![르네상스(Renaissance)](images/styles/Renaissance.png)### 바로크(Baroque)```{r}#| eval: falseresponse <-create_image(prompt ="draw good health and long life world in a Baroque style",n =1,size ="1024x1024",response_format ="url",openai_api_key =Sys.getenv('OPEN_AI_KEY'))Baroque <-image_read(response$data$url)print(Baroque)image_write(Baroque, path ="images/styles/Baroque.png", format ="png")```![바로크(Baroque)](images/styles/Baroque.png)### 인상주의(Impressionism)```{r}#| eval: falseresponse <-create_image(prompt ="draw good health and long life world in a Impressionism style",n =1,size ="1024x1024",response_format ="url",openai_api_key =Sys.getenv('OPEN_AI_KEY'))Impressionism <-image_read(response$data$url)print(Impressionism)image_write(Impressionism, path ="images/styles/Impressionism.png", format ="png")```![인상주의(Impressionism)](images/styles/Impressionism.png)### 초현실주의(Surrealism)```{r}#| eval: falseresponse <-create_image(prompt ="draw good health and long life world in a Surrealism style",n =1,size ="1024x1024",response_format ="url",openai_api_key =Sys.getenv('OPEN_AI_KEY'))Surrealism <-image_read(response$data$url)print(Surrealism)image_write(Surrealism, path ="images/styles/Surrealism.png", format ="png")```![초현실주의(Surrealism)](images/styles/Surrealism.png)### 추상표현주의(Abstract Expressionism)```{r}#| eval: falseresponse <-create_image(prompt ="draw good health and long life world in a Abstract Expressionism style",n =1,size ="1024x1024",response_format ="url",openai_api_key =Sys.getenv('OPEN_AI_KEY'))expressionism <-image_read(response$data$url)print(expressionism)image_write(expressionism, path ="images/styles/expressionism.png", format ="png")```![추상표현주의(Abstract Expressionism)](images/styles/expressionism.png):::# `BitGPT`[`bitGPT`](https://github.com/bit2r/bitGPT)는 chatGPT를 보다 쉽게 사용할 수 있는, 그리고 한국어를 지원하는 `openai`, `gpttools`와 같지만 한국 R 사용자회에서 개발하여 공개한 패키지로 차이가 있습니다.한글을 파파고 엔진을 사용하여 영어로 번역한다.```{r}# devtools::install_github("bit2r/bitGPT")library(bitGPT)text <-"무병장수 세상을 르네상스 화풍으로 그려줘"eng_prompt <-translate(text,source ="ko",target ="en",client_id =Sys.getenv("NAVER_CLIENT_ID"),client_secret =Sys.getenv("NAVER_CLIENT_SECRET"))eng_prompt````chatGPT` text-to-image 기능을 사용하여 앞서 한글을 영어로 번역한 **지시명령어(Prompt)**를 사용하여 이미지를 제작한다.```{r}#| eval: falsedraw_img( eng_prompt,ko2en =FALSE,n = 1L,size ="512x512",type ="file",format ="png",path ="./images",fname ="bitGPT_life",openai_api_key =Sys.getenv("OPEN_AI_KEY"))```![](images/bitGPT_life.png)마지막으로 `bitGPT`의 `draw_img()` 함수에 한글 넣어 바로 이미지로 뽑아내는 실험을 수행해보자.```{r}#| eval: falsedraw_img("인공지능과 사람이 공존하는 통일된 대한민국을 검사를 포함하여 그려줘",ko2en =TRUE,n = 1L,size ="512x512",type ="file",format ="png",path ="./images",fname ="bitGPT_korean",openai_api_key =Sys.getenv("OPEN_AI_KEY"))```![](images/bitGPT_korean.png)