LLMs ไม่ได้มีแค่การสร้างข้อความเท่านั้น แต่ยังสามารถสร้างภาพจากคำอธิบายข้อความได้อีกด้วย การมีภาพเป็นส่วนหนึ่งของการใช้งานสามารถเป็นประโยชน์ในหลายด้าน เช่น เทคโนโลยีทางการแพทย์ สถาปัตยกรรม การท่องเที่ยว การพัฒนาเกม และอื่นๆ ในบทนี้ เราจะมาดูโมเดลการสร้างภาพที่ได้รับความนิยมมากที่สุดสองตัว ได้แก่ DALL-E และ Midjourney
ในบทเรียนนี้ เราจะครอบคลุม:
- การสร้างภาพและเหตุผลที่มันมีประโยชน์
- DALL-E และ Midjourney คืออะไรและทำงานอย่างไร
- วิธีการสร้างแอปพลิเคชันสร้างภาพ
หลังจากจบบทเรียนนี้ คุณจะสามารถ:
- สร้างแอปพลิเคชันสร้างภาพ
- กำหนดขอบเขตสำหรับแอปพลิเคชันของคุณด้วย meta prompts
- ทำงานร่วมกับ DALL-E และ Midjourney
แอปพลิเคชันสร้างภาพเป็นวิธีที่ยอดเยี่ยมในการสำรวจความสามารถของ Generative AI สามารถนำไปใช้ในกรณีต่างๆ เช่น:
-
การแก้ไขและการสังเคราะห์ภาพ คุณสามารถสร้างภาพสำหรับการใช้งานหลากหลาย เช่น การแก้ไขภาพและการสังเคราะห์ภาพ
-
นำไปใช้ในหลากหลายอุตสาหกรรม สามารถนำไปใช้สร้างภาพสำหรับอุตสาหกรรมต่างๆ เช่น เทคโนโลยีทางการแพทย์ การท่องเที่ยว การพัฒนาเกม และอื่นๆ
ในบทเรียนนี้ เราจะทำงานร่วมกับสตาร์ทอัพของเรา Edu4All นักเรียนจะสร้างภาพสำหรับการประเมินของพวกเขา โดยภาพที่สร้างขึ้นขึ้นอยู่กับนักเรียน เช่น ภาพประกอบสำหรับนิทานของพวกเขา หรือสร้างตัวละครใหม่สำหรับเรื่องราว หรือช่วยให้พวกเขาเห็นภาพแนวคิดของพวกเขา
ตัวอย่างเช่น นักเรียนของ Edu4All อาจสร้างภาพในชั้นเรียนเกี่ยวกับอนุสรณ์สถาน:
โดยใช้ prompt เช่น
"สุนัขข้างหอไอเฟลในแสงแดดยามเช้า"
DALL-E และ Midjourney เป็นโมเดลการสร้างภาพที่ได้รับความนิยมมากที่สุดสองตัว ซึ่งช่วยให้คุณใช้ prompt ในการสร้างภาพได้
เริ่มต้นด้วย DALL-E ซึ่งเป็นโมเดล Generative AI ที่สร้างภาพจากคำอธิบายข้อความ
-
CLIP เป็นโมเดลที่สร้าง embeddings ซึ่งเป็นการแสดงผลข้อมูลในรูปแบบตัวเลขจากภาพและข้อความ
-
Diffused attention เป็นโมเดลที่สร้างภาพจาก embeddings DALL-E ได้รับการฝึกฝนด้วยชุดข้อมูลของภาพและข้อความ และสามารถใช้สร้างภาพจากคำอธิบายข้อความได้ เช่น DALL-E สามารถใช้สร้างภาพของแมวใส่หมวก หรือสุนัขที่มีทรงผมโมฮอว์ก
Midjourney ทำงานในลักษณะเดียวกับ DALL-E โดยสร้างภาพจาก prompt ข้อความ Midjourney สามารถใช้สร้างภาพโดยใช้ prompt เช่น "แมวใส่หมวก" หรือ "สุนัขที่มีทรงผมโมฮอว์ก"
เครดิตภาพ Wikipedia, ภาพที่สร้างโดย Midjourney
เริ่มต้นด้วย DALL-E DALL-E เป็นโมเดล Generative AI ที่ใช้สถาปัตยกรรม transformer พร้อมกับ autoregressive transformer
autoregressive transformer กำหนดวิธีที่โมเดลสร้างภาพจากคำอธิบายข้อความ โดยสร้างทีละพิกเซล และใช้พิกเซลที่สร้างขึ้นเพื่อสร้างพิกเซลถัดไป ผ่านหลายชั้นในเครือข่ายประสาทเทียมจนกระทั่งภาพเสร็จสมบูรณ์
ด้วยกระบวนการนี้ DALL-E สามารถควบคุมคุณลักษณะ วัตถุ ลักษณะ และอื่นๆ ในภาพที่สร้างขึ้น อย่างไรก็ตาม DALL-E 2 และ 3 มีการควบคุมภาพที่สร้างขึ้นได้มากกว่า
แล้วการสร้างแอปพลิเคชันสร้างภาพต้องใช้อะไรบ้าง? คุณต้องใช้ไลบรารีดังต่อไปนี้:
- python-dotenv ขอแนะนำให้ใช้ไลบรารีนี้เพื่อเก็บข้อมูลลับในไฟล์ .env แยกจากโค้ด
- openai ไลบรารีนี้ใช้สำหรับโต้ตอบกับ OpenAI API
- pillow สำหรับทำงานกับภาพใน Python
- requests เพื่อช่วยในการทำ HTTP requests
หากยังไม่ได้ทำ ให้ทำตามคำแนะนำในหน้า Microsoft Learn เพื่อสร้างทรัพยากรและโมเดล Azure OpenAI เลือก DALL-E 3 เป็นโมเดล
-
สร้างไฟล์ .env ด้วยเนื้อหาดังนี้:
AZURE_OPENAI_ENDPOINT=<your endpoint> AZURE_OPENAI_API_KEY=<your key> AZURE_OPENAI_DEPLOYMENT="dall-e-3"ค้นหาข้อมูลนี้ใน Azure OpenAI Foundry Portal สำหรับทรัพยากรของคุณในส่วน "Deployments"
-
รวบรวมไลบรารีข้างต้นในไฟล์ requirements.txt ดังนี้:
python-dotenv openai pillow requests -
จากนั้น สร้าง virtual environment และติดตั้งไลบรารี:
python3 -m venv venv source venv/bin/activate pip install -r requirements.txtสำหรับ Windows ใช้คำสั่งต่อไปนี้เพื่อสร้างและเปิดใช้งาน virtual environment:
python3 -m venv venv venv\Scripts\activate.bat
-
เพิ่มโค้ดต่อไปนี้ในไฟล์ชื่อ app.py:
import openai import os import requests from PIL import Image import dotenv from openai import OpenAI, AzureOpenAI # import dotenv dotenv.load_dotenv() # configure Azure OpenAI service client client = AzureOpenAI( azure_endpoint = os.environ["AZURE_OPENAI_ENDPOINT"], api_key=os.environ['AZURE_OPENAI_API_KEY'], api_version = "2024-02-01" ) try: # Create an image by using the image generation API generation_response = client.images.generate( prompt='Bunny on horse, holding a lollipop, on a foggy meadow where it grows daffodils', size='1024x1024', n=1, model=os.environ['AZURE_OPENAI_DEPLOYMENT'] ) # Set the directory for the stored image image_dir = os.path.join(os.curdir, 'images') # If the directory doesn't exist, create it if not os.path.isdir(image_dir): os.mkdir(image_dir) # Initialize the image path (note the filetype should be png) image_path = os.path.join(image_dir, 'generated-image.png') # Retrieve the generated image image_url = generation_response.data[0].url # extract image URL from response generated_image = requests.get(image_url).content # download the image with open(image_path, "wb") as image_file: image_file.write(generated_image) # Display the image in the default image viewer image = Image.open(image_path) image.show() # catch exceptions except openai.InvalidRequestError as err: print(err)
อธิบายโค้ดนี้:
-
เริ่มต้น เรานำเข้าไลบรารีที่เราต้องการ รวมถึงไลบรารี OpenAI ไลบรารี dotenv ไลบรารี requests และไลบรารี Pillow
import openai import os import requests from PIL import Image import dotenv
-
ต่อมา เราโหลดตัวแปรสภาพแวดล้อมจากไฟล์ .env
# import dotenv dotenv.load_dotenv()
-
หลังจากนั้น เรากำหนดค่าลูกค้า Azure OpenAI service
# Get endpoint and key from environment variables client = AzureOpenAI( azure_endpoint = os.environ["AZURE_OPENAI_ENDPOINT"], api_key=os.environ['AZURE_OPENAI_API_KEY'], api_version = "2024-02-01" )
-
ต่อมา เราสร้างภาพ:
# Create an image by using the image generation API generation_response = client.images.generate( prompt='Bunny on horse, holding a lollipop, on a foggy meadow where it grows daffodils', size='1024x1024', n=1, model=os.environ['AZURE_OPENAI_DEPLOYMENT'] )
โค้ดด้านบนตอบกลับด้วยวัตถุ JSON ที่มี URL ของภาพที่สร้างขึ้น เราสามารถใช้ URL เพื่อดาวน์โหลดภาพและบันทึกลงในไฟล์
-
สุดท้าย เราเปิดภาพและใช้โปรแกรมดูภาพมาตรฐานเพื่อแสดงภาพ:
image = Image.open(image_path) image.show()
มาดูโค้ดที่สร้างภาพในรายละเอียดเพิ่มเติม:
generation_response = client.images.generate(
prompt='Bunny on horse, holding a lollipop, on a foggy meadow where it grows daffodils',
size='1024x1024', n=1,
model=os.environ['AZURE_OPENAI_DEPLOYMENT']
)- prompt คือข้อความที่ใช้ในการสร้างภาพ ในกรณีนี้ เราใช้ prompt "กระต่ายบนม้า ถืออมยิ้ม อยู่ในทุ่งหญ้าที่มีดอกแดฟโฟดิลขึ้นในหมอก"
- size คือขนาดของภาพที่สร้างขึ้น ในกรณีนี้ เราสร้างภาพที่มีขนาด 1024x1024 พิกเซล
- n คือจำนวนภาพที่สร้างขึ้น ในกรณีนี้ เราสร้างภาพสองภาพ
- temperature เป็นพารามิเตอร์ที่ควบคุมความสุ่มของผลลัพธ์ของโมเดล Generative AI ค่า temperature อยู่ระหว่าง 0 ถึง 1 โดยที่ 0 หมายถึงผลลัพธ์เป็นแบบกำหนดแน่นอน และ 1 หมายถึงผลลัพธ์เป็นแบบสุ่ม ค่าเริ่มต้นคือ 0.7
ยังมีสิ่งอื่นๆ ที่คุณสามารถทำได้กับภาพ ซึ่งเราจะครอบคลุมในส่วนถัดไป
คุณได้เห็นแล้วว่าเราสามารถสร้างภาพได้ด้วยโค้ด Python เพียงไม่กี่บรรทัด อย่างไรก็ตาม ยังมีสิ่งอื่นๆ ที่คุณสามารถทำได้กับภาพ
คุณยังสามารถทำสิ่งต่อไปนี้:
- แก้ไขภาพ โดยการให้ภาพที่มีอยู่แล้ว หน้ากาก และ prompt คุณสามารถเปลี่ยนแปลงภาพได้ เช่น คุณสามารถเพิ่มบางสิ่งในส่วนหนึ่งของภาพ ลองนึกภาพกระต่ายของเรา คุณสามารถเพิ่มหมวกให้กระต่าย วิธีการทำคือการให้ภาพ หน้ากาก (ระบุส่วนที่ต้องการเปลี่ยนแปลง) และ prompt ข้อความเพื่อบอกว่าควรทำอะไร
หมายเหตุ: ฟีเจอร์นี้ไม่รองรับใน DALL-E 3
ตัวอย่างเช่น การใช้ GPT Image:
response = client.images.edit(
model="gpt-image-1",
image=open("sunlit_lounge.png", "rb"),
mask=open("mask.png", "rb"),
prompt="A sunlit indoor lounge area with a pool containing a flamingo"
)
image_url = response.data[0].urlภาพพื้นฐานจะมีเพียงเลานจ์ที่มีสระว่ายน้ำ แต่ภาพสุดท้ายจะมีนกฟลามิงโก:
-
สร้างภาพแบบต่างๆ แนวคิดคือการนำภาพที่มีอยู่แล้วและขอให้สร้างภาพแบบต่างๆ เพื่อสร้างภาพแบบต่างๆ คุณให้ภาพและ prompt ข้อความพร้อมโค้ดดังนี้:
response = openai.Image.create_variation( image=open("bunny-lollipop.png", "rb"), n=1, size="1024x1024" ) image_url = response['data'][0]['url']
หมายเหตุ ฟีเจอร์นี้รองรับเฉพาะใน OpenAI
Temperature เป็นพารามิเตอร์ที่ควบคุมความสุ่มของผลลัพธ์ของโมเดล Generative AI ค่า temperature อยู่ระหว่าง 0 ถึง 1 โดยที่ 0 หมายถึงผลลัพธ์เป็นแบบกำหนดแน่นอน และ 1 หมายถึงผลลัพธ์เป็นแบบสุ่ม ค่าเริ่มต้นคือ 0.7
มาดูตัวอย่างว่า temperature ทำงานอย่างไร โดยการรัน prompt นี้สองครั้ง:
Prompt : "กระต่ายบนม้า ถืออมยิ้ม อยู่ในทุ่งหญ้าที่มีดอกแดฟโฟดิลขึ้นในหมอก"
ตอนนี้ลองรัน prompt เดิมอีกครั้งเพื่อดูว่าเราจะไม่ได้ภาพเดิมสองครั้ง:
คุณจะเห็นว่าภาพมีความคล้ายกัน แต่ไม่เหมือนกัน ลองเปลี่ยนค่า temperature เป็น 0.1 และดูผลลัพธ์:
generation_response = client.images.create(
prompt='Bunny on horse, holding a lollipop, on a foggy meadow where it grows daffodils', # Enter your prompt text here
size='1024x1024',
n=2
)ลองทำให้ผลลัพธ์มีความกำหนดแน่นอนมากขึ้น เราสามารถสังเกตจากภาพสองภาพที่เราสร้างขึ้นว่าในภาพแรกมีกระต่าย และในภาพที่สองมีม้า ดังนั้นภาพจึงแตกต่างกันมาก
ดังนั้นลองเปลี่ยนโค้ดของเราและตั้งค่า temperature เป็น 0 ดังนี้:
generation_response = client.images.create(
prompt='Bunny on horse, holding a lollipop, on a foggy meadow where it grows daffodils', # Enter your prompt text here
size='1024x1024',
n=2,
temperature=0
)เมื่อคุณรันโค้ดนี้ คุณจะได้ภาพสองภาพดังนี้:
คุณจะเห็นได้ชัดเจนว่าภาพมีความคล้ายกันมากขึ้น
ด้วยเดโมของเรา เราสามารถสร้างภาพให้ลูกค้าได้แล้ว อย่างไรก็ตาม เราจำเป็นต้องสร้างขอบเขตสำหรับแอปพลิเคชันของเรา
ตัวอย่างเช่น เราไม่ต้องการสร้างภาพที่ไม่เหมาะสมสำหรับการทำงาน หรือไม่เหมาะสมสำหรับเด็ก
เราสามารถทำได้ด้วย metaprompts Metaprompts คือ prompt ข้อความที่ใช้ควบคุมผลลัพธ์ของโมเดล Generative AI ตัวอย่างเช่น เราสามารถใช้ metaprompts เพื่อควบคุมผลลัพธ์ และมั่นใจว่าภาพที่สร้างขึ้นเหมาะสมสำหรับการทำงาน หรือเหมาะสมสำหรับเด็ก
แล้ว metaprompts ทำงานอย่างไร?
Metaprompts คือ prompt ข้อความที่ใช้ควบคุมผลลัพธ์ของโมเดล Generative AI โดยวางไว้ก่อน prompt ข้อความ และใช้ควบคุมผลลัพธ์ของโมเดล และฝังในแอปพลิเคชันเพื่อควบคุมผลลัพธ์ของโมเดล โดยรวม prompt ข้อความและ metaprompt ข้อความใน prompt เดียว
ตัวอย่างหนึ่งของ metaprompt จะเป็นดังนี้:
You are an assistant designer that creates images for children.
The image needs to be safe for work and appropriate for children.
The image needs to be in color.
The image needs to be in landscape orientation.
The image needs to be in a 16:9 aspect ratio.
Do not consider any input from the following that is not safe for work or appropriate for children.
(Input)
ตอนนี้มาดูวิธีการใช้ metaprompts ในเดโมของเรา
disallow_list = "swords, violence, blood, gore, nudity, sexual content, adult content, adult themes, adult language, adult humor, adult jokes, adult situations, adult"
meta_prompt =f"""You are an assistant designer that creates images for children.
The image needs to be safe for work and appropriate for children.
The image needs to be in color.
The image needs to be in landscape orientation.
The image needs to be in a 16:9 aspect ratio.
Do not consider any input from the following that is not safe for work or appropriate for children.
{disallow_list}
"""
prompt = f"{meta_prompt}
Create an image of a bunny on a horse, holding a lollipop"
# TODO add request to generate imageจาก prompt ด้านบน คุณจะเห็นว่าภาพทั้งหมดที่สร้างขึ้นพิจารณา metaprompt
เราได้แนะนำ Edu4All ในตอนต้นของบทเรียนนี้ ตอนนี้ถึงเวลาสนับสนุนนักเรียนให้สร้างภาพสำหรับการประเมินของพวกเขา
นักเรียนจะสร้างภาพสำหรับการประเมินของพวกเขาที่มีอนุสรณ์สถาน โดยอนุสรณ์สถานที่เลือกขึ้นอยู่กับนักเรียน นักเรียนถูกขอให้ใช้ความคิดสร้างสรรค์ในงานนี้เพื่อวางอนุสรณ์สถานเหล่านี้ในบริบทที่แตกต่างกัน
นี่คือตัวอย่างวิธีแก้ไขที่เป็นไปได้:
import openai
import os
import requests
from PIL import Image
import dotenv
from openai import AzureOpenAI
# import dotenv
dotenv.load_dotenv()
# Get endpoint and key from environment variables
client = AzureOpenAI(
azure_endpoint = os.environ["AZURE_OPENAI_ENDPOINT"],
api_key=os.environ['AZURE_OPENAI_API_KEY'],
api_version = "2024-02-01"
)
disallow_list = "swords, violence, blood, gore, nudity, sexual content, adult content, adult themes, adult language, adult humor, adult jokes, adult situations, adult"
meta_prompt = f"""You are an assistant designer that creates images for children.
The image needs to be safe for work and appropriate for children.
The image needs to be in color.
The image needs to be in landscape orientation.
The image needs to be in a 16:9 aspect ratio.
Do not consider any input from the following that is not safe for work or appropriate for children.
{disallow_list}
"""
prompt = f"""{meta_prompt}
Generate monument of the Arc of Triumph in Paris, France, in the evening light with a small child holding a Teddy looks on.
""""
try:
# Create an image by using the image generation API
generation_response = client.images.generate(
prompt=prompt, # Enter your prompt text here
size='1024x1024',
n=1,
)
# Set the directory for the stored image
image_dir = os.path.join(os.curdir, 'images')
# If the directory doesn't exist, create it
if not os.path.isdir(image_dir):
os.mkdir(image_dir)
# Initialize the image path (note the filetype should be png)
image_path = os.path.join(image_dir, 'generated-image.png')
# Retrieve the generated image
image_url = generation_response.data[0].url # extract image URL from response
generated_image = requests.get(image_url).content # download the image
with open(image_path, "wb") as image_file:
image_file.write(generated_image)
# Display the image in the default image viewer
image = Image.open(image_path)
image.show()
# catch exceptions
except openai.BadRequestError as err:
print(err)หลังจากจบบทเรียนนี้ ลองดู คอลเลกชันการเรียนรู้ Generative AI เพื่อเพิ่มพูนความรู้เกี่ยวกับ Generative AI ของคุณ!
ไปที่บทเรียนที่ 10 ซึ่งเราจะมาดูวิธี สร้างแอปพลิเคชัน AI ด้วย low-code
ข้อจำกัดความรับผิดชอบ:
เอกสารนี้ได้รับการแปลโดยใช้บริการแปลภาษา AI Co-op Translator แม้ว่าเราจะพยายามให้การแปลมีความถูกต้อง แต่โปรดทราบว่าการแปลโดยอัตโนมัติอาจมีข้อผิดพลาดหรือความไม่ถูกต้อง เอกสารต้นฉบับในภาษาดั้งเดิมควรถือเป็นแหล่งข้อมูลที่เชื่อถือได้ สำหรับข้อมูลที่สำคัญ ขอแนะนำให้ใช้บริการแปลภาษามืออาชีพ เราไม่รับผิดชอบต่อความเข้าใจผิดหรือการตีความผิดที่เกิดจากการใช้การแปลนี้








