arsath-sm's picture
Update app.py
eb5982c verified
import gradio as gr
import tensorflow as tf
import numpy as np
from huggingface_hub import hf_hub_download
# Function to load model from H5 file
def load_model_from_hub(repo_id, filename):
model_path = hf_hub_download(repo_id=repo_id, filename=filename)
return tf.keras.models.load_model(model_path)
# Load models from Hugging Face Hub
model1 = load_model_from_hub("arsath-sm/real-fake-face-detection-model1", "face_detection_model1.h5")
model2 = load_model_from_hub("arsath-sm/real-fake-face-detection-model2", "face_detection_model2.h5")
def preprocess_image(image):
img = tf.convert_to_tensor(image)
img = tf.image.resize(img, (150, 150))
img = img / 255.0
return tf.expand_dims(img, 0)
def predict_image(image):
preprocessed_image = preprocess_image(image)
# Make predictions using both models
pred1 = model1.predict(preprocessed_image)[0][0]
pred2 = model2.predict(preprocessed_image)[0][0]
# Prepare results for each model
result1 = "Real" if pred1 > 0.5 else "Fake"
confidence1 = pred1 if pred1 > 0.5 else 1 - pred1
result2 = "Real" if pred2 > 0.5 else "Fake"
confidence2 = pred2 if pred2 > 0.5 else 1 - pred2
return (
f"Model 1: {result1} (Confidence: {confidence1:.2f})",
f"Model 2: {result2} (Confidence: {confidence2:.2f})"
)
iface = gr.Interface(
fn=predict_image,
inputs=gr.Image(),
outputs=[
gr.Textbox(label="Model 1 Prediction"),
gr.Textbox(label="Model 2 Prediction")
],
title="Real vs Fake Face Detection",
description="Upload an image to determine if it's a real or fake face using two different models."
)
iface.launch()