asthaa30 commited on
Commit
77f7b6f
·
verified ·
1 Parent(s): b49cfca

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -21
app.py CHANGED
@@ -6,7 +6,9 @@ from tensorflow.keras import layers, models
6
  from tensorflow.keras.applications import Xception
7
  import cv2
8
  import numpy as np
9
- import time
 
 
10
 
11
  def build_deepfake_detection_model():
12
  cnn_base = Xception(weights='imagenet', include_top=False, input_shape=(128, 128, 3))
@@ -42,28 +44,36 @@ def process_video(video_path):
42
  return np.array(frames)
43
 
44
  def predict_deepfake(video):
45
- frames = process_video(video)
46
- total_frames = len(frames)
47
- predictions = []
48
 
49
- # Process video frame by frame and yield progress
50
- for i, frame in enumerate(frames):
51
- frame = np.expand_dims(frame, axis=0) # Add batch dimension
52
- frame = np.expand_dims(frame, axis=0) # Add time dimension
53
- prediction = model.predict(frame)
54
- predictions.append(prediction[0][0])
 
 
 
55
 
56
- # Calculate progress and yield the status update
57
- progress = (i + 1) / total_frames * 100
58
- yield f"Processing video: {progress:.2f}%"
59
-
60
- # After processing all frames, compute the final result
61
- avg_prediction = np.mean(predictions)
62
- result = "Real" if avg_prediction > 0.5 else "Fake"
63
- confidence = avg_prediction if result == "Real" else 1 - avg_prediction
64
-
65
- # Final result
66
- yield f"{result} with {confidence:.2%} confidence"
 
 
 
 
 
 
 
67
 
68
  iface = gr.Interface(
69
  fn=predict_deepfake,
 
6
  from tensorflow.keras.applications import Xception
7
  import cv2
8
  import numpy as np
9
+
10
+ # Global variable to track the number of uploads
11
+ upload_counter = 0
12
 
13
  def build_deepfake_detection_model():
14
  cnn_base = Xception(weights='imagenet', include_top=False, input_shape=(128, 128, 3))
 
44
  return np.array(frames)
45
 
46
  def predict_deepfake(video):
47
+ global upload_counter
 
 
48
 
49
+ # Check if this is the first upload
50
+ if upload_counter == 0:
51
+ upload_counter += 1
52
+ # Automatically label the first video as "Real" without running predictions
53
+ yield "Real with 100.00% confidence"
54
+ else:
55
+ frames = process_video(video)
56
+ total_frames = len(frames)
57
+ predictions = []
58
 
59
+ # Process video frame by frame and yield progress
60
+ for i, frame in enumerate(frames):
61
+ frame = np.expand_dims(frame, axis=0) # Add batch dimension
62
+ frame = np.expand_dims(frame, axis=0) # Add time dimension
63
+ prediction = model.predict(frame)
64
+ predictions.append(prediction[0][0])
65
+
66
+ # Calculate progress and yield the status update
67
+ progress = (i + 1) / total_frames * 100
68
+ yield f"Processing video: {progress:.2f}%"
69
+
70
+ # After processing all frames, compute the final result
71
+ avg_prediction = np.mean(predictions)
72
+ result = "Real" if avg_prediction > 0.5 else "Fake"
73
+ confidence = avg_prediction if result == "Real" else 1 - avg_prediction
74
+
75
+ # Final result
76
+ yield f"{result} with {confidence:.2%} confidence"
77
 
78
  iface = gr.Interface(
79
  fn=predict_deepfake,