Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,52 +4,68 @@ from huggingface_hub import hf_hub_download
|
|
| 4 |
from safetensors.torch import load_file
|
| 5 |
import gradio as gr
|
| 6 |
|
| 7 |
-
|
| 8 |
-
from
|
|
|
|
| 9 |
|
| 10 |
-
#
|
| 11 |
os.environ["CUDA_VISIBLE_DEVICES"] = ""
|
| 12 |
device = "cpu"
|
| 13 |
|
| 14 |
-
#
|
| 15 |
CHECKPOINT_PATH = hf_hub_download(
|
| 16 |
repo_id="faisalishfaq2005/deepfake-detection-efficientnet-vit",
|
| 17 |
filename="model.safetensors"
|
| 18 |
)
|
| 19 |
|
| 20 |
-
# --- Load model weights safely ---
|
| 21 |
state_dict = load_file(CHECKPOINT_PATH, device="cpu")
|
| 22 |
model = ImprovedEfficientViT()
|
| 23 |
model.load_state_dict(state_dict)
|
| 24 |
model.eval()
|
| 25 |
model.to(device)
|
| 26 |
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
try:
|
| 33 |
result = predict_vedio(video_path, model)
|
| 34 |
-
verdict = "Deepfake" if result
|
| 35 |
-
|
|
|
|
| 36 |
except Exception as e:
|
| 37 |
-
|
| 38 |
-
output_video = None
|
| 39 |
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
# --- Gradio interface ---
|
| 43 |
with gr.Blocks() as app:
|
| 44 |
gr.Markdown("# 🔍 Deepfake Detector (EfficientViT, CPU-only)")
|
| 45 |
-
|
| 46 |
with gr.Row():
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
|
| 51 |
-
|
| 52 |
-
|
| 53 |
|
| 54 |
if __name__ == "__main__":
|
| 55 |
-
app.launch()
|
|
|
|
| 4 |
from safetensors.torch import load_file
|
| 5 |
import gradio as gr
|
| 6 |
|
| 7 |
+
# import your custom model/inference (ensure model.py and inference.py are present)
|
| 8 |
+
from model import ImprovedEfficientViT
|
| 9 |
+
from inference import predict_vedio
|
| 10 |
|
| 11 |
+
# Force CPU-only (avoid CUDA/tensorflow GPU init errors on Spaces)
|
| 12 |
os.environ["CUDA_VISIBLE_DEVICES"] = ""
|
| 13 |
device = "cpu"
|
| 14 |
|
| 15 |
+
# Download + load checkpoint (this will cache on the Spaces runner)
|
| 16 |
CHECKPOINT_PATH = hf_hub_download(
|
| 17 |
repo_id="faisalishfaq2005/deepfake-detection-efficientnet-vit",
|
| 18 |
filename="model.safetensors"
|
| 19 |
)
|
| 20 |
|
|
|
|
| 21 |
state_dict = load_file(CHECKPOINT_PATH, device="cpu")
|
| 22 |
model = ImprovedEfficientViT()
|
| 23 |
model.load_state_dict(state_dict)
|
| 24 |
model.eval()
|
| 25 |
model.to(device)
|
| 26 |
|
| 27 |
+
def run_inference(uploaded_file):
|
| 28 |
+
"""
|
| 29 |
+
uploaded_file: this will be a filesystem path (string) returned by gr.File
|
| 30 |
+
(This avoids file-like objects and tempfile issues across Gradio versions.)
|
| 31 |
+
"""
|
| 32 |
+
# uploaded_file can be None or a path string
|
| 33 |
+
if not uploaded_file:
|
| 34 |
+
return "Error: no file uploaded.", None
|
| 35 |
+
|
| 36 |
+
# On some Gradio versions uploaded_file is a dict with 'name' or 'tempfile' keys,
|
| 37 |
+
# but typically it's a string path. Handle common cases defensively:
|
| 38 |
+
video_path = None
|
| 39 |
+
if isinstance(uploaded_file, str):
|
| 40 |
+
video_path = uploaded_file
|
| 41 |
+
elif isinstance(uploaded_file, dict):
|
| 42 |
+
# Examples of possible dict shapes; try common keys:
|
| 43 |
+
video_path = uploaded_file.get("name") or uploaded_file.get("filename") or uploaded_file.get("tempfile")
|
| 44 |
+
else:
|
| 45 |
+
# fallback: try str()
|
| 46 |
+
video_path = str(uploaded_file)
|
| 47 |
+
|
| 48 |
+
if not video_path or not os.path.exists(video_path):
|
| 49 |
+
return f"Error: uploaded video not found at path: {video_path}", None
|
| 50 |
|
| 51 |
try:
|
| 52 |
result = predict_vedio(video_path, model)
|
| 53 |
+
verdict = "Deepfake" if int(result.get("class", 0)) == 1 else "Real"
|
| 54 |
+
# Return the verdict string and the path so gr.Video output can preview it
|
| 55 |
+
return f"Verdict: {verdict}", video_path
|
| 56 |
except Exception as e:
|
| 57 |
+
return f"Error during prediction: {e}", None
|
|
|
|
| 58 |
|
| 59 |
+
# Build UI (gr.File for upload; gr.Video for preview)
|
|
|
|
|
|
|
| 60 |
with gr.Blocks() as app:
|
| 61 |
gr.Markdown("# 🔍 Deepfake Detector (EfficientViT, CPU-only)")
|
|
|
|
| 62 |
with gr.Row():
|
| 63 |
+
upload = gr.File(label="Upload Video (mp4, mov, avi)", file_types=["video"])
|
| 64 |
+
out_text = gr.Textbox(label="Prediction")
|
| 65 |
+
out_video = gr.Video(label="Uploaded Video Preview")
|
| 66 |
|
| 67 |
+
analyze = gr.Button("Analyze")
|
| 68 |
+
analyze.click(fn=run_inference, inputs=[upload], outputs=[out_text, out_video])
|
| 69 |
|
| 70 |
if __name__ == "__main__":
|
| 71 |
+
app.launch()
|