Spaces:
Sleeping
Sleeping
import gradio as gr | |
import tensorflow as tf | |
import numpy as np | |
from PIL import Image | |
# Carregar o modelo treinado | |
# O modelo deve estar na mesma pasta que este script | |
model = tf.keras.models.load_model('meu_modelo.h5') | |
def predict_image(img): | |
# Converter a imagem PIL para um array NumPy | |
img = np.array(img) | |
# Redimensionar a imagem para o tamanho esperado pelo modelo (224, 224) | |
img = tf.image.resize(img, (224, 224)) | |
# Pré-processamento manual para MobileNetV2: | |
# Escalar os valores de pixel para o intervalo [-1, 1] | |
img = img / 127.5 - 1 | |
# Adicionar uma dimensão de batch | |
img = np.expand_dims(img, axis=0) | |
prediction = model.predict(img) | |
# Interpretar o resultado da predição e criar um dicionário para a saída | |
if prediction < 0.5: | |
result = {"ai": float(1 - prediction[0][0]), "human": float(prediction[0][0])} | |
else: | |
result = {"human": float(prediction[0][0]), "ai": float(1 - prediction[0][0])} | |
return result | |
# Lista de exemplos | |
# As imagens de exemplo devem estar na mesma pasta que este script | |
exemplos = [ | |
'vangoghai.jpg', | |
'vangoghhuman.jpg' | |
] | |
# Criar a interface do gradio | |
image_input = gr.Image() | |
label_output = gr.Label() | |
interface = gr.Interface( | |
fn=predict_image, | |
inputs=image_input, | |
outputs=label_output, | |
examples=exemplos | |
) | |
# Lançar a interface | |
interface.launch() | |