Update app.py
Browse files
app.py
CHANGED
|
@@ -1,83 +1,33 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import pipeline
|
| 3 |
from PIL import Image
|
| 4 |
-
import requests
|
| 5 |
-
from serpapi import GoogleSearch
|
| 6 |
-
import tempfile
|
| 7 |
-
|
| 8 |
-
# Replace with your actual API keys
|
| 9 |
-
IMGBB_API_KEY = "47cf5c6318ae2fc7d1ea3305189942c4"
|
| 10 |
-
SERPAPI_API_KEY = "d70b432deb7b0acfb440fada51e1446974913bb30cc753e78397752336b33db9"
|
| 11 |
|
| 12 |
# Load the deepfake detection model
|
| 13 |
deepfake_detector = pipeline("image-classification", model="Wvolf/ViT_Deepfake_Detection")
|
| 14 |
|
| 15 |
-
|
| 16 |
-
def upload_image_to_imgbb(image, api_key):
|
| 17 |
-
with tempfile.NamedTemporaryFile(suffix=".png", delete=False) as temp_file:
|
| 18 |
-
image.save(temp_file.name)
|
| 19 |
-
with open(temp_file.name, 'rb') as f:
|
| 20 |
-
response = requests.post(
|
| 21 |
-
"https://api.imgbb.com/1/upload",
|
| 22 |
-
data={'key': api_key},
|
| 23 |
-
files={'image': f}
|
| 24 |
-
)
|
| 25 |
-
if response.status_code == 200:
|
| 26 |
-
return response.json()['data']['url']
|
| 27 |
-
else:
|
| 28 |
-
return None
|
| 29 |
-
|
| 30 |
-
# Search via SerpAPI
|
| 31 |
-
def search_deepfakes(query, api_key):
|
| 32 |
-
params = {
|
| 33 |
-
"q": query,
|
| 34 |
-
"api_key": api_key,
|
| 35 |
-
"engine": "google",
|
| 36 |
-
"search_type": "image"
|
| 37 |
-
}
|
| 38 |
-
search = GoogleSearch(params)
|
| 39 |
-
results = search.get_dict()
|
| 40 |
-
return results.get("images_results", [])
|
| 41 |
-
|
| 42 |
-
# Main function
|
| 43 |
-
def detect_and_search(image):
|
| 44 |
result = deepfake_detector(image)
|
| 45 |
prediction = max(result, key=lambda x: x['score'])
|
| 46 |
-
label = prediction['label']
|
| 47 |
confidence = prediction['score'] * 100
|
| 48 |
-
threshold = 50.0
|
| 49 |
-
|
| 50 |
-
if prediction['score'] > threshold / 100:
|
| 51 |
-
status = f"π¨ Deepfake Detected! FAKE with {confidence:.2f}% confidence."
|
| 52 |
-
else:
|
| 53 |
-
status = f"β
REAL image with {confidence:.2f}% confidence."
|
| 54 |
|
| 55 |
-
#
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
results = search_deepfakes(query, SERPAPI_API_KEY)
|
| 61 |
-
if results:
|
| 62 |
-
first_result = results[0]
|
| 63 |
-
search_summary = (
|
| 64 |
-
f"\nπ Found {len(results)} visually similar images online.\n"
|
| 65 |
-
f"Example:\n- {first_result['title']}\n- {first_result['link']}"
|
| 66 |
-
)
|
| 67 |
-
else:
|
| 68 |
-
search_summary = "\nπ No similar deepfakes found online."
|
| 69 |
else:
|
| 70 |
-
|
| 71 |
|
| 72 |
-
return
|
| 73 |
|
| 74 |
-
# Gradio
|
| 75 |
iface = gr.Interface(
|
| 76 |
-
fn=
|
| 77 |
-
inputs=gr.Image(type="pil", label="Upload
|
| 78 |
outputs="text",
|
| 79 |
-
title="Deepfake Confidence
|
| 80 |
-
description="Upload
|
| 81 |
)
|
| 82 |
|
| 83 |
if __name__ == "__main__":
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import pipeline
|
| 3 |
from PIL import Image
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
# Load the deepfake detection model
|
| 6 |
deepfake_detector = pipeline("image-classification", model="Wvolf/ViT_Deepfake_Detection")
|
| 7 |
|
| 8 |
+
def detect_deepfake(image):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
result = deepfake_detector(image)
|
| 10 |
prediction = max(result, key=lambda x: x['score'])
|
| 11 |
+
label = prediction['label'].lower()
|
| 12 |
confidence = prediction['score'] * 100
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
+
# Adjusted threshold logic
|
| 15 |
+
if label == "fake" and confidence > 75:
|
| 16 |
+
verdict = f"π¨ Deepfake Detected! FAKE with {confidence:.2f}% confidence."
|
| 17 |
+
elif label == "real" and confidence > 75:
|
| 18 |
+
verdict = f"β
REAL image with {confidence:.2f}% confidence."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
else:
|
| 20 |
+
verdict = f"β οΈ Uncertain β low confidence: {label.upper()} with {confidence:.2f}% confidence."
|
| 21 |
|
| 22 |
+
return verdict
|
| 23 |
|
| 24 |
+
# Gradio UI β upload only (no webcam)
|
| 25 |
iface = gr.Interface(
|
| 26 |
+
fn=detect_deepfake,
|
| 27 |
+
inputs=gr.Image(type="pil", label="Upload an Image"),
|
| 28 |
outputs="text",
|
| 29 |
+
title="Deepfake Confidence Score Detector",
|
| 30 |
+
description="Upload an image to check if it's a deepfake. Powered by Hugging Face Transformers."
|
| 31 |
)
|
| 32 |
|
| 33 |
if __name__ == "__main__":
|