Spaces:
Sleeping
Sleeping
Enable ZeroGPU: add spaces dep and @spaces.GPU decorator
Browse files- README.md +1 -0
- app.py +11 -10
- requirements.txt +1 -0
README.md
CHANGED
|
@@ -7,6 +7,7 @@ sdk: gradio
|
|
| 7 |
sdk_version: 6.15.2
|
| 8 |
python_version: "3.12"
|
| 9 |
app_file: app.py
|
|
|
|
| 10 |
pinned: false
|
| 11 |
license: mit
|
| 12 |
---
|
|
|
|
| 7 |
sdk_version: 6.15.2
|
| 8 |
python_version: "3.12"
|
| 9 |
app_file: app.py
|
| 10 |
+
suggested_hardware: zero-a10g
|
| 11 |
pinned: false
|
| 12 |
license: mit
|
| 13 |
---
|
app.py
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import numpy as np
|
| 3 |
import torch
|
|
@@ -6,27 +7,26 @@ from transformers import AutoImageProcessor, AutoModel
|
|
| 6 |
|
| 7 |
REPO_NAME = "p1atdev/MangaLineExtraction-hf"
|
| 8 |
|
| 9 |
-
# Load
|
| 10 |
-
#
|
| 11 |
model = AutoModel.from_pretrained(REPO_NAME, trust_remote_code=True)
|
| 12 |
processor = AutoImageProcessor.from_pretrained(REPO_NAME, trust_remote_code=True)
|
| 13 |
model.eval()
|
|
|
|
| 14 |
|
| 15 |
-
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 16 |
-
# The checkpoint is bf16; cast to float32 on CPU for stable inference.
|
| 17 |
-
model = model.to(device=device, dtype=torch.float32)
|
| 18 |
|
| 19 |
-
|
| 20 |
-
@torch.no_grad()
|
| 21 |
def extract_lines(image: Image.Image) -> Image.Image:
|
| 22 |
if image is None:
|
| 23 |
return None
|
| 24 |
|
| 25 |
image = image.convert("RGB")
|
| 26 |
inputs = processor(image, return_tensors="pt")
|
| 27 |
-
pixel_values = inputs.pixel_values.to(device=device, dtype=torch.float32)
|
| 28 |
|
| 29 |
-
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
line = outputs.pixel_values[0].cpu().float().numpy()
|
| 32 |
line = np.clip(line, 0, 255).astype("uint8")
|
|
@@ -41,7 +41,8 @@ demo = gr.Interface(
|
|
| 41 |
description=(
|
| 42 |
"Extract clean line art from manga / illustration images using "
|
| 43 |
"[p1atdev/MangaLineExtraction-hf](https://huggingface.co/p1atdev/MangaLineExtraction-hf). "
|
| 44 |
-
"Upload an image and the model returns a grayscale line drawing."
|
|
|
|
| 45 |
),
|
| 46 |
flagging_mode="never",
|
| 47 |
)
|
|
|
|
| 1 |
+
import spaces
|
| 2 |
import gradio as gr
|
| 3 |
import numpy as np
|
| 4 |
import torch
|
|
|
|
| 7 |
|
| 8 |
REPO_NAME = "p1atdev/MangaLineExtraction-hf"
|
| 9 |
|
| 10 |
+
# Load on CPU at startup. On ZeroGPU there is no CUDA device until we enter a
|
| 11 |
+
# @spaces.GPU function, so the actual .to("cuda") happens inside extract_lines.
|
| 12 |
model = AutoModel.from_pretrained(REPO_NAME, trust_remote_code=True)
|
| 13 |
processor = AutoImageProcessor.from_pretrained(REPO_NAME, trust_remote_code=True)
|
| 14 |
model.eval()
|
| 15 |
+
model = model.to(dtype=torch.float32)
|
| 16 |
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
+
@spaces.GPU
|
|
|
|
| 19 |
def extract_lines(image: Image.Image) -> Image.Image:
|
| 20 |
if image is None:
|
| 21 |
return None
|
| 22 |
|
| 23 |
image = image.convert("RGB")
|
| 24 |
inputs = processor(image, return_tensors="pt")
|
|
|
|
| 25 |
|
| 26 |
+
with torch.no_grad():
|
| 27 |
+
model.to("cuda")
|
| 28 |
+
pixel_values = inputs.pixel_values.to(device="cuda", dtype=torch.float32)
|
| 29 |
+
outputs = model(pixel_values)
|
| 30 |
|
| 31 |
line = outputs.pixel_values[0].cpu().float().numpy()
|
| 32 |
line = np.clip(line, 0, 255).astype("uint8")
|
|
|
|
| 41 |
description=(
|
| 42 |
"Extract clean line art from manga / illustration images using "
|
| 43 |
"[p1atdev/MangaLineExtraction-hf](https://huggingface.co/p1atdev/MangaLineExtraction-hf). "
|
| 44 |
+
"Upload an image and the model returns a grayscale line drawing. "
|
| 45 |
+
"Runs on ZeroGPU."
|
| 46 |
),
|
| 47 |
flagging_mode="never",
|
| 48 |
)
|
requirements.txt
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
|
|
| 1 |
torch
|
| 2 |
transformers
|
| 3 |
huggingface_hub<1.0
|
|
|
|
| 1 |
+
spaces
|
| 2 |
torch
|
| 3 |
transformers
|
| 4 |
huggingface_hub<1.0
|