Spaces:
Runtime error
Runtime error
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) | |