Instructions to use hamzenium/ViT-Deepfake-Classifier with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use hamzenium/ViT-Deepfake-Classifier with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("image-classification", model="hamzenium/ViT-Deepfake-Classifier") pipe("https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/hub/parrots.png")# pip install -U transformers accelerate # Load model directly from transformers import AutoImageProcessor, AutoModelForImageClassification processor = AutoImageProcessor.from_pretrained("hamzenium/ViT-Deepfake-Classifier") model = AutoModelForImageClassification.from_pretrained("hamzenium/ViT-Deepfake-Classifier", device_map="auto") - Notebooks
- Google Colab
- Kaggle
Download main.py from hamzenium/ViT-Deepfake-Classifier: direct link, hf CLI and curl.
- Browser
- Download file 1.11 kB
-
https://huggingface.co/hamzenium/ViT-Deepfake-Classifier/resolve/main/main.py
- Command line
-
hf download hf://hamzenium/ViT-Deepfake-Classifier/main.py
-
curl -L -o main.py https://huggingface.co/hamzenium/ViT-Deepfake-Classifier/resolve/main/main.py
1.11 kB
| from pathlib import Path | |
| from PIL import Image | |
| import torch | |
| from transformers import AutoImageProcessor, ViTForImageClassification | |
| from fastapi import FastAPI, File, UploadFile | |
| from io import BytesIO | |
| import shutil | |
| model_path = "./best_model" | |
| app = FastAPI() | |
| processor = AutoImageProcessor.from_pretrained( | |
| model_path, | |
| local_files_only=True, | |
| use_fast=True | |
| ) | |
| model = ViTForImageClassification.from_pretrained( | |
| model_path, | |
| local_files_only=True, | |
| id2label={"0": "real", "1": "fake"}, | |
| label2id={"real": 0, "fake": 1} | |
| ) | |
| def predict_image(image: Image.Image): | |
| inputs = processor(image, return_tensors="pt") | |
| with torch.no_grad(): | |
| outputs = model(**inputs) | |
| pred_id = torch.argmax(outputs.logits, dim=1).item() | |
| pred_label = model.config.id2label[str(pred_id)] | |
| return pred_label | |
| async def upload_file(file: UploadFile = File(...)): | |
| image_data = await file.read() | |
| image = Image.open(BytesIO(image_data)).convert("RGB") | |
| prediction = predict_image(image) | |
| return {"filename": file.filename, "prediction": prediction} |