Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,240 +1,288 @@
|
|
| 1 |
-
"""
|
| 2 |
-
Deepfake Detection System -
|
| 3 |
-
"""
|
| 4 |
-
import
|
| 5 |
-
import os
|
| 6 |
-
from pathlib import Path
|
| 7 |
-
from utils.detect import DeepfakeDetector
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
"
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
)
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
)
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
return
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
|
| 233 |
-
|
| 234 |
-
|
| 235 |
-
|
| 236 |
-
|
| 237 |
-
|
| 238 |
-
|
| 239 |
-
|
| 240 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Deepfake Detection System - Gradio Application
|
| 3 |
+
"""
|
| 4 |
+
import gradio as gr
|
| 5 |
+
import os
|
| 6 |
+
from pathlib import Path
|
| 7 |
+
from utils.detect import DeepfakeDetector
|
| 8 |
+
import tempfile
|
| 9 |
+
|
| 10 |
+
def analyze_media(api_key, media_file, media_type):
|
| 11 |
+
"""
|
| 12 |
+
Analyze media file for deepfake content
|
| 13 |
+
|
| 14 |
+
Args:
|
| 15 |
+
api_key: Gemini API key
|
| 16 |
+
media_file: Uploaded file
|
| 17 |
+
media_type: "Image" or "Video"
|
| 18 |
+
|
| 19 |
+
Returns:
|
| 20 |
+
tuple: (result_html, confidence_score, verdict)
|
| 21 |
+
"""
|
| 22 |
+
# Validate API key
|
| 23 |
+
if not api_key or api_key.strip() == "":
|
| 24 |
+
return (
|
| 25 |
+
"β οΈ **Error**: Please enter your Gemini API key first.",
|
| 26 |
+
None,
|
| 27 |
+
None
|
| 28 |
+
)
|
| 29 |
+
|
| 30 |
+
# Validate file upload
|
| 31 |
+
if media_file is None:
|
| 32 |
+
return (
|
| 33 |
+
"β οΈ **Error**: Please upload a file to analyze.",
|
| 34 |
+
None,
|
| 35 |
+
None
|
| 36 |
+
)
|
| 37 |
+
|
| 38 |
+
try:
|
| 39 |
+
# Get file path
|
| 40 |
+
file_path = media_file.name if hasattr(media_file, 'name') else media_file
|
| 41 |
+
|
| 42 |
+
# Initialize detector
|
| 43 |
+
detector = DeepfakeDetector(api_key.strip())
|
| 44 |
+
|
| 45 |
+
# Perform analysis based on media type
|
| 46 |
+
if media_type == "Image":
|
| 47 |
+
results = detector.analyze_image(file_path)
|
| 48 |
+
else: # Video
|
| 49 |
+
results = detector.analyze_video(file_path, max_frames=10)
|
| 50 |
+
|
| 51 |
+
# Check for errors
|
| 52 |
+
if 'error' in results:
|
| 53 |
+
return (
|
| 54 |
+
f"β **Error during analysis**: {results['error']}\n\nPlease check your API key and try again.",
|
| 55 |
+
None,
|
| 56 |
+
None
|
| 57 |
+
)
|
| 58 |
+
|
| 59 |
+
# Format results
|
| 60 |
+
result_html = format_results(results, media_type)
|
| 61 |
+
confidence = results.get('confidence_score', 0)
|
| 62 |
+
verdict = "π΄ LIKELY DEEPFAKE" if results.get('is_deepfake', False) else "π’ LIKELY AUTHENTIC"
|
| 63 |
+
|
| 64 |
+
return result_html, confidence, verdict
|
| 65 |
+
|
| 66 |
+
except Exception as e:
|
| 67 |
+
return (
|
| 68 |
+
f"β **Error**: {str(e)}\n\nPlease check your file and API key, then try again.",
|
| 69 |
+
None,
|
| 70 |
+
None
|
| 71 |
+
)
|
| 72 |
+
|
| 73 |
+
def format_results(results, media_type):
|
| 74 |
+
"""Format analysis results as HTML"""
|
| 75 |
+
is_fake = results.get('is_deepfake', False)
|
| 76 |
+
confidence = results.get('confidence_score', 0)
|
| 77 |
+
analysis = results.get('analysis', 'No analysis available.')
|
| 78 |
+
indicators = results.get('indicators', [])
|
| 79 |
+
|
| 80 |
+
# Build HTML output
|
| 81 |
+
html = f"""
|
| 82 |
+
<div style="font-family: Arial, sans-serif;">
|
| 83 |
+
<h2>π Analysis Results</h2>
|
| 84 |
+
|
| 85 |
+
<div style="background: {'#fee'; border-left: 4px solid #f00;' if is_fake else '#efe; border-left: 4px solid #0f0;'} padding: 15px; margin: 10px 0;">
|
| 86 |
+
<h3>{'π΄ LIKELY DEEPFAKE/AI-GENERATED' if is_fake else 'π’ LIKELY AUTHENTIC'}</h3>
|
| 87 |
+
</div>
|
| 88 |
+
|
| 89 |
+
<div style="background: #f5f5f5; padding: 15px; margin: 10px 0; border-radius: 5px;">
|
| 90 |
+
<h3>π Detection Metrics</h3>
|
| 91 |
+
<p><strong>Confidence Score:</strong> {confidence:.1f}%</p>
|
| 92 |
+
<p><strong>Authenticity:</strong> {100 - confidence:.1f}%</p>
|
| 93 |
+
<p><strong>Risk Level:</strong> {'High' if confidence > 70 else 'Medium' if confidence > 40 else 'Low'}</p>
|
| 94 |
+
</div>
|
| 95 |
+
|
| 96 |
+
<div style="background: #f5f5f5; padding: 15px; margin: 10px 0; border-radius: 5px;">
|
| 97 |
+
<h3>π Detailed Analysis</h3>
|
| 98 |
+
<p style="white-space: pre-wrap;">{analysis}</p>
|
| 99 |
+
</div>
|
| 100 |
+
"""
|
| 101 |
+
|
| 102 |
+
# Add indicators if found
|
| 103 |
+
if indicators:
|
| 104 |
+
html += """
|
| 105 |
+
<div style="background: #fff3cd; padding: 15px; margin: 10px 0; border-radius: 5px; border-left: 4px solid #ffc107;">
|
| 106 |
+
<h3>β οΈ Deepfake Indicators Detected</h3>
|
| 107 |
+
<ul>
|
| 108 |
+
"""
|
| 109 |
+
for indicator in indicators[:5]:
|
| 110 |
+
html += f"<li>{indicator}</li>"
|
| 111 |
+
html += """
|
| 112 |
+
</ul>
|
| 113 |
+
</div>
|
| 114 |
+
"""
|
| 115 |
+
|
| 116 |
+
# Add video-specific results
|
| 117 |
+
if media_type == "Video" and 'frame_analysis' in results:
|
| 118 |
+
frame_data = results['frame_analysis']
|
| 119 |
+
html += f"""
|
| 120 |
+
<div style="background: #e7f3ff; padding: 15px; margin: 10px 0; border-radius: 5px; border-left: 4px solid #2196F3;">
|
| 121 |
+
<h3>π¬ Frame-by-Frame Analysis</h3>
|
| 122 |
+
<p><strong>Total Frames Analyzed:</strong> {frame_data.get('total_frames', 0)}</p>
|
| 123 |
+
<p><strong>Suspicious Frames:</strong> {frame_data.get('suspicious_frames', 0)}</p>
|
| 124 |
+
</div>
|
| 125 |
+
"""
|
| 126 |
+
|
| 127 |
+
# Add recommendations
|
| 128 |
+
html += """
|
| 129 |
+
<div style="background: #f5f5f5; padding: 15px; margin: 10px 0; border-radius: 5px;">
|
| 130 |
+
<h3>π‘ Recommendations</h3>
|
| 131 |
+
"""
|
| 132 |
+
|
| 133 |
+
if is_fake:
|
| 134 |
+
html += """
|
| 135 |
+
<p><strong>This media shows signs of manipulation or AI generation. Consider:</strong></p>
|
| 136 |
+
<ul>
|
| 137 |
+
<li>Verifying the source</li>
|
| 138 |
+
<li>Looking for corroborating evidence</li>
|
| 139 |
+
<li>Checking metadata</li>
|
| 140 |
+
<li>Consulting additional verification tools</li>
|
| 141 |
+
<li>Being cautious about sharing</li>
|
| 142 |
+
</ul>
|
| 143 |
+
"""
|
| 144 |
+
else:
|
| 145 |
+
html += """
|
| 146 |
+
<p><strong>This media appears authentic, but remember:</strong></p>
|
| 147 |
+
<ul>
|
| 148 |
+
<li>No detection system is 100% accurate</li>
|
| 149 |
+
<li>Always verify important content through multiple sources</li>
|
| 150 |
+
<li>Check the original source when possible</li>
|
| 151 |
+
</ul>
|
| 152 |
+
"""
|
| 153 |
+
|
| 154 |
+
html += """
|
| 155 |
+
</div>
|
| 156 |
+
</div>
|
| 157 |
+
"""
|
| 158 |
+
|
| 159 |
+
return html
|
| 160 |
+
|
| 161 |
+
# Create Gradio interface
|
| 162 |
+
def create_interface():
|
| 163 |
+
"""Create and configure Gradio interface"""
|
| 164 |
+
|
| 165 |
+
with gr.Blocks(title="Deepfake Detection System", theme=gr.themes.Soft()) as demo:
|
| 166 |
+
|
| 167 |
+
# Header
|
| 168 |
+
gr.Markdown("""
|
| 169 |
+
# π Deepfake Detection System
|
| 170 |
+
### Analyze images and videos for AI-generated or manipulated content
|
| 171 |
+
|
| 172 |
+
This system uses Google's Gemini AI to detect deepfakes and AI-generated content in media files.
|
| 173 |
+
""")
|
| 174 |
+
|
| 175 |
+
# API Key Section
|
| 176 |
+
with gr.Row():
|
| 177 |
+
with gr.Column(scale=3):
|
| 178 |
+
api_key_input = gr.Textbox(
|
| 179 |
+
label="π Gemini API Key",
|
| 180 |
+
type="password",
|
| 181 |
+
placeholder="Enter your Google Gemini API key here...",
|
| 182 |
+
info="Get your free API key from https://makersuite.google.com/app/apikey"
|
| 183 |
+
)
|
| 184 |
+
with gr.Column(scale=1):
|
| 185 |
+
gr.Markdown("""
|
| 186 |
+
### π How to get API Key:
|
| 187 |
+
1. Visit [Google AI Studio](https://makersuite.google.com/app/apikey)
|
| 188 |
+
2. Sign in with Google
|
| 189 |
+
3. Create API key
|
| 190 |
+
4. Paste it here
|
| 191 |
+
""")
|
| 192 |
+
|
| 193 |
+
gr.Markdown("---")
|
| 194 |
+
|
| 195 |
+
# Main Interface
|
| 196 |
+
with gr.Row():
|
| 197 |
+
with gr.Column(scale=1):
|
| 198 |
+
# Media Type Selection
|
| 199 |
+
media_type = gr.Radio(
|
| 200 |
+
choices=["Image", "Video"],
|
| 201 |
+
value="Image",
|
| 202 |
+
label="π Select Media Type",
|
| 203 |
+
info="Choose the type of media you want to analyze"
|
| 204 |
+
)
|
| 205 |
+
|
| 206 |
+
# File Upload
|
| 207 |
+
media_file = gr.File(
|
| 208 |
+
label="π€ Upload Media File",
|
| 209 |
+
file_types=["image", "video"],
|
| 210 |
+
type="filepath"
|
| 211 |
+
)
|
| 212 |
+
|
| 213 |
+
# Analyze Button
|
| 214 |
+
analyze_btn = gr.Button(
|
| 215 |
+
"π Analyze Media",
|
| 216 |
+
variant="primary",
|
| 217 |
+
size="lg"
|
| 218 |
+
)
|
| 219 |
+
|
| 220 |
+
# Quick Stats
|
| 221 |
+
gr.Markdown("""
|
| 222 |
+
### π Supported Formats
|
| 223 |
+
**Images:** JPG, PNG, WEBP
|
| 224 |
+
**Videos:** MP4, AVI, MOV, MKV
|
| 225 |
+
|
| 226 |
+
### β±οΈ Processing Time
|
| 227 |
+
**Images:** 5-15 seconds
|
| 228 |
+
**Videos:** 30-60 seconds
|
| 229 |
+
""")
|
| 230 |
+
|
| 231 |
+
with gr.Column(scale=2):
|
| 232 |
+
# Results Display
|
| 233 |
+
gr.Markdown("### π Analysis Results")
|
| 234 |
+
|
| 235 |
+
with gr.Row():
|
| 236 |
+
verdict_output = gr.Textbox(
|
| 237 |
+
label="Verdict",
|
| 238 |
+
interactive=False,
|
| 239 |
+
scale=2
|
| 240 |
+
)
|
| 241 |
+
confidence_output = gr.Number(
|
| 242 |
+
label="Confidence Score (%)",
|
| 243 |
+
interactive=False,
|
| 244 |
+
scale=1
|
| 245 |
+
)
|
| 246 |
+
|
| 247 |
+
result_output = gr.HTML(
|
| 248 |
+
label="Detailed Analysis"
|
| 249 |
+
)
|
| 250 |
+
|
| 251 |
+
# Footer
|
| 252 |
+
gr.Markdown("""
|
| 253 |
+
---
|
| 254 |
+
### β οΈ Important Notes
|
| 255 |
+
- **Not 100% Accurate**: No detection system is perfect. Always verify through multiple sources.
|
| 256 |
+
- **Privacy**: Your API key and files are not stored. They're only used for analysis.
|
| 257 |
+
- **For Educational Use**: This tool is for educational and research purposes.
|
| 258 |
+
|
| 259 |
+
### π οΈ Technology Stack
|
| 260 |
+
Built with: Google Gemini AI β’ Gradio β’ OpenCV β’ Python
|
| 261 |
+
""")
|
| 262 |
+
|
| 263 |
+
# Connect the analyze button
|
| 264 |
+
analyze_btn.click(
|
| 265 |
+
fn=analyze_media,
|
| 266 |
+
inputs=[api_key_input, media_file, media_type],
|
| 267 |
+
outputs=[result_output, confidence_output, verdict_output]
|
| 268 |
+
)
|
| 269 |
+
|
| 270 |
+
# Examples section
|
| 271 |
+
gr.Markdown("""
|
| 272 |
+
### π‘ Tips for Best Results
|
| 273 |
+
- Use high-quality images and videos
|
| 274 |
+
- Ensure good lighting in the media
|
| 275 |
+
- Videos should be at least 5 seconds long
|
| 276 |
+
- Check multiple suspicious areas if available
|
| 277 |
+
""")
|
| 278 |
+
|
| 279 |
+
return demo
|
| 280 |
+
|
| 281 |
+
# Launch the application
|
| 282 |
+
if __name__ == "__main__":
|
| 283 |
+
demo = create_interface()
|
| 284 |
+
demo.launch(
|
| 285 |
+
server_name="0.0.0.0",
|
| 286 |
+
server_port=7860,
|
| 287 |
+
share=False
|
| 288 |
+
)
|