Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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 |
-
|
| 19 |
-
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
output_image = pipe_ghibli(prompt="ghibli style", image=img, strength=0.5, guidance_scale=7.5).images[0]
|
| 31 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
else:
|
| 33 |
-
|
| 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 |
-
|
| 50 |
-
|
| 51 |
-
|
|
|
|
|
|
|
|
|
|
| 52 |
|
| 53 |
-
# Gradio UI
|
| 54 |
with gr.Blocks() as demo:
|
| 55 |
gr.Markdown("# 🎨 Ghibli & AnimeGAN Effects (ZeroGPU Compatible)")
|
| 56 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
with gr.Tab("Base64 API"):
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 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()
|