Spaces:
Runtime error
Runtime error
Initial commit
Browse files- app.py +90 -0
- requirements.txt +8 -0
app.py
ADDED
@@ -0,0 +1,90 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import numpy as np
|
3 |
+
from PIL import Image
|
4 |
+
import requests
|
5 |
+
import pandas as pd
|
6 |
+
import hopsworks
|
7 |
+
import joblib
|
8 |
+
|
9 |
+
#connect to hopsworks
|
10 |
+
project = hopsworks.login(project="test42")
|
11 |
+
fs = project.get_feature_store()
|
12 |
+
|
13 |
+
#get model
|
14 |
+
mr = project.get_model_registry()#connect to model registry
|
15 |
+
model = mr.get_model("titanic_model_modal", version=1) #retrieve model from hopsworks
|
16 |
+
model_dir = model.download() #download model to cur dir
|
17 |
+
model = joblib.load(model_dir + "/titanic_model.pkl") #load model from cur dir
|
18 |
+
|
19 |
+
|
20 |
+
def passenger(pclass,#index
|
21 |
+
age,#float
|
22 |
+
sibsp,#float
|
23 |
+
parch,#float
|
24 |
+
fare,#float
|
25 |
+
sex,#index 0-male,1-female
|
26 |
+
deck,# index abcdefgnt
|
27 |
+
embarked# index cnqs
|
28 |
+
):
|
29 |
+
deck_all="abcdefgnt"
|
30 |
+
embarked_all="cnqs"
|
31 |
+
deck_count=[0 for i in deck_all]
|
32 |
+
deck_count[deck]=1
|
33 |
+
embarked_count=[0 for i in embarked_all]
|
34 |
+
embarked_count[embarked]=1
|
35 |
+
|
36 |
+
input_df = pd.DataFrame({"pclass":[pclass+1],
|
37 |
+
"age":[age],
|
38 |
+
"sibsp":[sibsp],
|
39 |
+
"parch":[parch],
|
40 |
+
"fare":[fare],
|
41 |
+
"sex_female":[sex],
|
42 |
+
"sex_male":[1-sex],
|
43 |
+
"deck_a":deck_count[0],
|
44 |
+
"deck_b":deck_count[1],
|
45 |
+
"deck_c":deck_count[2],
|
46 |
+
"deck_d":deck_count[3],
|
47 |
+
"deck_e":deck_count[4],
|
48 |
+
"deck_f":deck_count[5],
|
49 |
+
"deck_g":deck_count[6],
|
50 |
+
"deck_n":deck_count[7],
|
51 |
+
"deck_t":deck_count[8],
|
52 |
+
"embarked_c":embarked_count[0],
|
53 |
+
"embarked_n":embarked_count[1],
|
54 |
+
"embarked_q":embarked_count[2],
|
55 |
+
"embarked_s":embarked_count[3],
|
56 |
+
})
|
57 |
+
# 'res' is a list of predictions returned as the label.
|
58 |
+
#print(input_df)
|
59 |
+
res = model.predict(input_df) #prediction from model based on input
|
60 |
+
# We add '[0]' to the result of the transformed 'res', because 'res' is a list, and we only want
|
61 |
+
# the first element.
|
62 |
+
if res==0:
|
63 |
+
url="https://i.imgflip.com/5jvc2d.jpg"
|
64 |
+
else:
|
65 |
+
url="https://encrypted-tbn3.gstatic.com/images?q=tbn:ANd9GcTv3XuQKvjiF_ZpUt8rKlsVBX--JXMpfa674H03N-aApj_HjK1S"
|
66 |
+
img = Image.open(requests.get(url, stream=True).raw) #get image from github
|
67 |
+
return img
|
68 |
+
|
69 |
+
#create hugging face interface
|
70 |
+
demo = gr.Interface(
|
71 |
+
passenger,
|
72 |
+
[
|
73 |
+
gr.Dropdown(["first", "second", "third"], type="index",label="Passenger Class"),
|
74 |
+
gr.Slider(0, 80, value=25,label="Age"),
|
75 |
+
gr.Slider(0, 10, step=1, value=0, label="Number of siblings/spouses aboard the Titanic"),
|
76 |
+
gr.Slider(0, 10, step=1, value=0, label="Number of parents/children aboard the Titanic"),
|
77 |
+
gr.Number(default=0, label="Passenger fare"),
|
78 |
+
gr.Radio(["Male","Female"],type="index",label="Sex"),
|
79 |
+
gr.Radio([f"Deck_{c}" for c in "ABCDEFGNT"],type="index",label="Deck (Select N if unknown)"),
|
80 |
+
gr.Radio([f"Embarked_{e}" for e in "CNQS"],type="index",label="Embark point (Select N if unknown)")
|
81 |
+
|
82 |
+
],
|
83 |
+
title="Titanic Survivor Predictive Analytics",
|
84 |
+
description="Who could surive the titanic",
|
85 |
+
allow_flagging="never",
|
86 |
+
outputs=gr.Image(type="pil")
|
87 |
+
)
|
88 |
+
|
89 |
+
demo.launch()
|
90 |
+
|
requirements.txt
ADDED
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
hopsworks
|
2 |
+
pandas
|
3 |
+
joblib
|
4 |
+
scikit-learn
|
5 |
+
seaborn
|
6 |
+
dataframe-image
|
7 |
+
modal-client
|
8 |
+
gradio
|