File size: 787 Bytes
95ba608
a23c39b
740ee6a
d71535c
494f2fa
740ee6a
d71535c
a23c39b
3591c2a
494f2fa
 
7ae7337
3591c2a
95ba608
 
3591c2a
da36322
 
 
 
d71535c
3591c2a
a23c39b
7ae7337
d71535c
 
 
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
import torch
from diffusers import StableDiffusionPipeline

# Hugging Face API key
api_key = "your_huggingface_api_key"

# Load the Stable Diffusion pipeline with the API key
pipeline = StableDiffusionPipeline.from_pretrained(
    "CompVis/stable-diffusion-v1-4",
    use_auth_token=api_key
)

# Move the pipeline to GPU if available
device = "cuda" if torch.cuda.is_available() else "cpu"
pipeline.to(device)

# Reduce the image resolution for faster generation
pipeline.set_format("jpeg")
pipeline.set_resolution(512)  # Reduce resolution (e.g., 512x512)

# Generate an image
prompt = "A serene lake surrounded by mountains during sunset"
image = pipeline(prompt).images[0]

# Save the generated image
image.save("generated_image.png")
print("Image generated and saved successfully!")