lysandre HF staff commited on
Commit
393b5eb
0 Parent(s):

Initial commit

Browse files
Files changed (6) hide show
  1. README.md +12 -0
  2. __init__.py +0 -0
  3. app.py +4 -0
  4. requirements.txt +2 -0
  5. text_to_image.py +44 -0
  6. tool_config.json +3 -0
README.md ADDED
@@ -0,0 +1,12 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ title: Text to Image
3
+ emoji: ⚡
4
+ colorFrom: blue
5
+ colorTo: green
6
+ sdk: gradio
7
+ sdk_version: 3.27.0
8
+ app_file: app.py
9
+ pinned: false
10
+ tags:
11
+ - tool
12
+ ---
__init__.py ADDED
File without changes
app.py ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ from transformers.tools.base import launch_gradio_demo
2
+ from text_to_image import TextToImageTool
3
+
4
+ launch_gradio_demo(TextToImageTool)
requirements.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ transformers @ git+https://github.com/huggingface/transformers@test_composition
2
+ diffusers
text_to_image.py ADDED
@@ -0,0 +1,44 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers.tools.base import Tool, get_default_device
2
+ from transformers.utils import is_accelerate_available, is_diffusers_available
3
+
4
+ if is_diffusers_available():
5
+ from diffusers import DiffusionPipeline
6
+
7
+
8
+ TEXT_TO_IMAGE_DESCRIPTION = (
9
+ "This is a tool that creates an image according to a prompt, which is a text description. It takes an input named `prompt` which "
10
+ "contains the image description and outputs an image."
11
+ )
12
+
13
+
14
+ class TextToImageTool(Tool):
15
+ default_checkpoint = "runwayml/stable-diffusion-v1-5"
16
+ description = TEXT_TO_IMAGE_DESCRIPTION
17
+
18
+ def __init__(self, device=None, **hub_kwargs) -> None:
19
+ if not is_accelerate_available():
20
+ raise ImportError("Accelerate should be installed in order to use tools.")
21
+ if not is_diffusers_available():
22
+ raise ImportError("Diffusers should be installed in order to use the StableDiffusionTool.")
23
+
24
+ super().__init__()
25
+
26
+ self.device = device
27
+ self.pipeline = None
28
+ self.hub_kwargs = hub_kwargs
29
+
30
+ def setup(self):
31
+ if self.device is None:
32
+ self.device = get_default_device()
33
+
34
+ self.pipeline = DiffusionPipeline.from_pretrained(self.default_checkpoint)
35
+ self.pipeline.to(self.device)
36
+
37
+ self.is_initialized = True
38
+
39
+ def __call__(self, prompt):
40
+ if not self.is_initialized:
41
+ self.setup()
42
+
43
+ return self.pipeline(prompt).images[0]
44
+
tool_config.json ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ {
2
+ "custom_tools": {"text-to-image": "text_to_image.TextToImageTool"}
3
+ }