carlpersson commited on
Commit
51fa3be
·
1 Parent(s): 5f0fc77

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -46
app.py CHANGED
@@ -1,67 +1,48 @@
1
  import gradio as gr
 
 
2
  import hopsworks
 
3
  import pandas as pd
4
- import keras
5
 
6
  project = hopsworks.login()
7
  fs = project.get_feature_store()
8
 
 
9
  mr = project.get_model_registry()
10
- model = mr.get_model("wine_model", version=1)
11
  model_dir = model.download()
12
-
13
- n_input = 7 # Number of features
14
- n_hidden = 256 # Number of hidden nodes
15
- n_out = 3 # Number of classes
16
- model = keras.Sequential([
17
- keras.layers.Dense(n_hidden, input_dim=n_input, activation='selu'),
18
- keras.layers.BatchNormalization(),
19
- keras.layers.Dense(n_hidden, activation='selu'),
20
- keras.layers.BatchNormalization(),
21
- keras.layers.Dense(n_hidden, activation='selu'),
22
- keras.layers.BatchNormalization(),
23
- keras.layers.Dense(n_hidden, activation='selu'),
24
- keras.layers.BatchNormalization(),
25
- keras.layers.Dense(n_hidden, activation='selu'),
26
- keras.layers.Dense(n_out, activation='softmax')
27
- ])
28
-
29
- model.load_weights(model_dir + '/wine_model.h5')
30
  print("Model downloaded")
31
 
32
-
33
- def wine(type, alcohol, density, sugar, vol_acid, chlorides, total_sulfur):
34
  print("Calling function")
35
- type = 0 if type == 'White' else 1
36
-
37
  # df = pd.DataFrame([[sepal_length],[sepal_width],[petal_length],[petal_width]],
38
- df = pd.DataFrame([[type, vol_acid, sugar, chlorides, total_sulfur, density, alcohol]],
39
- columns=['type', 'volatile acidity', 'sugar', 'chlorides', 'total sulfur dioxide', '´density', 'alcohol'])
40
  print("Predicting")
 
41
  # 'res' is a list of predictions returned as the label.
42
- wine_prediction = model.predict(df)
43
- wine_prediction = {'High Quality': float(wine_prediction[0][2]),
44
- 'Good Quality': float(wine_prediction[0][1]),
45
- 'Low Quality': float(wine_prediction[0][0])}
46
- return wine_prediction
47
-
48
-
 
 
49
  demo = gr.Interface(
50
- fn=wine,
51
- title="Wine Quality Analytics",
52
- description="Experiment with wine contents to predict what quality of wine it is.",
53
  allow_flagging="never",
54
  inputs=[
55
- gr.Radio(["White", "Red"], value='White', label="What kind of wine is it?"),
56
- gr.Number(value=9.6, label="Alcohol content (%), normal range 8-15 %"),
57
- gr.Number(value=0.9935, label="Density (kg/dm^3), normal range 0.99-0.101 kg/dm^3"),
58
- gr.Number(value=1.1, label="Residual sugar content (g/L), normal range 0.6-66 g/L"),
59
- gr.Number(value=0.26, label="Volatile acid content (g/L), normal range 0.1 - 1.6 g/L"),
60
- gr.Number(value=0.04, label="Chloride content (g/L), normal range 0.01-0.6 g/L"),
61
- gr.Number(value=147, label="Total sulfur dioxide content (ppm), normal range 6-450 ppm"),
62
  ],
63
- outputs=gr.Label(num_top_classes=3))
64
 
65
  demo.launch(debug=True)
66
-
67
-
 
1
  import gradio as gr
2
+ from PIL import Image
3
+ import requests
4
  import hopsworks
5
+ import joblib
6
  import pandas as pd
 
7
 
8
  project = hopsworks.login()
9
  fs = project.get_feature_store()
10
 
11
+
12
  mr = project.get_model_registry()
13
+ model = mr.get_model("iris_model", version=1)
14
  model_dir = model.download()
15
+ model = joblib.load(model_dir + "/iris_model.pkl")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16
  print("Model downloaded")
17
 
18
+ def iris(sepal_length, sepal_width, petal_length, petal_width):
 
19
  print("Calling function")
 
 
20
  # df = pd.DataFrame([[sepal_length],[sepal_width],[petal_length],[petal_width]],
21
+ df = pd.DataFrame([[sepal_length,sepal_width,petal_length,petal_width]],
22
+ columns=['sepal_length','sepal_width','petal_length','petal_width'])
23
  print("Predicting")
24
+ print(df)
25
  # 'res' is a list of predictions returned as the label.
26
+ res = model.predict(df)
27
+ # We add '[0]' to the result of the transformed 'res', because 'res' is a list, and we only want
28
+ # the first element.
29
+ # print("Res: {0}").format(res)
30
+ print(res)
31
+ flower_url = "https://raw.githubusercontent.com/featurestoreorg/serverless-ml-course/main/src/01-module/assets/" + res[0] + ".png"
32
+ img = Image.open(requests.get(flower_url, stream=True).raw)
33
+ return img
34
+
35
  demo = gr.Interface(
36
+ fn=iris,
37
+ title="Iris Flower Predictive Analytics",
38
+ description="Experiment with sepal/petal lengths/widths to predict which flower it is.",
39
  allow_flagging="never",
40
  inputs=[
41
+ gr.inputs.Number(default=2.0, label="sepal length (cm)"),
42
+ gr.inputs.Number(default=1.0, label="sepal width (cm)"),
43
+ gr.inputs.Number(default=2.0, label="petal length (cm)"),
44
+ gr.inputs.Number(default=1.0, label="petal width (cm)"),
 
 
 
45
  ],
46
+ outputs=gr.Image(type="pil"))
47
 
48
  demo.launch(debug=True)