Munaf1987 commited on
Commit
76217ba
·
verified ·
1 Parent(s): 0d40aa7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +43 -32
app.py CHANGED
@@ -1,58 +1,69 @@
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, base64, spaces
 
7
 
8
- # Ghibli model
9
  pipe_ghibli = StableDiffusionImg2ImgPipeline.from_pretrained(
10
- "nitrosocke/Ghibli-Diffusion", torch_dtype=torch.float16
 
11
  ).to("cuda")
12
 
13
- # CartoonGAN model via torch.hub
14
- cartoon_model = torch.hub.load(
15
- 'znxlwm/pytorch-CartoonGAN', 'CartoonGAN',
16
- pretrained=True, trust_repo=True
 
17
  ).to("cuda").eval()
18
 
19
- # Helpers: PIL Base64
20
- def pil_to_b64(img): buf=io.BytesIO(); img.save(buf,"PNG"); return base64.b64encode(buf.getvalue()).decode()
21
- def b64_to_pil(b): return Image.open(io.BytesIO(base64.b64decode(b))).convert("RGB")
 
 
 
 
22
 
23
- # CartoonGAN processor
24
- def apply_cartoon(img):
25
- t=transforms.Compose([transforms.Resize((256,256)), transforms.ToTensor()])
26
- x=t(img).unsqueeze(0).to("cuda")
27
- with torch.no_grad(): y=cartoon_model(x)[0].clamp(0,1).cpu()
28
- return transforms.ToPILImage()(y)
 
 
 
29
 
30
- # Unified image processor
31
- def process_image(img, effect):
32
- if effect=="ghibli":
33
- return pipe_ghibli(prompt="ghibli style", image=img, strength=0.5, guidance_scale=7.5).images[0]
34
- return apply_cartoon(img)
 
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 & CartoonGAN Effects (ZeroGPU)")
 
45
  with gr.Tab("Web UI"):
46
  inp = gr.Image(type="pil")
47
- eff = gr.Radio(["ghibli","cartoon"], label="Effect")
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
- in_b64 = gr.Textbox(lines=5)
53
- eff2 = gr.Radio(["ghibli","cartoon"], label="Effect")
54
  btn2 = gr.Button("Run API")
55
- out_b64 = gr.Textbox(lines=5)
56
- btn2.click(process_base64, [in_b64, eff2], out_b64)
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()