개발일기

[OpenAI] ChatGpt & DALL-E 이용하여 내 맘대로 이미지 만들기 본문

오늘의 공부일기/머신러닝, 딥러닝 공부일기

[OpenAI] ChatGpt & DALL-E 이용하여 내 맘대로 이미지 만들기

츄98 2023. 5. 18. 14:22

오늘은 AI 라이브러리 활용이라는 특강을 들었다.

이 특강에서 chatGPT와 DALL-E를 활용하여 이미지를 만드는 실습을 진행하였는데, 너무 재밌고 신나는 경험이었다!!

 

 

[준비물]

1. 먼저 패키지를 설치해야한다.

pip install streamlit openai

 

2. app.py 파일 만들고, 모듈 설치 및 api_key 입력하기

# app.py

import streamlit as st
import openai

openai.api_key = "#"
st.write("Hello World!")

Hello World!를 한번 출력해보자!

streamlit run app.py 터미널에서 실행하기

 

잘 출력이 되었다면 준비는 끝났다...!!

 

더보기

api_key는 chatGPT에서 발급받을 수 있으며, 유료서비스이다..

 

 

[실습하기]

# 효과주기
# 풍선효과 주기
st.balloons()

# 제목 만들기
st.title("ChatGPT Plus DALL-E!")

# 입력 form 만들기
with st.form("form"):
    user_input = st.text_input("Prompt")
    submit = st.form_submit_button("Submit")

# 만약 submit 버튼을 눌렀다면, 다음이 실행될 것이다.
if submit:

    gpt_prompt = []

    # 여기에 영어로 변역하라는 말도 적어줄 수 있음! 뭐든 추가할 수 있을듯!
    # "role" 역할은 system 또는 user
    gpt_prompt.append({
        "role": "system",
        "content": "Imagine the detail appearance of the input. Response shortly. Translate to english"
    })

    gpt_prompt.append({
        "role": "user",
        "content": user_input
    })
	
    # 스피너 효과를 줄 수 있다.
	with st.spinner("Waiting for chatGPT..."):
        prompt = openai.ChatCompletion.create(
            model="gpt-3.5-turbo", messages=gpt_prompt)
    
    # prompt에서 choices에서 첫번째 요소 중 message에서 content 부분만 가져오기
    prompt = prompt["choices"][0]["message"]["content"]
    st.caption(prompt)
    
    # DALL-E를 통해 이미지 생성하기
    with st.spinner("Waiting for DALL-E..."):
        result = openai.Image.create(
            prompt=prompt,
            size="1024x1024"
        )
        
    st.image(result["data"][0]["url"])

 

 

[결과물들]

이렇게 데이터가 전달되는 순간, 생성되는 순간에 스피너효과가 나타난다.
귀여운 고양이를 쳤는데... 왜 다람쥐가 나오는거지..;;
translate to english를 넣어주었기 때문에 한글도 잘 이해하는 것을 볼 수 있다.

 

 

 

[참고자료]

https://docs.streamlit.io/library/api-reference

 

Streamlit Docs

Join the community Streamlit is more than just a way to make data apps, it's also a community of creators that share their apps and ideas and help each other make their work better. Please come join us on the community forum. We love to hear your questions

docs.streamlit.io

https://news.hada.io/

 

GeekNews - 개발/기술/스타트업 뉴스 서비스

개발 뉴스, 기술 관련 새소식, 스타트업 정보와 노하우, 세상의 재미난 것들을 좋아하는 사람들을 위한 뉴스 사이트. 이메일 뉴스레터/트위터/슬랙 봇으로 구독 가능

news.hada.io