patrickvonplaten commited on
Commit
32e2fdf
1 Parent(s): 330339d

upload tool

Browse files
Files changed (5) hide show
  1. __init__.py +0 -0
  2. app.py +4 -0
  3. image_transformation.py +89 -0
  4. requirements.txt +5 -0
  5. tool_config.json +7 -0
__init__.py ADDED
File without changes
app.py ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ from transformers.tools.base import launch_gradio_demo
2
+ from image_transformation import ImageTransformationTool
3
+
4
+ launch_gradio_demo(ImageTransformationTool)
image_transformation.py ADDED
@@ -0,0 +1,89 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python3
2
+ from transformers.tools import Tool
3
+
4
+ import numpy as np
5
+ import torch
6
+
7
+ from transformers.tools.base import Tool, get_default_device
8
+ from transformers.utils import (
9
+ is_accelerate_available,
10
+ is_diffusers_available,
11
+ is_vision_available,
12
+ is_opencv_available,
13
+ )
14
+
15
+
16
+ if is_vision_available():
17
+ from PIL import Image
18
+
19
+ if is_diffusers_available():
20
+ from diffusers import StableDiffusionControlNetPipeline, ControlNetModel, UniPCMultistepScheduler
21
+
22
+ if is_opencv_available():
23
+ import cv2
24
+
25
+
26
+ IMAGE_TRANSFORMATION_DESCRIPTION = (
27
+ "This is a tool that transforms an image with ControlNet according to a prompt. It takes two inputs: `image`, which should be "
28
+ "the image to transform, and `prompt`, which should be the prompt to use to change it. It returns the "
29
+ "modified image."
30
+ )
31
+
32
+
33
+ class ControlNetTransformationTool(Tool):
34
+ default_stable_diffusion_checkpoint = "runwayml/stable-diffusion-v1-5"
35
+ default_controlnet_checkpoint = "lllyasviel/control_v11p_sd15_canny"
36
+ description = IMAGE_TRANSFORMATION_DESCRIPTION
37
+ inputs = ['image', 'text']
38
+ outputs = ['image']
39
+
40
+ def __init__(self, device=None, controlnet=None, stable_diffusion=None, **hub_kwargs) -> None:
41
+ if not is_accelerate_available():
42
+ raise ImportError("Accelerate should be installed in order to use tools.")
43
+ if not is_diffusers_available():
44
+ raise ImportError("Diffusers should be installed in order to use the StableDiffusionTool.")
45
+ if not is_vision_available():
46
+ raise ImportError("Pillow should be installed in order to use the StableDiffusionTool.")
47
+
48
+ super().__init__()
49
+
50
+ self.stable_diffusion = self.default_stable_diffusion_checkpoint
51
+ self.controlnet = self.default_controlnet_checkpoint
52
+
53
+ self.device = device
54
+ self.hub_kwargs = hub_kwargs
55
+
56
+ def setup(self):
57
+ if self.device is None:
58
+ self.device = get_default_device()
59
+
60
+ controlnet = ControlNetModel.from_pretrained(self.controlnet)
61
+ self.pipeline = StableDiffusionControlNetPipeline.from_pretrained(self.stable_diffusion, controlnet=controlnet)
62
+ self.pipeline.scheduler = UniPCMultistepScheduler.from_config(self.pipeline.scheduler.config)
63
+
64
+ self.pipeline.to(self.device)
65
+ if self.device.type == "cuda":
66
+ self.pipeline.to(torch_dtype=torch.float16)
67
+
68
+ self.is_initialized = True
69
+
70
+ def __call__(self, image, prompt):
71
+ if not self.is_initialized:
72
+ self.setup()
73
+
74
+ image = np.array(image)
75
+
76
+ image = cv2.Canny(image, 100, 200)
77
+ image = image[:, :, None]
78
+ image = np.concatenate([image, image, image], axis=2)
79
+ image = Image.fromarray(image)
80
+
81
+ negative_prompt = "low quality, bad quality, deformed, low resolution"
82
+ added_prompt = " , highest quality, highly realistic, very high resolution"
83
+
84
+ return self.pipeline(
85
+ prompt + added_prompt,
86
+ image,
87
+ negative_prompt=negative_prompt,
88
+ num_inference_steps=30,
89
+ ).images[0]
requirements.txt ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ transformers @ git+https://github.com/huggingface/transformers@test_composition
2
+ diffusers
3
+ accelerate
4
+ opencv-python
5
+ torch
tool_config.json ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ {
2
+ "image-transformation": {
3
+ "tool_class": "image_transformation.ControlNetTransformationTool",
4
+ "description": "This is a tool that transforms an image according to a prompt. It takes two inputs: `image`, which should be the image to transform, and `prompt`, which should be the prompt to use to change it. The prompt should only contain descriptive adjectives, as if completing the prompt of the original image. It returns the modified image.",
5
+ "name": "image_transformer"
6
+ }
7
+ }