File size: 1,821 Bytes
70e7571
 
 
 
71e8e9b
 
 
b113f1f
 
 
70e7571
 
534868b
 
70e7571
 
 
71e8e9b
70e7571
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
53
54
55
import gradio as gr
import tensorflow as tf
from tensorflow.keras.preprocessing import image
import numpy as np
from huggingface_hub import hf_hub_download
import os

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 the models
model1 = load_model_from_hub("arsath-sm/face_classification_model1", "face_classification_model1.h5")
model2 = load_model_from_hub("arsath-sm/face_classification_model2", "face_classification_model2.h5")

# Preprocess the image
def preprocess_image(img):
    img = img.resize((224, 224))  # Adjust size as needed
    img = image.img_to_array(img)
    img = np.expand_dims(img, axis=0)
    img = img / 255.0
    return img

# Make predictions
def predict(img):
    preprocessed_img = preprocess_image(img)
    
    prediction1 = model1.predict(preprocessed_img)[0][0]
    prediction2 = model2.predict(preprocessed_img)[0][0]
    
    result1 = "Real" if prediction1 > 0.5 else "Fake"
    result2 = "Real" if prediction2 > 0.5 else "Fake"
    
    confidence1 = prediction1 if result1 == "Real" else 1 - prediction1
    confidence2 = prediction2 if result2 == "Real" else 1 - prediction2
    
    return {
        "Model 1 Prediction": f"{result1} (Confidence: {confidence1:.2f})",
        "Model 2 Prediction": f"{result2} (Confidence: {confidence2:.2f})"
    }

# Create the Gradio interface
iface = gr.Interface(
    fn=predict,
    inputs=gr.Image(type="pil"),
    outputs={
        "Model 1 Prediction": gr.Textbox(),
        "Model 2 Prediction": gr.Textbox()
    },
    title="Real vs AI Face Classification",
    description="Upload an image to classify whether it's a real face or an AI-generated face using two different models."
)

# Launch the app
iface.launch()