Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import gradio as gr
|
| 3 |
+
import google.generativeai as genai
|
| 4 |
+
from PIL import Image
|
| 5 |
+
|
| 6 |
+
# Configure the Gemini API with environment variable
|
| 7 |
+
GOOGLE_API_KEY = os.getenv("GOOGLE_API_KEY")
|
| 8 |
+
if not GOOGLE_API_KEY:
|
| 9 |
+
raise ValueError("GOOGLE_API_KEY environment variable not set. Please configure it in the Hugging Face Space settings.")
|
| 10 |
+
|
| 11 |
+
genai.configure(api_key=GOOGLE_API_KEY)
|
| 12 |
+
|
| 13 |
+
# Use Gemini 1.5 Flash model
|
| 14 |
+
model = genai.GenerativeModel('gemini-1.5-flash-latest')
|
| 15 |
+
|
| 16 |
+
def detect_deepfake(image):
|
| 17 |
+
if image is None:
|
| 18 |
+
return "Error: Please upload or capture an image."
|
| 19 |
+
|
| 20 |
+
# Resize image for consistency (optional, Gemini handles various sizes)
|
| 21 |
+
image = image.resize((224, 224))
|
| 22 |
+
|
| 23 |
+
prompt = """
|
| 24 |
+
You are an image analysis assistant tasked with detecting whether an image is a deepfake or real.
|
| 25 |
+
A deepfake is a synthetically generated or manipulated image, often of a human face, showing unnatural features like inconsistent lighting, unnatural textures, or blending artifacts.
|
| 26 |
+
A real image typically has natural lighting, consistent facial features, and no synthetic artifacts.
|
| 27 |
+
Classify the image as "Real" or "Deepfake" and provide a brief reason for your classification.
|
| 28 |
+
Return the response in this format:
|
| 29 |
+
|
| 30 |
+
Prediction: [Real / Deepfake]
|
| 31 |
+
Reason: [Brief explanation]
|
| 32 |
+
|
| 33 |
+
Examples:
|
| 34 |
+
Image Description: A face with consistent lighting, natural skin texture, and realistic eye movements.
|
| 35 |
+
Prediction: Real
|
| 36 |
+
Reason: The image shows natural lighting and facial features consistent with a real human photograph.
|
| 37 |
+
|
| 38 |
+
Image Description: A face with unnatural blending around the eyes, inconsistent lighting on the face, and slight pixelation.
|
| 39 |
+
Prediction: Deepfake
|
| 40 |
+
Reason: The unnatural blending and inconsistent lighting suggest synthetic manipulation typical of deepfakes.
|
| 41 |
+
|
| 42 |
+
Image Description: A face with smooth, overly perfect skin and slightly distorted facial proportions.
|
| 43 |
+
Prediction: Deepfake
|
| 44 |
+
Reason: The overly smooth skin and distorted proportions are indicative of AI-generated or manipulated images.
|
| 45 |
+
|
| 46 |
+
Image Description: A face with natural shadows, realistic hair texture, and consistent background lighting.
|
| 47 |
+
Prediction: Real
|
| 48 |
+
Reason: The natural shadows and realistic textures align with characteristics of a genuine photograph.
|
| 49 |
+
|
| 50 |
+
Classify the provided image.
|
| 51 |
+
Prediction:
|
| 52 |
+
Reason:
|
| 53 |
+
"""
|
| 54 |
+
|
| 55 |
+
try:
|
| 56 |
+
response = model.generate_content([prompt, image])
|
| 57 |
+
return response.text.strip()
|
| 58 |
+
except Exception as e:
|
| 59 |
+
return f"Error: {str(e)}\nTip: Ensure your API key is valid at https://aistudio.google.com/"
|
| 60 |
+
|
| 61 |
+
# Define Gradio interface
|
| 62 |
+
iface = gr.Interface(
|
| 63 |
+
fn=detect_deepfake,
|
| 64 |
+
inputs=gr.Image(type="pil", label="Upload or capture a face image", sources=["upload", "webcam"]),
|
| 65 |
+
outputs=gr.Textbox(label="Deepfake Detection Result"),
|
| 66 |
+
title="Deepfake Image Detector",
|
| 67 |
+
description="Upload or capture a face image to determine if it is Real or a Deepfake using the Gemini API. Set your GOOGLE_API_KEY in the Hugging Face Space settings."
|
| 68 |
+
)
|
| 69 |
+
|
| 70 |
+
# Launch the interface
|
| 71 |
+
if __name__ == "__main__":
|
| 72 |
+
iface.launch()
|