Spaces:
Sleeping
Sleeping
Add application file
Browse files- Dockerfile +18 -0
- app.py +96 -0
- requirements.txt +0 -0
Dockerfile
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Use an official lightweight Python image
|
| 2 |
+
FROM python:3.10
|
| 3 |
+
|
| 4 |
+
# Set the working directory in the container
|
| 5 |
+
WORKDIR /app
|
| 6 |
+
|
| 7 |
+
# Copy required files
|
| 8 |
+
COPY requirements.txt .
|
| 9 |
+
COPY app.py .
|
| 10 |
+
|
| 11 |
+
# Install dependencies
|
| 12 |
+
RUN pip install --no-cache-dir -r requirements.txt
|
| 13 |
+
|
| 14 |
+
# Expose the API port
|
| 15 |
+
EXPOSE 7860
|
| 16 |
+
|
| 17 |
+
# Run FastAPI application
|
| 18 |
+
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
|
app.py
ADDED
|
@@ -0,0 +1,96 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
import torch.nn as nn
|
| 3 |
+
from torchvision import transforms
|
| 4 |
+
from PIL import Image
|
| 5 |
+
import cv2
|
| 6 |
+
import timm
|
| 7 |
+
from fastapi import FastAPI, File, UploadFile
|
| 8 |
+
import shutil
|
| 9 |
+
import os
|
| 10 |
+
from huggingface_hub import hf_hub_download
|
| 11 |
+
|
| 12 |
+
# FastAPI app instance
|
| 13 |
+
app = FastAPI()
|
| 14 |
+
|
| 15 |
+
# Device configuration
|
| 16 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 17 |
+
print("Initializing model...")
|
| 18 |
+
|
| 19 |
+
# Define image transformations
|
| 20 |
+
transform = transforms.Compose([
|
| 21 |
+
transforms.Resize((224, 224)),
|
| 22 |
+
transforms.ToTensor(),
|
| 23 |
+
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
|
| 24 |
+
])
|
| 25 |
+
|
| 26 |
+
# Hugging Face model repository details
|
| 27 |
+
HF_MODEL_REPO = "Maddy21/vit-deepfake-model" # Replace with your repo
|
| 28 |
+
HF_MODEL_FILE = "best_vit_model.pth"
|
| 29 |
+
|
| 30 |
+
# Download the model from Hugging Face
|
| 31 |
+
model_path = hf_hub_download(repo_id=HF_MODEL_REPO, filename=HF_MODEL_FILE)
|
| 32 |
+
|
| 33 |
+
# Load the trained ViT model
|
| 34 |
+
model = timm.create_model('vit_large_patch16_224', pretrained=False, num_classes=2)
|
| 35 |
+
model.load_state_dict(torch.load(model_path, map_location=device))
|
| 36 |
+
model.to(device)
|
| 37 |
+
model.eval()
|
| 38 |
+
print("Model loaded successfully.")
|
| 39 |
+
|
| 40 |
+
# Function to process video and classify frames
|
| 41 |
+
def predict_video(video_path):
|
| 42 |
+
print("Processing video for deepfake detection...")
|
| 43 |
+
cap = cv2.VideoCapture(video_path)
|
| 44 |
+
frame_count = 0
|
| 45 |
+
real_count = 0
|
| 46 |
+
manipulated_count = 0
|
| 47 |
+
|
| 48 |
+
while cap.isOpened():
|
| 49 |
+
ret, frame = cap.read()
|
| 50 |
+
if not ret:
|
| 51 |
+
break
|
| 52 |
+
|
| 53 |
+
frame_count += 1
|
| 54 |
+
image = Image.fromarray(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))
|
| 55 |
+
image = transform(image).unsqueeze(0).to(device)
|
| 56 |
+
|
| 57 |
+
with torch.no_grad():
|
| 58 |
+
outputs = model(image)
|
| 59 |
+
_, predicted = torch.max(outputs, 1)
|
| 60 |
+
|
| 61 |
+
if predicted.item() == 0:
|
| 62 |
+
real_count += 1
|
| 63 |
+
else:
|
| 64 |
+
manipulated_count += 1
|
| 65 |
+
|
| 66 |
+
cap.release()
|
| 67 |
+
|
| 68 |
+
result = "Real" if real_count > manipulated_count else "Manipulated"
|
| 69 |
+
return {
|
| 70 |
+
"total_frames": frame_count,
|
| 71 |
+
"real_frames": real_count,
|
| 72 |
+
"manipulated_frames": manipulated_count,
|
| 73 |
+
"result": result
|
| 74 |
+
}
|
| 75 |
+
|
| 76 |
+
# API Endpoint to check if API is running
|
| 77 |
+
@app.get("/")
|
| 78 |
+
def read_root():
|
| 79 |
+
return {"message": "Deepfake Detection API is running!"}
|
| 80 |
+
|
| 81 |
+
# API Endpoint to upload a video and get predictions
|
| 82 |
+
@app.post("/predict/")
|
| 83 |
+
async def predict(file: UploadFile = File(...)):
|
| 84 |
+
file_path = f"temp_{file.filename}"
|
| 85 |
+
|
| 86 |
+
# Save uploaded video
|
| 87 |
+
with open(file_path, "wb") as buffer:
|
| 88 |
+
shutil.copyfileobj(file.file, buffer)
|
| 89 |
+
|
| 90 |
+
# Run prediction
|
| 91 |
+
result = predict_video(file_path)
|
| 92 |
+
|
| 93 |
+
# Delete temp file after processing
|
| 94 |
+
os.remove(file_path)
|
| 95 |
+
return result
|
| 96 |
+
|
requirements.txt
ADDED
|
Binary file (1.51 kB). View file
|
|
|