Spaces:
Build error
Build error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import ViTFeatureExtractor, ViTForImageClassification
|
| 3 |
+
import torch
|
| 4 |
+
import requests
|
| 5 |
+
|
| 6 |
+
# Load the pre-trained ViT model and feature extractor
|
| 7 |
+
model = ViTForImageClassification.from_pretrained("umitkantar/ViTBasePatch16Art")
|
| 8 |
+
feature_extractor = ViTFeatureExtractor.from_pretrained("umitkantar/ViTBasePatch16Art")
|
| 9 |
+
|
| 10 |
+
# Define the prediction function
|
| 11 |
+
def ai_image_detector(image):
|
| 12 |
+
# Preprocess the input image
|
| 13 |
+
inputs = feature_extractor(images=image, return_tensors="pt")
|
| 14 |
+
|
| 15 |
+
# Make a prediction using the loaded model
|
| 16 |
+
outputs = model(**inputs)
|
| 17 |
+
predicted_class_idx = torch.argmax(outputs.logits).item()
|
| 18 |
+
|
| 19 |
+
# Calculate the confidence score (percentage)
|
| 20 |
+
confidence_score = torch.softmax(outputs.logits, dim=1)[0][predicted_class_idx].item() * 100
|
| 21 |
+
|
| 22 |
+
return {f"AI-generated Probability (%): {confidence_score:.2f}": confidence_score}
|
| 23 |
+
|
| 24 |
+
# Set up the Gradio interface
|
| 25 |
+
image_input = gr.inputs.Image()
|
| 26 |
+
gr.Interface(ai_image_detector, image_input, "label", capture_session=True).launch()
|