Update app.py
Browse files
app.py
CHANGED
@@ -7,17 +7,17 @@ import torch
|
|
7 |
import torch.nn.functional as F
|
8 |
from facenet_pytorch import MTCNN, InceptionResnetV1
|
9 |
import numpy as np
|
10 |
-
from
|
11 |
-
import
|
|
|
12 |
|
13 |
app = Flask(__name__)
|
14 |
-
socketio = SocketIO(app, cors_allowed_origins="*")
|
15 |
|
16 |
# Configuration
|
17 |
UPLOAD_FOLDER = 'uploads'
|
18 |
ALLOWED_EXTENSIONS = {'mp4', 'avi', 'mov'}
|
19 |
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
|
20 |
-
app.config['MAX_CONTENT_LENGTH'] = 16 * 1024 * 1024
|
21 |
|
22 |
os.makedirs(UPLOAD_FOLDER, exist_ok=True)
|
23 |
|
@@ -27,6 +27,7 @@ DEVICE = 'cuda:0' if torch.cuda.is_available() else 'cpu'
|
|
27 |
mtcnn = MTCNN(select_largest=False, post_process=False, device=DEVICE).to(DEVICE).eval()
|
28 |
|
29 |
model = InceptionResnetV1(pretrained="vggface2", classify=True, num_classes=1, device=DEVICE)
|
|
|
30 |
checkpoint = torch.load("resnetinceptionv1_epoch_32.pth", map_location=torch.device('cpu'))
|
31 |
model.load_state_dict(checkpoint['model_state_dict'])
|
32 |
model.to(DEVICE)
|
@@ -60,7 +61,6 @@ def analyze_video(video_path, sample_rate=30):
|
|
60 |
frame_count = 0
|
61 |
fake_count = 0
|
62 |
total_processed = 0
|
63 |
-
total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
|
64 |
|
65 |
while cap.isOpened():
|
66 |
ret, frame = cap.read()
|
@@ -76,10 +76,6 @@ def analyze_video(video_path, sample_rate=30):
|
|
76 |
if prediction == "fake":
|
77 |
fake_count += 1
|
78 |
|
79 |
-
# Emit progress update
|
80 |
-
progress = (frame_count / total_frames) * 100
|
81 |
-
socketio.emit('analysis_progress', {'progress': progress})
|
82 |
-
|
83 |
frame_count += 1
|
84 |
|
85 |
cap.release()
|
@@ -90,12 +86,7 @@ def analyze_video(video_path, sample_rate=30):
|
|
90 |
else:
|
91 |
return 0
|
92 |
|
93 |
-
@app.route('/', methods=['GET'])
|
94 |
-
def home():
|
95 |
-
return jsonify({'homepage': 'https://deepfake-checker.dhairyashah.dev'})
|
96 |
-
|
97 |
@app.route('/analyze', methods=['POST'])
|
98 |
-
@spaces.GPU
|
99 |
def analyze_video_api():
|
100 |
if 'video' not in request.files:
|
101 |
return jsonify({'error': 'No video file provided'}), 400
|
@@ -108,23 +99,9 @@ def analyze_video_api():
|
|
108 |
if file and allowed_file(file.filename):
|
109 |
filename = secure_filename(file.filename)
|
110 |
filepath = os.path.join(app.config['UPLOAD_FOLDER'], filename)
|
111 |
-
|
112 |
-
# Save file and emit upload progress
|
113 |
-
chunk_size = 4096
|
114 |
-
file_size = int(request.headers.get('Content-Length', 0))
|
115 |
-
bytes_read = 0
|
116 |
-
with open(filepath, 'wb') as f:
|
117 |
-
while True:
|
118 |
-
chunk = file.read(chunk_size)
|
119 |
-
if not chunk:
|
120 |
-
break
|
121 |
-
f.write(chunk)
|
122 |
-
bytes_read += len(chunk)
|
123 |
-
progress = (bytes_read / file_size) * 100
|
124 |
-
socketio.emit('upload_progress', {'progress': progress})
|
125 |
|
126 |
try:
|
127 |
-
socketio.emit('analysis_start', {'message': 'Starting video analysis'})
|
128 |
fake_percentage = analyze_video(filepath)
|
129 |
os.remove(filepath) # Remove the file after analysis
|
130 |
|
@@ -133,14 +110,12 @@ def analyze_video_api():
|
|
133 |
'is_likely_deepfake': fake_percentage >= 60
|
134 |
}
|
135 |
|
136 |
-
socketio.emit('analysis_complete', result)
|
137 |
return jsonify(result), 200
|
138 |
except Exception as e:
|
139 |
os.remove(filepath) # Remove the file if an error occurs
|
140 |
-
socketio.emit('analysis_error', {'error': str(e)})
|
141 |
return jsonify({'error': str(e)}), 500
|
142 |
else:
|
143 |
return jsonify({'error': 'Invalid file type'}), 400
|
144 |
|
145 |
if __name__ == '__main__':
|
146 |
-
|
|
|
7 |
import torch.nn.functional as F
|
8 |
from facenet_pytorch import MTCNN, InceptionResnetV1
|
9 |
import numpy as np
|
10 |
+
from pytorch_grad_cam import GradCAM
|
11 |
+
from pytorch_grad_cam.utils.model_targets import ClassifierOutputTarget
|
12 |
+
import os
|
13 |
|
14 |
app = Flask(__name__)
|
|
|
15 |
|
16 |
# Configuration
|
17 |
UPLOAD_FOLDER = 'uploads'
|
18 |
ALLOWED_EXTENSIONS = {'mp4', 'avi', 'mov'}
|
19 |
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
|
20 |
+
app.config['MAX_CONTENT_LENGTH'] = 16 * 1024 * 1024
|
21 |
|
22 |
os.makedirs(UPLOAD_FOLDER, exist_ok=True)
|
23 |
|
|
|
27 |
mtcnn = MTCNN(select_largest=False, post_process=False, device=DEVICE).to(DEVICE).eval()
|
28 |
|
29 |
model = InceptionResnetV1(pretrained="vggface2", classify=True, num_classes=1, device=DEVICE)
|
30 |
+
# Model Credits: https://huggingface.co/spaces/dhairyashah/deepfake-alpha-version/blob/main/CREDITS.md
|
31 |
checkpoint = torch.load("resnetinceptionv1_epoch_32.pth", map_location=torch.device('cpu'))
|
32 |
model.load_state_dict(checkpoint['model_state_dict'])
|
33 |
model.to(DEVICE)
|
|
|
61 |
frame_count = 0
|
62 |
fake_count = 0
|
63 |
total_processed = 0
|
|
|
64 |
|
65 |
while cap.isOpened():
|
66 |
ret, frame = cap.read()
|
|
|
76 |
if prediction == "fake":
|
77 |
fake_count += 1
|
78 |
|
|
|
|
|
|
|
|
|
79 |
frame_count += 1
|
80 |
|
81 |
cap.release()
|
|
|
86 |
else:
|
87 |
return 0
|
88 |
|
|
|
|
|
|
|
|
|
89 |
@app.route('/analyze', methods=['POST'])
|
|
|
90 |
def analyze_video_api():
|
91 |
if 'video' not in request.files:
|
92 |
return jsonify({'error': 'No video file provided'}), 400
|
|
|
99 |
if file and allowed_file(file.filename):
|
100 |
filename = secure_filename(file.filename)
|
101 |
filepath = os.path.join(app.config['UPLOAD_FOLDER'], filename)
|
102 |
+
file.save(filepath)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
103 |
|
104 |
try:
|
|
|
105 |
fake_percentage = analyze_video(filepath)
|
106 |
os.remove(filepath) # Remove the file after analysis
|
107 |
|
|
|
110 |
'is_likely_deepfake': fake_percentage >= 60
|
111 |
}
|
112 |
|
|
|
113 |
return jsonify(result), 200
|
114 |
except Exception as e:
|
115 |
os.remove(filepath) # Remove the file if an error occurs
|
|
|
116 |
return jsonify({'error': str(e)}), 500
|
117 |
else:
|
118 |
return jsonify({'error': 'Invalid file type'}), 400
|
119 |
|
120 |
if __name__ == '__main__':
|
121 |
+
app.run(host='0.0.0.0', port=7860)
|