728x90
๋ฐ์ํ
ํด๋น ๊ฒ์๊ธ์ ์๋ ๊ฐ๋ฐํ๊ฒฝ์ ์ฌ์ฉํ์ฌ ์์ฑ๋จ
IDE : VSCode
Python : 3.11.3
1. Kakao Karlo API ๋ฐ๊ธ
- Kakao API Key ๋ฐ๊ธ ๋ฐฉ๋ฒ์ ์๋ ๊ฒ์๊ธ์ ์ฐธ๊ณ
[Python] ChatGPT #3 ChatGPT X Kakao Karlo API ์ด๋ฏธ์ง ์์ฑ 1
ํด๋น ๊ฒ์๊ธ์ ์๋ ๊ฐ๋ฐํ๊ฒฝ์ ์ฌ์ฉํ์ฌ ์์ฑ๋จ IDE : VSCode Python : 3.11.3 1. PyKakao PyKakao ๋ผ์ด๋ธ๋ฆฌ๋ฌ๋ Kakao Developers์์ ์ ๊ณตํ๋ ๋ค์ํ Kakao API๋ฅผ Python์ผ๋ก ์ฝ๊ฒ ์ฌ์ฉํ ์ ์๋๋ก ๋ค์ํ ๊ธฐ๋ฅ์
ssvip.tistory.com
2. ChatGPT X Kakao Karlo
- ์ฝ๋๋ฅผ ์์ฑํ๊ธฐ์ ์์ OpenAI API์ Kakao API๋ฅผ ์ฌ์ฉํ๊ธฐ ์ํ ๊ธฐ๋ณธ ์ฝ๋๋ฅผ ์์ฑํ๋ค.
๋๋ณด๊ธฐ
import openai
from PyKakao import Karlo
OPENAI_API_KEY = 'OpenAI API Key ์
๋ ฅ'
KAKAO_API_KEY = 'Kakao API REST API Key ์
๋ ฅ'
# OpenAI API Key ์ธ์ฆ
openai.api_key = OPENAI_API_KEY
# Karlo API ์ธ์คํด์ค ์์ฑ
karlo = Karlo(service_key = KAKAO_API_KEY)
2.1 ChatGPT - ์ง๋ฌธํ๊ธฐ
- ChatGPT์ ์ง์๋ฅผ ์์ฑํ๋ค.
- ์ง๋ฌธ : ์ธ๊ฐ์ด ํ์ฑ์ ์ ์ฐฉํ๋ค๋ฉด ๊ฑด์ถ๋ฌผ์ ๋ชจ์ต์ ์ด๋ค ๋ชจ์ต์ผ์ง ์์ ๋กญ๊ฒ ์์ํด์ ์ธํ์ ๋ฌ์ฌํด์ฃผ์ธ์.
๋๋ณด๊ธฐ
# STEP 1 - ChatGPT์ ์ง๋ฌธํ๊ธฐ
# ๋ชจ๋ธ - GPT 3.5 Turbo ์ ํ
model = "gpt-3.5-turbo"
# ์ง๋ฌธ ์์ฑํ๊ธฐ
prompt = "์ธ๊ฐ์ด ํ์ฑ์ ์ ์ฐฉํ๋ค๋ฉด ๊ฑด์ถ๋ฌผ์ ๋ชจ์ต์ ์ด๋ค ๋ชจ์ต์ผ์ง ์์ ๋กญ๊ฒ ์์ํด์ ์ธํ์ ๋ฌ์ฌํด์ฃผ์ธ์."
# ๋ฉ์์ง ์ค์ ํ๊ธฐ
messages = [
{
"role": "system",
"content": "You are a helpful assistant who is good at detailing."
},
{
"role": "user",
"content": prompt
}
]
# API ํธ์ถ
response = openai.ChatCompletion.create(
model=model,
messages=messages
)
answer = response['choices'][0]['message']['content']
print(answer)
2.2 ChatGPT - ๋ํ ์ผํ ์ง๋ฌธํ๊ธฐ
- 2.1์์ ์๋ต๋ฐ์ answer๋ฅผ ์ด์ฉํ์ฌ ChatGPT์๊ฒ ๋ค์ ์ง๋ฌธ
๋๋ณด๊ธฐ
# STEP2 - ๋ํ
์ผํ ์๋ต ์์ฒญํ๊ธฐ
# ์ด์ ์ด์์คํด์ค ์๋ต ๋ฉ์์ง ์ค์ ํ๊ธฐ
messages.append(
{
"role": "assistant",
"content": answer
},
)
# ์ฌ์ฉ์ ๋ฉ์์ง ์ถ๊ฐ
messages.append(
{
"role": "user",
"content": "์ ๋ด์ฉ์ ๋ฐํ์ผ๋ก ์ธํ์ ์ธ ๋ชจ์ต์ ๋ ์์ธํ ์์ํด์ ๋ฌ์ฌํด์ฃผ์ธ์."
}
)
# ChatGPT API ํธ์ถํ๊ธฐ
response = openai.ChatCompletion.create(
model=model,
messages=messages
)
answer2 = response['choices'][0]['message']['content']
print(answer2)
2.3 ChatGPT - ์์ด ๋ฒ์ญ ์์ฒญ
- 2.2๋ฅผ ํตํด ์๋ต๋ฐ์ ๋ต๋ณ์ ์์ด๋ก ๋ฒ์ญ ์์ฒญํ๊ธฐ
๋๋ณด๊ธฐ
# STEP3 - ์์ด ๋ฒ์ญ ์์ฒญํ๊ธฐ
# ์ ๋ฉ์์ง ๊ตฌ์ฑ
messages = [
{
"role": "system",
"content": "You are a helpful assistant who is good at translating."
},
{
"role": "assistant",
"content": answer2
}
]
# ์ฌ์ฉ์ ๋ฉ์์ง ์ถ๊ฐ
messages.append(
{
"role": "user",
"content": "์์ด๋ก ๋ฒ์ญํด์ฃผ์ธ์."
}
)
# ChatGPT API ํธ์ถํ๊ธฐ
response = openai.ChatCompletion.create(
model=model,
messages=messages
)
answer3 = response['choices'][0]['message']['content']
print(answer3)
2.4 ChatGPT - ํ๋กฌํํธ ์์ฑ ์์ฒญํ๊ธฐ
- 2.3์์ ์๋ต๋ฐ์ ๋ต๋ณ์ ํ๋กฌํํธ ํํ๋ก ๋ณํํด๋ฌ๋ผ๊ณ ์์ฒญํ๊ธฐ
๋๋ณด๊ธฐ
# STEP4 - ํ๋กฌํํธ ์์ฑ ์์ฒญํ๊ธฐ
# ์ ๋ฉ์์ง ๊ตฌ์ฑ
messages = [
{
"role": "system",
"content": "You are an assistant who is good at creating prompts for image creation."
},
{
"role": "assistant",
"content": answer3
}
]
# ์ฌ์ฉ์ ๋ฉ์์ง ์ถ๊ฐ
messages.append(
{
"role": "user",
"content": "Condense up to 4 outward description to focus on nouns and adjectives separated by ,"
}
)
# ChatGPT API ํธ์ถํ๊ธฐ
response = openai.ChatCompletion.create(
model=model,
messages=messages
)
answer4 = response['choices'][0]['message']['content']
print(answer4)
# ์ด๋ฏธ์ง ์์ฑ์ ์ํ ํ๋กฌํํธ
params = ", concept art, realistic lighting, ultra-detailed, 8K, photorealism, digital art"
prompt = f"Futuristic mars villages, {answer4}{params}"
print(prompt)
2.5 Karlo - ์ด๋ฏธ์ง ์์ฑ ์์ฒญํ๊ธฐ
- 2.4์์ ์๋ต๋ฐ์ ํ๋กฌํํธ๋ฅผ ์ฌ์ฉํ์ฌ Karlo์ ์ด๋ฏธ์ง ์์ฑ ์์ฒญ
๋๋ณด๊ธฐ
# ์ด๋ฏธ์ง ์์ฑํ๊ธฐ REST API ํธ์ถ
img_dict = karlo.text_to_image(prompt, 1)
# ์์ฑ๋ ์ด๋ฏธ์ง ์ ๋ณด
img_str = img_dict.get("images")[0].get('image')
# base64 string์ ์ด๋ฏธ์ง๋ก ๋ณํ
img = karlo.string_to_image(base64_string = img_str, mode = 'RGBA')
# ์ด๋ฏธ์ง ์ ์ฅํ๊ธฐ
img.save("./new_original.png")
์ ์ฒด ์์ค์ฝ๋
- 2.1 ~ 2.5๊น์ง์ ์ ์ฒด ์์ค์ฝ๋๋ ์๋์ ๊ฐ๋ค.
๋๋ณด๊ธฐ
import openai
from PyKakao import Karlo
OPENAI_API_KEY = 'OpenAI API Key ์
๋ ฅ'
KAKAO_API_KEY = 'Kakao API REST API Key ์
๋ ฅ'
# OpenAI API Key ์ธ์ฆ
openai.api_key = OPENAI_API_KEY
# Karlo API ์ธ์คํด์ค ์์ฑ
karlo = Karlo(service_key = KAKAO_API_KEY)
# STEP 1 - ChatGPT์ ์ง๋ฌธํ๊ธฐ
# ๋ชจ๋ธ - GPT 3.5 Turbo ์ ํ
model = "gpt-3.5-turbo"
# ์ง๋ฌธ ์์ฑํ๊ธฐ
prompt = "์ธ๊ฐ์ด ํ์ฑ์ ์ ์ฐฉํ๋ค๋ฉด ๊ฑด์ถ๋ฌผ์ ๋ชจ์ต์ ์ด๋ค ๋ชจ์ต์ผ์ง ์์ ๋กญ๊ฒ ์์ํด์ ์ธํ์ ๋ฌ์ฌํด์ฃผ์ธ์."
# ๋ฉ์์ง ์ค์ ํ๊ธฐ
messages = [
{
"role": "system",
"content": "You are a helpful assistant who is good at detailing."
},
{
"role": "user",
"content": prompt
}
]
# API ํธ์ถ
response = openai.ChatCompletion.create(
model=model,
messages=messages
)
answer = response['choices'][0]['message']['content']
print(answer)
# STEP2 - ๋ํ
์ผํ ์๋ต ์์ฒญํ๊ธฐ
# ์ด์ ์ด์์คํด์ค ์๋ต ๋ฉ์์ง ์ค์ ํ๊ธฐ
messages.append(
{
"role": "assistant",
"content": answer
},
)
# ์ฌ์ฉ์ ๋ฉ์์ง ์ถ๊ฐ
messages.append(
{
"role": "user",
"content": "์ ๋ด์ฉ์ ๋ฐํ์ผ๋ก ์ธํ์ ์ธ ๋ชจ์ต์ ๋ ์์ธํ ์์ํด์ ๋ฌ์ฌํด์ฃผ์ธ์."
}
)
# ChatGPT API ํธ์ถํ๊ธฐ
response = openai.ChatCompletion.create(
model=model,
messages=messages
)
answer2 = response['choices'][0]['message']['content']
print(answer2)
# STEP3 - ์์ด ๋ฒ์ญ ์์ฒญํ๊ธฐ
# ์ ๋ฉ์์ง ๊ตฌ์ฑ
messages = [
{
"role": "system",
"content": "You are a helpful assistant who is good at translating."
},
{
"role": "assistant",
"content": answer2
}
]
# ์ฌ์ฉ์ ๋ฉ์์ง ์ถ๊ฐ
messages.append(
{
"role": "user",
"content": "์์ด๋ก ๋ฒ์ญํด์ฃผ์ธ์."
}
)
# ChatGPT API ํธ์ถํ๊ธฐ
response = openai.ChatCompletion.create(
model=model,
messages=messages
)
answer3 = response['choices'][0]['message']['content']
print(answer3)
# STEP4 - ํ๋กฌํํธ ์์ฑ ์์ฒญํ๊ธฐ
# ์ ๋ฉ์์ง ๊ตฌ์ฑ
messages = [
{
"role": "system",
"content": "You are an assistant who is good at creating prompts for image creation."
},
{
"role": "assistant",
"content": answer3
}
]
# ์ฌ์ฉ์ ๋ฉ์์ง ์ถ๊ฐ
messages.append(
{
"role": "user",
"content": "Condense up to 4 outward description to focus on nouns and adjectives separated by ,"
}
)
# ChatGPT API ํธ์ถํ๊ธฐ
response = openai.ChatCompletion.create(
model=model,
messages=messages
)
answer4 = response['choices'][0]['message']['content']
print(answer4)
# ์ด๋ฏธ์ง ์์ฑ์ ์ํ ํ๋กฌํํธ
params = ", concept art, realistic lighting, ultra-detailed, 8K, photorealism, digital art"
prompt = f"Futuristic mars villages, {answer4}{params}"
print(prompt)
# STEP5 - Karlo์๊ฒ ์ด๋ฏธ์ง ์์ฑ ์์ฒญํ๊ธฐ
# ์ด๋ฏธ์ง ์์ฑํ๊ธฐ REST API ํธ์ถ
img_dict = karlo.text_to_image(prompt, 1)
# ์์ฑ๋ ์ด๋ฏธ์ง ์ ๋ณด
img_str = img_dict.get("images")[0].get('image')
# base64 string์ ์ด๋ฏธ์ง๋ก ๋ณํ
img = karlo.string_to_image(base64_string = img_str, mode = 'RGBA')
# ์ด๋ฏธ์ง ์ ์ฅํ๊ธฐ
img.save("./new_original.png")
728x90
๋ฐ์ํ
'๊ฐ๋ฐ๐ป > Python' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Python] Google Gemini #2 Gemini Key ๋ฐ๊ธ (0) | 2024.03.17 |
---|---|
[Python] Google Gemini #1 Gemini๋? (0) | 2024.03.17 |
[Python] ChatGPT #3 ChatGPT X Kakao Karlo API ์ด๋ฏธ์ง ์์ฑ 1 (0) | 2023.06.02 |
[Python] ChatGPT #2 Python ChatGPT ์ฐ๋ (0) | 2023.05.31 |
[Python] ChatGPT #1 OpenAI API Key ๋ฐ๊ธ (2) | 2023.05.30 |