Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,12 +3,52 @@ import torch
|
|
| 3 |
import spaces
|
| 4 |
from PIL import Image
|
| 5 |
from torchvision import transforms
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
from transformers import AutoModelForImageSegmentation
|
| 7 |
|
| 8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
model = AutoModelForImageSegmentation.from_pretrained(
|
| 10 |
-
|
| 11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
@spaces.GPU
|
| 14 |
def remove_background(image, resolution="1024x1024"):
|
|
@@ -46,7 +86,7 @@ demo = gr.Interface(
|
|
| 46 |
],
|
| 47 |
outputs=gr.Image(type="pil", label="Result"),
|
| 48 |
title="ToonOut - Anime Background Removal",
|
| 49 |
-
description="99.5% accuracy on anime/cartoon images. MIT License - free for commercial use."
|
| 50 |
)
|
| 51 |
|
| 52 |
demo.launch()
|
|
|
|
| 3 |
import spaces
|
| 4 |
from PIL import Image
|
| 5 |
from torchvision import transforms
|
| 6 |
+
from huggingface_hub import hf_hub_download
|
| 7 |
+
|
| 8 |
+
# Fix for BiRefNet compatibility
|
| 9 |
+
import transformers.configuration_utils
|
| 10 |
+
original_getattribute = transformers.configuration_utils.PretrainedConfig.__getattribute__
|
| 11 |
+
def patched_getattribute(self, key):
|
| 12 |
+
if key == 'is_encoder_decoder':
|
| 13 |
+
return False
|
| 14 |
+
return original_getattribute(self, key)
|
| 15 |
+
transformers.configuration_utils.PretrainedConfig.__getattribute__ = patched_getattribute
|
| 16 |
+
|
| 17 |
from transformers import AutoModelForImageSegmentation
|
| 18 |
|
| 19 |
+
# Download ToonOut weights
|
| 20 |
+
print("Downloading ToonOut weights...")
|
| 21 |
+
weights_path = hf_hub_download(
|
| 22 |
+
repo_id="joelseytre/toonout",
|
| 23 |
+
filename="birefnet_finetuned_toonout.pth"
|
| 24 |
+
)
|
| 25 |
+
print(f"Weights downloaded to: {weights_path}")
|
| 26 |
+
|
| 27 |
+
# Load base BiRefNet model
|
| 28 |
+
print("Loading BiRefNet base model...")
|
| 29 |
model = AutoModelForImageSegmentation.from_pretrained(
|
| 30 |
+
"ZhengPeng7/BiRefNet",
|
| 31 |
+
trust_remote_code=True
|
| 32 |
+
)
|
| 33 |
+
|
| 34 |
+
# Load ToonOut fine-tuned weights
|
| 35 |
+
print("Applying ToonOut weights...")
|
| 36 |
+
state_dict = torch.load(weights_path, map_location='cpu')
|
| 37 |
+
clean_state_dict = {}
|
| 38 |
+
for k, v in state_dict.items():
|
| 39 |
+
if k.startswith("module._orig_mod."):
|
| 40 |
+
clean_state_dict[k[len("module._orig_mod."):]] = v
|
| 41 |
+
elif k.startswith("module."):
|
| 42 |
+
clean_state_dict[k[len("module."):]] = v
|
| 43 |
+
else:
|
| 44 |
+
clean_state_dict[k] = v
|
| 45 |
+
|
| 46 |
+
model.load_state_dict(clean_state_dict)
|
| 47 |
+
model.eval()
|
| 48 |
+
print("ToonOut model loaded successfully!")
|
| 49 |
+
|
| 50 |
+
device = 'cuda' if torch.cuda.is_available() else 'cpu'
|
| 51 |
+
model.to(device)
|
| 52 |
|
| 53 |
@spaces.GPU
|
| 54 |
def remove_background(image, resolution="1024x1024"):
|
|
|
|
| 86 |
],
|
| 87 |
outputs=gr.Image(type="pil", label="Result"),
|
| 88 |
title="ToonOut - Anime Background Removal",
|
| 89 |
+
description="99.5% accuracy on anime/cartoon images. Fine-tuned BiRefNet for anime. MIT License - free for commercial use."
|
| 90 |
)
|
| 91 |
|
| 92 |
demo.launch()
|