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", "model_6": "ideepankarsharma2003/AI_ImageClassification_MidjourneyV6_SDXL" } 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'], "model_6": ['ai_gen', 'human'], } # 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) feature_extractor_6 = AutoFeatureExtractor.from_pretrained(MODEL_PATHS["model_6"], device=device) model_6 = AutoModelForImageClassification.from_pretrained(MODEL_PATHS["model_6"]).to(device) return clf_1, clf_2, feature_extractor_3, model_3, feature_extractor_4, model_4, clf_5, clf_5b, model_6, feature_extractor_6 clf_1, clf_2, feature_extractor_3, model_3, feature_extractor_4, model_4, clf_5, clf_5b, feature_extractor_6, model_6 = 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) label_6, result_6output = predict_with_model(img_pilvits, model_6, CLASS_NAMES["model_6"], confidence_threshold, "Swin Midjourney/SDXL", 6, feature_extractor_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, "SwinMidSDXL": label_6 } combined_outputs = [result_1output, result_2output, result_3output, result_4output, result_5output, result_5boutput, result_6output] 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' def generate_tile_html(index, result, model_name, contributor, model_path): label = result[-1] header_colors = get_header_color(label) real_conf = result[2] ai_conf = result[3] return f"""
MODEL {index + 1}: {'' if label == 'REAL' else ''}

{label}

Conf: {real_conf:.4f}

Conf: {ai_conf:.4f}

{model_name}

Real: {real_conf:.4f}, AI: {ai_conf:.4f}
by @{contributor}
""" html_content = f"""
{generate_tile_html(0, results[0], "SwinV2 Based", "haywoodsloan", MODEL_PATHS["model_1"])} {generate_tile_html(1, results[1], "ViT Based", "Heem2", MODEL_PATHS["model_2"])} {generate_tile_html(2, results[2], "SDXL Dataset", "Organika", MODEL_PATHS["model_3"])} {generate_tile_html(3, results[3], "SDXL + FLUX", "cmckinle", MODEL_PATHS["model_4"])} {generate_tile_html(4, results[4], "Vit Based", "prithivMLmods", MODEL_PATHS["model_5"])} {generate_tile_html(5, results[5], "Vit Based, Newer Dataset", "prithivMLmods", MODEL_PATHS["model_5b"])} {generate_tile_html(6, results[6], "Swin, Midjourney+SDXL", "ideepankarsharma2003", MODEL_PATHS["model_6"])}
""" return html_content def predict_image_with_html(img, confidence_threshold, augment_methods, rotate_degrees, noise_level, sharpen_strength): if augment_methods: img_pil, _ = augment_image(img, augment_methods, rotate_degrees, noise_level, sharpen_strength) else: img_pil = img img_pil, results = predict_image(img_pil, confidence_threshold) html_content = generate_results_html(results) return img_pil, html_content 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"): augment_checkboxgroup = gr.CheckboxGroup(["rotate", "add_noise", "sharpen"], label="Augmentation Methods") rotate_slider = gr.Slider(0, 360, value=0, step=1, label="Rotate Degrees", visible=False) noise_slider = gr.Slider(0, 100, value=0, step=1, label="Noise Level", visible=False) sharpen_slider = gr.Slider(0, 100, value=1, step=1, label="Sharpen Strength", visible=False) confidence_slider = gr.Slider(0.0, 1.0, value=0.5, step=0.01, label="Confidence Threshold") inputs = [image_input, confidence_slider, augment_checkboxgroup, rotate_slider, noise_slider, sharpen_slider] predict_button = gr.Button("Predict") image_output = gr.Image(label="Processed Image", visible=True) with gr.Column(scale=2): with gr.Accordion("Project OpenSight - Model Evaluations & Playground", open=False, 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.") # Custom HTML component to display results in 5 columns results_html = gr.HTML(label="Model Predictions") outputs = [image_output, results_html] # Show/hide rotate slider based on selected augmentation method augment_checkboxgroup.change(lambda methods: gr.update(visible="rotate" in methods), inputs=[augment_checkboxgroup], outputs=[rotate_slider]) augment_checkboxgroup.change(lambda methods: gr.update(visible="add_noise" in methods), inputs=[augment_checkboxgroup], outputs=[noise_slider]) augment_checkboxgroup.change(lambda methods: gr.update(visible="sharpen" in methods), inputs=[augment_checkboxgroup], outputs=[sharpen_slider]) 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()