Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,69 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import numpy as np
|
3 |
+
from PIL import Image
|
4 |
+
import requests
|
5 |
+
|
6 |
+
import hopsworks
|
7 |
+
import joblib
|
8 |
+
|
9 |
+
project = hopsworks.login()
|
10 |
+
fs = project.get_feature_store()
|
11 |
+
|
12 |
+
|
13 |
+
mr = project.get_model_registry()
|
14 |
+
model = mr.get_model("titanic_modal", version=1)
|
15 |
+
model_dir = model.download()
|
16 |
+
model = joblib.load(model_dir + "/titanic_model.pkl")
|
17 |
+
|
18 |
+
CLASS_TO_VALUE = {
|
19 |
+
"1st class": "1",
|
20 |
+
"2nd class": "2",
|
21 |
+
"3rd class": "3",
|
22 |
+
}
|
23 |
+
|
24 |
+
PORT_TO_VALUE = {
|
25 |
+
"Cherbourg": "C",
|
26 |
+
"Queenstown": "Q",
|
27 |
+
"Southampton": "S",
|
28 |
+
}
|
29 |
+
|
30 |
+
|
31 |
+
def titanic(ticket_class, sex, port, fare, age, sibsp, parch):
|
32 |
+
input_list = []
|
33 |
+
input_list.append(CLASS_TO_VALUE[ticket_class])
|
34 |
+
input_list.append(sex)
|
35 |
+
input_list.append(PORT_TO_VALUE[port])
|
36 |
+
input_list.append(fare)
|
37 |
+
input_list.append(age)
|
38 |
+
input_list.append(sibsp)
|
39 |
+
input_list.append(parch)
|
40 |
+
# 'res' is a list of predictions returned as the label.
|
41 |
+
res = model.predict(np.asarray(input_list).reshape(1, -1))
|
42 |
+
# We add '[0]' to the result of the transformed 'res', because 'res' is a list, and we only want
|
43 |
+
# the first element.
|
44 |
+
|
45 |
+
if res:
|
46 |
+
url = "https://m.media-amazon.com/images/I/71M6k7ZQNcL._RI_.jpg"
|
47 |
+
else:
|
48 |
+
url = "https://thumbs.dreamstime.com/b/allvarlig-sten-med-skallen-34707626.jpg"
|
49 |
+
|
50 |
+
img = Image.open(requests.get(url, stream=True).raw)
|
51 |
+
return img
|
52 |
+
|
53 |
+
demo = gr.Interface(
|
54 |
+
fn=titanic,
|
55 |
+
title="Titanic survival prediction",
|
56 |
+
description="Experiment with parameters to predict if the fictional passenger survived",
|
57 |
+
allow_flagging="never",
|
58 |
+
inputs=[
|
59 |
+
gr.inputs.Dropdown(["1st class", "2nd class", "3rd class"], value="1", label="Ticket class"),
|
60 |
+
gr.inputs.Dropdown(["female", "male"], label="Sex"),
|
61 |
+
gr.inputs.Dropdown(["Cherbourg", "Queenstown", "Southampton"], label="Port of Embarkation"),
|
62 |
+
gr.inputs.Number(default=50.0, label="Fare"),
|
63 |
+
gr.inputs.Number(default=20.0, label="Age"),
|
64 |
+
gr.inputs.Number(default=0, precision=0, label="Number of siblings/spouses aboard the Titanic"),
|
65 |
+
gr.inputs.Number(default=0, precision=0, label="Number of parents/children aboard the Titanic"),
|
66 |
+
],
|
67 |
+
outputs=gr.Image(type="pil"))
|
68 |
+
|
69 |
+
demo.launch()
|