Spaces:
Sleeping
Sleeping
import gradio as gr | |
import tensorflow as tf | |
from huggingface_hub import hf_hub_download | |
from tensorflow.keras.preprocessing import image | |
import numpy as np | |
# Step 1: Download the model from the Hugging Face Hub | |
model_path = hf_hub_download(repo_id="Zeyadd-Mostaffa/my_tensorflow_model", filename="my_model.h5") | |
# Step 2: Load the TensorFlow model | |
model = tf.keras.models.load_model(model_path) | |
# Step 3: Function to preprocess the input image | |
def load_and_preprocess_image(img, target_size=(256, 256)): | |
img = img.resize(target_size) | |
img_array = np.array(img) / 255.0 | |
img_array = np.expand_dims(img_array, axis=0) | |
return img_array | |
# Step 4: Function to make predictions | |
def predict_image(img): | |
img_array = load_and_preprocess_image(img) | |
prediction = model.predict(img_array)[0][0] | |
real_confidence = prediction * 100 | |
fake_confidence = (1 - prediction) * 100 | |
result_label = "Real" if real_confidence > fake_confidence else "Fake" | |
result_text = f"The model predicts this image is '{result_label}' with {max(real_confidence, fake_confidence):.2f}% confidence." | |
explanation = f"Real Confidence: {real_confidence:.2f}% | Fake Confidence: {fake_confidence:.2f}%" | |
return result_text, explanation | |
# Step 5: Define the Gradio interface | |
interface = gr.Interface( | |
fn=predict_image, | |
inputs=gr.Image(type="pil", label="Upload an Image"), | |
outputs=[ | |
gr.Textbox(label="Prediction Result"), | |
gr.Textbox(label="Confidence Scores") | |
], | |
title="Deepfake Image Detector", | |
description="Upload an image, and the model will classify whether it is a 'real' or 'fake' image using deep learning." | |
) | |
# Step 6: Launch the app | |
if __name__ == "__main__": | |
interface.launch() | |