-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdif.py
46 lines (37 loc) · 1.33 KB
/
dif.py
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import torch
from diffusers import StableDiffusionPipeline
from transformers import CLIPTextModel, CLIPTokenizer
from PIL import Image
import matplotlib.pyplot as plt
# Initialize the Stable Diffusion pipeline
def initialize_pipeline(model_id="CompVis/stable-diffusion-v1-4", device="cuda"):
pipe = StableDiffusionPipeline.from_pretrained(model_id)
pipe = pipe.to(device)
return pipe
# Generate an image from a text prompt
def generate_image(pipe, prompt, num_inference_steps=50, guidance_scale=7.5, seed=None):
if seed is not None:
generator = torch.manual_seed(seed)
else:
generator = None
# Generate image
image = pipe(prompt, num_inference_steps=num_inference_steps, guidance_scale=guidance_scale, generator=generator).images[0]
return image
# Display the image using matplotlib
def display_image(image):
plt.imshow(image)
plt.axis("off")
plt.show()
# Main function
def main():
device = "cuda" if torch.cuda.is_available() else "cpu"
prompt = "A fantasy landscape with mountains and a river"
seed = 42 # Optional seed for reproducibility
# Initialize pipeline
pipe = initialize_pipeline(device=device)
# Generate image
image = generate_image(pipe, prompt, seed=seed)
# Display image
display_image(image)
if __name__ == "__main__":
main()