Spaces:
Runtime error
Runtime error
File size: 3,298 Bytes
e0ba984 effff23 45450a9 3579fac e0ba984 ad65e12 e0ba984 5558cd0 e0ba984 5558cd0 e0ba984 5558cd0 e0ba984 5558cd0 e0ba984 5558cd0 e0ba984 5558cd0 e0ba984 97847d3 e0ba984 97847d3 e0ba984 97847d3 e0ba984 97847d3 e0ba984 97847d3 e0ba984 97847d3 e0ba984 97847d3 e0ba984 97847d3 e0ba984 97847d3 e0ba984 2312791 e0ba984 8862399 e0ba984 3d24d8f e0ba984 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
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() |