Create text-to-image
#356
by
Athmiiii - opened
- tools/text-to-image +16 -0
tools/text-to-image
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from diffusers import pipeline
|
| 2 |
+
import uuid
|
| 3 |
+
import os
|
| 4 |
+
|
| 5 |
+
class TextToImageTool:
|
| 6 |
+
def __init__(self, model_name="stabilityai/stable-diffusion-2"):
|
| 7 |
+
self.pipe = pipeline("text-to-image", model=model_name)
|
| 8 |
+
self.output_dir = "generated_images"
|
| 9 |
+
os.makedirs(self.output_dir, exist_ok=True)
|
| 10 |
+
|
| 11 |
+
def __call__(self, prompt: str) -> str:
|
| 12 |
+
"""Generates image from text and returns local file path"""
|
| 13 |
+
result = self.pipe(prompt)
|
| 14 |
+
image_path = os.path.join(self.output_dir, f"{uuid.uuid4()}.png")
|
| 15 |
+
result.images[0].save(image_path)
|
| 16 |
+
return image_path
|