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_model", version=6) model_dir = model.download() model = joblib.load(model_dir + "/titanic_modal.pkl") def titanic(pclass, sex, age, fare, embarked, familysize, appellation, cabin): input_list = [] # PClass input_list.append(int(pclass)) # Gender if sex == "Male": input_list.append(0) else: input_list.append(1) # Age input_list.append(age) # Fare input_list.append(fare) # Embarked if embarked == "S": input_list.append(0) elif embarked == "C": input_list.append(1) elif embarked == "Q": input_list.append(2) # Family Size input_list.append(familysize) # Appellation if appellation == "master": input_list.extend([1,0,0,0,0,0]) elif appellation == "miss": input_list.extend([0,1,0,0,0,0]) elif appellation == "mr": input_list.extend([0,0,1,0,0,0]) elif appellation == "mrs": input_list.extend([0,0,0,1,0,0]) elif appellation == "officer": input_list.extend([0,0,0,0,1,0]) elif appellation == "royalty": input_list.extend([0,0,0,0,0,1]) # Cabin if cabin == "A": input_list.extend([1,0,0,0,0,0,0,0,0]) elif cabin == "B": input_list.extend([0,1,0,0,0,0,0,0,0]) elif cabin == "C": input_list.extend([0,0,1,0,0,0,0,0,0]) elif cabin == "D": input_list.extend([0,0,0,1,0,0,0,0,0]) elif cabin == "E": input_list.extend([0,0,0,0,1,0,0,0,0]) elif cabin == "F": input_list.extend([0,0,0,0,0,1,0,0,0]) elif cabin == "G": input_list.extend([0,0,0,0,0,0,1,0,0]) elif cabin == "T": input_list.extend([0,0,0,0,0,0,0,1,0]) else: input_list.extend([0,0,0,0,0,0,0,0,1]) # 'res' is a list of predictions returned as the label. res = model.predict(np.asarray(input_list).reshape(1, -1)) res = res.astype(int) # We add '[0]' to the result of the transformed 'res', because 'res' is a list, and we only want # the first element. titanic_url = "https://github.com/Qinglin2000/ID2223/blob/main/" + str(res[0]) + ".png?raw=true" img = Image.open(requests.get(titanic_url, stream=True).raw) return img demo = gr.Interface( fn=titanic, title="Titanic Predictive Analytics", description="Experiment with titanic dataset values.", allow_flagging="never", inputs=[ gr.Dropdown(choices=["1", "2", "3"], label="PClass", value="1"), gr.Radio(choices=["Male", "Female"], label="Gender", value="Male"), gr.inputs.Number(default=30.0, label="Age"), gr.inputs.Number(default=40.99, label="Fare"), gr.Dropdown(choices=["S","C","Q"], label="Embarked", value="S"), gr.Number(label="Family Size", precision=0, value=1), gr.Dropdown(choices=["master", "miss", "mr", "mrs", "officer", "royalty"], label="Appellation", value="master"), gr.Dropdown(choices=["A", "B", "C", "D", "E", "F", "G", "T", "U"], label="Cabin", value="A"), ], outputs=gr.Image(type="pil")) demo.launch()