Prashanthsrn commited on
Commit
4e9fdd8
·
verified ·
1 Parent(s): 40d94e9

Update model.py

Browse files
Files changed (1) hide show
  1. model.py +21 -13
model.py CHANGED
@@ -2,18 +2,26 @@ from diffusers import StableDiffusionPipeline
2
  import torch
3
 
4
  def load_model(model_name):
5
- if model_name == "Stable Diffusion":
6
- # Check if GPU is available, else use CPU
7
- if torch.cuda.is_available():
8
- pipe = StableDiffusionPipeline.from_pretrained("CompVis/stable-diffusion-v1-4", torch_dtype=torch.float16)
9
- pipe = pipe.to("cuda") # Use GPU
10
- else:
11
- pipe = StableDiffusionPipeline.from_pretrained("CompVis/stable-diffusion-v1-4")
12
- pipe = pipe.to("cpu") # Use CPU if no GPU is found
13
- return pipe
 
 
 
 
14
 
15
  def generate_image(model, prompt, model_name):
16
- if model_name == "Stable Diffusion":
17
- result = model(prompt, num_inference_steps=20, height=512, width=512) # Reduced resolution for faster generation
18
- image = result.images[0]
19
- return image
 
 
 
 
 
2
  import torch
3
 
4
  def load_model(model_name):
5
+ try:
6
+ if model_name == "Stable Diffusion":
7
+ # Check if GPU is available, else use CPU
8
+ if torch.cuda.is_available():
9
+ pipe = StableDiffusionPipeline.from_pretrained("CompVis/stable-diffusion-v1-4", torch_dtype=torch.float16)
10
+ pipe = pipe.to("cuda") # Use GPU
11
+ else:
12
+ pipe = StableDiffusionPipeline.from_pretrained("CompVis/stable-diffusion-v1-4")
13
+ pipe = pipe.to("cpu") # Use CPU if no GPU is found
14
+ return pipe
15
+ except Exception as e:
16
+ print(f"Error loading model: {e}")
17
+ return None
18
 
19
  def generate_image(model, prompt, model_name):
20
+ try:
21
+ if model_name == "Stable Diffusion":
22
+ result = model(prompt, num_inference_steps=10, height=256, width=256) # Reduced resolution for faster generation
23
+ image = result.images[0]
24
+ return image
25
+ except Exception as e:
26
+ print(f"Error generating image: {e}")
27
+ return None