File size: 25,294 Bytes
93a8bce 93f5629 4a03e59 93f5629 e88a32d 12cea06 1ea7edd d5a469d 67f3560 4378fd8 b1387d5 e88a32d 93f5629 67f3560 52ae10e 67f3560 4a03e59 67f3560 d9b67e8 67f3560 387e421 67f3560 4a03e59 67f3560 d5a469d 67f3560 8ef48b9 67f3560 8ef48b9 67f3560 57b2083 67f3560 57b2083 67f3560 c65deae 67f3560 d5a469d 4a03e59 e88a32d 7820a52 12cea06 f0208ec 2484926 67f3560 b6b6807 57b2083 67f3560 7513f66 52ae10e 1d42aa4 387e421 67f3560 52ae10e 8ef48b9 67f3560 7513f66 4378fd8 77f3a53 30c764e 77f3a53 30c764e 77f3a53 b29bd8c 77f3a53 30c764e 77f3a53 30c764e dd0760e b29bd8c 4378fd8 28af384 3bc1c8a cd4052c 7c1f022 1835e62 b29bd8c 8ef48b9 b29bd8c 06126f5 b29bd8c 8ef48b9 b29bd8c a26ccca b29bd8c 77f3a53 b29bd8c 9bdd986 e60282c dd0760e b29bd8c 77f3a53 06126f5 9bdd986 e60282c b29bd8c dd0760e b29bd8c 77f3a53 b29bd8c 8ef48b9 b29bd8c 8ef48b9 d9b67e8 3cb2f30 b29bd8c 8ef48b9 b29bd8c a26ccca b29bd8c 8ef48b9 b29bd8c a26ccca b29bd8c 9bdd986 e60282c b29bd8c dd0760e b29bd8c 77f3a53 30c764e 9bdd986 e60282c b29bd8c dd0760e b29bd8c 77f3a53 b29bd8c 8ef48b9 dd0760e 8ef48b9 d9b67e8 b29bd8c 8ef48b9 b29bd8c a26ccca b29bd8c 8ef48b9 b29bd8c a26ccca b29bd8c 9bdd986 e60282c b29bd8c dd0760e b29bd8c 3cb2f30 a26ccca 9bdd986 e60282c b29bd8c dd0760e b29bd8c 3cb2f30 b29bd8c d9b67e8 8ef48b9 dd0760e 8ef48b9 d9b67e8 b29bd8c 8ef48b9 b29bd8c 06126f5 b29bd8c 8ef48b9 b29bd8c a26ccca b29bd8c 9bdd986 e60282c b29bd8c dd0760e b29bd8c 77f3a53 a26ccca 9bdd986 e60282c b29bd8c dd0760e b29bd8c 77f3a53 b29bd8c d9b67e8 8ef48b9 dd0760e 8ef48b9 d9b67e8 b29bd8c 407a629 b29bd8c 8ef48b9 b29bd8c d7d030d b29bd8c 8ef48b9 b29bd8c a26ccca b29bd8c 407a629 b29bd8c 9bdd986 e60282c b29bd8c dd0760e b29bd8c 3cb2f30 a26ccca 9bdd986 e60282c b29bd8c dd0760e b29bd8c 3cb2f30 b29bd8c d9b67e8 8ef48b9 dd0760e 387e421 8ef48b9 d9b67e8 b29bd8c 3cb2f30 4378fd8 a9d7990 4439436 2669b96 4439436 b29bd8c 4439436 3933565 f0208ec 4439436 b29bd8c 3933565 f0208ec 30c764e bd23e86 4439436 407a629 9e875de 407a629 e88a32d 8a3f635 4439436 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 |
import spaces
import gradio as gr
from transformers import pipeline, AutoImageProcessor, Swinv2ForImageClassification, AutoFeatureExtractor, AutoModelForImageClassification
from torchvision import transforms
import torch
from PIL import Image
import numpy as np
import io
import logging
from utils.utils import softmax, augment_image, convert_pil_to_bytes
# Configure logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
# Ensure using GPU if available
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
# Model paths and class names
MODEL_PATHS = {
"model_1": "haywoodsloan/ai-image-detector-deploy",
"model_2": "Heem2/AI-vs-Real-Image-Detection",
"model_3": "Organika/sdxl-detector",
"model_4": "cmckinle/sdxl-flux-detector",
"model_5": "prithivMLmods/Deep-Fake-Detector-v2-Model",
"model_5b": "prithivMLmods/Deepfake-Detection-Exp-02-22"
}
CLASS_NAMES = {
"model_1": ['artificial', 'real'],
"model_2": ['AI Image', 'Real Image'],
"model_3": ['AI', 'Real'],
"model_4": ['AI', 'Real'],
"model_5": ['Realism', 'Deepfake'],
"model_5b": ['Real', 'Deepfake']
}
# Load models and processors
def load_models():
image_processor_1 = AutoImageProcessor.from_pretrained(MODEL_PATHS["model_1"], use_fast=True)
model_1 = Swinv2ForImageClassification.from_pretrained(MODEL_PATHS["model_1"])
model_1 = model_1.to(device)
clf_1 = pipeline(model=model_1, task="image-classification", image_processor=image_processor_1, device=device)
clf_2 = pipeline("image-classification", model=MODEL_PATHS["model_2"], device=device)
feature_extractor_3 = AutoFeatureExtractor.from_pretrained(MODEL_PATHS["model_3"], device=device)
model_3 = AutoModelForImageClassification.from_pretrained(MODEL_PATHS["model_3"]).to(device)
feature_extractor_4 = AutoFeatureExtractor.from_pretrained(MODEL_PATHS["model_4"], device=device)
model_4 = AutoModelForImageClassification.from_pretrained(MODEL_PATHS["model_4"]).to(device)
clf_5 = pipeline("image-classification", model=MODEL_PATHS["model_5"], device=device)
clf_5b = pipeline("image-classification", model=MODEL_PATHS["model_5b"], device=device)
return clf_1, clf_2, feature_extractor_3, model_3, feature_extractor_4, model_4, clf_5, clf_5b
clf_1, clf_2, feature_extractor_3, model_3, feature_extractor_4, model_4, clf_5, clf_5b = load_models()
@spaces.GPU(duration=10)
def predict_with_model(img_pil, clf, class_names, confidence_threshold, model_name, model_id, feature_extractor=None):
try:
if feature_extractor:
inputs = feature_extractor(img_pil, return_tensors="pt").to(device)
with torch.no_grad():
outputs = clf(**inputs)
logits = outputs.logits
probabilities = softmax(logits.cpu().numpy()[0])
result = {class_names[i]: probabilities[i] for i in range(len(class_names))}
else:
prediction = clf(img_pil)
result = {pred['label']: pred['score'] for pred in prediction}
result_output = [model_id, model_name, result.get(class_names[1], 0.0), result.get(class_names[0], 0.0)]
logger.info(result_output)
for class_name in class_names:
if class_name not in result:
result[class_name] = 0.0
if result[class_names[0]] >= confidence_threshold:
label = f"AI, Confidence: {result[class_names[0]]:.4f}"
result_output.append('AI')
elif result[class_names[1]] >= confidence_threshold:
label = f"Real, Confidence: {result[class_names[1]]:.4f}"
result_output.append('REAL')
else:
label = "Uncertain Classification"
result_output.append('UNCERTAIN')
except Exception as e:
label = f"Error: {str(e)}"
result_output = [model_id, model_name, 0.0, 0.0, 'ERROR'] # Ensure result_output is assigned in case of error
return label, result_output
@spaces.GPU(duration=10)
def predict_image(img, confidence_threshold):
if not isinstance(img, Image.Image):
raise ValueError(f"Expected a PIL Image, but got {type(img)}")
if img.mode != 'RGB':
img_pil = img.convert('RGB')
else:
img_pil = img
img_pil = transforms.Resize((256, 256))(img_pil)
img_pilvits = transforms.Resize((224, 224))(img_pil)
label_1, result_1output = predict_with_model(img_pil, clf_1, CLASS_NAMES["model_1"], confidence_threshold, "SwinV2-base", 1)
label_2, result_2output = predict_with_model(img_pilvits, clf_2, CLASS_NAMES["model_2"], confidence_threshold, "ViT-base Classifier", 2)
label_3, result_3output = predict_with_model(img_pil, model_3, CLASS_NAMES["model_3"], confidence_threshold, "SDXL-Trained", 3, feature_extractor_3)
label_4, result_4output = predict_with_model(img_pil, model_4, CLASS_NAMES["model_4"], confidence_threshold, "SDXL + FLUX", 4, feature_extractor_4)
label_5, result_5output = predict_with_model(img_pilvits, clf_5, CLASS_NAMES["model_5"], confidence_threshold, "ViT-base Newcomer", 5)
label_5b, result_5boutput = predict_with_model(img_pilvits, clf_5b, CLASS_NAMES["model_5b"], confidence_threshold, "ViT-base Newcomer", 6)
combined_results = {
"SwinV2/detect": label_1,
"ViT/AI-vs-Real": label_2,
"Swin/SDXL": label_3,
"Swin/SDXL-FLUX": label_4,
"prithivMLmods": label_5,
"prithivMLmods-2-22": label_5b
}
combined_outputs = [result_1output, result_2output, result_3output, result_4output, result_5output, result_5boutput]
return img_pil, combined_outputs
# Define a function to generate the HTML content
def generate_results_html(results):
def get_header_color(label):
if label == 'AI':
return 'bg-red-500 text-red-700', 'bg-red-400', 'bg-red-100', 'bg-red-700 text-red-700', 'bg-red-200'
elif label == 'REAL':
return 'bg-green-500 text-green-700', 'bg-green-400', 'bg-green-100', 'bg-green-700 text-green-700', 'bg-green-200'
elif label == 'UNCERTAIN':
return 'bg-yellow-500 text-yellow-700 bg-yellow-100', 'bg-yellow-400', 'bg-yellow-100', 'bg-yellow-700 text-yellow-700', 'bg-yellow-200'
elif label == 'MAINTENANCE':
return 'bg-blue-500 text-blue-700', 'bg-blue-400', 'bg-blue-100', 'bg-blue-700 text-blue-700', 'bg-blue-200'
else:
return 'bg-gray-300 text-gray-700', 'bg-gray-400', 'bg-gray-100', 'bg-gray-700 text-gray-700', 'bg-gray-200'
html_content = f"""
<link href="https://unpkg.com/[email protected]/dist/tailwind.min.css" rel="stylesheet">
<div class="container mx-auto">
<div class="grid xl:grid-cols-5 md:grid-cols-5 grid-cols-1 gap-1">
<!-- Tile 1: SwinV2/detect -->
<div
class="flex flex-col bg-gray-800 rounded-sm p-4 m-1 border border-gray-800 shadow-xs transition hover:shadow-lg dark:shadow-gray-700/25">
<div
class="-m-4 h-24 {get_header_color(results[0][-1])[0]} rounded-sm rounded-b-none transition border group-hover:border-gray-100 group-hover:shadow-lg">
<span class="text-gray-300 font-mono tracking-widest p-4 pb-3 block text-xs text-center">MODEL 1:</span>
<span
class="flex w-24 mx-auto tracking-wide items-center justify-center rounded-full {get_header_color(results[0][-1])[2]} px-1 py-0.5 {get_header_color(results[0][-1])[3]}"
>
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="3" stroke="currentColor" class="w-4 h-4 mr-2 -ml-3 group-hover:animate group-hover:animate-pulse">
{'<path stroke-linecap="round" stroke-linejoin="round" d="M9 12.75 11.25 15 15 9.75M21 12a9 9 0 1 1-18 0 9 9 0 0 1 18 0Z" />' if results[0][-1] == 'REAL' else '<path stroke-linecap="round" stroke-linejoin="round" d="m9.75 9.75 4.5 4.5m0-4.5-4.5 4.5M21 12a9 9 0 1 1-18 0 9 9 0 0 1 18 0Z" />'}
</svg>
<p class="whitespace-nowrap text-lg leading-normal font-bold text-center self-center align-middle py-px">{results[0][-1]}</p>
</span>
</div>
<div>
<div class="mt-4 relative -mx-4 bg-gray-800">
<div class="w-full bg-gray-400 rounded-none h-8">
<div class="inline-flex whitespace-nowrap bg-green-400 h-full rounded-none" style="width: {results[0][2] * 100:.2f}%;">
<p class="p-2 px-4 text-xs self-center align-middle">Conf:
<span class="ml-1 font-medium font-mono">{results[0][2]:.4f}</span>
</p>
</div>
</div>
</div>
<div class="relative -mx-4 bg-gray-800">
<div class="w-full bg-gray-400 rounded-none h-8">
<div class="inline-flex whitespace-nowrap bg-red-400 h-full rounded-none" style="width: {results[0][3] * 100:.2f}%;">
<p class="p-2 px-4 text-xs self-center align-middle">Conf:
<span class="ml-1 font-medium font-mono">{results[0][3]:.4f}</span>
</p>
</div>
</div>
</div>
</div>
<div class="flex flex-col items-start">
<h4 class="mt-4 text-sm font-semibold tracking-wide">SwinV2 Based</h4>
<div class="text-xs font-mono">Real: {results[0][2]:.4f}, AI: {results[0][3]:.4f}</div>
<a class="mt-2 text-xs tracking-wide">@haywoodsloan / more info</a>
</div>
</div>
<!-- Tile 2: ViT/AI-vs-Real -->
<div
class="flex flex-col bg-gray-800 rounded-sm p-4 m-1 border border-gray-800 shadow-xs transition hover:shadow-lg dark:shadow-gray-700/25">
<div
class="-m-4 h-24 {get_header_color(results[1][-1])[0]} rounded-sm rounded-b-none transition border group-hover:border-gray-100 group-hover:shadow-lg group-hover:{get_header_color(results[1][-1])[4]}">
<span class="text-gray-300 font-mono tracking-widest p-4 pb-3 block text-xs text-center">MODEL 2:</span>
<span
class="flex w-24 mx-auto tracking-wide items-center justify-center rounded-full {get_header_color(results[1][-1])[2]} px-1 py-0.5 {get_header_color(results[1][-1])[3]}"
>
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="3" stroke="currentColor" class="w-4 h-4 mr-2 -ml-3 group-hover:animate group-hover:animate-pulse">
{'<path stroke-linecap="round" stroke-linejoin="round" d="M9 12.75 11.25 15 15 9.75M21 12a9 9 0 1 1-18 0 9 9 0 0 1 18 0Z" />' if results[1][-1] == 'REAL' else '<path stroke-linecap="round" stroke-linejoin="round" d="m9.75 9.75 4.5 4.5m0-4.5-4.5 4.5M21 12a9 9 0 1 1-18 0 9 9 0 0 1 18 0Z" />'}
</svg>
<p class="whitespace-nowrap text-lg leading-normal font-bold text-center self-center align-middle py-px">{results[1][-1]}</p>
</span>
</div>
<div>
<div class="mt-4 relative -mx-4 bg-gray-800">
<div class="w-full bg-gray-400 rounded-none h-8">
<div class="inline-flex whitespace-nowrap bg-green-400 h-full rounded-none" style="width: {results[1][2] * 100:.2f}%;">
<p class="p-2 px-4 text-xs self-center align-middle">Conf:
<span class="ml-1 font-medium font-mono">{results[1][2]:.4f}</span>
</p>
</div>
</div>
</div>
<div class="relative -mx-4 bg-gray-800">
<div class="w-full bg-gray-400 rounded-none h-8">
<div class="inline-flex whitespace-nowrap bg-red-400 h-full rounded-none" style="width: {results[1][3] * 100:.2f}%;">
<p class="p-2 px-4 text-xs self-center align-middle">Conf:
<span class="ml-1 font-medium font-mono">{results[1][3]:.4f}</span>
</p>
</div>
</div>
</div>
</div>
<div class="flex flex-col items-start">
<h4 class="mt-4 text-sm font-semibold tracking-wide">ViT Based</h4>
<div class="text-xs font-mono">Real: {results[1][2]:.4f}, AI: {results[1][3]:.4f}</div>
<a class="mt-2 text-xs tracking-wide">@Heem2 / more info</a>
</div>
</div>
<!-- Tile 3: Swin/SDXL -->
<div
class="flex flex-col bg-gray-800 rounded-sm p-4 m-1 border border-gray-800 shadow-xs transition hover:shadow-lg dark:shadow-gray-700/25">
<div
class="-m-4 h-24 {get_header_color(results[2][-1])[0]} rounded-sm rounded-b-none transition border group-hover:border-gray-100 group-hover:shadow-lg group-hover:{get_header_color(results[2][-1])[4]}">
<span class="text-gray-300 font-mono tracking-widest p-4 pb-3 block text-xs text-center">MODEL 3:</span>
<span
class="flex w-24 mx-auto tracking-wide items-center justify-center rounded-full {get_header_color(results[2][-1])[2]} px-1 py-0.5 {get_header_color(results[2][-1])[3]}"
>
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="3" stroke="currentColor" class="w-4 h-4 mr-2 -ml-3 group-hover:animate group-hover:animate-pulse">
{'<path stroke-linecap="round" stroke-linejoin="round" d="M9 12.75 11.25 15 15 9.75M21 12a9 9 0 1 1-18 0 9 9 0 0 1 18 0Z" />' if results[2][-1] == 'REAL' else '<path stroke-linecap="round" stroke-linejoin="round" d="m9.75 9.75 4.5 4.5m0-4.5-4.5 4.5M21 12a9 9 0 1 1-18 0 9 9 0 0 1 18 0Z" />'}
</svg>
<p class="whitespace-nowrap text-lg leading-normal font-bold text-center self-center align-middle py-px">{results[2][-1]}</p>
</span>
</div>
<div>
<div class="mt-4 relative -mx-4 bg-gray-800">
<div class="w-full bg-gray-400 rounded-none h-8">
<div class="inline-flex whitespace-nowrap bg-green-400 h-full rounded-none" style="width: {results[2][2] * 100:.2f}%;">
<p class="p-2 px-4 text-xs self-center align-middle">Conf:
<span class="ml-1 font-medium font-mono">{results[2][2]:.4f}</span>
</p>
</div>
</div>
</div>
<div class="relative -mx-4 bg-gray-800">
<div class="w-full bg-gray-400 rounded-none h-8">
<div class="inline-flex whitespace-nowrap bg-red-400 h-full rounded-none" style="width: {results[2][3] * 100:.2f}%;">
<p class="p-2 px-4 text-xs self-center align-middle">Conf:
<span class="ml-1 font-medium font-mono">{results[2][3]:.4f}</span>
</p>
</div>
</div>
</div>
</div>
<div class="flex flex-col items-start">
<h4 class="mt-4 text-sm font-semibold tracking-wide">SDXL Dataset</h4>
<div class="text-xs font-mono">Real: {results[2][2]:.4f}, AI: {results[2][3]:.4f}</div>
<a class="mt-2 text-xs tracking-wide">@Organika / more info</a>
</div>
</div>
<!-- Tile 4: Swin/SDXL-FLUX -->
<div
class="flex flex-col bg-gray-800 rounded-sm p-4 m-1 border border-gray-800 shadow-xs transition hover:shadow-lg dark:shadow-gray-700/25">
<div
class="-m-4 h-24 {get_header_color(results[3][-1])[0]} rounded-sm rounded-b-none transition border group-hover:border-gray-100 group-hover:shadow-lg group-hover:{get_header_color(results[3][-1])[4]}">
<span class="text-gray-300 font-mono tracking-widest p-4 pb-3 block text-xs text-center">MODEL 4:</span>
<span
class="flex w-24 mx-auto tracking-wide items-center justify-center rounded-full {get_header_color(results[3][-1])[2]} px-1 py-0.5 {get_header_color(results[3][-1])[3]}"
>
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="3" stroke="currentColor" class="w-4 h-4 mr-2 -ml-3 group-hover:animate group-hover:animate-pulse">
{'<path stroke-linecap="round" stroke-linejoin="round" d="M9 12.75 11.25 15 15 9.75M21 12a9 9 0 1 1-18 0 9 9 0 0 1 18 0Z" />' if results[3][-1] == 'REAL' else '<path stroke-linecap="round" stroke-linejoin="round" d="m9.75 9.75 4.5 4.5m0-4.5-4.5 4.5M21 12a9 9 0 1 1-18 0 9 9 0 0 1 18 0Z" />'}
</svg>
<p class="whitespace-nowrap text-lg leading-normal font-bold text-center self-center align-middle py-px">{results[3][-1]}</p>
</span>
</div>
<div>
<div class="mt-4 relative -mx-4 bg-gray-800">
<div class="w-full bg-gray-400 rounded-none h-8">
<div class="inline-flex whitespace-nowrap bg-green-400 h-full rounded-none" style="width: {results[3][2] * 100:.2f}%;">
<p class="p-2 px-4 text-xs self-center align-middle">Conf:
<span class="ml-1 font-medium font-mono">{results[3][2]:.4f}</span>
</p>
</div>
</div>
</div>
<div class="relative -mx-4 bg-gray-800">
<div class="w-full bg-gray-400 rounded-none h-8">
<div class="inline-flex whitespace-nowrap bg-red-400 h-full rounded-none" style="width: {results[3][3] * 100:.2f}%;">
<p class="p-2 px-4 text-xs self-center align-middle">Conf:
<span class="ml-1 font-medium font-mono">{results[3][3]:.4f}</span>
</p>
</div>
</div>
</div>
</div>
<div class="flex flex-col items-start">
<h4 class="mt-4 text-sm font-semibold tracking-wide">SDXL + FLUX</h4>
<div class="text-xs font-mono">Real: {results[3][2]:.4f}, AI: {results[3][3]:.4f}</div>
<a class="mt-2 text-xs tracking-wide">@cmckinle / more info</a>
</div>
</div>
<!-- Tile 5: Newcomer -->
<div
class="flex flex-col bg-gray-800 rounded-sm p-4 m-1 border border-gray-800 shadow-xs transition hover:shadow-lg dark:shadow-gray-700/25">
<div
class="-m-4 h-24 {get_header_color(results[4][-1])[0]} rounded-sm rounded-b-none transition border group-hover:border-gray-100 group-hover:shadow-lg group-hover:{get_header_color(results[4][-1])[4]}">
<span class="text-gray-300 font-mono tracking-widest p-4 pb-3 block text-xs text-center">MODEL 5:</span>
<span
class="flex w-24 mx-auto tracking-wide items-center justify-center rounded-full {get_header_color(results[4][-1])[2]} px-1 py-0.5 {get_header_color(results[4][-1])[3]}"
>
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="3" stroke="currentColor" class="w-4 h-4 mr-2 -ml-3 group-hover:animate group-hover:animate-pulse">
{'<path stroke-linecap="round" stroke-linejoin="round" d="M9 12.75 11.25 15 15 9.75M21 12a9 9 0 1 1-18 0 9 9 0 0 1 18 0Z" />' if results[4][-1] == 'REAL' else '<path stroke-linecap="round" stroke-linejoin="round" d="m9.75 9.75 4.5 4.5m0-4.5-4.5 4.5M21 12a9 9 0 1 1-18 0 9 9 0 0 1 18 0Z" />'}
</svg>
<p class="whitespace-nowrap text-lg leading-normal font-bold text-center self-center align-middle py-px">{results[4][-1]}</p>
</span>
</div>
<div>
<div class="mt-4 relative -mx-4 bg-gray-800">
<div class="w-full bg-gray-400 rounded-none h-8">
<div class="inline-flex whitespace-nowrap bg-green-400 h-full rounded-none" style="width: {results[4][2] * 100:.2f}%;">
<p class="p-2 px-4 text-xs self-center align-middle">Conf:
<span class="ml-1 font-medium font-mono">{results[4][2]:.4f}</span>
</p>
</div>
</div>
</div>
<div class="relative -mx-4 bg-gray-800">
<div class="w-full bg-gray-400 rounded-none h-8">
<div class="inline-flex whitespace-nowrap bg-red-400 h-full rounded-none" style="width: {results[4][3] * 100:.2f}%;">
<p class="p-2 px-4 text-xs self-center align-middle">Conf:
<span class="ml-1 font-medium font-mono">{results[4][3]:.4f}</span>
</p>
</div>
</div>
</div>
</div>
<div class="flex flex-col items-start">
<h4 class="mt-4 text-sm font-semibold tracking-wide">Vits Model</h4>
<div class="text-xs font-mono">Real: {results[4][2]:.4f}, AI: {results[4][3]:.4f}</div>
<div class="text-xs font-mono">Real: {results[5][2]:.4f}, AI: {results[5][3]:.4f}</div>
<a class="mt-2 text-xs tracking-wide">@prithivMLmods / more info</a>
</div>
</div>
</div>
</div>
"""
return html_content
# Modify the predict_image function to return the HTML content
def predict_image_with_html(img, confidence_threshold):
img_pil, results = predict_image(img, confidence_threshold)
html_content = generate_results_html(results)
return img_pil, html_content
# Define the Gradio interface
with gr.Blocks() as iface:
gr.Markdown("# AI Generated Image / Deepfake Detection Models Evaluation")
with gr.Row():
with gr.Column(scale=1):
image_input = gr.Image(label="Upload Image to Analyze", sources=['upload'], type='pil')
with gr.Accordion("Settings", open=False, elem_id="settings_accordion"):
confidence_slider = gr.Slider(0.0, 1.0, value=0.5, step=0.01, label="Confidence Threshold")
inputs = [image_input, confidence_slider]
with gr.Column(scale=2):
with gr.Accordion("Project OpenSight - Model Evaluations & Playground", open=True, elem_id="project_accordion"):
gr.Markdown("## OpenSight is a SOTA gen. image detection model, in pre-release prep.\n\nThis HF Space is a temporary home for us and the public to evaluate the shortcomings of current open source models.\n\n<-- Feel free to play around by starting with an image as we prepare our formal announcement.")
image_output = gr.Image(label="Processed Image", visible=False)
# Custom HTML component to display results in 5 columns
results_html = gr.HTML(label="Model Predictions")
outputs = [image_output, results_html]
# gr.Button("Predict").click(fn=predict_image_with_html, inputs=inputs, outputs=outputs)
predict_button = gr.Button("Predict")
predict_button.click(
fn=predict_image_with_html,
inputs=inputs,
outputs=outputs
)
predict_button.click(
fn=None,
js="() => {document.getElementById('project_accordion').open = false;}", # Close the project accordion
inputs=[],
outputs=[]
)
# Launch the interface
iface.launch() |