SanskarModi commited on
Commit
0cb9695
·
verified ·
1 Parent(s): e5efe9b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +44 -40
app.py CHANGED
@@ -1,50 +1,54 @@
1
- import gradio as gr
2
- import tempfile
3
  import cv2
 
 
 
4
  from prediction import Prediction
 
5
 
6
- # Initialize the predictor
7
- predictor = Prediction()
8
 
9
- # Define your inference function
10
- def detect_deepfake(video_file, sequence_length=None):
11
- try:
12
- # Save the uploaded video to a temporary file
13
- with tempfile.NamedTemporaryFile(delete=False, suffix=".mp4") as temp_video:
14
- temp_video.write(video_file.read())
15
- temp_video_path = temp_video.name
16
 
17
- # Run prediction
18
- prediction_str, explanation_image, details = predictor.predict(
19
- temp_video_path, sequence_length
20
- )
21
 
22
- # Return prediction and image
23
- explanation_img = None
24
- if explanation_image is not None:
25
- explanation_img = cv2.cvtColor(explanation_image, cv2.COLOR_BGR2RGB)
 
26
 
27
- return prediction_str, explanation_img, str(details)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
28
 
29
  except Exception as e:
30
- return f"Error: {str(e)}", None, ""
31
-
32
- # Gradio UI
33
- demo = gr.Interface(
34
- fn=detect_deepfake,
35
- inputs=[
36
- gr.File(label="Upload Video (.mp4)", file_types=[".mp4"]),
37
- gr.Number(label="Sequence Length (Optional)", value=None),
38
- ],
39
- outputs=[
40
- gr.Textbox(label="Prediction"),
41
- gr.Image(label="Explanation Image"),
42
- gr.Textbox(label="Details"),
43
- ],
44
- title="Deepdetect",
45
- description="Upload a video to detect deepfakes using Face2Face, FaceSwap, FaceShifter, and NeuralTextures models.",
46
- allow_flagging="never",
47
- )
48
 
49
- if __name__ == "__main__":
50
- demo.launch()
 
 
1
+ import streamlit as st
 
2
  import cv2
3
+ import os
4
+ import tempfile
5
+ import numpy as np
6
  from prediction import Prediction
7
+ from PIL import Image
8
 
9
+ st.set_page_config(page_title="Deepfake Detection", layout="wide")
10
+ st.title("🔍 Deepfake Video Detector with Grad-CAM Overlay")
11
 
12
+ st.markdown(
13
+ "Upload a video file below to analyze it for deepfakes. The model will detect faces, "
14
+ "analyze the content, and display the result with a Grad-CAM overlay for interpretability."
15
+ )
 
 
 
16
 
17
+ # File uploader
18
+ uploaded_file = st.file_uploader("📤 Upload a video file (e.g., MP4, AVI)", type=["mp4", "avi", "mov"])
 
 
19
 
20
+ # Prediction button
21
+ if uploaded_file is not None:
22
+ with tempfile.NamedTemporaryFile(delete=False, suffix=".mp4") as tmp_file:
23
+ tmp_file.write(uploaded_file.read())
24
+ tmp_file_path = tmp_file.name
25
 
26
+ st.info("⏳ Processing video... Please wait.")
27
+
28
+ try:
29
+ # Initialize prediction class
30
+ predictor = Prediction()
31
+
32
+ # Make prediction
33
+ prediction_result, gradcam_image, classification_details = predictor.predict(tmp_file_path)
34
+
35
+ # Display prediction
36
+ st.subheader("🧠 Prediction Result")
37
+ st.success(prediction_result)
38
+
39
+ # Display detailed classification info
40
+ if classification_details:
41
+ st.subheader("📊 Classification Details")
42
+ st.json(classification_details)
43
+
44
+ # Display Grad-CAM visualization
45
+ if gradcam_image is not None:
46
+ st.subheader("🔥 Grad-CAM Visualization (Middle Frame)")
47
+ st.image(gradcam_image, channels="BGR", caption="Grad-CAM Overlay")
48
 
49
  except Exception as e:
50
+ st.error(f"❌ An error occurred during prediction: {e}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
51
 
52
+ finally:
53
+ # Clean up temp file
54
+ os.remove(tmp_file_path)