Jinglong Xiong commited on
Commit
33dda45
·
1 Parent(s): d27affb

add gen_image

Browse files
Files changed (2) hide show
  1. gen_image.py +34 -0
  2. requirements.txt +3 -0
gen_image.py ADDED
@@ -0,0 +1,34 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from diffusers import StableDiffusionPipeline, EulerDiscreteScheduler
2
+ import torch
3
+
4
+ class ImageGenerator:
5
+ def __init__(self, model_id="stabilityai/stable-diffusion-2-1-base", device="cuda"):
6
+ scheduler = EulerDiscreteScheduler.from_pretrained(model_id, subfolder="scheduler")
7
+ self.pipe = StableDiffusionPipeline.from_pretrained(
8
+ model_id,
9
+ scheduler=scheduler,
10
+ torch_dtype=torch.float16
11
+ )
12
+ self.pipe = self.pipe.to(device)
13
+
14
+ def generate(self, prompt, negative_prompt=None, output_path=None):
15
+ image = self.pipe(prompt, negative_prompt=negative_prompt).images[0]
16
+
17
+ if output_path:
18
+ image.save(output_path)
19
+
20
+ return image
21
+
22
+
23
+ # Example usage
24
+ if __name__ == "__main__":
25
+ generator = ImageGenerator()
26
+ import time
27
+ start_time = time.time()
28
+ image = generator.generate(
29
+ prompt="magenta trapezoids layered on a transluscent silver sheet, simple, icon",
30
+ negative_prompt="3d, blurry, complex geometry, realistic",
31
+ output_path="sheet.png"
32
+ )
33
+ end_time = time.time()
34
+ print(f"Time taken: {end_time - start_time} seconds")
requirements.txt CHANGED
@@ -16,6 +16,9 @@ accelerate
16
  openai
17
  tqdm
18
  dotenv
 
 
 
19
 
20
  # pip install 'tensorflow[and-cuda]'
21
  # pip install git+https://github.com/openai/CLIP.git
 
16
  openai
17
  tqdm
18
  dotenv
19
+ diffusers
20
+ safetensors
21
+ xformers
22
 
23
  # pip install 'tensorflow[and-cuda]'
24
  # pip install git+https://github.com/openai/CLIP.git