Spaces:
Runtime error
Runtime error
Create models/local_image_model.py
Browse files- models/local_image_model.py +27 -0
models/local_image_model.py
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
try:
|
| 2 |
+
from diffusers import StableDiffusionPipeline
|
| 3 |
+
import torch
|
| 4 |
+
from io import BytesIO
|
| 5 |
+
except ImportError:
|
| 6 |
+
StableDiffusionPipeline = None
|
| 7 |
+
torch = None
|
| 8 |
+
BytesIO = None
|
| 9 |
+
|
| 10 |
+
class ImageLocal:
|
| 11 |
+
def __init__(self):
|
| 12 |
+
self.pipe = None
|
| 13 |
+
self.load_model()
|
| 14 |
+
|
| 15 |
+
def load_model(self):
|
| 16 |
+
if StableDiffusionPipeline is None:
|
| 17 |
+
raise RuntimeError("Diffusers no disponible")
|
| 18 |
+
self.pipe = StableDiffusionPipeline.from_pretrained("runwayml/stable-diffusion-v1-5")
|
| 19 |
+
if torch.cuda.is_available():
|
| 20 |
+
self.pipe.to("cuda")
|
| 21 |
+
|
| 22 |
+
def generate(self, prompt: str) -> bytes:
|
| 23 |
+
image = self.pipe(prompt).images[0]
|
| 24 |
+
buf = BytesIO()
|
| 25 |
+
image.save(buf, format="PNG")
|
| 26 |
+
return buf.getvalue()
|
| 27 |
+
|