Spaces:
Running on Zero
Running on Zero
fix: pass file paths instead of PIL objects to ZeroGPU worker to avoid serialization TypeError
Browse files
app.py
CHANGED
|
@@ -3,6 +3,7 @@ AD-Copilot Demo: Comparison-Aware Anomaly Detection with Vision-Language Model
|
|
| 3 |
"""
|
| 4 |
|
| 5 |
import os
|
|
|
|
| 6 |
import traceback
|
| 7 |
import spaces
|
| 8 |
import gradio as gr
|
|
@@ -36,17 +37,18 @@ model = AutoModelForImageTextToText.from_pretrained(
|
|
| 36 |
# ---------------------------------------------------------------------------
|
| 37 |
@spaces.GPU(duration=120)
|
| 38 |
def _predict_inner(
|
| 39 |
-
|
| 40 |
-
|
| 41 |
prompt: str,
|
| 42 |
max_new_tokens: int,
|
| 43 |
):
|
| 44 |
with torch.inference_mode():
|
| 45 |
max_new_tokens = int(max_new_tokens)
|
| 46 |
|
|
|
|
|
|
|
|
|
|
| 47 |
# Resize long edge to 512 to save GPU memory / speed up
|
| 48 |
-
reference_image = reference_image.copy()
|
| 49 |
-
test_image = test_image.copy()
|
| 50 |
reference_image.thumbnail((512, 512), Image.Resampling.LANCZOS)
|
| 51 |
test_image.thumbnail((512, 512), Image.Resampling.LANCZOS)
|
| 52 |
|
|
@@ -97,7 +99,17 @@ def predict(
|
|
| 97 |
if reference_image is None or test_image is None:
|
| 98 |
return "Please upload both a reference (good) image and a test image."
|
| 99 |
try:
|
| 100 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 101 |
except Exception as e:
|
| 102 |
tb = traceback.format_exc()
|
| 103 |
print(tb, flush=True)
|
|
|
|
| 3 |
"""
|
| 4 |
|
| 5 |
import os
|
| 6 |
+
import tempfile
|
| 7 |
import traceback
|
| 8 |
import spaces
|
| 9 |
import gradio as gr
|
|
|
|
| 37 |
# ---------------------------------------------------------------------------
|
| 38 |
@spaces.GPU(duration=120)
|
| 39 |
def _predict_inner(
|
| 40 |
+
ref_path: str,
|
| 41 |
+
test_path: str,
|
| 42 |
prompt: str,
|
| 43 |
max_new_tokens: int,
|
| 44 |
):
|
| 45 |
with torch.inference_mode():
|
| 46 |
max_new_tokens = int(max_new_tokens)
|
| 47 |
|
| 48 |
+
reference_image = Image.open(ref_path).convert("RGB")
|
| 49 |
+
test_image = Image.open(test_path).convert("RGB")
|
| 50 |
+
|
| 51 |
# Resize long edge to 512 to save GPU memory / speed up
|
|
|
|
|
|
|
| 52 |
reference_image.thumbnail((512, 512), Image.Resampling.LANCZOS)
|
| 53 |
test_image.thumbnail((512, 512), Image.Resampling.LANCZOS)
|
| 54 |
|
|
|
|
| 99 |
if reference_image is None or test_image is None:
|
| 100 |
return "Please upload both a reference (good) image and a test image."
|
| 101 |
try:
|
| 102 |
+
# Save PIL images to temp files to avoid serialization issues with ZeroGPU
|
| 103 |
+
with tempfile.NamedTemporaryFile(suffix=".png", delete=False) as f:
|
| 104 |
+
reference_image.save(f, format="PNG")
|
| 105 |
+
ref_path = f.name
|
| 106 |
+
with tempfile.NamedTemporaryFile(suffix=".png", delete=False) as f:
|
| 107 |
+
test_image.save(f, format="PNG")
|
| 108 |
+
test_path = f.name
|
| 109 |
+
result = _predict_inner(ref_path, test_path, prompt, max_new_tokens)
|
| 110 |
+
os.unlink(ref_path)
|
| 111 |
+
os.unlink(test_path)
|
| 112 |
+
return result
|
| 113 |
except Exception as e:
|
| 114 |
tb = traceback.format_exc()
|
| 115 |
print(tb, flush=True)
|