File size: 1,622 Bytes
ccef867
 
eae8589
 
 
ccef867
eae8589
ccef867
 
 
 
 
eae8589
 
ccef867
 
 
 
 
 
 
 
 
eae8589
ccef867
 
 
 
 
 
 
eae8589
ccef867
 
 
 
eae8589
ccef867
 
 
 
eae8589
ccef867
 
eae8589
ccef867
 
eae8589
 
ccef867
eae8589
1
2
3
4
5
6
7
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


from tensorflow.keras.models import load_model
from PIL import Image
import numpy as np
import gradio as gr

# Loading the trained model
try:
    model = load_model('/model.h5')  # Replacing with the path to your saved model
except Exception as e:
    print("Error loading the model:", e)

def detect_image(input_image):
    try:
        # Function to detect image
        img = Image.fromarray(input_image).resize((256, 256))  # Resize image
        img_array = np.array(img) / 255.0  # Normalize pixel values
        img_array = np.expand_dims(img_array, axis=0)  # Add batch dimension

        prediction = model.predict(img_array)[0][0]
        probability_real = prediction * 100  # Convert prediction to percentage
        probability_ai = (1 - prediction) * 100

        # Determine the final output
        if probability_real > probability_ai:
            result = 'Input Image is Real'
            confidence = probability_real
        else:
            result = 'Input Image is AI Generated'
            confidence = probability_ai

        return result, confidence
    except Exception as e:
        print("Error detecting image:", e)
        return "Error detecting image", 0

# Define input and output components for Gradio
input_image = gr.Image()
output_text = gr.Textbox(label="Result")
output_confidence = gr.Textbox(label="Confidence (%)")

# Create Gradio interface
gr.Interface(
    fn=detect_image,
    inputs=input_image,
    outputs=[output_text, output_confidence],
    title="Deepfake Detection",
    description="Upload an image to detect if it's real or AI generated."
).launch(share=True)