yaya36095 commited on
Commit
dd5cb06
·
verified ·
1 Parent(s): 3555163

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +26 -17
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
- # Load model and processor
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
- # Open image
31
  image = Image.open(image_path)
 
 
32
 
33
- # Process image
34
  inputs = processor(images=image, return_tensors="pt")
35
 
36
- # Get predictions
37
  with torch.no_grad():
38
  outputs = model(**inputs)
39
  predictions = outputs.logits.softmax(dim=-1)
40
 
41
- # Map predictions to class labels
 
42
  results = [
43
- {"label": class_labels[i], "confidence": f"{(predictions[0][i].item() * 100):.2f}%"}
44
- for i in range(len(class_labels))
45
  ]
46
 
47
- # Sort results by confidence (optional)
48
- results = sorted(results, key=lambda x: float(x["confidence"].strip('%')), reverse=True)
49
 
50
- return results
51
-
52
- # Example usage
 
 
 
 
 
 
 
53
  # result = detect_image("path/to/image.jpg")
54
- # for r in result:
55
- # print(f"Class: {r['label']}, Confidence: {r['confidence']}")
 
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