dhairyashah commited on
Commit
9ec11c2
·
verified ·
1 Parent(s): 90b8b39

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -12
app.py CHANGED
@@ -1,4 +1,4 @@
1
- from flask import Flask, request, jsonify, Response
2
  import os
3
  from werkzeug.utils import secure_filename
4
  import cv2
@@ -6,10 +6,11 @@ import torch
6
  import torch.nn.functional as F
7
  from facenet_pytorch import MTCNN, InceptionResnetV1
8
  import numpy as np
9
- import json
10
  import time
11
 
12
  app = Flask(__name__)
 
13
 
14
  # Configuration
15
  UPLOAD_FOLDER = 'uploads'
@@ -52,7 +53,7 @@ def process_frame(frame):
52
 
53
  return prediction, output.item()
54
 
55
- def event_stream(video_path, sample_rate=30):
56
  cap = cv2.VideoCapture(video_path)
57
  frame_count = 0
58
  fake_count = 0
@@ -73,9 +74,9 @@ def event_stream(video_path, sample_rate=30):
73
  if prediction == "fake":
74
  fake_count += 1
75
 
76
- # Send progress update
77
  progress = (frame_count / total_frames) * 100
78
- yield f"data: {json.dumps({'progress': progress})}\n\n"
79
 
80
  frame_count += 1
81
 
@@ -83,10 +84,9 @@ def event_stream(video_path, sample_rate=30):
83
 
84
  if total_processed > 0:
85
  fake_percentage = (fake_count / total_processed) * 100
 
86
  else:
87
- fake_percentage = 0
88
-
89
- yield f"data: {json.dumps({'fake_percentage': round(fake_percentage, 2), 'is_likely_deepfake': fake_percentage >= 60})}\n\n"
90
 
91
  @app.route('/analyze', methods=['POST'])
92
  def analyze_video_api():
@@ -101,14 +101,36 @@ def analyze_video_api():
101
  if file and allowed_file(file.filename):
102
  filename = secure_filename(file.filename)
103
  filepath = os.path.join(app.config['UPLOAD_FOLDER'], filename)
104
- file.save(filepath)
 
 
 
 
 
 
 
 
 
 
 
 
 
105
 
106
  try:
107
- return Response(event_stream(filepath), content_type='text/event-stream')
108
- finally:
109
  os.remove(filepath) # Remove the file after analysis
 
 
 
 
 
 
 
 
 
 
110
  else:
111
  return jsonify({'error': 'Invalid file type'}), 400
112
 
113
  if __name__ == '__main__':
114
- app.run(host='0.0.0.0', port=7860)
 
1
+ from flask import Flask, request, jsonify
2
  import os
3
  from werkzeug.utils import secure_filename
4
  import cv2
 
6
  import torch.nn.functional as F
7
  from facenet_pytorch import MTCNN, InceptionResnetV1
8
  import numpy as np
9
+ from flask_socketio import SocketIO, emit
10
  import time
11
 
12
  app = Flask(__name__)
13
+ socketio = SocketIO(app, cors_allowed_origins="*")
14
 
15
  # Configuration
16
  UPLOAD_FOLDER = 'uploads'
 
53
 
54
  return prediction, output.item()
55
 
56
+ def event_sdef analyze_video(video_path, sample_rate=30):
57
  cap = cv2.VideoCapture(video_path)
58
  frame_count = 0
59
  fake_count = 0
 
74
  if prediction == "fake":
75
  fake_count += 1
76
 
77
+ # Emit progress update
78
  progress = (frame_count / total_frames) * 100
79
+ socketio.emit('analysis_progress', {'progress': progress})
80
 
81
  frame_count += 1
82
 
 
84
 
85
  if total_processed > 0:
86
  fake_percentage = (fake_count / total_processed) * 100
87
+ return fake_percentage
88
  else:
89
+ return 0
 
 
90
 
91
  @app.route('/analyze', methods=['POST'])
92
  def analyze_video_api():
 
101
  if file and allowed_file(file.filename):
102
  filename = secure_filename(file.filename)
103
  filepath = os.path.join(app.config['UPLOAD_FOLDER'], filename)
104
+
105
+ # Save file and emit upload progress
106
+ chunk_size = 4096
107
+ file_size = int(request.headers.get('Content-Length', 0))
108
+ bytes_read = 0
109
+ with open(filepath, 'wb') as f:
110
+ while True:
111
+ chunk = file.read(chunk_size)
112
+ if not chunk:
113
+ break
114
+ f.write(chunk)
115
+ bytes_read += len(chunk)
116
+ progress = (bytes_read / file_size) * 100
117
+ socketio.emit('upload_progress', {'progress': progress})
118
 
119
  try:
120
+ fake_percentage = analyze_video(filepath)
 
121
  os.remove(filepath) # Remove the file after analysis
122
+
123
+ result = {
124
+ 'fake_percentage': round(fake_percentage, 2),
125
+ 'is_likely_deepfake': fake_percentage >= 60
126
+ }
127
+
128
+ return jsonify(result), 200
129
+ except Exception as e:
130
+ os.remove(filepath) # Remove the file if an error occurs
131
+ return jsonify({'error': str(e)}), 500
132
  else:
133
  return jsonify({'error': 'Invalid file type'}), 400
134
 
135
  if __name__ == '__main__':
136
+ socketio.run(app, host='0.0.0.0', port=7860)