File size: 1,138 Bytes
303795f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import numpy as np
from tensorflow.keras.models import load_model
from tensorflow.keras.preprocessing.image import img_to_array
from PIL import Image

# Load the trained model
model = load_model("ai_vs_human_image.h5")

# Preprocessing function
def preprocess_image(img):
    img = img.resize((64, 64))  # Resize to model's expected input
    img = img_to_array(img)
    img = img / 255.0  # Normalize
    img = np.expand_dims(img, axis=0)  # Add batch dimension
    return img

# Prediction function
def predict(image):
    processed = preprocess_image(image)
    prediction = model.predict(processed)[0][0]
    label = "AI-Generated Image 🧠" if prediction >= 0.5 else "Human-Generated Image 👤"
    confidence = prediction if prediction >= 0.5 else 1 - prediction
    return f"{label} (Confidence: {confidence:.2%})"

# Gradio interface
interface = gr.Interface(
    fn=predict,
    inputs=gr.Image(type="pil"),
    outputs="text",
    title="AI vs Human Image Classifier",
    description="Upload an image and find out whether it was AI-generated or created by a human."
)

# Launch the app
interface.launch()