import tensorflow as tf import gradio as gr import pathlib # Kiểm tra hệ điều hành plt = platform.system() if plt == 'Linux': pathlib.WindowsPath = pathlib.PosixPath # Tải mô hình từ file .h5 model = tf.keras.models.load_model('model.h5') # Các danh mục cần phân loại categories = ('dog', 'wolf') # Hàm dự đoán ảnh def classify_image(img): img = img.resize((128, 128)) # Đảm bảo kích thước ảnh đúng như mô hình yêu cầu img_array = tf.keras.utils.img_to_array(img) / 255.0 # Chuyển ảnh thành mảng và chuẩn hóa img_array = tf.expand_dims(img_array, axis=0) # Thêm batch dimension probs = model.predict(img_array)[0] # Lấy dự đoán return dict(zip(categories, map(float, probs))) # Tạo giao diện Gradio image = gr.inputs.Image(shape=(192, 192)) label = gr.outputs.Label() examples = ['dog.jpg', 'wolf.jpg', 'bird.jpg'] intf = gr.Interface(fn=classify_image, inputs=image, outputs=label, examples=examples) intf.launch(inline=False)