Munaf1987 commited on
Commit
7fe7d39
·
verified ·
1 Parent(s): 76217ba

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -19
app.py CHANGED
@@ -1,14 +1,17 @@
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
@@ -18,6 +21,7 @@ animegan = torch.hub.load(
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")
@@ -26,20 +30,21 @@ def pil_to_b64(img: Image.Image) -> str:
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
 
@@ -49,21 +54,22 @@ def process_base64(b64: str, effect: str) -> str:
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()
 
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",
13
+ torch_dtype=torch.float16,
14
+ use_safetensors=True
15
  ).to("cuda")
16
 
17
  # Load AnimeGANv2 model
 
21
  pretrained="face_paint_512_v2"
22
  ).to("cuda").eval()
23
 
24
+ # Base64 conversion helpers
25
  def pil_to_b64(img: Image.Image) -> str:
26
  buf = io.BytesIO()
27
  img.save(buf, format="PNG")
 
30
  def b64_to_pil(b64: str) -> Image.Image:
31
  return Image.open(io.BytesIO(base64.b64decode(b64))).convert("RGB")
32
 
33
+ # AnimeGAN processor
34
  def apply_animegan(img: Image.Image) -> Image.Image:
35
+ transform = transforms.Compose([
36
+ transforms.Resize((512, 512)),
37
+ transforms.ToTensor()
38
+ ])
39
+ img_tensor = transform(img).unsqueeze(0).to("cuda")
40
  with torch.no_grad():
41
+ out = animegan(img_tensor)[0].clamp(0, 1).cpu()
42
+ return transforms.ToPILImage()(out)
43
 
44
+ # Unified image processor
45
  def process_image(img: Image.Image, effect: str) -> Image.Image:
46
  if effect == "ghibli":
47
+ return pipe_ghibli(prompt="ghibli style", image=img, strength=0.5, guidance_scale=7.5).images[0]
 
48
  else:
49
  return apply_animegan(img)
50
 
 
54
  out = process_image(img, effect)
55
  return pil_to_b64(out)
56
 
57
+ # Gradio UI
58
  with gr.Blocks() as demo:
59
+ gr.Markdown("# 🎨 Ghibli & AnimeGAN Effects (ZeroGPU Compatible)")
60
 
61
  with gr.Tab("Web UI"):
62
+ inp = gr.Image(type="pil", label="Upload Image")
63
+ eff = gr.Radio(["ghibli", "anime"], label="Select Effect")
64
  btn = gr.Button("Apply Effect")
65
+ out_img = gr.Image(label="Output Image")
66
  btn.click(process_image, [inp, eff], out_img)
67
 
68
  with gr.Tab("Base64 API"):
69
+ b64_in = gr.Textbox(label="Input Image (Base64)", lines=5)
70
+ eff2 = gr.Radio(["ghibli", "anime"], label="Select Effect")
71
  btn2 = gr.Button("Run API")
72
+ b64_out = gr.Textbox(label="Output Image (Base64)", lines=5)
73
  btn2.click(process_base64, [b64_in, eff2], b64_out)
74
 
75
  demo.launch()