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()