creek

The AI Image Editor of 2030

models/sketch2img.py (1371B)


 1"""
 2Sketch to Image
 3Original file: https://colab.research.google.com/drive/1rE5rC71QRvdyxlhnjZUZ320xOV0xrdgx
 4"""
 5
 6## Stable Diffusion
 7
 8!pip install diffusers==0.30.0 transformers accelerate safetensors xformers torch --upgrade
 9
10from diffusers import StableDiffusionPipeline
11import torch
12from PIL import Image
13
14model_id = "runwayml/stable-diffusion-v1-5"
15
16pipe = StableDiffusionPipeline.from_pretrained(
17    model_id, torch_dtype=torch.float16, use_safetensors=True
18).to("cuda")
19
20pipe.enable_xformers_memory_efficient_attention()
21pipe.enable_attention_slicing()
22
23import time
24from PIL import Image
25import subprocess
26
27prompt = "A luxury wristwatch placed on a black velvet pedestal, enclosed within a subtle metallic frame. Low-key dramatic lighting with controlled highlights. High-end jewelry product atmosphere. Deep neutral palette with soft contrast transitions."
28negative_prompt = ""
29
30height, width = 512, 512
31steps = 30
32cfg = 7
33
34torch.cuda.empty_cache()
35start = time.time()
36
37image = pipe(
38    prompt=prompt,
39    negative_prompt=negative_prompt,
40    height=height,
41    width=width,
42    guidance_scale=cfg,
43    num_inference_steps=steps,
44).images[0]
45
46end = time.time()
47
48image.save("output.png")
49
50print(f"Generation Time: {end - start:.2f} seconds")
51
52## GPU Usage
53
54!nvidia-smi
55
56gpu_usage = subprocess.getoutput("nvidia-smi").split("\n")
57print("\n".join(gpu_usage[:10]))