Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -7,21 +7,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
|
| 18 |
-
animegan = torch.hub.load(
|
| 19 |
-
"bryandlee/animegan2-pytorch:main",
|
| 20 |
-
"generator",
|
| 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,46 +15,50 @@ def pil_to_b64(img: Image.Image) -> str:
|
|
| 30 |
def b64_to_pil(b64: str) -> Image.Image:
|
| 31 |
return Image.open(io.BytesIO(base64.b64decode(b64))).convert("RGB")
|
| 32 |
|
| 33 |
-
|
| 34 |
-
def
|
| 35 |
-
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
else:
|
| 49 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
|
| 57 |
-
|
|
|
|
|
|
|
| 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 |
-
|
| 71 |
-
|
| 72 |
b64_out = gr.Textbox(label="Output Image (Base64)", lines=5)
|
| 73 |
-
|
| 74 |
|
| 75 |
demo.launch()
|
|
|
|
| 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 |
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()
|