Spaces:
Running on Zero
Running on Zero
Support filepath input and handle corrupted files gracefully
Browse files
app.py
CHANGED
|
@@ -113,16 +113,22 @@ def run_inference(image):
|
|
| 113 |
"Awaiting image upload."
|
| 114 |
)
|
| 115 |
|
| 116 |
-
#
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 126 |
|
| 127 |
# 2. Ensure model is loaded
|
| 128 |
model, device, preprocess = load_model()
|
|
@@ -141,9 +147,6 @@ def run_inference(image):
|
|
| 141 |
if next(model.parameters()).device != active_device:
|
| 142 |
model.to(active_device)
|
| 143 |
|
| 144 |
-
# Convert image to RGB
|
| 145 |
-
img_rgb = image.convert("RGB")
|
| 146 |
-
|
| 147 |
# Apply exact preprocessing: Resize(224) -> CenterCrop(224) -> ToTensor -> Normalize
|
| 148 |
input_tensor = preprocess(img_rgb).unsqueeze(0).to(active_device)
|
| 149 |
|
|
@@ -297,7 +300,7 @@ with gr.Blocks(title="UniversalFakeDetect - Single Image Analysis", css=custom_c
|
|
| 297 |
|
| 298 |
with gr.Row():
|
| 299 |
with gr.Column(scale=1):
|
| 300 |
-
input_image = gr.Image(type="
|
| 301 |
with gr.Row():
|
| 302 |
analyze_btn = gr.Button("🔍 Analyze Image", variant="primary", scale=2)
|
| 303 |
clear_btn = gr.ClearButton(components=[input_image], scale=1)
|
|
@@ -349,4 +352,4 @@ with gr.Blocks(title="UniversalFakeDetect - Single Image Analysis", css=custom_c
|
|
| 349 |
)
|
| 350 |
|
| 351 |
if __name__ == "__main__":
|
| 352 |
-
demo.launch()
|
|
|
|
| 113 |
"Awaiting image upload."
|
| 114 |
)
|
| 115 |
|
| 116 |
+
# Safely load and validate image
|
| 117 |
+
try:
|
| 118 |
+
if isinstance(image, str):
|
| 119 |
+
with Image.open(image) as opened_img:
|
| 120 |
+
img_rgb = opened_img.convert("RGB")
|
| 121 |
+
elif isinstance(image, Image.Image):
|
| 122 |
+
img_rgb = image.convert("RGB")
|
| 123 |
+
else:
|
| 124 |
+
img_rgb = Image.fromarray(image).convert("RGB")
|
| 125 |
+
except Exception as img_err:
|
| 126 |
+
err_msg = f"Invalid or corrupted image file: {str(img_err)}"
|
| 127 |
+
return (
|
| 128 |
+
f"<div style='background: #fef2f2; border: 1.5px solid #f87171; color: #991b1b; padding: 16px; border-radius: 8px;'>❌ <b>Invalid or Corrupted Image:</b> {err_msg}</div>",
|
| 129 |
+
"",
|
| 130 |
+
f"Failed: {err_msg}"
|
| 131 |
+
)
|
| 132 |
|
| 133 |
# 2. Ensure model is loaded
|
| 134 |
model, device, preprocess = load_model()
|
|
|
|
| 147 |
if next(model.parameters()).device != active_device:
|
| 148 |
model.to(active_device)
|
| 149 |
|
|
|
|
|
|
|
|
|
|
| 150 |
# Apply exact preprocessing: Resize(224) -> CenterCrop(224) -> ToTensor -> Normalize
|
| 151 |
input_tensor = preprocess(img_rgb).unsqueeze(0).to(active_device)
|
| 152 |
|
|
|
|
| 300 |
|
| 301 |
with gr.Row():
|
| 302 |
with gr.Column(scale=1):
|
| 303 |
+
input_image = gr.Image(type="filepath", label="Upload Image for Analysis")
|
| 304 |
with gr.Row():
|
| 305 |
analyze_btn = gr.Button("🔍 Analyze Image", variant="primary", scale=2)
|
| 306 |
clear_btn = gr.ClearButton(components=[input_image], scale=1)
|
|
|
|
| 352 |
)
|
| 353 |
|
| 354 |
if __name__ == "__main__":
|
| 355 |
+
demo.launch(show_error=True)
|