File size: 4,004 Bytes
1ef2bd5 |
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 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
# -*- coding: utf-8 -*-
"""
Created on Wed Nov 23 06:07:56 2022
@author: limei
"""
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=6)
model_dir = model.download()
model = joblib.load(model_dir + "/titanic_model.pkl")
def titanic(pclass, sex, fare, embarked, familysize, family, appellation, cabin):
input_list = []
# PClass
if pclass == "1":
input_list.extend([1,0,0])
elif pclass == "2":
input_list.extend([0,1,0])
elif pclass == "3":
input_list.extend([0,0,1])
# 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.extend([1,0,0])
elif embarked == "C":
input_list.extend([0,1,0])
elif embarked == "Q":
input_list.extend([0,0,1])
# Family Size
input_list.append(familysize)
if family == "Family_Single":
input_list.extend([1,0,0])
elif family == "Family_Small":
input_list.extend([0,1,0])
elif family == "Family_Large":
input_list.extend([0,0,1])
# 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://raw.githubusercontent.com/M75583/Machinelearning/main/" + str(res[0]) + ".png"
#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=["Family_Single","Family_Small","Family_Large"], label="Family", value="Family_Single"),
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()
|