import gradio as gr from keras.models import model_from_json from tensorflow.keras.preprocessing import image import numpy as np def predict_age_gender(image): json_file = open('xception.json', 'r') loaded_file_json = json_file.read() json_file.close() model = model_from_json(loaded_file_json) model.load_weights('xception_modelv2.h5') images = np.resize(image, new_shape=(1, 128, 128, 3)) img_pixels = np.expand_dims(images, axis=0) img_pixels = images.astype('float') img_pixels = img_pixels.reshape((1, 128, 128, 3)) img_pixels /= 255 predict = model.predict(img_pixels) gender_predict = predict[0] age_predict = predict[1] return {'Gender': ['Fmale' if gender_predict > 0.5 else 'Male'], 'Age': age_predict[0][0]} iface = gr.Interface(predict_age_gender, gr.Image(), gr.Text()) iface.launch()