humeur's picture
Quick fix
f51246b
raw
history blame
2.03 kB
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(Sex, Age, Pclass, Fare, Parch, SibSp, Embarked):
input_list = []
input_list.append(Sex)
input_list.append(Age)
input_list.append(Pclass)
input_list.append(Fare)
input_list.append(Parch)
input_list.append(SibSp)
input_list.append(Embarked)
# '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.
pic_url = "https://raw.githubusercontent.com/backgroundhumeur/id2223_labs/main/src/titanic/assets/titanic_" + res[0] + ".jpg"
img = Image.open(requests.get(pic_url, stream=True).raw)
return img
demo = gr.Interface(
fn=titanic,
title="Titanic Passenger Survival Predictive Analytics",
description="Experiment with different characteristics of a passenger to predict whether he would have survived if he were aboard the titanic.",
allow_flagging="never",
inputs=[
gr.inputs.Dropdown(choices=["male","female"], default="male", label="Sex"),
gr.inputs.Number(default=28.0, label="Age", precision=0),
gr.inputs.Slider(minimum=1.0,maximum=3.0,default=3.0,step=1.0, label="Ticket class (1st to 3rd)"),
gr.inputs.Number(default=14.4542, label="Fare ($)"),
gr.inputs.Number(default=0.0, label="Number of parents/children aboard", precision=0),
gr.inputs.Number(default=0.0, label="Number of siblings/spouses aboard", precision=0),
gr.inputs.Dropdown(choices=["S","C", "Q"], default="C", label="Port of Embarkation")
],
outputs=gr.Image(type="pil"))
demo.launch()