File size: 1,591 Bytes
e56435b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
## write a diffuers function to generate images
import torch
from diffusers import StableDiffusion3Pipeline
import random

class ImageGenerator:
    def __init__(self, repo: str):
        self.repo = repo
        self.pipeline = StableDiffusion3Pipeline.from_pretrained(self.repo, torch_dtype=torch.float16, local_files_only = True)
        self.pipeline.enable_model_cpu_offload()

    def generate_image(self, prompt: str, width: int = 1024, height: int = 1024, scale_factor: float = 4.5, steps: int = 28, seed: int = None):
        seed = seed if seed is not None else random.randint(0, 2**32 - 1)
        print(f"using {seed} to generate image...")
        generator = torch.Generator("cuda").manual_seed(seed)
        image = self.pipeline(
            prompt,
            negative_prompt="",
            width=width,
            height=height,
            num_inference_steps=steps,
            guidance_scale=scale_factor,
            max_sequence_length=512,
            generator=generator,
        ).images[0]
        return image
    
Sd_repo = "/tmp/pretrainmodel/stable-diffusion-3.5-medium-ungated"
sd_model = ImageGenerator(Sd_repo)
prompt = "A close-up portrait of an Asian girl with blunt bangs and big eyes, side profile, holding a red apple on top of her head, in a winter beach setting. She looks very happy, with snowflakes gently falling on her hair. The scene is captured with a high-quality DSLR camera, showcasing natural light and bokeh effects, with a fresh, crisp light and shadow play, reminiscent of a snowy film scene."
image = sd_model.generate_image(prompt)