|
import gradio as gr |
|
|
|
from transformers import pipeline, AutoImageProcessor, Swinv2ForImageClassification, Swinv2Model |
|
from torchvision import transforms |
|
|
|
|
|
|
|
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) |
|
|
|
class_names = ['artificial', 'real'] |
|
|
|
def predict_image(img): |
|
img = transforms.ToPILImage()(img) |
|
img = transforms.Resize((256,256))(img) |
|
prediction=clf.predict(img) |
|
return {class_names[i]: float(prediction[i]["score"]) for i in range(2)} |
|
|
|
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() |