Update README.md
Browse files
README.md
CHANGED
@@ -13,39 +13,34 @@ license: mit
|
|
13 |
This model is designed to detect whether an image is real or AI-generated. It uses Vision Transformer (VIT) architecture to provide accurate classification.
|
14 |
|
15 |
## Model Usage
|
16 |
-
|
17 |
```python
|
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 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
});
|
49 |
-
|
50 |
-
return await response.json();
|
51 |
-
}
|
|
|
13 |
This model is designed to detect whether an image is real or AI-generated. It uses Vision Transformer (VIT) architecture to provide accurate classification.
|
14 |
|
15 |
## Model Usage
|
|
|
16 |
```python
|
17 |
+
from transformers import ViTImageProcessor, ViTForImageClassification
|
18 |
+
from PIL import Image
|
19 |
+
import torch
|
20 |
+
|
21 |
+
# Load model and processor
|
22 |
+
processor = ViTImageProcessor.from_pretrained("yaya36095/ai-image-detector")
|
23 |
+
model = ViTForImageClassification.from_pretrained("yaya36095/ai-image-detector")
|
24 |
+
|
25 |
+
def detect_image(image_path):
|
26 |
+
# Open image
|
27 |
+
image = Image.open(image_path)
|
28 |
+
|
29 |
+
# Process image
|
30 |
+
inputs = processor(images=image, return_tensors="pt")
|
31 |
+
|
32 |
+
# Get predictions
|
33 |
+
with torch.no_grad():
|
34 |
+
outputs = model(**inputs)
|
35 |
+
predictions = outputs.logits.softmax(dim=-1)
|
36 |
+
|
37 |
+
# Get result
|
38 |
+
prediction_id = torch.argmax(predictions).item()
|
39 |
+
confidence = predictions[0][prediction_id].item() * 100
|
40 |
+
|
41 |
+
result = "AI Generated" if prediction_id == 1 else "Real Image"
|
42 |
+
return result, confidence
|
43 |
+
|
44 |
+
# Example usage
|
45 |
+
# result, confidence = detect_image("path/to/image.jpg")
|
46 |
+
# print(f"Result: {result} (Confidence: {confidence:.2f}%)")
|
|
|
|
|
|
|
|