V0
Browse files- app.py +70 -15
- requirements.txt +3 -1
app.py
CHANGED
|
@@ -133,11 +133,24 @@ def setup_damage_detector(model_path, threshold=0.7):
|
|
| 133 |
import traceback
|
| 134 |
traceback.print_exc()
|
| 135 |
return None, None
|
|
|
|
| 136 |
def load_vit_deepfake_model(model_path, device):
|
| 137 |
"""Load the AutoAdapter model for deepfake detection"""
|
| 138 |
try:
|
| 139 |
-
#
|
| 140 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 141 |
|
| 142 |
# Create model from pretrained base
|
| 143 |
print(f"Loading AutoAdapter model for deepfake detection...")
|
|
@@ -153,11 +166,36 @@ def load_vit_deepfake_model(model_path, device):
|
|
| 153 |
except Exception as e:
|
| 154 |
print(f"Error loading AutoAdapter model: {e}")
|
| 155 |
traceback.print_exc()
|
| 156 |
-
return None
|
|
|
|
| 157 |
|
| 158 |
|
| 159 |
def preprocess_for_vit(image, device):
|
| 160 |
-
"""Preprocess an image for
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 161 |
try:
|
| 162 |
# Import AutoProcessor
|
| 163 |
from transformers import AutoProcessor
|
|
@@ -244,7 +282,7 @@ def detect_damage(img, damage_detector):
|
|
| 244 |
return None, None, []
|
| 245 |
|
| 246 |
def check_deepfake_vit(image, damage_regions, deepfake_model, device, threshold=0.5):
|
| 247 |
-
"""Check if damage regions are deepfakes using
|
| 248 |
results = []
|
| 249 |
|
| 250 |
if deepfake_model is None:
|
|
@@ -378,22 +416,39 @@ def visualize_results(image, damage_outputs, deepfake_results, damage_threshold)
|
|
| 378 |
def process_image(input_image, damage_model_path, deepfake_model_path, damage_threshold,
|
| 379 |
deepfake_threshold, skip_damage, device_str, usage_count):
|
| 380 |
"""Process an image through the detection pipeline"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 381 |
# Increment usage count and check if limit reached
|
| 382 |
usage_count = usage_count + 1
|
| 383 |
|
| 384 |
progress_info = []
|
| 385 |
progress_info.append(f"Usage: {usage_count}/{MAX_TRIES}")
|
| 386 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 387 |
# Check model files
|
| 388 |
if not skip_damage and damage_model_path:
|
| 389 |
if not os.path.exists(damage_model_path):
|
| 390 |
progress_info.append(f"ERROR: Damage model not found at {damage_model_path}")
|
| 391 |
|
| 392 |
if deepfake_model_path and not os.path.exists(deepfake_model_path):
|
| 393 |
-
progress_info.append(f"ERROR:
|
| 394 |
|
| 395 |
# Convert image to proper format
|
| 396 |
try:
|
|
|
|
|
|
|
|
|
|
| 397 |
if isinstance(input_image, dict) and "path" in input_image:
|
| 398 |
img = cv2.imread(input_image["path"])
|
| 399 |
elif isinstance(input_image, str):
|
|
@@ -427,14 +482,14 @@ def process_image(input_image, damage_model_path, deepfake_model_path, damage_th
|
|
| 427 |
else:
|
| 428 |
progress_info.append("❌ Failed to initialize damage detector")
|
| 429 |
|
| 430 |
-
# Setup
|
| 431 |
if deepfake_model_path:
|
| 432 |
-
progress_info.append("Setting up
|
| 433 |
deepfake_model = load_vit_deepfake_model(deepfake_model_path, device)
|
| 434 |
if deepfake_model:
|
| 435 |
-
progress_info.append("✅
|
| 436 |
else:
|
| 437 |
-
progress_info.append("❌ Failed to initialize
|
| 438 |
|
| 439 |
# Step 1: Detect damage
|
| 440 |
progress_info.append("Detecting damaged regions...")
|
|
@@ -447,10 +502,10 @@ def process_image(input_image, damage_model_path, deepfake_model_path, damage_th
|
|
| 447 |
else:
|
| 448 |
progress_info.append("No damage regions detected")
|
| 449 |
|
| 450 |
-
# Step 2: Check for deepfakes
|
| 451 |
deepfake_results = []
|
| 452 |
if deepfake_model is not None:
|
| 453 |
-
progress_info.append("Analyzing regions for deepfakes
|
| 454 |
start_time = time.time()
|
| 455 |
deepfake_results = check_deepfake_vit(
|
| 456 |
img, damage_regions, deepfake_model, device, float(deepfake_threshold)
|
|
@@ -505,7 +560,7 @@ def process_image(input_image, damage_model_path, deepfake_model_path, damage_th
|
|
| 505 |
progress_info.append(f"\nRemaining tries: {MAX_TRIES - usage_count}")
|
| 506 |
|
| 507 |
return result_img, "\n".join(progress_info), usage_count
|
| 508 |
-
|
| 509 |
def auto_install_dependencies():
|
| 510 |
"""Attempt to install dependencies if needed"""
|
| 511 |
try:
|
|
@@ -560,7 +615,6 @@ def create_gradio_interface():
|
|
| 560 |
)
|
| 561 |
|
| 562 |
# Initialize usage counter
|
| 563 |
-
usage_counter = gr.State(0)
|
| 564 |
|
| 565 |
with gr.Blocks(title="Car Damage & Deepfake Detector", theme=theme) as app:
|
| 566 |
gr.Markdown("""
|
|
@@ -574,7 +628,8 @@ def create_gradio_interface():
|
|
| 574 |
|
| 575 |
⚠️ **Note: You have a maximum of 3 tries to analyze images.**
|
| 576 |
""")
|
| 577 |
-
|
|
|
|
| 578 |
# Main Interface Tab
|
| 579 |
with gr.Tab("Analyze Image"):
|
| 580 |
with gr.Row():
|
|
|
|
| 133 |
import traceback
|
| 134 |
traceback.print_exc()
|
| 135 |
return None, None
|
| 136 |
+
|
| 137 |
def load_vit_deepfake_model(model_path, device):
|
| 138 |
"""Load the AutoAdapter model for deepfake detection"""
|
| 139 |
try:
|
| 140 |
+
# First try to install the adapter-transformers library if not present
|
| 141 |
+
try:
|
| 142 |
+
import importlib.util
|
| 143 |
+
if importlib.util.find_spec("transformers") is None:
|
| 144 |
+
print("Installing transformers...")
|
| 145 |
+
os.system("pip install transformers")
|
| 146 |
+
if importlib.util.find_spec("adapter_transformers") is None:
|
| 147 |
+
print("Installing adapter-transformers...")
|
| 148 |
+
os.system("pip install -U adapter-transformers")
|
| 149 |
+
except Exception as e:
|
| 150 |
+
print(f"Warning: Could not install adapter-transformers: {e}")
|
| 151 |
+
|
| 152 |
+
# Import AutoAdapterModel from adapter_transformers
|
| 153 |
+
from adapter_transformers import AutoAdapterModel
|
| 154 |
|
| 155 |
# Create model from pretrained base
|
| 156 |
print(f"Loading AutoAdapter model for deepfake detection...")
|
|
|
|
| 166 |
except Exception as e:
|
| 167 |
print(f"Error loading AutoAdapter model: {e}")
|
| 168 |
traceback.print_exc()
|
| 169 |
+
return None
|
| 170 |
+
|
| 171 |
|
| 172 |
|
| 173 |
def preprocess_for_vit(image, device):
|
| 174 |
+
"""Preprocess an image for adapter-transformers model deepfake detection"""
|
| 175 |
+
try:
|
| 176 |
+
# Import AutoProcessor from adapter_transformers
|
| 177 |
+
from adapter_transformers import AutoTokenizer, AutoFeatureExtractor
|
| 178 |
+
|
| 179 |
+
# Get the processor for the adapter model - using feature extractor for vision models
|
| 180 |
+
processor = AutoFeatureExtractor.from_pretrained("Askhedi/Car_fraud_damage_detector")
|
| 181 |
+
|
| 182 |
+
# Ensure image is RGB
|
| 183 |
+
if len(image.shape) == 3 and image.shape[2] == 3:
|
| 184 |
+
if image.dtype != np.uint8:
|
| 185 |
+
image = (image * 255).astype(np.uint8)
|
| 186 |
+
rgb_img = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
|
| 187 |
+
else:
|
| 188 |
+
rgb_img = image
|
| 189 |
+
|
| 190 |
+
# Use the processor to transform the image
|
| 191 |
+
inputs = processor(images=Image.fromarray(rgb_img), return_tensors="pt")
|
| 192 |
+
inputs = {k: v.to(device) for k, v in inputs.items()}
|
| 193 |
+
|
| 194 |
+
return inputs
|
| 195 |
+
except Exception as e:
|
| 196 |
+
print(f"Error preprocessing image for adapter model: {e}")
|
| 197 |
+
traceback.print_exc()
|
| 198 |
+
return None """Preprocess an image for AutoAdapter model deepfake detection"""
|
| 199 |
try:
|
| 200 |
# Import AutoProcessor
|
| 201 |
from transformers import AutoProcessor
|
|
|
|
| 282 |
return None, None, []
|
| 283 |
|
| 284 |
def check_deepfake_vit(image, damage_regions, deepfake_model, device, threshold=0.5):
|
| 285 |
+
"""Check if damage regions are deepfakes using adapter-transformers model"""
|
| 286 |
results = []
|
| 287 |
|
| 288 |
if deepfake_model is None:
|
|
|
|
| 416 |
def process_image(input_image, damage_model_path, deepfake_model_path, damage_threshold,
|
| 417 |
deepfake_threshold, skip_damage, device_str, usage_count):
|
| 418 |
"""Process an image through the detection pipeline"""
|
| 419 |
+
# Handle empty or None usage_count
|
| 420 |
+
if usage_count is None:
|
| 421 |
+
usage_count = 0
|
| 422 |
+
|
| 423 |
+
# Ensure usage_count is an integer
|
| 424 |
+
try:
|
| 425 |
+
usage_count = int(usage_count)
|
| 426 |
+
except (TypeError, ValueError):
|
| 427 |
+
usage_count = 0
|
| 428 |
+
|
| 429 |
# Increment usage count and check if limit reached
|
| 430 |
usage_count = usage_count + 1
|
| 431 |
|
| 432 |
progress_info = []
|
| 433 |
progress_info.append(f"Usage: {usage_count}/{MAX_TRIES}")
|
| 434 |
|
| 435 |
+
# Check if usage limit reached
|
| 436 |
+
if usage_count > MAX_TRIES:
|
| 437 |
+
return None, f"⚠️ You have reached the maximum number of tries allowed ({MAX_TRIES}).", usage_count
|
| 438 |
+
|
| 439 |
# Check model files
|
| 440 |
if not skip_damage and damage_model_path:
|
| 441 |
if not os.path.exists(damage_model_path):
|
| 442 |
progress_info.append(f"ERROR: Damage model not found at {damage_model_path}")
|
| 443 |
|
| 444 |
if deepfake_model_path and not os.path.exists(deepfake_model_path):
|
| 445 |
+
progress_info.append(f"ERROR: Deepfake model not found at {deepfake_model_path}")
|
| 446 |
|
| 447 |
# Convert image to proper format
|
| 448 |
try:
|
| 449 |
+
if input_image is None:
|
| 450 |
+
return None, "Please upload an image to analyze.", usage_count
|
| 451 |
+
|
| 452 |
if isinstance(input_image, dict) and "path" in input_image:
|
| 453 |
img = cv2.imread(input_image["path"])
|
| 454 |
elif isinstance(input_image, str):
|
|
|
|
| 482 |
else:
|
| 483 |
progress_info.append("❌ Failed to initialize damage detector")
|
| 484 |
|
| 485 |
+
# Setup deepfake detector
|
| 486 |
if deepfake_model_path:
|
| 487 |
+
progress_info.append("Setting up deepfake detector...")
|
| 488 |
deepfake_model = load_vit_deepfake_model(deepfake_model_path, device)
|
| 489 |
if deepfake_model:
|
| 490 |
+
progress_info.append("✅ Deepfake detector initialized")
|
| 491 |
else:
|
| 492 |
+
progress_info.append("❌ Failed to initialize deepfake detector")
|
| 493 |
|
| 494 |
# Step 1: Detect damage
|
| 495 |
progress_info.append("Detecting damaged regions...")
|
|
|
|
| 502 |
else:
|
| 503 |
progress_info.append("No damage regions detected")
|
| 504 |
|
| 505 |
+
# Step 2: Check for deepfakes
|
| 506 |
deepfake_results = []
|
| 507 |
if deepfake_model is not None:
|
| 508 |
+
progress_info.append("Analyzing regions for deepfakes...")
|
| 509 |
start_time = time.time()
|
| 510 |
deepfake_results = check_deepfake_vit(
|
| 511 |
img, damage_regions, deepfake_model, device, float(deepfake_threshold)
|
|
|
|
| 560 |
progress_info.append(f"\nRemaining tries: {MAX_TRIES - usage_count}")
|
| 561 |
|
| 562 |
return result_img, "\n".join(progress_info), usage_count
|
| 563 |
+
|
| 564 |
def auto_install_dependencies():
|
| 565 |
"""Attempt to install dependencies if needed"""
|
| 566 |
try:
|
|
|
|
| 615 |
)
|
| 616 |
|
| 617 |
# Initialize usage counter
|
|
|
|
| 618 |
|
| 619 |
with gr.Blocks(title="Car Damage & Deepfake Detector", theme=theme) as app:
|
| 620 |
gr.Markdown("""
|
|
|
|
| 628 |
|
| 629 |
⚠️ **Note: You have a maximum of 3 tries to analyze images.**
|
| 630 |
""")
|
| 631 |
+
usage_counter = gr.State(0)
|
| 632 |
+
|
| 633 |
# Main Interface Tab
|
| 634 |
with gr.Tab("Analyze Image"):
|
| 635 |
with gr.Row():
|
requirements.txt
CHANGED
|
@@ -7,4 +7,6 @@ gradio>=3.50.0
|
|
| 7 |
python-box>=7.1.1
|
| 8 |
fvcore>=0.1.5.post20221221; platform_system!="Darwin"
|
| 9 |
iopath>=0.1.9; platform_system!="Darwin"
|
| 10 |
-
pycocotools>=2.0.6; platform_system!="Darwin"
|
|
|
|
|
|
|
|
|
| 7 |
python-box>=7.1.1
|
| 8 |
fvcore>=0.1.5.post20221221; platform_system!="Darwin"
|
| 9 |
iopath>=0.1.9; platform_system!="Darwin"
|
| 10 |
+
pycocotools>=2.0.6; platform_system!="Darwin"
|
| 11 |
+
adapters
|
| 12 |
+
transformers
|