|
import torch |
|
from diffusers import StableDiffusionPipeline |
|
|
|
|
|
api_key = "your_huggingface_api_key" |
|
|
|
|
|
pipeline = StableDiffusionPipeline.from_pretrained( |
|
"stabilityai/stable-diffusion-2", |
|
use_auth_token=api_key |
|
) |
|
|
|
|
|
device = "cuda" if torch.cuda.is_available() else "cpu" |
|
pipeline.to(device) |
|
|
|
|
|
height = 512 |
|
width = 512 |
|
|
|
|
|
prompt = "A serene lake surrounded by mountains during sunset" |
|
image = pipeline(prompt, height=height, width=width).images[0] |
|
|
|
|
|
image.save("generated_image.png") |
|
print("Image generated and saved successfully!") |
|
|