Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -2,6 +2,7 @@ import gradio as gr
|
|
2 |
import torch
|
3 |
import logging
|
4 |
import numpy as np
|
|
|
5 |
from PIL import Image
|
6 |
from transformers import ViTForImageClassification, ViTImageProcessor
|
7 |
|
@@ -23,15 +24,26 @@ except Exception as e:
|
|
23 |
logger.error(f"Failed to load model: {str(e)}")
|
24 |
raise RuntimeError(f"Model initialization failed: {str(e)}")
|
25 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
26 |
def preprocess_image(image_path):
|
27 |
"""Preprocess image for model input with proper error handling"""
|
28 |
try:
|
|
|
29 |
pil_image = Image.open(image_path).convert("RGB")
|
30 |
# Resize while maintaining aspect ratio
|
31 |
width, height = pil_image.size
|
32 |
new_size = (224, 224)
|
33 |
pil_image = pil_image.resize(new_size, Image.Resampling.LANCZOS)
|
34 |
-
|
|
|
35 |
return pil_image
|
36 |
except Exception as e:
|
37 |
logger.error(f"Image preprocessing error: {str(e)}")
|
@@ -93,7 +105,8 @@ def detect(image, confidence_threshold=0.7, detailed_analysis=False):
|
|
93 |
feature_analysis = analyze_facial_features(pil_image, probabilities)
|
94 |
|
95 |
# Logging for diagnostics and auditing
|
96 |
-
|
|
|
97 |
logger.info(f" - Raw probabilities: Real={confidence_real:.2f}%, Fake={confidence_fake:.2f}%")
|
98 |
logger.info(f" - Threshold ({confidence_threshold}): Predicted as {threshold_predicted}")
|
99 |
|
|
|
2 |
import torch
|
3 |
import logging
|
4 |
import numpy as np
|
5 |
+
import os
|
6 |
from PIL import Image
|
7 |
from transformers import ViTForImageClassification, ViTImageProcessor
|
8 |
|
|
|
24 |
logger.error(f"Failed to load model: {str(e)}")
|
25 |
raise RuntimeError(f"Model initialization failed: {str(e)}")
|
26 |
|
27 |
+
def get_filename(image_path):
|
28 |
+
"""Helper function to safely get a filename regardless of input type"""
|
29 |
+
if hasattr(image_path, 'name'):
|
30 |
+
return image_path.name
|
31 |
+
elif isinstance(image_path, str):
|
32 |
+
return os.path.basename(image_path)
|
33 |
+
else:
|
34 |
+
return "unknown_image"
|
35 |
+
|
36 |
def preprocess_image(image_path):
|
37 |
"""Preprocess image for model input with proper error handling"""
|
38 |
try:
|
39 |
+
# Handle both string paths and file objects
|
40 |
pil_image = Image.open(image_path).convert("RGB")
|
41 |
# Resize while maintaining aspect ratio
|
42 |
width, height = pil_image.size
|
43 |
new_size = (224, 224)
|
44 |
pil_image = pil_image.resize(new_size, Image.Resampling.LANCZOS)
|
45 |
+
filename = get_filename(image_path)
|
46 |
+
logger.info(f"Successfully preprocessed image: {filename} ({width}x{height} → 224x224)")
|
47 |
return pil_image
|
48 |
except Exception as e:
|
49 |
logger.error(f"Image preprocessing error: {str(e)}")
|
|
|
105 |
feature_analysis = analyze_facial_features(pil_image, probabilities)
|
106 |
|
107 |
# Logging for diagnostics and auditing
|
108 |
+
filename = get_filename(image)
|
109 |
+
logger.info(f"Analysis results for {filename}:")
|
110 |
logger.info(f" - Raw probabilities: Real={confidence_real:.2f}%, Fake={confidence_fake:.2f}%")
|
111 |
logger.info(f" - Threshold ({confidence_threshold}): Predicted as {threshold_predicted}")
|
112 |
|