Update README.md
Browse files
README.md
CHANGED
@@ -15,44 +15,53 @@ This model is designed to detect whether an image is real or AI-generated. It us
|
|
15 |
|
16 |
## Model Usage
|
17 |
```python
|
|
|
18 |
from transformers import ViTImageProcessor, ViTForImageClassification
|
19 |
from PIL import Image
|
20 |
import torch
|
21 |
|
22 |
-
#
|
23 |
processor = ViTImageProcessor.from_pretrained("yaya36095/ai-image-detector")
|
24 |
model = ViTForImageClassification.from_pretrained("yaya36095/ai-image-detector")
|
25 |
|
26 |
-
# Define class labels (Replace these with the labels used during training)
|
27 |
-
class_labels = ["Real Image", "AI Generated"]
|
28 |
-
|
29 |
def detect_image(image_path):
|
30 |
-
#
|
31 |
image = Image.open(image_path)
|
|
|
|
|
32 |
|
33 |
-
#
|
34 |
inputs = processor(images=image, return_tensors="pt")
|
35 |
|
36 |
-
#
|
37 |
with torch.no_grad():
|
38 |
outputs = model(**inputs)
|
39 |
predictions = outputs.logits.softmax(dim=-1)
|
40 |
|
41 |
-
#
|
|
|
42 |
results = [
|
43 |
-
{"label":
|
44 |
-
|
45 |
]
|
46 |
|
47 |
-
#
|
48 |
-
results
|
49 |
|
50 |
-
return
|
51 |
-
|
52 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
53 |
# result = detect_image("path/to/image.jpg")
|
54 |
-
#
|
55 |
-
#
|
|
|
56 |
|
57 |
```
|
58 |
|
|
|
15 |
|
16 |
## Model Usage
|
17 |
```python
|
18 |
+
```python
|
19 |
from transformers import ViTImageProcessor, ViTForImageClassification
|
20 |
from PIL import Image
|
21 |
import torch
|
22 |
|
23 |
+
# تحميل النموذج والمعالج
|
24 |
processor = ViTImageProcessor.from_pretrained("yaya36095/ai-image-detector")
|
25 |
model = ViTForImageClassification.from_pretrained("yaya36095/ai-image-detector")
|
26 |
|
|
|
|
|
|
|
27 |
def detect_image(image_path):
|
28 |
+
# فتح وتجهيز الصورة
|
29 |
image = Image.open(image_path)
|
30 |
+
if image.mode != 'RGB':
|
31 |
+
image = image.convert('RGB')
|
32 |
|
33 |
+
# معالجة الصورة
|
34 |
inputs = processor(images=image, return_tensors="pt")
|
35 |
|
36 |
+
# الحصول على التنبؤات
|
37 |
with torch.no_grad():
|
38 |
outputs = model(**inputs)
|
39 |
predictions = outputs.logits.softmax(dim=-1)
|
40 |
|
41 |
+
# تحليل النتائج
|
42 |
+
scores = predictions[0].tolist()
|
43 |
results = [
|
44 |
+
{"label": "REAL", "score": scores[0]},
|
45 |
+
{"label": "FAKE", "score": scores[1]}
|
46 |
]
|
47 |
|
48 |
+
# ترتيب النتائج حسب درجة الثقة
|
49 |
+
results.sort(key=lambda x: x["score"], reverse=True)
|
50 |
|
51 |
+
return {
|
52 |
+
"prediction": results[0]["label"],
|
53 |
+
"confidence": f"{results[0]['score']*100:.2f}%",
|
54 |
+
"detailed_scores": [
|
55 |
+
f"{r['label']}: {r['score']*100:.2f}%"
|
56 |
+
for r in results
|
57 |
+
]
|
58 |
+
}
|
59 |
+
|
60 |
+
# مثال للاستخدام
|
61 |
# result = detect_image("path/to/image.jpg")
|
62 |
+
# print(f"النتيجة: {result['prediction']}")
|
63 |
+
# print(f"درجة الثقة: {result['confidence']}")
|
64 |
+
# print("التفاصيل:", *result['detailed_scores'], sep="\n- ")
|
65 |
|
66 |
```
|
67 |
|