Fix: Correct EndpointHandler class name and add robust error handling for model loading
Browse files- handler.py +24 -18
handler.py
CHANGED
|
@@ -15,27 +15,33 @@ import random
|
|
| 15 |
import math
|
| 16 |
import re
|
| 17 |
|
| 18 |
-
class
|
| 19 |
-
def __init__(self):
|
| 20 |
self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 21 |
self.model_id = "runwayml/stable-diffusion-v1-5"
|
| 22 |
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
self.
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
|
| 40 |
def __call__(self, inputs: Union[str, Dict[str, Any]]) -> Image.Image:
|
| 41 |
"""
|
|
|
|
| 15 |
import math
|
| 16 |
import re
|
| 17 |
|
| 18 |
+
class EndpointHandler:
|
| 19 |
+
def __init__(self, path=""):
|
| 20 |
self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 21 |
self.model_id = "runwayml/stable-diffusion-v1-5"
|
| 22 |
|
| 23 |
+
try:
|
| 24 |
+
# Initialize the diffusion pipeline
|
| 25 |
+
self.pipe = StableDiffusionPipeline.from_pretrained(
|
| 26 |
+
self.model_id,
|
| 27 |
+
torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,
|
| 28 |
+
safety_checker=None,
|
| 29 |
+
requires_safety_checker=False
|
| 30 |
+
).to(self.device)
|
| 31 |
+
|
| 32 |
+
# Use DDIM scheduler for better control
|
| 33 |
+
self.pipe.scheduler = DDIMScheduler.from_config(self.pipe.scheduler.config)
|
| 34 |
+
|
| 35 |
+
# CLIP model for guidance
|
| 36 |
+
self.clip_model = self.pipe.text_encoder
|
| 37 |
+
self.clip_tokenizer = self.pipe.tokenizer
|
| 38 |
+
|
| 39 |
+
print("DiffSketchEdit handler initialized successfully!")
|
| 40 |
+
except Exception as e:
|
| 41 |
+
print(f"Warning: Could not load diffusion model: {e}")
|
| 42 |
+
self.pipe = None
|
| 43 |
+
self.clip_model = None
|
| 44 |
+
self.clip_tokenizer = None
|
| 45 |
|
| 46 |
def __call__(self, inputs: Union[str, Dict[str, Any]]) -> Image.Image:
|
| 47 |
"""
|