|
|
import openai |
|
|
import torch |
|
|
from diffusers import StableDiffusionPipeline |
|
|
import os |
|
|
from dotenv import load_dotenv |
|
|
|
|
|
|
|
|
load_dotenv() |
|
|
|
|
|
|
|
|
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY") |
|
|
openai.api_key = OPENAI_API_KEY |
|
|
|
|
|
|
|
|
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", |
|
|
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 |
|
|
|
|
|
|