import gradio as gr import numpy as np from PIL import Image import requests from feature_engineering import feat_eng import hopsworks import joblib import pandas as pd project = hopsworks.login() fs = project.get_feature_store() mr = project.get_model_registry() model = mr.get_model("titanic_modal_simple_classifier", version=1) model_dir = model.download() model = joblib.load(model_dir + "/titanic_model.pkl") leo_url = "https://media.tenor.com/FghTtX3ZgbAAAAAC/drowning-leo.gif" rose_url = "https://media4.giphy.com/media/6A5zBPtbknIGY/giphy.gif?cid=ecf05e477syp5zeoheii45de76uicvgu0nuegojslz3zgodt&rid=giphy.gif&ct=g" def titanic(pclass, name, sex, age, sibsp, parch, ticket, fare, cabin, embarked): df_pre = pd.DataFrame({"PassengerId":[-1], "Pclass": [pclass], "Name": [name], "Sex": [sex], "Age": [age], "SibSp": [sibsp], "Parch": [parch], "Ticket": [ticket], "Fare": [fare], "Cabin": [cabin], "Embarked": [embarked]}) df_post = feat_eng(df_pre) # 'res' is a list of predictions returned as the label. res = model.predict(df_post)[0] # We add '[0]' to the result of the transformed 'res', because 'res' is a list, and we only want # the first element. img = Image.open(leo_url) if res == 0 else Image.open(rose_url) return img demo = gr.Interface( fn=titanic, title="Titanic Survival Predictive Analytics", description="Experiment with Titanic Passenger data to predict survival", allow_flagging="never", inputs=[ gr.inputs.Number(default=1.0, label="pclass, [1,2,3]"), gr.inputs.Textbox(default="Anton", label="name"), gr.inputs.Textbox(default="male", label="sex, male or female"), gr.inputs.Number(default=25, label="age"), gr.inputs.Number(default=2, label="sibsb"), gr.inputs.Number(default=2, label="parch"), gr.inputs.Textbox(default="blabla", label="Ticket"), gr.inputs.Number(default=200, label="Fare"), gr.inputs.Textbox(default="blabla", label="Cabin"), gr.inputs.Textbox(default="blabla", label="Embarked: [S, C, Q]") ], outputs=gr.Image(type="pil")) demo.launch()