| import gradio as gr | |
| from tensorflow.keras.models import load_model | |
| from PIL import Image | |
| import numpy as np | |
| # Load the model | |
| model = load_model('./model.h5') | |
| def detect_image(input_image): | |
| img = Image.fromarray(input_image).resize((256, 256)) | |
| img_array = np.array(img) / 255.0 | |
| img_array = np.expand_dims(img_array, axis=0) | |
| prediction = model.predict(img_array)[0][0] | |
| probability_real = prediction * 100 | |
| probability_ai = (1 - prediction) * 100 | |
| 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 | |
| demo = gr.Interface( | |
| fn=detect_image, | |
| inputs=gr.Image(type="numpy", shape=(256, 256)), | |
| outputs=[gr.Textbox(label="Result"), gr.Textbox(label="Confidence (%)")], | |
| title="Deepfake Detection", | |
| description="Upload an image to detect if it's real or AI generated." | |
| ) | |
| # Deploy the interface on Gradio Hub | |
| demo.launch(share=True) | |