Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,113 +1,28 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
-
from
|
| 3 |
import os
|
| 4 |
-
import
|
| 5 |
-
|
| 6 |
-
# Try multiple import approaches for transformers
|
| 7 |
-
try:
|
| 8 |
-
# For newer versions of transformers
|
| 9 |
-
from transformers import pipeline
|
| 10 |
-
TRANSFORMERS_AVAILABLE = True
|
| 11 |
-
print("β
Transformers imported successfully")
|
| 12 |
-
except ImportError as e:
|
| 13 |
-
st.error(f"β Transformers import error: {e}")
|
| 14 |
-
TRANSFORMERS_AVAILABLE = False
|
| 15 |
-
|
| 16 |
-
# Page configuration
|
| 17 |
-
st.set_page_config(
|
| 18 |
-
page_title="Jerry - Deepfake Detector",
|
| 19 |
-
page_icon="π΅οΈ",
|
| 20 |
-
layout="wide",
|
| 21 |
-
initial_sidebar_state="collapsed"
|
| 22 |
-
)
|
| 23 |
|
| 24 |
-
#
|
| 25 |
-
|
| 26 |
-
<style>
|
| 27 |
-
.main-header {
|
| 28 |
-
font-size: 3rem;
|
| 29 |
-
color: #1f77b4;
|
| 30 |
-
text-align: center;
|
| 31 |
-
margin-bottom: 1rem;
|
| 32 |
-
}
|
| 33 |
-
.sub-header {
|
| 34 |
-
font-size: 1.2rem;
|
| 35 |
-
color: #666;
|
| 36 |
-
text-align: center;
|
| 37 |
-
margin-bottom: 2rem;
|
| 38 |
-
}
|
| 39 |
-
.verdict-real {
|
| 40 |
-
padding: 20px;
|
| 41 |
-
background-color: #d4edda;
|
| 42 |
-
border: 2px solid #c3e6cb;
|
| 43 |
-
border-radius: 10px;
|
| 44 |
-
color: #155724;
|
| 45 |
-
font-size: 1.5rem;
|
| 46 |
-
font-weight: bold;
|
| 47 |
-
text-align: center;
|
| 48 |
-
margin: 20px 0;
|
| 49 |
-
}
|
| 50 |
-
.verdict-fake {
|
| 51 |
-
padding: 20px;
|
| 52 |
-
background-color: #f8d7da;
|
| 53 |
-
border: 2px solid #f5c6cb;
|
| 54 |
-
border-radius: 10px;
|
| 55 |
-
color: #721c24;
|
| 56 |
-
font-size: 1.5rem;
|
| 57 |
-
font-weight: bold;
|
| 58 |
-
text-align: center;
|
| 59 |
-
margin: 20px 0;
|
| 60 |
-
}
|
| 61 |
-
.confidence-bar {
|
| 62 |
-
height: 25px;
|
| 63 |
-
border-radius: 5px;
|
| 64 |
-
margin: 10px 0;
|
| 65 |
-
}
|
| 66 |
-
.metric-card {
|
| 67 |
-
padding: 15px;
|
| 68 |
-
border-radius: 10px;
|
| 69 |
-
background-color: #f8f9fa;
|
| 70 |
-
border-left: 4px solid #1f77b4;
|
| 71 |
-
margin: 10px 0;
|
| 72 |
-
}
|
| 73 |
-
.warning-box {
|
| 74 |
-
padding: 20px;
|
| 75 |
-
background-color: #fff3cd;
|
| 76 |
-
border: 2px solid #ffeaa7;
|
| 77 |
-
border-radius: 10px;
|
| 78 |
-
color: #856404;
|
| 79 |
-
margin: 20px 0;
|
| 80 |
-
}
|
| 81 |
-
</style>
|
| 82 |
-
""", unsafe_allow_html=True)
|
| 83 |
|
| 84 |
-
@st.cache_resource
|
| 85 |
def load_model():
|
| 86 |
"""Load the model once and cache it"""
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
st.info("π Loading Deepfake Detection Model... This might take a moment.")
|
| 92 |
-
pipe = pipeline(
|
| 93 |
"image-classification",
|
| 94 |
model="prithivMLmods/Deep-Fake-Detector-v2-Model"
|
| 95 |
)
|
| 96 |
-
st.success("
|
| 97 |
-
|
| 98 |
-
except Exception as e:
|
| 99 |
-
st.error(f"β Failed to load model: {str(e)}")
|
| 100 |
-
return None
|
| 101 |
|
| 102 |
def predict_deepfake(image):
|
| 103 |
"""Predict if image is deepfake or real"""
|
| 104 |
-
if not TRANSFORMERS_AVAILABLE:
|
| 105 |
-
return None, "Transformers library not available. Please check dependencies."
|
| 106 |
-
|
| 107 |
try:
|
|
|
|
| 108 |
pipe = load_model()
|
| 109 |
-
if pipe is None:
|
| 110 |
-
return None, "Model failed to load. Please check your internet connection and try again."
|
| 111 |
|
| 112 |
# Make prediction
|
| 113 |
results = pipe(image)
|
|
@@ -115,208 +30,79 @@ def predict_deepfake(image):
|
|
| 115 |
# Format results
|
| 116 |
prediction = {result['label']: result['score'] for result in results}
|
| 117 |
|
| 118 |
-
#
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 123 |
|
| 124 |
except Exception as e:
|
| 125 |
-
return
|
| 126 |
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
def show_demo_results():
|
| 134 |
-
"""Show demo results when transformers is not available"""
|
| 135 |
-
st.markdown("### Confidence Scores")
|
| 136 |
-
|
| 137 |
-
# Realism score
|
| 138 |
-
col_real, col_real_pct = st.columns([3, 1])
|
| 139 |
-
with col_real:
|
| 140 |
-
st.progress(0.78, text="Realism: 78%")
|
| 141 |
-
with col_real_pct:
|
| 142 |
-
st.metric("Realism", "78.0%")
|
| 143 |
-
|
| 144 |
-
# Deepfake score
|
| 145 |
-
col_fake, col_fake_pct = st.columns([3, 1])
|
| 146 |
-
with col_fake:
|
| 147 |
-
st.progress(0.22, text="Deepfake: 22%")
|
| 148 |
-
with col_fake_pct:
|
| 149 |
-
st.metric("Deepfake", "22.0%")
|
| 150 |
-
|
| 151 |
-
# Verdict
|
| 152 |
-
st.markdown("### π― Verdict")
|
| 153 |
-
st.markdown('<div class="verdict-real">β
REAL IMAGE<br><small>Confidence: 78.0%</small></div>', unsafe_allow_html=True)
|
| 154 |
|
| 155 |
def main():
|
| 156 |
-
|
| 157 |
-
st.markdown(
|
| 158 |
-
st.markdown('<p class="sub-header">Upload an image to check if it\'s real or AI-generated!</p>', unsafe_allow_html=True)
|
| 159 |
-
|
| 160 |
-
# Show warning if transformers is not available
|
| 161 |
-
if not TRANSFORMERS_AVAILABLE:
|
| 162 |
-
st.markdown("""
|
| 163 |
-
<div class="warning-box">
|
| 164 |
-
<h3>β οΈ Dependency Issue Detected</h3>
|
| 165 |
-
<p>The transformers library could not be imported properly. This is usually due to version compatibility issues.</p>
|
| 166 |
-
<p><strong>Solution:</strong> Please check that you're using compatible versions of transformers and torch.</p>
|
| 167 |
-
</div>
|
| 168 |
-
""", unsafe_allow_html=True)
|
| 169 |
-
|
| 170 |
-
# Sidebar for information
|
| 171 |
-
with st.sidebar:
|
| 172 |
-
st.header("βΉοΈ About")
|
| 173 |
-
st.markdown("""
|
| 174 |
-
**How it works:**
|
| 175 |
-
- Upload any facial image
|
| 176 |
-
- Jerry analyzes it using AI
|
| 177 |
-
- Get instant results with confidence scores
|
| 178 |
-
|
| 179 |
-
**Detection Categories:**
|
| 180 |
-
- π’ **Realism**: Likely a real human face
|
| 181 |
-
- π΄ **Deepfake**: AI-generated or manipulated
|
| 182 |
-
""")
|
| 183 |
-
|
| 184 |
-
st.header("βοΈ Technical Info")
|
| 185 |
-
st.write(f"**Transformers Available:** {'β
Yes' if TRANSFORMERS_AVAILABLE else 'β No'}")
|
| 186 |
-
st.write("**Model:** Deep-Fake-Detector-v2")
|
| 187 |
-
|
| 188 |
-
if not TRANSFORMERS_AVAILABLE:
|
| 189 |
-
st.header("π§ Fix Required")
|
| 190 |
-
st.markdown("""
|
| 191 |
-
**Install compatible versions:**
|
| 192 |
-
```bash
|
| 193 |
-
pip install transformers==4.30.0
|
| 194 |
-
pip install torch==2.0.0
|
| 195 |
-
pip install pillow==10.0.0
|
| 196 |
-
```
|
| 197 |
-
""")
|
| 198 |
-
|
| 199 |
-
st.markdown("---")
|
| 200 |
-
st.markdown("Built with β€οΈ using Streamlit")
|
| 201 |
|
| 202 |
-
#
|
| 203 |
-
col1, col2 = st.columns(
|
| 204 |
|
| 205 |
with col1:
|
| 206 |
-
st.subheader("
|
| 207 |
-
|
| 208 |
-
# File uploader
|
| 209 |
uploaded_file = st.file_uploader(
|
| 210 |
-
"Choose an image
|
| 211 |
-
type=['
|
| 212 |
-
|
| 213 |
)
|
| 214 |
|
| 215 |
if uploaded_file is not None:
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
st.image(image, caption="Uploaded Image", use_column_width=True)
|
| 220 |
-
|
| 221 |
-
# Analyze button
|
| 222 |
-
if st.button("π Analyze Image", type="primary", use_container_width=True):
|
| 223 |
-
if not TRANSFORMERS_AVAILABLE:
|
| 224 |
-
st.error("β Cannot analyze - transformers library not available. Please check the sidebar for installation instructions.")
|
| 225 |
-
else:
|
| 226 |
-
with st.spinner("π Analyzing image... This may take a few seconds."):
|
| 227 |
-
prediction, error = predict_deepfake(image)
|
| 228 |
-
|
| 229 |
-
if error:
|
| 230 |
-
st.error(f"β {error}")
|
| 231 |
-
else:
|
| 232 |
-
# Store results in session state
|
| 233 |
-
st.session_state.prediction = prediction
|
| 234 |
-
st.session_state.image = image
|
| 235 |
-
st.session_state.analysis_done = True
|
| 236 |
-
|
| 237 |
-
# Show success message
|
| 238 |
-
st.success("β
Analysis complete!")
|
| 239 |
-
except Exception as e:
|
| 240 |
-
st.error(f"β Error processing image: {str(e)}")
|
| 241 |
|
| 242 |
with col2:
|
| 243 |
-
st.subheader("
|
| 244 |
-
|
| 245 |
-
|
| 246 |
-
|
| 247 |
-
|
| 248 |
-
|
| 249 |
-
|
| 250 |
-
|
| 251 |
-
|
| 252 |
-
|
| 253 |
-
|
| 254 |
-
|
| 255 |
-
|
| 256 |
-
|
| 257 |
-
# Display confidence scores with progress bars
|
| 258 |
-
st.markdown("### Confidence Scores")
|
| 259 |
-
|
| 260 |
-
# Realism score
|
| 261 |
-
col_real, col_real_pct = st.columns([3, 1])
|
| 262 |
-
with col_real:
|
| 263 |
-
st.progress(float(realism_score), text=f"Realism: {realism_score:.1%}")
|
| 264 |
-
with col_real_pct:
|
| 265 |
-
st.metric("Realism", f"{realism_score:.1%}")
|
| 266 |
-
|
| 267 |
-
# Deepfake score
|
| 268 |
-
col_fake, col_fake_pct = st.columns([3, 1])
|
| 269 |
-
with col_fake:
|
| 270 |
-
st.progress(float(deepfake_score), text=f"Deepfake: {deepfake_score:.1%}")
|
| 271 |
-
with col_fake_pct:
|
| 272 |
-
st.metric("Deepfake", f"{deepfake_score:.1%}")
|
| 273 |
-
|
| 274 |
-
# Verdict
|
| 275 |
-
st.markdown("### π― Verdict")
|
| 276 |
-
if realism_score > deepfake_score:
|
| 277 |
-
st.markdown(f'<div class="verdict-real">β
REAL IMAGE<br><small>Confidence: {realism_score:.2%}</small></div>', unsafe_allow_html=True)
|
| 278 |
-
else:
|
| 279 |
-
st.markdown(f'<div class="verdict-fake">π¨ DEEPFAKE DETECTED<br><small>Confidence: {deepfake_score:.2%}</small></div>', unsafe_allow_html=True)
|
| 280 |
-
|
| 281 |
-
# Detailed prediction breakdown
|
| 282 |
-
st.markdown("### π Detailed Analysis")
|
| 283 |
-
for label, score in prediction.items():
|
| 284 |
-
col_label, col_score, col_bar = st.columns([2, 1, 3])
|
| 285 |
-
with col_label:
|
| 286 |
-
st.write(f"**{label}**")
|
| 287 |
-
with col_score:
|
| 288 |
-
st.write(f"{float(score):.1%}")
|
| 289 |
-
with col_bar:
|
| 290 |
-
st.progress(float(score))
|
| 291 |
-
else:
|
| 292 |
-
st.error("β No valid prediction results available. Please try again.")
|
| 293 |
-
|
| 294 |
-
else:
|
| 295 |
-
# Placeholder before analysis
|
| 296 |
-
st.info("π Upload an image and click 'Analyze Image' to see results here.")
|
| 297 |
-
|
| 298 |
-
# Sample results preview
|
| 299 |
-
st.markdown("### Example Output:")
|
| 300 |
-
st.progress(0.85, text="Realism: 85%")
|
| 301 |
-
st.progress(0.15, text="Deepfake: 15%")
|
| 302 |
-
st.markdown('<div class="verdict-real">β
REAL IMAGE</div>', unsafe_allow_html=True)
|
| 303 |
|
| 304 |
-
#
|
| 305 |
-
st.markdown(
|
| 306 |
-
|
| 307 |
-
|
| 308 |
-
|
| 309 |
-
|
| 310 |
-
|
| 311 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 312 |
|
| 313 |
if __name__ == "__main__":
|
| 314 |
-
|
| 315 |
-
|
| 316 |
-
st.session_state.prediction = None
|
| 317 |
-
if 'image' not in st.session_state:
|
| 318 |
-
st.session_state.image = None
|
| 319 |
-
if 'analysis_done' not in st.session_state:
|
| 320 |
-
st.session_state.analysis_done = False
|
| 321 |
-
|
| 322 |
main()
|
|
|
|
| 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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
|
|
|
| 9 |
def load_model():
|
| 10 |
"""Load the model once and cache it"""
|
| 11 |
+
global deepfake_pipe
|
| 12 |
+
if deepfake_pipe is None:
|
| 13 |
+
st.info("Loading Deepfake Detection Model...")
|
| 14 |
+
deepfake_pipe = pipeline(
|
|
|
|
|
|
|
| 15 |
"image-classification",
|
| 16 |
model="prithivMLmods/Deep-Fake-Detector-v2-Model"
|
| 17 |
)
|
| 18 |
+
st.success("Model loaded successfully!")
|
| 19 |
+
return deepfake_pipe
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
def predict_deepfake(image):
|
| 22 |
"""Predict if image is deepfake or real"""
|
|
|
|
|
|
|
|
|
|
| 23 |
try:
|
| 24 |
+
# Load model if not already loaded
|
| 25 |
pipe = load_model()
|
|
|
|
|
|
|
| 26 |
|
| 27 |
# Make prediction
|
| 28 |
results = pipe(image)
|
|
|
|
| 30 |
# Format results
|
| 31 |
prediction = {result['label']: result['score'] for result in results}
|
| 32 |
|
| 33 |
+
# Determine final verdict
|
| 34 |
+
deepfake_score = prediction.get('Deepfake', 0)
|
| 35 |
+
realism_score = prediction.get('Realism', 0)
|
| 36 |
+
|
| 37 |
+
if deepfake_score > realism_score:
|
| 38 |
+
verdict = f"π¨ DEEPFAKE DETECTED ({deepfake_score:.2%} confidence)"
|
| 39 |
+
color = "red"
|
| 40 |
+
else:
|
| 41 |
+
verdict = f"β
REAL IMAGE ({realism_score:.2%} confidence)"
|
| 42 |
+
color = "green"
|
| 43 |
+
|
| 44 |
+
return prediction, verdict, color
|
| 45 |
|
| 46 |
except Exception as e:
|
| 47 |
+
return {"Error": 1.0}, f"β Prediction failed: {str(e)}", "red"
|
| 48 |
|
| 49 |
+
# Streamlit app configuration
|
| 50 |
+
st.set_page_config(
|
| 51 |
+
page_title="Jerry - Deepfake Detector",
|
| 52 |
+
page_icon="π΅οΈ",
|
| 53 |
+
layout="wide"
|
| 54 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
|
| 56 |
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 |
+
# Create two columns
|
| 61 |
+
col1, col2 = st.columns(2)
|
| 62 |
|
| 63 |
with col1:
|
| 64 |
+
st.subheader("Upload Image")
|
|
|
|
|
|
|
| 65 |
uploaded_file = st.file_uploader(
|
| 66 |
+
"Choose an image",
|
| 67 |
+
type=['png', 'jpg', 'jpeg'],
|
| 68 |
+
label_visibility="collapsed"
|
| 69 |
)
|
| 70 |
|
| 71 |
if uploaded_file is not None:
|
| 72 |
+
# Display uploaded image
|
| 73 |
+
image = Image.open(uploaded_file)
|
| 74 |
+
st.image(image, caption="Uploaded Image", use_column_width=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
|
| 76 |
with col2:
|
| 77 |
+
st.subheader("Detection Results")
|
| 78 |
+
if uploaded_file is not None:
|
| 79 |
+
if st.button("π Analyze Image", type="primary"):
|
| 80 |
+
with st.spinner("Analyzing image..."):
|
| 81 |
+
# Convert uploaded file to filepath for pipeline
|
| 82 |
+
prediction, verdict, color = predict_deepfake(uploaded_file)
|
| 83 |
+
|
| 84 |
+
# Display results
|
| 85 |
+
st.write("**Prediction Scores:**")
|
| 86 |
+
for label, score in prediction.items():
|
| 87 |
+
st.write(f"{label}: {score:.2%}")
|
| 88 |
+
|
| 89 |
+
# Display verdict with colored text
|
| 90 |
+
st.markdown(f"<p style='color:{color};font-size:18px;'><b>{verdict}</b></p>", unsafe_allow_html=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 91 |
|
| 92 |
+
# Additional information
|
| 93 |
+
st.markdown(
|
| 94 |
+
"""
|
| 95 |
+
---
|
| 96 |
+
**How it works:**
|
| 97 |
+
- Upload any facial image
|
| 98 |
+
- Jerry analyzes it and gives appropriate verdict
|
| 99 |
+
- Returns confidence scores for "Deepfake" vs "Realism"
|
| 100 |
+
|
| 101 |
+
**Note:** The model loads only once when first used, then runs quickly for subsequent predictions!
|
| 102 |
+
"""
|
| 103 |
+
)
|
| 104 |
|
| 105 |
if __name__ == "__main__":
|
| 106 |
+
print("π Starting Jerry - Deepfake Detection App...")
|
| 107 |
+
print("Model will load on first prediction...")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 108 |
main()
|