Spaces:
Runtime error
Runtime error
Add new ViT-based deep fake detection model
Browse files- Integrate prithivMLmods/Deep-Fake-Detector-v2-Model as the fifth classification model
- Update prediction logic to handle the new model's classification
- Modify README to include the new model in preload_from_hub
- Adjust result tile styling and labels for the new model
- Resize input images to 224x224 for ViT models
README.md
CHANGED
|
@@ -1,10 +1,10 @@
|
|
| 1 |
---
|
| 2 |
-
title:
|
| 3 |
emoji: 🔥
|
| 4 |
colorFrom: indigo
|
| 5 |
colorTo: purple
|
| 6 |
sdk: gradio
|
| 7 |
-
sdk_version:
|
| 8 |
app_file: app.py
|
| 9 |
pinned: false
|
| 10 |
preload_from_hub:
|
|
@@ -12,6 +12,7 @@ preload_from_hub:
|
|
| 12 |
- haywoodsloan/ai-image-detector-deploy
|
| 13 |
- cmckinle/sdxl-flux-detector
|
| 14 |
- Organika/sdxl-detector
|
|
|
|
| 15 |
---
|
| 16 |
|
| 17 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
|
| 1 |
---
|
| 2 |
+
title: OpenSight
|
| 3 |
emoji: 🔥
|
| 4 |
colorFrom: indigo
|
| 5 |
colorTo: purple
|
| 6 |
sdk: gradio
|
| 7 |
+
sdk_version: 5.15.0
|
| 8 |
app_file: app.py
|
| 9 |
pinned: false
|
| 10 |
preload_from_hub:
|
|
|
|
| 12 |
- haywoodsloan/ai-image-detector-deploy
|
| 13 |
- cmckinle/sdxl-flux-detector
|
| 14 |
- Organika/sdxl-detector
|
| 15 |
+
- prithivMLmods/Deep-Fake-Detector-v2-Model
|
| 16 |
---
|
| 17 |
|
| 18 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
app.py
CHANGED
|
@@ -32,11 +32,16 @@ model_3 = AutoModelForImageClassification.from_pretrained(models[0]).to(device)
|
|
| 32 |
feature_extractor_4 = AutoFeatureExtractor.from_pretrained(models[1], device=device)
|
| 33 |
model_4 = AutoModelForImageClassification.from_pretrained(models[1]).to(device)
|
| 34 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
# Define class names for all models
|
| 36 |
class_names_1 = ['artificial', 'real']
|
| 37 |
class_names_2 = ['AI Image', 'Real Image']
|
| 38 |
labels_3 = ['AI', 'Real']
|
| 39 |
labels_4 = ['AI', 'Real']
|
|
|
|
| 40 |
|
| 41 |
def softmax(vector):
|
| 42 |
e = np.exp(vector - np.max(vector)) # for numerical stability
|
|
@@ -84,6 +89,8 @@ def predict_image(img, confidence_threshold):
|
|
| 84 |
|
| 85 |
# Resize the image
|
| 86 |
img_pil = transforms.Resize((256, 256))(img_pil)
|
|
|
|
|
|
|
| 87 |
|
| 88 |
# Predict using the first model
|
| 89 |
try:
|
|
@@ -111,7 +118,7 @@ def predict_image(img, confidence_threshold):
|
|
| 111 |
print(result_1output)
|
| 112 |
# Predict using the second model
|
| 113 |
try:
|
| 114 |
-
prediction_2 = clf_2(
|
| 115 |
result_2 = {pred['label']: pred['score'] for pred in prediction_2}
|
| 116 |
result_2output = [2, 'ViT-base Classifer', result_2['Real Image'], result_2['AI Image']]
|
| 117 |
print(result_2output)
|
|
@@ -193,19 +200,42 @@ def predict_image(img, confidence_threshold):
|
|
| 193 |
label_4 = f"Error: {str(e)}"
|
| 194 |
|
| 195 |
try:
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 206 |
except Exception as e:
|
| 207 |
label_5 = f"Error: {str(e)}"
|
| 208 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 209 |
|
| 210 |
# Combine results
|
| 211 |
combined_results = {
|
|
@@ -213,7 +243,7 @@ def predict_image(img, confidence_threshold):
|
|
| 213 |
"ViT/AI-vs-Real": label_2,
|
| 214 |
"Swin/SDXL": label_3,
|
| 215 |
"Swin/SDXL-FLUX": label_4,
|
| 216 |
-
"
|
| 217 |
}
|
| 218 |
# Generate HTML content
|
| 219 |
|
|
@@ -285,7 +315,7 @@ def generate_results_html(results):
|
|
| 285 |
|
| 286 |
<div class="text-xs font-mono">Real: {results[0][2]:.4f}, AI: {results[0][3]:.4f}</div>
|
| 287 |
|
| 288 |
-
<a class="mt-2 text-
|
| 289 |
</div>
|
| 290 |
</div>
|
| 291 |
<!-- Tile 2: ViT/AI-vs-Real -->
|
|
@@ -328,7 +358,7 @@ def generate_results_html(results):
|
|
| 328 |
|
| 329 |
<div class="text-xs font-mono">Real: {results[1][2]:.4f}, AI: {results[1][3]:.4f}</div>
|
| 330 |
|
| 331 |
-
<a class="mt-2 text-
|
| 332 |
</div>
|
| 333 |
</div>
|
| 334 |
<!-- Tile 3: Swin/SDXL -->
|
|
@@ -367,11 +397,11 @@ def generate_results_html(results):
|
|
| 367 |
</div>
|
| 368 |
</div>
|
| 369 |
<div class="flex flex-col items-start">
|
| 370 |
-
<h4 class="mt-4 text-sm font-semibold tracking-wide">
|
| 371 |
|
| 372 |
<div class="text-xs font-mono">Real: {results[2][2]:.4f}, AI: {results[2][3]:.4f}</div>
|
| 373 |
|
| 374 |
-
<a class="mt-2 text-
|
| 375 |
</div>
|
| 376 |
</div>
|
| 377 |
<!-- Tile 4: Swin/SDXL-FLUX -->
|
|
@@ -410,11 +440,11 @@ def generate_results_html(results):
|
|
| 410 |
</div>
|
| 411 |
</div>
|
| 412 |
<div class="flex flex-col items-start">
|
| 413 |
-
<h4 class="mt-4 text-sm font-semibold tracking-wide">
|
| 414 |
|
| 415 |
<div class="text-xs font-mono">Real: {results[3][2]:.4f}, AI: {results[3][3]:.4f}</div>
|
| 416 |
|
| 417 |
-
<a class="mt-2 text-
|
| 418 |
</div>
|
| 419 |
</div>
|
| 420 |
<!-- Tile 5: GOAT -->
|
|
@@ -453,11 +483,11 @@ def generate_results_html(results):
|
|
| 453 |
</div>
|
| 454 |
</div>
|
| 455 |
<div class="flex flex-col items-start">
|
| 456 |
-
<h4 class="mt-4 text-sm font-semibold tracking-wide">
|
| 457 |
|
| 458 |
<div class="text-xs font-mono">Real: {results[4][2]:.4f}, AI: {results[4][3]:.4f}</div>
|
| 459 |
|
| 460 |
-
<a class="mt-2 text-
|
| 461 |
</div>
|
| 462 |
</div>
|
| 463 |
</div>
|
|
|
|
| 32 |
feature_extractor_4 = AutoFeatureExtractor.from_pretrained(models[1], device=device)
|
| 33 |
model_4 = AutoModelForImageClassification.from_pretrained(models[1]).to(device)
|
| 34 |
|
| 35 |
+
# Load the second model
|
| 36 |
+
model_5_path = "prithivMLmods/Deep-Fake-Detector-v2-Model"
|
| 37 |
+
clf_5 = pipeline("image-classification", model=model_5_path, device=device)
|
| 38 |
+
|
| 39 |
# Define class names for all models
|
| 40 |
class_names_1 = ['artificial', 'real']
|
| 41 |
class_names_2 = ['AI Image', 'Real Image']
|
| 42 |
labels_3 = ['AI', 'Real']
|
| 43 |
labels_4 = ['AI', 'Real']
|
| 44 |
+
class_names_5 = ['Realism', 'Deepfake']
|
| 45 |
|
| 46 |
def softmax(vector):
|
| 47 |
e = np.exp(vector - np.max(vector)) # for numerical stability
|
|
|
|
| 89 |
|
| 90 |
# Resize the image
|
| 91 |
img_pil = transforms.Resize((256, 256))(img_pil)
|
| 92 |
+
# Size 224 for vits models
|
| 93 |
+
img_pilvits = transforms.Resize((224, 224))(img_pilvits)
|
| 94 |
|
| 95 |
# Predict using the first model
|
| 96 |
try:
|
|
|
|
| 118 |
print(result_1output)
|
| 119 |
# Predict using the second model
|
| 120 |
try:
|
| 121 |
+
prediction_2 = clf_2(img_pilvits)
|
| 122 |
result_2 = {pred['label']: pred['score'] for pred in prediction_2}
|
| 123 |
result_2output = [2, 'ViT-base Classifer', result_2['Real Image'], result_2['AI Image']]
|
| 124 |
print(result_2output)
|
|
|
|
| 200 |
label_4 = f"Error: {str(e)}"
|
| 201 |
|
| 202 |
try:
|
| 203 |
+
prediction_5 = clf_5(img_pilvits)
|
| 204 |
+
result_5 = {pred['label']: pred['score'] for pred in prediction_2}
|
| 205 |
+
result_5output = [5, 'ViT-base Newcomer', result_5['Realism'], result_5['Deepfake']]
|
| 206 |
+
print(result_2output)
|
| 207 |
+
# Ensure the result dictionary contains all class names
|
| 208 |
+
for class_name in class_names_5:
|
| 209 |
+
if class_name not in result_5:
|
| 210 |
+
result_5[class_name] = 0.0
|
| 211 |
+
# Check if either class meets the confidence threshold
|
| 212 |
+
if result_5['AI Image'] >= confidence_threshold:
|
| 213 |
+
label_5 = f"AI, Confidence: {result_5['Deepfake']:.4f}"
|
| 214 |
+
result_5output += ['AI']
|
| 215 |
+
elif result_5['Real Image'] >= confidence_threshold:
|
| 216 |
+
label_5 = f"Real, Confidence: {result_5['Realism']:.4f}"
|
| 217 |
+
result_5output += ['REAL']
|
| 218 |
+
else:
|
| 219 |
+
label_5 = "Uncertain Classification"
|
| 220 |
+
result_5output += ['UNCERTAIN']
|
| 221 |
except Exception as e:
|
| 222 |
label_5 = f"Error: {str(e)}"
|
| 223 |
|
| 224 |
+
|
| 225 |
+
# try:
|
| 226 |
+
# result_5output = [5, 'TBA', 0.0, 0.0, 'MAINTENANCE']
|
| 227 |
+
# img_bytes = convert_pil_to_bytes(img_pil)
|
| 228 |
+
# # print(img)
|
| 229 |
+
# # print(img_bytes)
|
| 230 |
+
# response5_raw = call_inference(img)
|
| 231 |
+
# print(response5_raw)
|
| 232 |
+
# response5 = response5_raw
|
| 233 |
+
# print(response5)
|
| 234 |
+
# label_5 = f"Result: {response5}"
|
| 235 |
+
|
| 236 |
+
# except Exception as e:
|
| 237 |
+
# label_5 = f"Error: {str(e)}"
|
| 238 |
+
|
| 239 |
|
| 240 |
# Combine results
|
| 241 |
combined_results = {
|
|
|
|
| 243 |
"ViT/AI-vs-Real": label_2,
|
| 244 |
"Swin/SDXL": label_3,
|
| 245 |
"Swin/SDXL-FLUX": label_4,
|
| 246 |
+
"prithivMLmods": label_5
|
| 247 |
}
|
| 248 |
# Generate HTML content
|
| 249 |
|
|
|
|
| 315 |
|
| 316 |
<div class="text-xs font-mono">Real: {results[0][2]:.4f}, AI: {results[0][3]:.4f}</div>
|
| 317 |
|
| 318 |
+
<a class="mt-2 text-xs tracking-wide">@haywoodsloan / more info</a>
|
| 319 |
</div>
|
| 320 |
</div>
|
| 321 |
<!-- Tile 2: ViT/AI-vs-Real -->
|
|
|
|
| 358 |
|
| 359 |
<div class="text-xs font-mono">Real: {results[1][2]:.4f}, AI: {results[1][3]:.4f}</div>
|
| 360 |
|
| 361 |
+
<a class="mt-2 text-xs tracking-wide">@Heem2 / more info</a>
|
| 362 |
</div>
|
| 363 |
</div>
|
| 364 |
<!-- Tile 3: Swin/SDXL -->
|
|
|
|
| 397 |
</div>
|
| 398 |
</div>
|
| 399 |
<div class="flex flex-col items-start">
|
| 400 |
+
<h4 class="mt-4 text-sm font-semibold tracking-wide">SDXL Dataset</h4>
|
| 401 |
|
| 402 |
<div class="text-xs font-mono">Real: {results[2][2]:.4f}, AI: {results[2][3]:.4f}</div>
|
| 403 |
|
| 404 |
+
<a class="mt-2 text-xs tracking-wide">@Organika / more info</a>
|
| 405 |
</div>
|
| 406 |
</div>
|
| 407 |
<!-- Tile 4: Swin/SDXL-FLUX -->
|
|
|
|
| 440 |
</div>
|
| 441 |
</div>
|
| 442 |
<div class="flex flex-col items-start">
|
| 443 |
+
<h4 class="mt-4 text-sm font-semibold tracking-wide">SDXL + FLUX</h4>
|
| 444 |
|
| 445 |
<div class="text-xs font-mono">Real: {results[3][2]:.4f}, AI: {results[3][3]:.4f}</div>
|
| 446 |
|
| 447 |
+
<a class="mt-2 text-xs tracking-wide">@cmckinle / more info</a>
|
| 448 |
</div>
|
| 449 |
</div>
|
| 450 |
<!-- Tile 5: GOAT -->
|
|
|
|
| 483 |
</div>
|
| 484 |
</div>
|
| 485 |
<div class="flex flex-col items-start">
|
| 486 |
+
<h4 class="mt-4 text-sm font-semibold tracking-wide">Vits Model</h4>
|
| 487 |
|
| 488 |
<div class="text-xs font-mono">Real: {results[4][2]:.4f}, AI: {results[4][3]:.4f}</div>
|
| 489 |
|
| 490 |
+
<a class="mt-2 text-xs tracking-wide">@prithivMLmods / more info</a>
|
| 491 |
</div>
|
| 492 |
</div>
|
| 493 |
</div>
|