Update README.md
Browse files
README.md
CHANGED
|
@@ -43,32 +43,40 @@ pip install transformers torch pillow
|
|
| 43 |
```
|
| 44 |
|
| 45 |
```python
|
|
|
|
| 46 |
from transformers import AutoModelForImageClassification, AutoFeatureExtractor
|
| 47 |
from PIL import Image
|
| 48 |
import torch
|
| 49 |
|
| 50 |
-
|
| 51 |
-
|
|
|
|
|
|
|
| 52 |
|
| 53 |
-
# Load
|
| 54 |
-
|
| 55 |
-
|
|
|
|
| 56 |
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
logits = model(**inputs).logits
|
| 64 |
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
labels = ["REAL", "FAKE"]
|
| 68 |
-
prediction = labels[predicted_class_id]
|
| 69 |
-
confidence = torch.nn.functional.softmax(logits, dim=-1)[0][predicted_class_id].item()
|
| 70 |
|
| 71 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
```
|
| 73 |
---
|
| 74 |
license: mit
|
|
|
|
| 43 |
```
|
| 44 |
|
| 45 |
```python
|
| 46 |
+
import argparse
|
| 47 |
from transformers import AutoModelForImageClassification, AutoFeatureExtractor
|
| 48 |
from PIL import Image
|
| 49 |
import torch
|
| 50 |
|
| 51 |
+
def main():
|
| 52 |
+
parser = argparse.ArgumentParser(description="Predict image class using fine-tuned model from Hugging Face Hub")
|
| 53 |
+
parser.add_argument("--image", type=str, required=True, help="Path to the input image")
|
| 54 |
+
args = parser.parse_args()
|
| 55 |
|
| 56 |
+
# Load model and feature extractor directly from Hugging Face Hub
|
| 57 |
+
model_name = "SADRACODING/SDXL-Deepfake-Detector"
|
| 58 |
+
model = AutoModelForImageClassification.from_pretrained(model_name)
|
| 59 |
+
feature_extractor = AutoFeatureExtractor.from_pretrained(model_name)
|
| 60 |
|
| 61 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 62 |
+
model.to(device)
|
| 63 |
+
model.eval()
|
| 64 |
|
| 65 |
+
image = Image.open(args.image).convert("RGB")
|
| 66 |
+
inputs = feature_extractor(images=image, return_tensors="pt").to(device)
|
|
|
|
| 67 |
|
| 68 |
+
with torch.no_grad():
|
| 69 |
+
outputs = model(**inputs)
|
|
|
|
|
|
|
|
|
|
| 70 |
|
| 71 |
+
logits = outputs.logits
|
| 72 |
+
predicted_class_idx = logits.argmax(-1).item()
|
| 73 |
+
predicted_label = model.config.id2label[predicted_class_idx]
|
| 74 |
+
|
| 75 |
+
print(f"Predicted class index: {predicted_class_idx}")
|
| 76 |
+
print(f"Predicted label: {predicted_label}")
|
| 77 |
+
|
| 78 |
+
if __name__ == "__main__":
|
| 79 |
+
main()
|
| 80 |
```
|
| 81 |
---
|
| 82 |
license: mit
|