Update app.py
Browse files
app.py
CHANGED
@@ -1,62 +1,77 @@
|
|
1 |
-
# app.py
|
2 |
import gradio as gr
|
3 |
-
import torch
|
4 |
-
from PIL import Image
|
5 |
from diffusers import StableDiffusionImg2ImgPipeline
|
6 |
-
from
|
|
|
|
|
|
|
7 |
import spaces
|
8 |
|
9 |
-
# Load
|
10 |
pipe_ghibli = StableDiffusionImg2ImgPipeline.from_pretrained(
|
11 |
"nitrosocke/Ghibli-Diffusion", torch_dtype=torch.float16
|
12 |
-
).to("cuda")
|
13 |
|
14 |
-
|
|
|
15 |
|
16 |
-
#
|
17 |
-
def
|
18 |
-
|
19 |
-
img.save(
|
20 |
-
return base64.b64encode(
|
21 |
|
22 |
-
def
|
23 |
-
|
24 |
-
return Image.open(io.BytesIO(
|
25 |
|
26 |
-
#
|
27 |
-
def
|
28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
29 |
if effect == "ghibli":
|
30 |
-
|
31 |
else:
|
32 |
-
|
33 |
-
return
|
34 |
-
|
35 |
-
@gr.utils.decorators.thread_safe()
|
36 |
-
@spaces.GPU # enables GPU on ZeroGPU Infra
|
37 |
-
def api_process(input_b64, effect):
|
38 |
-
return run_effect(input_b64, effect)
|
39 |
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
|
|
44 |
|
|
|
45 |
with gr.Blocks() as demo:
|
46 |
-
gr.Markdown("# Ghibli &
|
47 |
|
48 |
with gr.Tab("Web UI"):
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
|
|
|
|
|
|
|
|
|
|
61 |
|
62 |
demo.launch()
|
|
|
|
|
1 |
import gradio as gr
|
2 |
+
import torch
|
|
|
3 |
from diffusers import StableDiffusionImg2ImgPipeline
|
4 |
+
from torchvision import transforms
|
5 |
+
from PIL import Image
|
6 |
+
import io
|
7 |
+
import base64
|
8 |
import spaces
|
9 |
|
10 |
+
# Load Ghibli model
|
11 |
pipe_ghibli = StableDiffusionImg2ImgPipeline.from_pretrained(
|
12 |
"nitrosocke/Ghibli-Diffusion", torch_dtype=torch.float16
|
13 |
+
).to("cuda")
|
14 |
|
15 |
+
# Load CartoonGAN model
|
16 |
+
cartoon_model = torch.hub.load('AK391/CartoonGAN', 'cartoon_gan', pretrained=True).to("cuda").eval()
|
17 |
|
18 |
+
# Base64 utilities
|
19 |
+
def pil_to_base64(img: Image.Image) -> str:
|
20 |
+
buffer = io.BytesIO()
|
21 |
+
img.save(buffer, format="PNG")
|
22 |
+
return base64.b64encode(buffer.getvalue()).decode()
|
23 |
|
24 |
+
def base64_to_pil(b64: str) -> Image.Image:
|
25 |
+
image_data = base64.b64decode(b64)
|
26 |
+
return Image.open(io.BytesIO(image_data)).convert("RGB")
|
27 |
|
28 |
+
# CartoonGAN processor
|
29 |
+
def apply_cartoon_gan(img: Image.Image) -> Image.Image:
|
30 |
+
transform = transforms.Compose([
|
31 |
+
transforms.Resize((256, 256)),
|
32 |
+
transforms.ToTensor()
|
33 |
+
])
|
34 |
+
img_tensor = transform(img).unsqueeze(0).to("cuda")
|
35 |
+
with torch.no_grad():
|
36 |
+
output = cartoon_model(img_tensor)[0].clamp(0, 1).cpu()
|
37 |
+
output_pil = transforms.ToPILImage()(output)
|
38 |
+
return output_pil
|
39 |
+
|
40 |
+
# Unified effect processor
|
41 |
+
def process_image(input_image: Image.Image, effect: str) -> Image.Image:
|
42 |
if effect == "ghibli":
|
43 |
+
output_image = pipe_ghibli(prompt="ghibli style", image=input_image, strength=0.5, guidance_scale=7.5).images[0]
|
44 |
else:
|
45 |
+
output_image = apply_cartoon_gan(input_image)
|
46 |
+
return output_image
|
|
|
|
|
|
|
|
|
|
|
47 |
|
48 |
+
@spaces.GPU
|
49 |
+
def process_base64(input_b64: str, effect: str) -> str:
|
50 |
+
input_image = base64_to_pil(input_b64)
|
51 |
+
output_image = process_image(input_image, effect)
|
52 |
+
return pil_to_base64(output_image)
|
53 |
|
54 |
+
# Gradio UI
|
55 |
with gr.Blocks() as demo:
|
56 |
+
gr.Markdown("# 🎨 Ghibli & CartoonGAN Effects")
|
57 |
|
58 |
with gr.Tab("Web UI"):
|
59 |
+
with gr.Row():
|
60 |
+
image_input = gr.Image(type="pil", label="Upload Image")
|
61 |
+
effect_selector = gr.Radio(["ghibli", "cartoon"], label="Select Effect")
|
62 |
+
with gr.Row():
|
63 |
+
apply_button = gr.Button("Apply Effect")
|
64 |
+
with gr.Row():
|
65 |
+
image_output = gr.Image(label="Processed Image")
|
66 |
+
|
67 |
+
apply_button.click(process_image, [image_input, effect_selector], image_output)
|
68 |
+
|
69 |
+
with gr.Tab("Base64 API"):
|
70 |
+
base64_input = gr.Textbox(label="Input Image (Base64)", lines=5)
|
71 |
+
effect_choice = gr.Radio(["ghibli", "cartoon"], label="Select Effect")
|
72 |
+
api_button = gr.Button("Run API")
|
73 |
+
base64_output = gr.Textbox(label="Output Image (Base64)", lines=5)
|
74 |
+
|
75 |
+
api_button.click(process_base64, [base64_input, effect_choice], base64_output)
|
76 |
|
77 |
demo.launch()
|