Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,7 +1,7 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
from transformers import pipeline
|
| 3 |
-
import os
|
| 4 |
from PIL import Image
|
|
|
|
| 5 |
|
| 6 |
# Global variable to store the pipeline
|
| 7 |
deepfake_pipe = None
|
|
@@ -24,6 +24,10 @@ def predict_deepfake(image):
|
|
| 24 |
# Load model if not already loaded
|
| 25 |
pipe = load_model()
|
| 26 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
# Make prediction
|
| 28 |
results = pipe(image)
|
| 29 |
|
|
@@ -57,37 +61,61 @@ def main():
|
|
| 57 |
st.title("π΅οΈ Jerry - Deepfake Detection Tool")
|
| 58 |
st.markdown("**Upload an image to check if it's real or AI-generated!**")
|
| 59 |
|
| 60 |
-
#
|
| 61 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
|
| 63 |
with col1:
|
| 64 |
st.subheader("Upload Image")
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
|
| 76 |
with col2:
|
| 77 |
st.subheader("Detection Results")
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
|
| 84 |
# Display results
|
| 85 |
st.write("**Prediction Scores:**")
|
| 86 |
-
for label, score in
|
| 87 |
st.write(f"{label}: {score:.2%}")
|
| 88 |
|
| 89 |
# Display verdict with colored text
|
| 90 |
-
st.markdown(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 91 |
|
| 92 |
# Additional information
|
| 93 |
st.markdown(
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
from transformers import pipeline
|
|
|
|
| 3 |
from PIL import Image
|
| 4 |
+
import os
|
| 5 |
|
| 6 |
# Global variable to store the pipeline
|
| 7 |
deepfake_pipe = None
|
|
|
|
| 24 |
# Load model if not already loaded
|
| 25 |
pipe = load_model()
|
| 26 |
|
| 27 |
+
# Ensure image is in PIL format
|
| 28 |
+
if not isinstance(image, Image.Image):
|
| 29 |
+
image = Image.open(image)
|
| 30 |
+
|
| 31 |
# Make prediction
|
| 32 |
results = pipe(image)
|
| 33 |
|
|
|
|
| 61 |
st.title("π΅οΈ Jerry - Deepfake Detection Tool")
|
| 62 |
st.markdown("**Upload an image to check if it's real or AI-generated!**")
|
| 63 |
|
| 64 |
+
# Use session state to store results and prevent unnecessary re-renders
|
| 65 |
+
if 'results' not in st.session_state:
|
| 66 |
+
st.session_state.results = None
|
| 67 |
+
st.session_state.verdict = None
|
| 68 |
+
st.session_state.color = None
|
| 69 |
+
|
| 70 |
+
# Create two columns with fixed widths
|
| 71 |
+
col1, col2 = st.columns([1, 1], gap="medium")
|
| 72 |
|
| 73 |
with col1:
|
| 74 |
st.subheader("Upload Image")
|
| 75 |
+
# Fixed-height container for image to prevent layout shifts
|
| 76 |
+
with st.container(height=400, border=True):
|
| 77 |
+
uploaded_file = st.file_uploader(
|
| 78 |
+
"Choose an image",
|
| 79 |
+
type=['png', 'jpg', 'jpeg'],
|
| 80 |
+
label_visibility="collapsed",
|
| 81 |
+
key="file_uploader"
|
| 82 |
+
)
|
| 83 |
+
|
| 84 |
+
if uploaded_file is not None:
|
| 85 |
+
# Display uploaded image with fixed dimensions
|
| 86 |
+
image = Image.open(uploaded_file)
|
| 87 |
+
st.image(
|
| 88 |
+
image,
|
| 89 |
+
caption="Uploaded Image",
|
| 90 |
+
use_container_width=True,
|
| 91 |
+
clamp=True
|
| 92 |
+
)
|
| 93 |
|
| 94 |
with col2:
|
| 95 |
st.subheader("Detection Results")
|
| 96 |
+
# Fixed-height container for results
|
| 97 |
+
with st.container(height=400, border=True):
|
| 98 |
+
if uploaded_file is not None:
|
| 99 |
+
if st.button("π Analyze Image", type="primary"):
|
| 100 |
+
with st.spinner("Analyzing image..."):
|
| 101 |
+
# Perform prediction and store in session state
|
| 102 |
+
prediction, verdict, color = predict_deepfake(uploaded_file)
|
| 103 |
+
st.session_state.results = prediction
|
| 104 |
+
st.session_state.verdict = verdict
|
| 105 |
+
st.session_state.color = color
|
| 106 |
|
| 107 |
# Display results
|
| 108 |
st.write("**Prediction Scores:**")
|
| 109 |
+
for label, score in st.session_state.results.items():
|
| 110 |
st.write(f"{label}: {score:.2%}")
|
| 111 |
|
| 112 |
# Display verdict with colored text
|
| 113 |
+
st.markdown(
|
| 114 |
+
f"<p style='color:{st.session_state.color};font-size:18px;'><b>{st.session_state.verdict}</b></p>",
|
| 115 |
+
unsafe_allow_html=True
|
| 116 |
+
)
|
| 117 |
+
else:
|
| 118 |
+
st.write("Please upload an image to analyze.")
|
| 119 |
|
| 120 |
# Additional information
|
| 121 |
st.markdown(
|