Munaf1987 commited on
Commit
c88a35f
Β·
verified Β·
1 Parent(s): adde61a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +50 -30
app.py CHANGED
@@ -6,7 +6,9 @@ from PIL import Image
6
  import io
7
  import base64
8
  import spaces
 
9
 
 
10
  def pil_to_b64(img: Image.Image) -> str:
11
  buf = io.BytesIO()
12
  img.save(buf, format="PNG")
@@ -15,50 +17,68 @@ def pil_to_b64(img: Image.Image) -> str:
15
  def b64_to_pil(b64: str) -> Image.Image:
16
  return Image.open(io.BytesIO(base64.b64decode(b64))).convert("RGB")
17
 
18
- @spaces.GPU
19
- def process_base64(b64: str, effect: str) -> str:
20
- img = b64_to_pil(b64)
 
 
 
 
 
 
21
 
22
- if effect == "ghibli":
23
- # Load model inside GPU function (ZeroGPU compliant)
24
- pipe_ghibli = StableDiffusionImg2ImgPipeline.from_pretrained(
25
- "nitrosocke/Ghibli-Diffusion",
26
- torch_dtype=torch.float16,
27
- use_safetensors=True
28
- ).to("cuda")
29
-
30
- output_image = pipe_ghibli(prompt="ghibli style", image=img, strength=0.5, guidance_scale=7.5).images[0]
31
 
 
 
 
 
 
 
32
  else:
33
- # Load model inside GPU function (ZeroGPU compliant)
34
- animegan = torch.hub.load(
35
- "bryandlee/animegan2-pytorch:main",
36
- "generator",
37
- pretrained="face_paint_512_v2"
38
- ).to("cuda").eval()
39
-
40
  transform = transforms.Compose([
41
  transforms.Resize((512, 512)),
42
  transforms.ToTensor()
43
  ])
44
  img_tensor = transform(img).unsqueeze(0).to("cuda")
45
-
46
  with torch.no_grad():
47
  out = animegan(img_tensor)[0].clamp(0, 1).cpu()
 
 
48
 
49
- output_image = transforms.ToPILImage()(out)
50
-
51
- return pil_to_b64(output_image)
 
 
 
52
 
53
- # Gradio UI (runs in CPU - ZeroGPU safe)
54
  with gr.Blocks() as demo:
55
  gr.Markdown("# 🎨 Ghibli & AnimeGAN Effects (ZeroGPU Compatible)")
56
 
 
 
 
 
 
 
 
 
 
57
  with gr.Tab("Base64 API"):
58
- b64_in = gr.Textbox(label="Input Image (Base64)", lines=5)
59
- eff = gr.Radio(["ghibli", "anime"], label="Select Effect")
60
- btn = gr.Button("Run API")
61
- b64_out = gr.Textbox(label="Output Image (Base64)", lines=5)
62
- btn.click(process_base64, [b64_in, eff], b64_out)
63
 
64
- demo.launch()
 
6
  import io
7
  import base64
8
  import spaces
9
+ from functools import lru_cache
10
 
11
+ # Base64 utilities
12
  def pil_to_b64(img: Image.Image) -> str:
13
  buf = io.BytesIO()
14
  img.save(buf, format="PNG")
 
17
  def b64_to_pil(b64: str) -> Image.Image:
18
  return Image.open(io.BytesIO(base64.b64decode(b64))).convert("RGB")
19
 
20
+ # βœ… Cached Model Loaders (ZeroGPU Safe)
21
+ @lru_cache(maxsize=2)
22
+ def load_ghibli_model():
23
+ pipe = StableDiffusionImg2ImgPipeline.from_pretrained(
24
+ "nitrosocke/Ghibli-Diffusion",
25
+ torch_dtype=torch.float16,
26
+ use_safetensors=True
27
+ ).to("cuda")
28
+ return pipe
29
 
30
+ @lru_cache(maxsize=2)
31
+ def load_animegan_model():
32
+ model = torch.hub.load(
33
+ "bryandlee/animegan2-pytorch:main",
34
+ "generator",
35
+ pretrained="face_paint_512_v2"
36
+ ).to("cuda").eval()
37
+ return model
 
38
 
39
+ # βœ… Image Processing (Gradio Image Upload)
40
+ @spaces.GPU
41
+ def process_image(img: Image.Image, effect: str) -> Image.Image:
42
+ if effect == "ghibli":
43
+ pipe = load_ghibli_model()
44
+ out_img = pipe(prompt="ghibli style", image=img, strength=0.5, guidance_scale=7.5).images[0]
45
  else:
46
+ animegan = load_animegan_model()
 
 
 
 
 
 
47
  transform = transforms.Compose([
48
  transforms.Resize((512, 512)),
49
  transforms.ToTensor()
50
  ])
51
  img_tensor = transform(img).unsqueeze(0).to("cuda")
 
52
  with torch.no_grad():
53
  out = animegan(img_tensor)[0].clamp(0, 1).cpu()
54
+ out_img = transforms.ToPILImage()(out)
55
+ return out_img
56
 
57
+ # βœ… Base64 API Processing
58
+ @spaces.GPU
59
+ def process_base64(b64: str, effect: str) -> str:
60
+ img = b64_to_pil(b64)
61
+ out_img = process_image(img, effect)
62
+ return pil_to_b64(out_img)
63
 
64
+ # βœ… Gradio UI
65
  with gr.Blocks() as demo:
66
  gr.Markdown("# 🎨 Ghibli & AnimeGAN Effects (ZeroGPU Compatible)")
67
 
68
+ # Image Upload Tab
69
+ with gr.Tab("Web UI"):
70
+ img_input = gr.Image(type="pil", label="Upload Image")
71
+ effect_choice = gr.Radio(["ghibli", "anime"], label="Select Effect")
72
+ process_btn = gr.Button("Apply Effect")
73
+ img_output = gr.Image(label="Processed Image")
74
+ process_btn.click(process_image, [img_input, effect_choice], img_output)
75
+
76
+ # Base64 API Tab
77
  with gr.Tab("Base64 API"):
78
+ b64_input = gr.Textbox(label="Input Image (Base64)", lines=5)
79
+ effect_choice_b64 = gr.Radio(["ghibli", "anime"], label="Select Effect")
80
+ process_btn_b64 = gr.Button("Run API")
81
+ b64_output = gr.Textbox(label="Output Image (Base64)", lines=5)
82
+ process_btn_b64.click(process_base64, [b64_input, effect_choice_b64], b64_output)
83
 
84
+ demo.launch()