Spaces:
Runtime error
Runtime error
File size: 1,481 Bytes
393b5eb 9c61b4b 393b5eb |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
from transformers.tools.base import Tool, get_default_device
from transformers.utils import is_accelerate_available, is_diffusers_available
if is_diffusers_available():
from diffusers import DiffusionPipeline
TEXT_TO_IMAGE_DESCRIPTION = (
"This is a tool that creates an image according to a prompt, which is a text description. It takes an input named `prompt` which "
"contains the image description and outputs an image."
)
class TextToImageTool(Tool):
default_checkpoint = "runwayml/stable-diffusion-v1-5"
description = TEXT_TO_IMAGE_DESCRIPTION
inputs = ['text']
outputs = ['image']
def __init__(self, device=None, **hub_kwargs) -> None:
if not is_accelerate_available():
raise ImportError("Accelerate should be installed in order to use tools.")
if not is_diffusers_available():
raise ImportError("Diffusers should be installed in order to use the StableDiffusionTool.")
super().__init__()
self.device = device
self.pipeline = None
self.hub_kwargs = hub_kwargs
def setup(self):
if self.device is None:
self.device = get_default_device()
self.pipeline = DiffusionPipeline.from_pretrained(self.default_checkpoint)
self.pipeline.to(self.device)
self.is_initialized = True
def __call__(self, prompt):
if not self.is_initialized:
self.setup()
return self.pipeline(prompt).images[0]
|