import gradio as gr import numpy as np from PIL import Image import requests import hopsworks import joblib project = hopsworks.login() fs = project.get_feature_store() mr = project.get_model_registry() model = mr.get_model("titanic_modal", version=1) model_dir = model.download() model = joblib.load(model_dir + "/titanic_model.pkl") def titanic(pclass, sex, age, fare): input_list = [] bins = [-np.infty, 20, 25, 29, 30, 40, np.infty] input_list.append(int(np.digitize([age], bins)[0])) input_list.append(int(sex)) input_list.append(int(pclass + 1)) input_list.append(fare) # 'res' is a list of predictions returned as the label. res = model.predict(np.asarray(input_list).reshape(1, -1)) # We add '[0]' to the result of the transformed 'res', because 'res' is a list, and we only want # the first element. # print('The result we get :: ', str(res[0])) passenger_survival_url = "https://raw.githubusercontent.com/abdullabdull/id2223-images/main/" + str(res[0]) + ".png" img = Image.open(requests.get(passenger_survival_url, stream=True).raw) return img demo = gr.Interface( fn=titanic, title="Titanic Survival Predictive Analytics", description="Experiment with different passenger features to predict if they survived or not.", allow_flagging="never", inputs=[ gr.inputs.Dropdown(choices=["Class 1", "Class 2", "Class 3"], type="index", label="Pclass"), gr.inputs.Dropdown(choices=["Male", "Female"], type="index", label="Sex"), gr.inputs.Number(default=1, label="Age"), gr.inputs.Slider(minimum=0, maximum=550, default=50, label="Fare"), ], outputs=gr.Image(type="pil")) demo.launch()