pan
commited on
Upload SD3.py
Browse files
SD3.py
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
## write a diffuers function to generate images
|
| 2 |
+
import torch
|
| 3 |
+
from diffusers import StableDiffusion3Pipeline
|
| 4 |
+
import random
|
| 5 |
+
|
| 6 |
+
class ImageGenerator:
|
| 7 |
+
def __init__(self, repo: str):
|
| 8 |
+
self.repo = repo
|
| 9 |
+
self.pipeline = StableDiffusion3Pipeline.from_pretrained(self.repo, torch_dtype=torch.float16, local_files_only = True)
|
| 10 |
+
self.pipeline.enable_model_cpu_offload()
|
| 11 |
+
|
| 12 |
+
def generate_image(self, prompt: str, width: int = 1024, height: int = 1024, scale_factor: float = 4.5, steps: int = 28, seed: int = None):
|
| 13 |
+
seed = seed if seed is not None else random.randint(0, 2**32 - 1)
|
| 14 |
+
print(f"using {seed} to generate image...")
|
| 15 |
+
generator = torch.Generator("cuda").manual_seed(seed)
|
| 16 |
+
image = self.pipeline(
|
| 17 |
+
prompt,
|
| 18 |
+
negative_prompt="",
|
| 19 |
+
width=width,
|
| 20 |
+
height=height,
|
| 21 |
+
num_inference_steps=steps,
|
| 22 |
+
guidance_scale=scale_factor,
|
| 23 |
+
max_sequence_length=512,
|
| 24 |
+
generator=generator,
|
| 25 |
+
).images[0]
|
| 26 |
+
return image
|
| 27 |
+
|
| 28 |
+
Sd_repo = "/tmp/pretrainmodel/stable-diffusion-3.5-medium-ungated"
|
| 29 |
+
sd_model = ImageGenerator(Sd_repo)
|
| 30 |
+
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."
|
| 31 |
+
image = sd_model.generate_image(prompt)
|