File size: 949 Bytes
c688ecf 1f70bfa c688ecf 1f70bfa c688ecf 1f70bfa c688ecf 1f70bfa c688ecf 6a814d3 1f70bfa c688ecf 1f70bfa |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
import openai
import torch
from diffusers import StableDiffusionPipeline
import os
from dotenv import load_dotenv
# ε θ½½η―ε’ει
load_dotenv()
# θ·ε API ε―ι₯
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
openai.api_key = OPENAI_API_KEY
# ε θ½½ Stable Diffusion 樑ε
pipe = StableDiffusionPipeline.from_pretrained("SfinOe/stable-diffusion-v1.5", torch_dtype=torch.float16).to("cpu")
def generate_prompt(description):
""" δ½Ώη¨ OpenAI ηζεΎεηζηζη€Ί """
prompt = f"Generate a detailed prompt for stable diffusion image generation based on the description: {description}"
response = openai.Completion.create(
engine="text-davinci-003", # ζε
Άδ» GPT-3 樑ε
prompt=prompt,
max_tokens=100
)
return response.choices[0].text.strip()
def generate_image_from_prompt(prompt):
""" δ½Ώη¨ Stable Diffusion ηζεΎε """
image = pipe(prompt).images[0]
return image
|