Upload 2 files
Browse files- app.py +61 -0
- requirements.txt +5 -0
app.py
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
from PIL import Image as PILImage
|
| 4 |
+
from transformers import AutoImageProcessor, SiglipForImageClassification
|
| 5 |
+
|
| 6 |
+
# --- Model Loading ---
|
| 7 |
+
MODEL_IDENTIFIER = r"Ateeqq/ai-vs-human-image-detector"
|
| 8 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 9 |
+
|
| 10 |
+
print(f"Loading model: {MODEL_IDENTIFIER} on device: {device}")
|
| 11 |
+
processor = AutoImageProcessor.from_pretrained(MODEL_IDENTIFIER)
|
| 12 |
+
model = SiglipForImageClassification.from_pretrained(MODEL_IDENTIFIER)
|
| 13 |
+
model.to(device)
|
| 14 |
+
model.eval()
|
| 15 |
+
print("Model loaded successfully.")
|
| 16 |
+
|
| 17 |
+
# --- Prediction Function ---
|
| 18 |
+
def predict(image):
|
| 19 |
+
"""
|
| 20 |
+
Takes a PIL image, preprocesses it, and returns the prediction probabilities.
|
| 21 |
+
"""
|
| 22 |
+
if image is None:
|
| 23 |
+
return None
|
| 24 |
+
|
| 25 |
+
# Preprocess the image
|
| 26 |
+
inputs = processor(images=image, return_tensors="pt").to(device)
|
| 27 |
+
|
| 28 |
+
# Perform inference
|
| 29 |
+
with torch.no_grad():
|
| 30 |
+
outputs = model(**inputs)
|
| 31 |
+
logits = outputs.logits
|
| 32 |
+
|
| 33 |
+
# Get probabilities
|
| 34 |
+
probabilities = torch.softmax(logits, dim=-1)[0]
|
| 35 |
+
|
| 36 |
+
# Create a dictionary of labels and their scores
|
| 37 |
+
confidences = {model.config.id2label[i]: score.item() for i, score in enumerate(probabilities)}
|
| 38 |
+
return confidences
|
| 39 |
+
|
| 40 |
+
# --- Gradio Interface ---
|
| 41 |
+
# Define the Gradio interface components
|
| 42 |
+
image_input = gr.Image(type="pil", label="Upload an Image")
|
| 43 |
+
label_output = gr.Label(num_top_classes=2, label="Prediction")
|
| 44 |
+
|
| 45 |
+
# The title and description for the app
|
| 46 |
+
title = "AI vs Human Image Detector"
|
| 47 |
+
description = """
|
| 48 |
+
This Space uses the `Ateeqq/ai-vs-human-image-detector` model to classify an image as either AI-generated or Human-made.
|
| 49 |
+
Upload an image to see the prediction.
|
| 50 |
+
"""
|
| 51 |
+
article = "Model by [Ateeqq](https://huggingface.co/Ateeqq) | Gradio app created with AI"
|
| 52 |
+
|
| 53 |
+
# Launch the Gradio app
|
| 54 |
+
gr.Interface(
|
| 55 |
+
fn=predict,
|
| 56 |
+
inputs=image_input,
|
| 57 |
+
outputs=label_output,
|
| 58 |
+
title=title,
|
| 59 |
+
description=description,
|
| 60 |
+
article=article
|
| 61 |
+
).launch(share=True, server_name="0.0.0.0")
|
requirements.txt
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
transformers
|
| 2 |
+
torch
|
| 3 |
+
Pillow
|
| 4 |
+
accelerate
|
| 5 |
+
gradio
|