Spaces:
Runtime error
Runtime error
patrickvonplaten
commited on
Commit
•
ce1b66b
1
Parent(s):
4088da7
Upload tool
Browse files- app.py +4 -0
- image_upscaling.py +62 -0
- requirements.txt +3 -0
- tool_config.json +5 -0
app.py
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from transformers.tools.base import launch_gradio_demo
|
2 |
+
from image_upscaling import ImageUpscalingTool
|
3 |
+
|
4 |
+
launch_gradio_demo(ImageUpscalingTool)
|
image_upscaling.py
ADDED
@@ -0,0 +1,62 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
import numpy as np
|
3 |
+
import torch
|
4 |
+
|
5 |
+
from transformers.tools.base import Tool, get_default_device
|
6 |
+
from transformers.utils import (
|
7 |
+
is_accelerate_available,
|
8 |
+
is_diffusers_available,
|
9 |
+
)
|
10 |
+
|
11 |
+
|
12 |
+
if is_diffusers_available():
|
13 |
+
from diffusers import DiffusionPipeline
|
14 |
+
|
15 |
+
|
16 |
+
IMAGE_UPSCALING_DESCRIPTION = (
|
17 |
+
"This is a tool that upscales an image. It takes one input: `image`, which should be "
|
18 |
+
"the image to upscale. It returns the upscaled image."
|
19 |
+
)
|
20 |
+
|
21 |
+
|
22 |
+
class ImageUpscalingTool(Tool):
|
23 |
+
default_stable_diffusion_checkpoint = "stabilityai/sd-x2-latent-upscaler"
|
24 |
+
description = IMAGE_UPSCALING_DESCRIPTION
|
25 |
+
inputs = ['image']
|
26 |
+
outputs = ['image']
|
27 |
+
|
28 |
+
def __init__(self, device=None, controlnet=None, stable_diffusion=None, **hub_kwargs) -> None:
|
29 |
+
if not is_accelerate_available():
|
30 |
+
raise ImportError("Accelerate should be installed in order to use tools.")
|
31 |
+
if not is_diffusers_available():
|
32 |
+
raise ImportError("Diffusers should be installed in order to use the StableDiffusionTool.")
|
33 |
+
|
34 |
+
super().__init__()
|
35 |
+
|
36 |
+
self.stable_diffusion = self.default_stable_diffusion_checkpoint
|
37 |
+
|
38 |
+
self.device = device
|
39 |
+
self.hub_kwargs = hub_kwargs
|
40 |
+
|
41 |
+
def setup(self):
|
42 |
+
if self.device is None:
|
43 |
+
self.device = get_default_device()
|
44 |
+
|
45 |
+
self.pipeline = DiffusionPipeline.from_pretrained(self.stable_diffusion)
|
46 |
+
|
47 |
+
self.pipeline.to(self.device)
|
48 |
+
if self.device.type == "cuda":
|
49 |
+
self.pipeline.to(torch_dtype=torch.float16)
|
50 |
+
|
51 |
+
self.is_initialized = True
|
52 |
+
|
53 |
+
def __call__(self, image):
|
54 |
+
if not self.is_initialized:
|
55 |
+
self.setup()
|
56 |
+
|
57 |
+
return self.pipeline(
|
58 |
+
image=image,
|
59 |
+
prompt="",
|
60 |
+
num_inference_steps=30,
|
61 |
+
guidance_scale=0,
|
62 |
+
).images[0]
|
requirements.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
diffusers
|
2 |
+
transformers
|
3 |
+
torch
|
tool_config.json
ADDED
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"description": "This is a tool that upscales an image. It takes one input: `image`, which should be the image to upscale. It returns the upscaled image.",
|
3 |
+
"name": "",
|
4 |
+
"tool_class": "image_upscaling.ImageUpscalingTool"
|
5 |
+
}
|