Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -8,23 +8,20 @@ import traceback
|
|
| 8 |
from huggingface_hub import hf_hub_download
|
| 9 |
|
| 10 |
# =======================================
|
| 11 |
-
# Step 1:
|
| 12 |
# =======================================
|
| 13 |
-
keras_model = None
|
| 14 |
interpreter, input_details, output_details = None, None, None
|
| 15 |
-
use_tflite = False
|
| 16 |
|
| 17 |
try:
|
| 18 |
-
|
| 19 |
-
# model_path = hf_hub_download("Shakeel401/Deepfake-Detector", "deepfake_model_select_ops.tflite")
|
| 20 |
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
print("β
Loaded
|
| 26 |
except Exception as e:
|
| 27 |
-
print("β Failed to load
|
| 28 |
|
| 29 |
# =======================================
|
| 30 |
# Step 2: OpenCV face detector
|
|
@@ -58,14 +55,13 @@ def preprocess_frames(frames, target_size=(299, 299)):
|
|
| 58 |
return processed
|
| 59 |
|
| 60 |
# =======================================
|
| 61 |
-
# Step 4: Inference wrapper
|
| 62 |
# =======================================
|
| 63 |
def run_inference(processed):
|
| 64 |
try:
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
return 0.5 # fallback neutral
|
| 69 |
except Exception as e:
|
| 70 |
print("β Inference failed:", e)
|
| 71 |
return 0.5
|
|
@@ -76,8 +72,8 @@ def run_inference(processed):
|
|
| 76 |
MAX_VIDEO_SIZE_MB = 50
|
| 77 |
|
| 78 |
def predict_video_overlay(video_file):
|
| 79 |
-
if
|
| 80 |
-
return "β No model available"
|
| 81 |
|
| 82 |
try:
|
| 83 |
file_size_mb = os.path.getsize(video_file)/(1024*1024)
|
|
@@ -136,7 +132,7 @@ def predict_video_overlay(video_file):
|
|
| 136 |
# =======================================
|
| 137 |
# Step 6: Gradio App
|
| 138 |
# =======================================
|
| 139 |
-
title = "π¬ Deepfake Detection (
|
| 140 |
description = f"Upload MP4 (Max {MAX_VIDEO_SIZE_MB} MB). Detects Real/Fake and overlays predictions."
|
| 141 |
|
| 142 |
gr.Interface(
|
|
|
|
| 8 |
from huggingface_hub import hf_hub_download
|
| 9 |
|
| 10 |
# =======================================
|
| 11 |
+
# Step 1: Load TFLite model from Hugging Face Hub
|
| 12 |
# =======================================
|
|
|
|
| 13 |
interpreter, input_details, output_details = None, None, None
|
|
|
|
| 14 |
|
| 15 |
try:
|
| 16 |
+
model_path = hf_hub_download("Shakeel401/Deepfake-Detector", "deepfake_model_select_ops.tflite")
|
|
|
|
| 17 |
|
| 18 |
+
interpreter = tf.lite.Interpreter(model_path=model_path)
|
| 19 |
+
interpreter.allocate_tensors()
|
| 20 |
+
input_details = interpreter.get_input_details()
|
| 21 |
+
output_details = interpreter.get_output_details()
|
| 22 |
+
print("β
Loaded TFLite model from HF Hub")
|
| 23 |
except Exception as e:
|
| 24 |
+
print("β Failed to load TFLite model:", e)
|
| 25 |
|
| 26 |
# =======================================
|
| 27 |
# Step 2: OpenCV face detector
|
|
|
|
| 55 |
return processed
|
| 56 |
|
| 57 |
# =======================================
|
| 58 |
+
# Step 4: Inference wrapper (TFLite only)
|
| 59 |
# =======================================
|
| 60 |
def run_inference(processed):
|
| 61 |
try:
|
| 62 |
+
interpreter.set_tensor(input_details[0]['index'], processed)
|
| 63 |
+
interpreter.invoke()
|
| 64 |
+
return float(interpreter.get_tensor(output_details[0]['index'])[0][0])
|
|
|
|
| 65 |
except Exception as e:
|
| 66 |
print("β Inference failed:", e)
|
| 67 |
return 0.5
|
|
|
|
| 72 |
MAX_VIDEO_SIZE_MB = 50
|
| 73 |
|
| 74 |
def predict_video_overlay(video_file):
|
| 75 |
+
if interpreter is None:
|
| 76 |
+
return "β No TFLite model available"
|
| 77 |
|
| 78 |
try:
|
| 79 |
file_size_mb = os.path.getsize(video_file)/(1024*1024)
|
|
|
|
| 132 |
# =======================================
|
| 133 |
# Step 6: Gradio App
|
| 134 |
# =======================================
|
| 135 |
+
title = "π¬ Deepfake Detection (TFLite from Hugging Face Hub)"
|
| 136 |
description = f"Upload MP4 (Max {MAX_VIDEO_SIZE_MB} MB). Detects Real/Fake and overlays predictions."
|
| 137 |
|
| 138 |
gr.Interface(
|