|
|
|
""" |
|
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 = [] |
|
|
|
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]) |
|
|
|
|
|
if sex == "Male": |
|
input_list.append(0) |
|
else: |
|
input_list.append(1) |
|
|
|
|
|
|
|
|
|
|
|
input_list.append(fare) |
|
|
|
|
|
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]) |
|
|
|
|
|
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]) |
|
|
|
|
|
|
|
|
|
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]) |
|
|
|
|
|
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 = model.predict(np.asarray(input_list).reshape(1, -1)) |
|
res = res.astype(int) |
|
|
|
|
|
titanic_url = "https://raw.githubusercontent.com/M75583/Machinelearning/main/" + str(res[0]) + ".png" |
|
|
|
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=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() |
|
|
|
|
|
|