Shiwanni commited on
Commit
e72bd1c
·
verified ·
1 Parent(s): 229a7a4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -27
app.py CHANGED
@@ -1,32 +1,33 @@
1
- from flask import Flask, render_template, request, jsonify
2
- from detect import DeepfakeDetector
3
- import os
 
4
 
5
- app = Flask(__name__)
6
- detector = DeepfakeDetector()
 
 
7
 
8
- @app.route("/")
9
- def home():
10
- return render_template("index.html")
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
- temp_path = f"temp_{file.filename}"
22
- file.save(temp_path)
 
 
23
 
24
- try:
25
- result = detector.detect_from_file(temp_path)
26
- return jsonify({"result": result})
27
- finally:
28
- if os.path.exists(temp_path):
29
- os.remove(temp_path)
 
 
 
 
 
 
30
 
31
- if __name__ == "__main__":
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()