titanic / app.py
Mei000's picture
Upload 2 files
1ef2bd5
# -*- 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()