File size: 1,286 Bytes
51a174a
 
 
b9089ed
51a174a
b9089ed
 
51a174a
b9089ed
51a174a
b9089ed
 
51a174a
b9089ed
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
51a174a
b9089ed
51a174a
b9089ed
 
51a174a
351984d
b9089ed
 
 
51a174a
 
 
b9089ed
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
import gradio as gr
from transformers import pipeline
from PIL import Image
import torch

# Load the Hugging Face model
MODEL_ID = "jacoballessio/ai-image-detect-distilled"

pipe = pipeline("image-classification", model=MODEL_ID)

# Prediction function
def predict_image(image):
    try:
        results = pipe(image)
        
        # Extract top two predictions
        top_results = sorted(results, key=lambda x: x['score'], reverse=True)[:2]
        labels = [r['label'] for r in top_results]
        scores = [round(r['score'] * 100, 2) for r in top_results]

        # Determine final result
        final_label = labels[0]
        final_score = scores[0]

        # Display result
        return {
            "Model Results": top_results,
            "Final AI Probability (%)": final_score,
            "Overall Decision": "AI-generated" if "ai" in final_label.lower() else "Human"
        }
    except Exception as e:
        return {"error": str(e)}

# Gradio Interface
iface = gr.Interface(
    fn=predict_image,
    inputs=gr.Image(type="pil", label="Upload Image"),
    outputs="json",
    title="AI Image Detector",
    description="Detect whether an image is AI-generated or real using Jacob Allessio's distilled model."
)

if __name__ == "__main__":
    iface.launch()