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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -49
app.py CHANGED
@@ -1,54 +1,44 @@
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)
 
1
+ import gradio as gr
 
 
 
2
  import numpy as np
3
+ import cv2
4
  from prediction import Prediction
 
 
 
 
5
 
6
+ # Initialize the Prediction class
7
+ predictor = Prediction()
8
+
9
+ def inference(video):
10
+ """
11
+ Gradio-compatible inference function.
12
+
13
+ Args:
14
+ video (str): Path to the uploaded video file
15
+
16
+ Returns:
17
+ tuple: (Prediction string, Grad-CAM image, Classification details)
18
+ """
19
+ prediction, gradcam_image, classification_details = predictor.predict(video)
20
+
21
+ # Convert Grad-CAM image to RGB for display in Gradio
22
+ if gradcam_image is not None:
23
+ gradcam_image = cv2.cvtColor(gradcam_image, cv2.COLOR_BGR2RGB)
24
+ else:
25
+ gradcam_image = np.zeros((256, 256, 3), dtype=np.uint8) # fallback image
26
+
27
+ return prediction, gradcam_image, classification_details
28
+
29
+ # Define Gradio interface
30
+ demo = gr.Interface(
31
+ fn=inference,
32
+ inputs=gr.Video(label="Upload a video"),
33
+ outputs=[
34
+ gr.Textbox(label="Prediction"),
35
+ gr.Image(label="Grad-CAM Visualization"),
36
+ gr.Label(label="Detailed Classification"),
37
+ ],
38
+ title="Deepfake Detection with Grad-CAM",
39
+ description="Upload a video and detect whether it is real or a deepfake. Grad-CAM will highlight the most influential region.",
40
+ theme="default",
41
  )
42
 
43
+ if __name__ == "__main__":
44
+ demo.launch()