SandaAbhishekSagar commited on
Commit
50936f2
1 Parent(s): 558a114

revamped code

Browse files
Files changed (2) hide show
  1. image_generator.py +25 -11
  2. requirements.txt +2 -1
image_generator.py CHANGED
@@ -31,20 +31,34 @@
31
  # print("Generated Image Path:", generate_image(prompt))
32
 
33
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
34
  from diffusers import StableDiffusionPipeline
35
  import torch
36
 
37
- # Preload the model globally
38
- device = "cuda" if torch.cuda.is_available() else "cpu"
39
- model = StableDiffusionPipeline.from_pretrained(
40
- "stabilityai/stable-diffusion-2-1-base",
41
- torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32
42
- )
43
- model.to(device)
44
 
45
  def generate_image(prompt):
46
  """Generate an image from a text prompt."""
47
- image = model(prompt).images[0]
48
- output_path = "output.png"
49
- image.save(output_path)
50
- return output_path
 
31
  # print("Generated Image Path:", generate_image(prompt))
32
 
33
 
34
+ # from diffusers import StableDiffusionPipeline
35
+ # import torch
36
+
37
+ # # Preload the model globally
38
+ # device = "cuda" if torch.cuda.is_available() else "cpu"
39
+ # model = StableDiffusionPipeline.from_pretrained(
40
+ # "stabilityai/stable-diffusion-2-1-base",
41
+ # torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32
42
+ # )
43
+ # model.to(device)
44
+
45
+ # def generate_image(prompt):
46
+ # """Generate an image from a text prompt."""
47
+ # image = model(prompt).images[0]
48
+ # output_path = "output.png"
49
+ # image.save(output_path)
50
+ # return output_path
51
+
52
  from diffusers import StableDiffusionPipeline
53
  import torch
54
 
55
+ # Load the Stable Diffusion pipeline on the CPU
56
+ pipe = StableDiffusionPipeline.from_pretrained("runwayml/stable-diffusion-v1-5", torch_dtype=torch.float32)
57
+ pipe.to("cpu") # Force the pipeline to run on the CPU
 
 
 
 
58
 
59
  def generate_image(prompt):
60
  """Generate an image from a text prompt."""
61
+ image = pipe(prompt).images[0]
62
+ image_path = "output_image.png"
63
+ image.save(image_path)
64
+ return image_path
requirements.txt CHANGED
@@ -6,4 +6,5 @@ sentencepiece
6
  accelerate
7
  sacremoses
8
  --extra-index-url https://download.pytorch.org/whl/cu113
9
- torch
 
 
6
  accelerate
7
  sacremoses
8
  --extra-index-url https://download.pytorch.org/whl/cu113
9
+ torch
10
+ pillow