Spaces:
Build error
Build error
Update model.py
Browse files
model.py
CHANGED
|
@@ -2,18 +2,26 @@ from diffusers import StableDiffusionPipeline
|
|
| 2 |
import torch
|
| 3 |
|
| 4 |
def load_model(model_name):
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
def generate_image(model, prompt, model_name):
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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
|