Shakeel401 commited on
Commit
2d9b55b
Β·
verified Β·
1 Parent(s): 3662b3d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -19
app.py CHANGED
@@ -8,23 +8,20 @@ import traceback
8
  from huggingface_hub import hf_hub_download
9
 
10
  # =======================================
11
- # Step 1: Download model from Hugging Face Hub
12
  # =======================================
13
- keras_model = None
14
  interpreter, input_details, output_details = None, None, None
15
- use_tflite = False
16
 
17
  try:
18
- # If you uploaded TFLite, use that:
19
- # model_path = hf_hub_download("Shakeel401/Deepfake-Detector", "deepfake_model_select_ops.tflite")
20
 
21
- # But you uploaded .keras file:
22
- model_path = hf_hub_download("Shakeel401/Deepfake-Detector", "deepfake_model.keras")
23
-
24
- keras_model = tf.keras.models.load_model(model_path, compile=False)
25
- print("βœ… Loaded Keras model from HF Hub")
26
  except Exception as e:
27
- print("❌ Failed to load model from HF Hub:", e)
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
- if keras_model is not None:
66
- return float(keras_model.predict(processed, verbose=0)[0][0])
67
- else:
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 keras_model is None:
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 (Keras from Hugging Face Hub)"
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(