File size: 1,285 Bytes
93f5629
17c8406
93f5629
 
a9d7990
433b282
 
93f5629
 
a9d7990
f43015f
93f5629
 
a9d7990
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
93f5629
a9d7990
f43015f
93f5629
f43015f
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
import gradio as gr
from transformers import pipeline, AutoImageProcessor, Swinv2ForImageClassification
from torchvision import transforms

# Load the model and processor
image_processor = AutoImageProcessor.from_pretrained("haywoodsloan/ai-image-detector-deploy")
model = Swinv2ForImageClassification.from_pretrained("haywoodsloan/ai-image-detector-deploy")
clf = pipeline(model=model, task="image-classification", image_processor=image_processor)

# Define class names
class_names = ['artificial', 'real']

def predict_image(img):
    # Convert the image to a PIL Image and resize it
    img = transforms.ToPILImage()(img)
    img = transforms.Resize((256, 256))(img)
    
    # Get the prediction
    prediction = clf(img)
    
    # Process the prediction to match the class names
    result = {pred['label']: pred['score'] for pred in prediction}
    
    # Ensure the result dictionary contains both class names
    for class_name in class_names:
        if class_name not in result:
            result[class_name] = 0.0
    
    return result

# Define the Gradio interface
image = gr.Image(label="Image to Analyze", sources=['upload'])
label = gr.Label(num_top_classes=2)
gr.Interface(fn=predict_image, inputs=image, outputs=label, title="AI Generated Classification").launch()