Update app.py
Browse files
app.py
CHANGED
@@ -1,58 +1,69 @@
|
|
1 |
import gradio as gr
|
2 |
import torch
|
3 |
-
|
4 |
-
from torchvision import transforms
|
5 |
from PIL import Image
|
6 |
-
|
|
|
7 |
|
8 |
-
# Ghibli model
|
9 |
pipe_ghibli = StableDiffusionImg2ImgPipeline.from_pretrained(
|
10 |
-
"nitrosocke/Ghibli-Diffusion",
|
|
|
11 |
).to("cuda")
|
12 |
|
13 |
-
#
|
14 |
-
|
15 |
-
|
16 |
-
|
|
|
17 |
).to("cuda").eval()
|
18 |
|
19 |
-
|
20 |
-
|
21 |
-
|
|
|
|
|
|
|
|
|
22 |
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
|
|
|
|
|
|
29 |
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
|
|
35 |
|
36 |
@spaces.GPU
|
37 |
-
def process_base64(b64, effect):
|
38 |
img = b64_to_pil(b64)
|
39 |
out = process_image(img, effect)
|
40 |
return pil_to_b64(out)
|
41 |
|
42 |
-
# Gradio UI
|
43 |
with gr.Blocks() as demo:
|
44 |
-
gr.Markdown("# 🎨 Ghibli &
|
|
|
45 |
with gr.Tab("Web UI"):
|
46 |
inp = gr.Image(type="pil")
|
47 |
-
eff = gr.Radio(["ghibli","
|
48 |
-
btn = gr.Button("Apply")
|
49 |
out_img = gr.Image()
|
50 |
btn.click(process_image, [inp, eff], out_img)
|
|
|
51 |
with gr.Tab("Base64 API"):
|
52 |
-
|
53 |
-
eff2 = gr.Radio(["ghibli","
|
54 |
btn2 = gr.Button("Run API")
|
55 |
-
|
56 |
-
btn2.click(process_base64, [
|
57 |
|
58 |
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
import torch
|
3 |
+
import io, base64
|
|
|
4 |
from PIL import Image
|
5 |
+
from diffusers import StableDiffusionImg2ImgPipeline
|
6 |
+
import spaces
|
7 |
|
8 |
+
# Load Ghibli model
|
9 |
pipe_ghibli = StableDiffusionImg2ImgPipeline.from_pretrained(
|
10 |
+
"nitrosocke/Ghibli-Diffusion",
|
11 |
+
torch_dtype=torch.float16
|
12 |
).to("cuda")
|
13 |
|
14 |
+
# Load AnimeGANv2 model
|
15 |
+
animegan = torch.hub.load(
|
16 |
+
"bryandlee/animegan2-pytorch:main",
|
17 |
+
"generator",
|
18 |
+
pretrained="face_paint_512_v2"
|
19 |
).to("cuda").eval()
|
20 |
|
21 |
+
def pil_to_b64(img: Image.Image) -> str:
|
22 |
+
buf = io.BytesIO()
|
23 |
+
img.save(buf, format="PNG")
|
24 |
+
return base64.b64encode(buf.getvalue()).decode()
|
25 |
+
|
26 |
+
def b64_to_pil(b64: str) -> Image.Image:
|
27 |
+
return Image.open(io.BytesIO(base64.b64decode(b64))).convert("RGB")
|
28 |
|
29 |
+
def apply_animegan(img: Image.Image) -> Image.Image:
|
30 |
+
img = img.resize((512,512))
|
31 |
+
tensor = torch.from_numpy(
|
32 |
+
(torch.FloatTensor(torch.ByteTensor(torch.ByteStorage.from_buffer(img.tobytes())))
|
33 |
+
.float()/255.)
|
34 |
+
).permute(2,0,1).unsqueeze(0).to("cuda")
|
35 |
+
with torch.no_grad():
|
36 |
+
out = animegan(tensor)[0].clamp(0,1).cpu()
|
37 |
+
return Image.fromarray((out.permute(1,2,0).numpy()*255).astype("uint8"))
|
38 |
|
39 |
+
def process_image(img: Image.Image, effect: str) -> Image.Image:
|
40 |
+
if effect == "ghibli":
|
41 |
+
return pipe_ghibli(prompt="ghibli style", image=img,
|
42 |
+
strength=0.5, guidance_scale=7.5).images[0]
|
43 |
+
else:
|
44 |
+
return apply_animegan(img)
|
45 |
|
46 |
@spaces.GPU
|
47 |
+
def process_base64(b64: str, effect: str) -> str:
|
48 |
img = b64_to_pil(b64)
|
49 |
out = process_image(img, effect)
|
50 |
return pil_to_b64(out)
|
51 |
|
|
|
52 |
with gr.Blocks() as demo:
|
53 |
+
gr.Markdown("# 🎨 Ghibli & AnimeGANv2 (Cartoon) Effects")
|
54 |
+
|
55 |
with gr.Tab("Web UI"):
|
56 |
inp = gr.Image(type="pil")
|
57 |
+
eff = gr.Radio(["ghibli", "anime"], label="Effect")
|
58 |
+
btn = gr.Button("Apply Effect")
|
59 |
out_img = gr.Image()
|
60 |
btn.click(process_image, [inp, eff], out_img)
|
61 |
+
|
62 |
with gr.Tab("Base64 API"):
|
63 |
+
b64_in = gr.Textbox(lines=5)
|
64 |
+
eff2 = gr.Radio(["ghibli", "anime"], label="Effect")
|
65 |
btn2 = gr.Button("Run API")
|
66 |
+
b64_out = gr.Textbox(lines=5)
|
67 |
+
btn2.click(process_base64, [b64_in, eff2], b64_out)
|
68 |
|
69 |
demo.launch()
|