|
import gradio as gr |
|
from gradio_client import Client, handle_file |
|
from PIL import Image, ImageFilter |
|
import numpy as np |
|
import os |
|
import time |
|
import logging |
|
import io |
|
import collections |
|
import onnxruntime |
|
|
|
from utils.utils import softmax, augment_image |
|
from forensics.gradient import gradient_processing |
|
from forensics.minmax import minmax_process |
|
from forensics.ela import ELA |
|
from forensics.wavelet import noise_estimation |
|
from forensics.bitplane import bit_plane_extractor |
|
from utils.hf_logger import log_inference_data |
|
from utils.load import load_image |
|
from agents.ensemble_team import EnsembleMonitorAgent, WeightOptimizationAgent, SystemHealthAgent |
|
from agents.smart_agents import ContextualIntelligenceAgent, ForensicAnomalyDetectionAgent |
|
from utils.registry import register_model, MODEL_REGISTRY, ModelEntry |
|
from agents.ensemble_weights import ModelWeightManager |
|
from transformers import pipeline, AutoImageProcessor, SwinForImageClassification, Swinv2ForImageClassification, AutoFeatureExtractor, AutoModelForImageClassification |
|
from torchvision import transforms |
|
import torch |
|
import json |
|
from huggingface_hub import CommitScheduler |
|
from dotenv import load_dotenv |
|
|
|
logging.basicConfig(level=logging.INFO) |
|
logger = logging.getLogger(__name__) |
|
os.environ['HF_HUB_CACHE'] = './models' |
|
|
|
|
|
class GradioLogHandler(logging.Handler): |
|
def __init__(self, log_queue): |
|
super().__init__() |
|
self.log_queue = log_queue |
|
self.setFormatter(logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')) |
|
|
|
def emit(self, record): |
|
self.log_queue.append(self.format(record)) |
|
|
|
log_queue = collections.deque(maxlen=1000) |
|
gradio_handler = GradioLogHandler(log_queue) |
|
|
|
|
|
logging.getLogger().setLevel(logging.INFO) |
|
logging.getLogger().addHandler(gradio_handler) |
|
|
|
|
|
LOCAL_LOG_DIR = "./hf_inference_logs" |
|
HF_DATASET_NAME="aiwithoutborders-xyz/degentic_rd0" |
|
load_dotenv() |
|
|
|
|
|
class NumpyEncoder(json.JSONEncoder): |
|
def default(self, obj): |
|
if isinstance(obj, np.float32): |
|
return float(obj) |
|
return json.JSONEncoder.default(self, obj) |
|
|
|
|
|
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') |
|
|
|
|
|
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_v1.1", |
|
"model_5": "prithivMLmods/Deep-Fake-Detector-v2-Model", |
|
"model_6": "ideepankarsharma2003/AI_ImageClassification_MidjourneyV6_SDXL", |
|
"model_7": "date3k2/vit-real-fake-classification-v4" |
|
} |
|
|
|
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_6": ['ai_gen', 'human'], |
|
"model_7": ['Fake', 'Real'], |
|
} |
|
|
|
def preprocess_resize_256(image): |
|
if image.mode != 'RGB': |
|
image = image.convert('RGB') |
|
return transforms.Resize((256, 256))(image) |
|
|
|
def preprocess_resize_224(image): |
|
if image.mode != 'RGB': |
|
image = image.convert('RGB') |
|
return transforms.Resize((224, 224))(image) |
|
|
|
def postprocess_pipeline(prediction, class_names): |
|
|
|
return {pred['label']: pred['score'] for pred in prediction} |
|
|
|
def postprocess_logits(outputs, class_names): |
|
|
|
logits = outputs.logits.cpu().numpy()[0] |
|
probabilities = softmax(logits) |
|
return {class_names[i]: probabilities[i] for i in range(len(class_names))} |
|
|
|
def register_model_with_metadata(model_id, model, preprocess, postprocess, class_names, display_name, contributor, model_path, architecture=None, dataset=None): |
|
entry = ModelEntry(model, preprocess, postprocess, class_names, display_name=display_name, contributor=contributor, model_path=model_path, architecture=architecture, dataset=dataset) |
|
MODEL_REGISTRY[model_id] = entry |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ONNX_QUANTIZED_MODEL_PATH = "./models/model_1_quantized.onnx" |
|
|
|
def preprocess_onnx_input(image: Image.Image): |
|
|
|
if image.mode != 'RGB': |
|
image = image.convert('RGB') |
|
|
|
transform = transforms.Compose([ |
|
transforms.Resize((256, 256)), |
|
transforms.ToTensor(), |
|
transforms.Normalize(mean=[0.485,0.456,0.406], std=[0.229,0.224,0.225]), |
|
]) |
|
input_tensor = transform(image) |
|
|
|
return input_tensor.unsqueeze(0).cpu().numpy() |
|
|
|
def infer_onnx_model(preprocessed_image_np): |
|
try: |
|
|
|
if not os.path.exists(ONNX_QUANTIZED_MODEL_PATH): |
|
logger.error(f"ONNX quantized model not found at: {ONNX_QUANTIZED_MODEL_PATH}") |
|
raise FileNotFoundError(f"ONNX quantized model not found at: {ONNX_QUANTIZED_MODEL_PATH}") |
|
|
|
ort_session = onnxruntime.InferenceSession(ONNX_QUANTIZED_MODEL_PATH) |
|
ort_inputs = {ort_session.get_inputs()[0].name: preprocessed_image_np} |
|
ort_outputs = ort_session.run(None, ort_inputs) |
|
|
|
|
|
logits = ort_outputs[0] |
|
probabilities = softmax(logits[0]) |
|
return {"logits": logits, "probabilities": probabilities} |
|
|
|
except Exception as e: |
|
logger.error(f"Error during ONNX inference: {e}") |
|
|
|
return {"logits": np.array([]), "probabilities": np.array([])} |
|
|
|
def postprocess_onnx_output(onnx_output, class_names): |
|
probabilities = onnx_output.get("probabilities") |
|
if probabilities is not None and len(probabilities) == len(class_names): |
|
return {class_names[i]: probabilities[i] for i in range(len(class_names))} |
|
else: |
|
logger.warning("ONNX post-processing failed or class names mismatch.") |
|
return {name: 0.0 for name in class_names} |
|
|
|
|
|
register_model_with_metadata( |
|
"model_1_onnx_quantized", |
|
infer_onnx_model, |
|
preprocess_onnx_input, |
|
postprocess_onnx_output, |
|
CLASS_NAMES["model_1"], |
|
display_name="SWIN1", |
|
contributor="haywoodsloan", |
|
model_path=ONNX_QUANTIZED_MODEL_PATH, |
|
architecture="SwinV2", |
|
dataset="TBA" |
|
) |
|
|
|
|
|
clf_2 = pipeline("image-classification", model=MODEL_PATHS["model_2"], device=device) |
|
register_model_with_metadata( |
|
"model_2", clf_2, preprocess_resize_224, postprocess_pipeline, CLASS_NAMES["model_2"], |
|
display_name="VIT2", contributor="Heem2", model_path=MODEL_PATHS["model_2"], |
|
architecture="ViT", dataset="TBA" |
|
) |
|
|
|
feature_extractor_3 = AutoFeatureExtractor.from_pretrained(MODEL_PATHS["model_3"], device=device) |
|
model_3 = AutoModelForImageClassification.from_pretrained(MODEL_PATHS["model_3"]).to(device) |
|
def preprocess_256(image): |
|
if image.mode != 'RGB': |
|
image = image.convert('RGB') |
|
return transforms.Resize((256, 256))(image) |
|
def postprocess_logits_model3(outputs, class_names): |
|
logits = outputs.logits.cpu().numpy()[0] |
|
probabilities = softmax(logits) |
|
return {class_names[i]: probabilities[i] for i in range(len(class_names))} |
|
def model3_infer(image): |
|
inputs = feature_extractor_3(image, return_tensors="pt").to(device) |
|
with torch.no_grad(): |
|
outputs = model_3(**inputs) |
|
return outputs |
|
register_model_with_metadata( |
|
"model_3", model3_infer, preprocess_256, postprocess_logits_model3, CLASS_NAMES["model_3"], |
|
display_name="SDXL3", contributor="Organika", model_path=MODEL_PATHS["model_3"], |
|
architecture="VIT", dataset="SDXL" |
|
) |
|
|
|
feature_extractor_4 = AutoFeatureExtractor.from_pretrained(MODEL_PATHS["model_4"], device=device) |
|
model_4 = AutoModelForImageClassification.from_pretrained(MODEL_PATHS["model_4"]).to(device) |
|
def model4_infer(image): |
|
inputs = feature_extractor_4(image, return_tensors="pt").to(device) |
|
with torch.no_grad(): |
|
outputs = model_4(**inputs) |
|
return outputs |
|
def postprocess_logits_model4(outputs, class_names): |
|
logits = outputs.logits.cpu().numpy()[0] |
|
probabilities = softmax(logits) |
|
return {class_names[i]: probabilities[i] for i in range(len(class_names))} |
|
register_model_with_metadata( |
|
"model_4", model4_infer, preprocess_256, postprocess_logits_model4, CLASS_NAMES["model_4"], |
|
display_name="XLFLUX4", contributor="cmckinle", model_path=MODEL_PATHS["model_4"], |
|
architecture="VIT", dataset="SDXL, FLUX" |
|
) |
|
|
|
clf_5 = pipeline("image-classification", model=MODEL_PATHS["model_5"], device=device) |
|
register_model_with_metadata( |
|
"model_5", clf_5, preprocess_resize_224, postprocess_pipeline, CLASS_NAMES["model_5"], |
|
display_name="VIT5", contributor="prithivMLmods", model_path=MODEL_PATHS["model_5"], |
|
architecture="VIT", dataset="TBA" |
|
) |
|
|
|
image_processor_6 = AutoImageProcessor.from_pretrained(MODEL_PATHS["model_6"], use_fast=True) |
|
model_6 = SwinForImageClassification.from_pretrained(MODEL_PATHS["model_6"]).to(device) |
|
clf_6 = pipeline(model=model_6, task="image-classification", image_processor=image_processor_6, device=device) |
|
register_model_with_metadata( |
|
"model_6", clf_6, preprocess_resize_224, postprocess_pipeline, CLASS_NAMES["model_6"], |
|
display_name="SWIN6", contributor="ideepankarsharma2003", model_path=MODEL_PATHS["model_6"], |
|
architecture="SWINv1", dataset="SDXL, Midjourney" |
|
) |
|
|
|
image_processor_7 = AutoImageProcessor.from_pretrained(MODEL_PATHS["model_7"], use_fast=True) |
|
model_7 = AutoModelForImageClassification.from_pretrained(MODEL_PATHS["model_7"]).to(device) |
|
clf_7 = pipeline(model=model_7, task="image-classification", image_processor=image_processor_7, device=device) |
|
register_model_with_metadata( |
|
"model_7", clf_7, preprocess_resize_224, postprocess_pipeline, CLASS_NAMES["model_7"], |
|
display_name="VIT7", contributor="date3k2", model_path=MODEL_PATHS["model_7"], |
|
architecture="VIT", dataset="TBA" |
|
) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def infer(image: Image.Image, model_id: str, confidence_threshold: float = 0.75) -> dict: |
|
"""Predict using a specific model. |
|
|
|
Args: |
|
image (Image.Image): The input image to classify. |
|
model_id (str): The ID of the model to use for classification. |
|
confidence_threshold (float, optional): The confidence threshold for classification. Defaults to 0.75. |
|
|
|
Returns: |
|
dict: A dictionary containing the model details, classification scores, and label. |
|
""" |
|
entry = MODEL_REGISTRY[model_id] |
|
img = entry.preprocess(image) if entry.preprocess else image |
|
try: |
|
result = entry.model(img) |
|
scores = entry.postprocess(result, entry.class_names) |
|
ai_score = float(scores.get(entry.class_names[0], 0.0)) |
|
real_score = float(scores.get(entry.class_names[1], 0.0)) |
|
label = "AI" if ai_score >= confidence_threshold else ("REAL" if real_score >= confidence_threshold else "UNCERTAIN") |
|
return { |
|
"Model": entry.display_name, |
|
"Contributor": entry.contributor, |
|
"HF Model Path": entry.model_path, |
|
"AI Score": ai_score, |
|
"Real Score": real_score, |
|
"Label": label |
|
} |
|
except Exception as e: |
|
return { |
|
"Model": entry.display_name, |
|
"Contributor": entry.contributor, |
|
"HF Model Path": entry.model_path, |
|
"AI Score": 0.0, |
|
"Real Score": 0.0, |
|
"Label": f"Error: {str(e)}" |
|
} |
|
|
|
def full_prediction(img, confidence_threshold, rotate_degrees, noise_level, sharpen_strength): |
|
"""Full prediction run, with a team of ensembles and agents. |
|
|
|
Args: |
|
img (url: str, Image.Image, np.ndarray): The input image to classify. |
|
confidence_threshold (float, optional): The confidence threshold for classification. Defaults to 0.75. |
|
rotate_degrees (int, optional): The degrees to rotate the image. |
|
noise_level (int, optional): The noise level to use. |
|
sharpen_strength (int, optional): The sharpen strength to use. |
|
|
|
Returns: |
|
dict: A dictionary containing the model details, classification scores, and label. |
|
""" |
|
|
|
if img is None: |
|
raise gr.Error("No image provided. Please upload an image to analyze.") |
|
|
|
if isinstance(img, str): |
|
try: |
|
img = load_image(img) |
|
except Exception as e: |
|
logger.error(f"Error loading image from path: {e}") |
|
raise gr.Error(f"Could not load image from the provided path. Error: {str(e)}") |
|
|
|
if not isinstance(img, Image.Image): |
|
try: |
|
img = Image.fromarray(img) |
|
except Exception as e: |
|
logger.error(f"Error converting input image to PIL: {e}") |
|
raise gr.Error("Input image could not be converted to a valid image format. Please try another image.") |
|
|
|
|
|
if img.mode != 'RGB': |
|
img = img.convert('RGB') |
|
|
|
monitor_agent = EnsembleMonitorAgent() |
|
weight_manager = ModelWeightManager(strongest_model_id="simple_prediction") |
|
optimization_agent = WeightOptimizationAgent(weight_manager) |
|
health_agent = SystemHealthAgent() |
|
context_agent = ContextualIntelligenceAgent() |
|
anomaly_agent = ForensicAnomalyDetectionAgent() |
|
health_agent.monitor_system_health() |
|
if rotate_degrees or noise_level or sharpen_strength: |
|
img_pil, _ = augment_image(img, ["rotate", "add_noise", "sharpen"], rotate_degrees, noise_level, sharpen_strength) |
|
else: |
|
img_pil = img |
|
img_np_og = np.array(img) |
|
|
|
model_predictions_raw = {} |
|
confidence_scores = {} |
|
results = [] |
|
table_rows = [] |
|
|
|
|
|
for model_id in MODEL_REGISTRY: |
|
model_start = time.time() |
|
result = infer(img_pil, model_id, confidence_threshold) |
|
model_end = time.time() |
|
monitor_agent.monitor_prediction( |
|
model_id, |
|
result["Label"], |
|
max(result.get("AI Score", 0.0), result.get("Real Score", 0.0)), |
|
model_end - model_start |
|
) |
|
model_predictions_raw[model_id] = result |
|
confidence_scores[model_id] = max(result.get("AI Score", 0.0), result.get("Real Score", 0.0)) |
|
results.append(result) |
|
table_rows.append([ |
|
result.get("Model", ""), |
|
result.get("Contributor", ""), |
|
round(result.get("AI Score", 0.0), 3) if result.get("AI Score") is not None else 0.0, |
|
round(result.get("Real Score", 0.0), 3) if result.get("Real Score") is not None else 0.0, |
|
result.get("Label", "Error") |
|
]) |
|
|
|
yield None, None, table_rows, None, None |
|
|
|
|
|
image_data_for_context = { |
|
"width": img.width, |
|
"height": img.height, |
|
"mode": img.mode, |
|
} |
|
detected_context_tags = context_agent.infer_context_tags(image_data_for_context, model_predictions_raw) |
|
logger.info(f"Detected context tags: {detected_context_tags}") |
|
adjusted_weights = weight_manager.adjust_weights(model_predictions_raw, confidence_scores, context_tags=detected_context_tags) |
|
weighted_predictions = {"AI": 0.0, "REAL": 0.0, "UNCERTAIN": 0.0} |
|
for model_id, prediction in model_predictions_raw.items(): |
|
prediction_label = prediction.get("Label") |
|
if prediction_label in weighted_predictions: |
|
weighted_predictions[prediction_label] += adjusted_weights[model_id] |
|
else: |
|
logger.warning(f"Unexpected prediction label '{prediction_label}' from model '{model_id}'. Skipping its weight in consensus.") |
|
final_prediction_label = "UNCERTAIN" |
|
if weighted_predictions["AI"] > weighted_predictions["REAL"] and weighted_predictions["AI"] > weighted_predictions["UNCERTAIN"]: |
|
final_prediction_label = "AI" |
|
elif weighted_predictions["REAL"] > weighted_predictions["AI"] and weighted_predictions["REAL"] > weighted_predictions["UNCERTAIN"]: |
|
final_prediction_label = "REAL" |
|
optimization_agent.analyze_performance(final_prediction_label, None) |
|
gradient_image = gradient_processing(img_np_og) |
|
gradient_image2 = gradient_processing(img_np_og, intensity=45, equalize=True) |
|
minmax_image = minmax_process(img_np_og) |
|
minmax_image2 = minmax_process(img_np_og, radius=6) |
|
|
|
ela1 = ELA(img_np_og, quality=75, scale=50, contrast=20, linear=False, grayscale=True) |
|
ela2 = ELA(img_np_og, quality=75, scale=75, contrast=25, linear=False, grayscale=True) |
|
ela3 = ELA(img_np_og, quality=75, scale=75, contrast=25, linear=False, grayscale=False) |
|
forensics_images = [img_pil, ela1, ela2, ela3, gradient_image, gradient_image2, minmax_image, minmax_image2] |
|
forensic_output_descriptions = [ |
|
f"Original augmented image (PIL): {img_pil.width}x{img_pil.height}", |
|
"ELA analysis (Pass 1): Grayscale error map, quality 75.", |
|
"ELA analysis (Pass 2): Grayscale error map, quality 75, enhanced contrast.", |
|
"ELA analysis (Pass 3): Color error map, quality 75, enhanced contrast.", |
|
"Gradient processing: Highlights edges and transitions.", |
|
"Gradient processing: Int=45, Equalize=True", |
|
"MinMax processing: Deviations in local pixel values.", |
|
"MinMax processing (Radius=6): Deviations in local pixel values.", |
|
|
|
] |
|
anomaly_detection_results = anomaly_agent.analyze_forensic_outputs(forensic_output_descriptions) |
|
logger.info(f"Forensic anomaly detection: {anomaly_detection_results['summary']}") |
|
consensus_html = f"<div style='font-size: 2.2em; font-weight: bold;padding: 10px;'>Consensus: <span style='color:{'red' if final_prediction_label == 'AI' else ('green' if final_prediction_label == 'REAL' else 'orange')}'>{final_prediction_label}</span></div>" |
|
inference_params = { |
|
"confidence_threshold": confidence_threshold, |
|
"rotate_degrees": rotate_degrees, |
|
"noise_level": noise_level, |
|
"sharpen_strength": sharpen_strength, |
|
"detected_context_tags": detected_context_tags |
|
} |
|
ensemble_output_data = { |
|
"final_prediction_label": final_prediction_label, |
|
"weighted_predictions": weighted_predictions, |
|
"adjusted_weights": adjusted_weights |
|
} |
|
agent_monitoring_data_log = { |
|
"ensemble_monitor": { |
|
"alerts": monitor_agent.alerts, |
|
"performance_metrics": monitor_agent.performance_metrics |
|
}, |
|
"weight_optimization": { |
|
"prediction_history_length": len(optimization_agent.prediction_history), |
|
}, |
|
"system_health": { |
|
"memory_usage": health_agent.health_metrics["memory_usage"], |
|
"gpu_utilization": health_agent.health_metrics["gpu_utilization"] |
|
}, |
|
"context_intelligence": { |
|
"detected_context_tags": detected_context_tags |
|
}, |
|
"forensic_anomaly_detection": anomaly_detection_results |
|
} |
|
log_inference_data( |
|
original_image=img, |
|
inference_params=inference_params, |
|
model_predictions=results, |
|
ensemble_output=ensemble_output_data, |
|
forensic_images=forensics_images, |
|
agent_monitoring_data=agent_monitoring_data_log, |
|
human_feedback=None |
|
) |
|
cleaned_forensics_images = [] |
|
for f_img in forensics_images: |
|
if isinstance(f_img, Image.Image): |
|
cleaned_forensics_images.append(f_img) |
|
elif isinstance(f_img, np.ndarray): |
|
try: |
|
cleaned_forensics_images.append(Image.fromarray(f_img)) |
|
except Exception as e: |
|
logger.warning(f"Could not convert numpy array to PIL Image for gallery: {e}") |
|
else: |
|
logger.warning(f"Unexpected type in forensic_images: {type(f_img)}. Skipping.") |
|
logger.info(f"Cleaned forensic images types: {[type(img) for img in cleaned_forensics_images]}") |
|
for i, res_dict in enumerate(results): |
|
for key in ["AI Score", "Real Score"]: |
|
value = res_dict.get(key) |
|
if isinstance(value, np.float32): |
|
res_dict[key] = float(value) |
|
logger.info(f"Converted {key} for result {i} from numpy.float32 to float.") |
|
json_results = json.dumps(results, cls=NumpyEncoder) |
|
yield img_pil, cleaned_forensics_images, table_rows, json_results, consensus_html |
|
|
|
detection_model_eval_playground = gr.Interface( |
|
fn=full_prediction, |
|
inputs=[ |
|
gr.Image(label="Upload Image to Analyze", sources=['upload', 'webcam'], type='filepath'), |
|
gr.Slider(0.0, 1.0, value=0.7, step=0.05, label="Confidence Threshold"), |
|
gr.Slider(0, 45, value=0, step=1, label="Rotate Degrees", visible=False), |
|
gr.Slider(0, 50, value=0, step=1, label="Noise Level", visible=False), |
|
gr.Slider(0, 50, value=0, step=1, label="Sharpen Strength", visible=False) |
|
], |
|
outputs=[ |
|
gr.Image(label="Processed Image", visible=False), |
|
gr.Gallery(label="Post Processed Images", visible=True, columns=[4], rows=[2], container=False, height="auto", object_fit="contain", elem_id="post-gallery"), |
|
gr.Dataframe( |
|
label="Model Predictions", |
|
headers=["Arch / Dataset", "By", "AI", "Real", "Label"], |
|
datatype=["str", "str", "number", "number", "str"] |
|
), |
|
gr.JSON(label="Raw Model Results", visible=False), |
|
gr.Markdown(label="Consensus", value="") |
|
], |
|
title="Multi-Model Ensemble + Agentic Coordinated Deepfake Detection (Paper in Progress)", |
|
description="The detection of AI-generated images has entered a critical inflection point. While existing solutions struggle with outdated datasets and inflated claims, our approach prioritizes agility, community collaboration, and an offensive approach to deepfake detection.", |
|
api_name="predict", |
|
live=True |
|
) |
|
|
|
|
|
|
|
|
|
|
|
def predict(img): |
|
""" |
|
Predicts whether an image is AI-generated or real using the SOTA Community Forensics model. |
|
|
|
Args: |
|
img (str): Path to the input image file to analyze. |
|
|
|
Returns: |
|
dict: A dictionary containing: |
|
- 'Fake Probability' (float): Probability score between 0 and 1 indicating likelihood of being AI-generated |
|
- 'Result Description' (str): Human-readable description of the prediction result |
|
|
|
Example: |
|
>>> result = predict("path/to/image.jpg") |
|
>>> print(result) |
|
{'Fake Probability': 0.002, 'Result Description': 'The image is likely real.'} |
|
""" |
|
client = Client("aiwithoutborders-xyz/OpenSight-Community-Forensics-Preview") |
|
client.view_api() |
|
result = client.predict( |
|
handle_file(img), |
|
api_name="/simple_predict" |
|
) |
|
return str(result) |
|
community_forensics_preview = gr.Interface( |
|
fn=predict, |
|
inputs=gr.Image(type="filepath"), |
|
outputs=gr.HTML(), |
|
title="Quick and simple prediction by our strongest model.", |
|
description="No ensemble, no context, no agents, just a quick and simple prediction by our strongest model.", |
|
api_name="predict" |
|
) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def simple_prediction(img): |
|
""" |
|
Quick and simple deepfake or real image prediction by the strongest open-source model on the hub. |
|
|
|
Args: |
|
img (str): The input image to analyze, provided as a file path. |
|
|
|
Returns: |
|
str: The prediction result stringified from dict. Example: `{'Fake Probability': 0.002, 'Result Description': 'The image is likely real.'}` |
|
""" |
|
client = Client("aiwithoutborders-xyz/OpenSight-Community-Forensics-Preview") |
|
client.view_api() |
|
client.predict( |
|
handle_file(img), |
|
api_name="simple_predict" |
|
) |
|
simple_predict_interface = gr.Interface( |
|
fn=simple_prediction, |
|
inputs=gr.Image(type="filepath"), |
|
outputs=gr.Text(), |
|
title="Quick and simple prediction by our strongest model.", |
|
description="No ensemble, no context, no agents, just a quick and simple prediction by our strongest model.", |
|
api_name="simple_predict" |
|
) |
|
|
|
noise_estimation_interface = gr.Interface( |
|
fn=noise_estimation, |
|
inputs=[gr.Image(type="pil"), gr.Slider(1, 32, value=8, step=1, label="Block Size")], |
|
outputs=gr.Image(type="pil"), |
|
title="Wavelet-Based Noise Analysis", |
|
description="Analyzes image noise patterns using wavelet decomposition. This tool helps detect compression artifacts and artificial noise patterns that may indicate image manipulation. Higher noise levels in specific regions can reveal areas of potential tampering.", |
|
api_name="tool_waveletnoise" |
|
) |
|
|
|
bit_plane_interface = gr.Interface( |
|
fn=bit_plane_extractor, |
|
inputs=[ |
|
gr.Image(type="pil"), |
|
gr.Dropdown(["Luminance", "Red", "Green", "Blue", "RGB Norm"], label="Channel", value="Luminance"), |
|
gr.Slider(0, 7, value=0, step=1, label="Bit Plane"), |
|
gr.Dropdown(["Disabled", "Median", "Gaussian"], label="Filter", value="Disabled") |
|
], |
|
outputs=gr.Image(type="pil"), |
|
title="Bit Plane Analysis", |
|
description="Extracts and visualizes individual bit planes from different color channels. This forensic tool helps identify hidden patterns and artifacts in image data that may indicate manipulation. Different bit planes can reveal inconsistencies in image processing or editing.", |
|
api_name="tool_bitplane" |
|
) |
|
|
|
ela_interface = gr.Interface( |
|
fn=ELA, |
|
inputs=[ |
|
gr.Image(type="pil", label="Input Image"), |
|
gr.Slider(1, 100, value=75, step=1, label="JPEG Quality"), |
|
gr.Slider(1, 100, value=50, step=1, label="Output Scale (Multiplicative Gain)"), |
|
gr.Slider(0, 100, value=20, step=1, label="Output Contrast (Tonality Compression)"), |
|
gr.Checkbox(value=False, label="Use Linear Difference"), |
|
gr.Checkbox(value=False, label="Grayscale Output") |
|
], |
|
outputs=gr.Image(type="pil"), |
|
title="Error Level Analysis (ELA)", |
|
description="Performs Error Level Analysis to detect re-saved JPEG images, which can indicate tampering. ELA highlights areas of an image that have different compression levels.", |
|
api_name="tool_ela" |
|
) |
|
|
|
gradient_processing_interface = gr.Interface( |
|
fn=gradient_processing, |
|
inputs=[ |
|
gr.Image(type="pil", label="Input Image"), |
|
gr.Slider(0, 100, value=90, step=1, label="Intensity"), |
|
gr.Dropdown(["Abs", "None", "Flat", "Norm"], label="Blue Mode", value="Abs"), |
|
gr.Checkbox(value=False, label="Invert Gradients"), |
|
gr.Checkbox(value=False, label="Equalize Histogram") |
|
], |
|
outputs=gr.Image(type="pil"), |
|
title="Gradient Processing", |
|
description="Applies gradient filters to an image to enhance edges and transitions, which can reveal inconsistencies due to manipulation.", |
|
api_name="tool_gradient_processing" |
|
) |
|
|
|
minmax_processing_interface = gr.Interface( |
|
fn=minmax_process, |
|
inputs=[ |
|
gr.Image(type="pil", label="Input Image"), |
|
gr.Radio([0, 1, 2, 3, 4], label="Channel (0:Grayscale, 1:Blue, 2:Green, 3:Red, 4:RGB Norm)", value=4), |
|
gr.Slider(0, 10, value=2, step=1, label="Radius") |
|
], |
|
outputs=gr.Image(type="pil"), |
|
title="MinMax Processing", |
|
description="Analyzes local pixel value deviations to detect subtle changes in image data, often indicative of digital forgeries.", |
|
api_name="tool_minmax_processing" |
|
) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
demo = gr.TabbedInterface( |
|
[ |
|
detection_model_eval_playground, |
|
community_forensics_preview, |
|
noise_estimation_interface, |
|
bit_plane_interface, |
|
ela_interface, |
|
gradient_processing_interface, |
|
minmax_processing_interface, |
|
|
|
], |
|
[ |
|
"Run Ensemble Prediction", |
|
"Open-Source SOTA Model", |
|
"Wavelet Blocking Noise Estimation", |
|
"Bit Plane Values", |
|
"Error Level Analysis (ELA)", |
|
"Gradient Processing", |
|
"MinMax Processing", |
|
|
|
], |
|
title="Deepfake Detection & Forensics Tools", |
|
theme=None, |
|
|
|
) |
|
footerMD = """ |
|
### ⚠️ ENSEMBLE TEAM IN TRAINING ⚠️ \n\n |
|
|
|
1. **DISCLAIMER: METADATA AS WELL AS MEDIA SUBMITTED TO THIS SPACE MAY BE VIEWED AND SELECTED FOR FUTURE DATASETS, PLEASE DO NOT SUBMIT PERSONAL CONTENT. FOR UNTRACKED, PRIVATE USE OF THE MODELS YOU MAY STILL USE [THE ORIGINAL SPACE HERE](https://huggingface.co/spaces/aiwithoutborders-xyz/OpenSight-Deepfake-Detection-Models-Playground), SOTA MODEL INCLUDED.** |
|
2. **UPDATE 6-13-25**: APOLOGIES FOR THE CONFUSION, WE ARE WORKING TO REVERT THE ORIGINAL REPO BACK TO ITS NON-DATA COLLECTION STATE -- ONLY THE "SIMPLE PREDICTION" ENDPOINT IS CURRENTLY 100% PRIVATE. PLEASE STAY TUNED AS WE FIGURE OUT A SOLUTION FOR THE ENSEMBLE + AGENT TEAM ENDPOINT. IT CAN GET RESOURCE INTENSIVE TO RUN A FULL PREDICTION. ALTERNATIVELY, WE **ENCOURAGE** ANYONE TO FORK AND CONTRIBUTE TO THE PROJECT. |
|
3. **UPDATE 6-13-25 (cont.)**: WHILE WE HAVE NOT TAKEN A STANCE ON NSFW AND EXPLICIT CONTENT, PLEASE REFRAIN FROM ... YOUR HUMAN DESIRES UNTIL WE GET THIS PRIVACY SITUATION SORTED OUT. DO NOT BE RECKLESS PLEASE. OUR PAPER WILL BE OUT SOON ON ARXIV WHICH WILL EXPLAIN EVERYTHING WITH DATA-BACKED RESEARCH ON WHY THIS PROJECT IS NEEDED, BUT WE CANNOT DO IT WITHOUT THE HELP OF THE COMMUNITY. |
|
|
|
TO SUMMARIZE: DATASET COLLECTION WILL CONTINUE FOR OUR NOVEL ENSEMBLE-TEAM PREDICTION PIPELINE UNTIL WE CAN GET THINGS SORTED OUT. FOR THOSE THAT WISH TO OPT-OUT, WE OFFER THE SIMPLE, BUT [MOST POWERFUL DETECTION MODEL HERE.](https://huggingface.co/spaces/aiwithoutborders-xyz/OpenSight-Community-Forensics-Preview) |
|
|
|
""" |
|
footer = gr.Markdown("", elem_classes="footer") |
|
|
|
with gr.Blocks() as app: |
|
demo.render() |
|
footer.render() |
|
|
|
|
|
app.queue(max_size=10, default_concurrency_limit=2).launch(mcp_server=True) |