Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,32 +1,33 @@
|
|
1 |
-
from
|
2 |
-
|
3 |
-
import
|
|
|
4 |
|
5 |
-
|
6 |
-
|
|
|
|
|
7 |
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
@app.route("/detect", methods=["POST"])
|
13 |
-
def detect():
|
14 |
-
if "file" not in request.files:
|
15 |
-
return jsonify({"error": "No file uploaded"}), 400
|
16 |
-
|
17 |
-
file = request.files["file"]
|
18 |
-
if file.filename == "":
|
19 |
-
return jsonify({"error": "Empty filename"}), 400
|
20 |
|
21 |
-
|
22 |
-
|
|
|
|
|
23 |
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
30 |
|
31 |
-
|
32 |
-
app.run(debug=True)
|
|
|
1 |
+
from transformers import ViTForImageClassification, ViTImageProcessor
|
2 |
+
import torch
|
3 |
+
from PIL import Image
|
4 |
+
import gradio as gr
|
5 |
|
6 |
+
# Load pre-trained model and processor
|
7 |
+
model_name = "facebook/deit-base-distilled-patch16-224"
|
8 |
+
processor = ViTImageProcessor.from_pretrained(model_name)
|
9 |
+
model = ViTForImageClassification.from_pretrained(model_name)
|
10 |
|
11 |
+
def detect_deepfake(image):
|
12 |
+
# Preprocess the image
|
13 |
+
inputs = processor(images=image, return_tensors="pt")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
|
15 |
+
# Make prediction
|
16 |
+
outputs = model(**inputs)
|
17 |
+
logits = outputs.logits
|
18 |
+
predicted_class_idx = logits.argmax(-1).item()
|
19 |
|
20 |
+
# For demonstration, we'll assume class 0 is real and 1 is fake
|
21 |
+
# (In a real project, you'd need to verify this with your model)
|
22 |
+
return "Real" if predicted_class_idx == 0 else "Fake (Possible Deepfake)"
|
23 |
+
|
24 |
+
# Create a simple interface
|
25 |
+
iface = gr.Interface(
|
26 |
+
fn=detect_deepfake,
|
27 |
+
inputs=gr.Image(type="pil"),
|
28 |
+
outputs="text",
|
29 |
+
title="Deepfake Detection",
|
30 |
+
description="Upload an image to check if it might be a deepfake."
|
31 |
+
)
|
32 |
|
33 |
+
iface.launch()
|
|