FredBonux commited on
Commit
4942701
1 Parent(s): 79ab211

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -61
app.py CHANGED
@@ -1,68 +1,36 @@
1
  import gradio as gr
2
  import hopsworks
3
- import joblib
4
- import pandas as pd
5
 
6
- features = ['fixed_acidity',
7
- 'volatile_acidity',
8
- 'citric_acid',
9
- 'residual_sugar',
10
- 'chlorides',
11
- 'free_sulfur_dioxide',
12
- 'total_sulfur_dioxide',
13
- 'density',
14
- 'pH',
15
- 'sulphates',
16
- 'alcohol',
17
- 'is_white']
18
- labels = ["Low", "Medium", "High"]
19
 
20
  project = hopsworks.login()
21
  fs = project.get_feature_store()
22
 
23
- mr = project.get_model_registry()
24
- model = mr.get_model("wine_model", version=1)
25
- model_dir = model.download()
26
- model = joblib.load(model_dir + "/wine_model.pkl")
27
- print("Model downloaded")
28
-
29
-
30
- def wine(fixed_acidity, volatile_acidity, citric_acid, residual_sugar, chlorides, free_sulfur_dioxide,
31
- total_sulfur_dioxide, density, pH, sulphates, alcohol, white) -> str:
32
- print("Calling function")
33
- df = pd.DataFrame([[fixed_acidity, volatile_acidity, citric_acid, residual_sugar, chlorides, free_sulfur_dioxide,
34
- total_sulfur_dioxide, density, pH, sulphates, alcohol, white]], columns=features)
35
- print("Predicting")
36
- print(df)
37
- # 'res' is a list of predictions returned as the label.
38
- res = model.predict(df)
39
- # We add '[0]' to the result of the transformed 'res', because 'res' is a list, and we only want
40
- # the first element.
41
- # print("Res: {0}").format(res)
42
- print(res)
43
-
44
- return f"{labels[res[0]]} quality"
45
-
46
-
47
- demo = gr.Interface(
48
- fn=wine,
49
- title="Wine Quality Predictive Analytics",
50
- description="Experiment with wine characteristics to get the wine quality (low, medium, high)",
51
- allow_flagging="never",
52
- inputs=[
53
- gr.components.Number(label='fixed acidity'),
54
- gr.components.Number(label='volatile acidity'),
55
- gr.components.Number(label='citric acid'),
56
- gr.components.Number(label='residual sugar'),
57
- gr.components.Number(label='chlorides'),
58
- gr.components.Number(label='free sulfur dioxide'),
59
- gr.components.Number(label='total sulfur dioxide'),
60
- gr.components.Number(label='density'),
61
- gr.components.Number(label='pH'),
62
- gr.components.Number(label='sulphates'),
63
- gr.components.Number(label='alcohol'),
64
- gr.components.Checkbox(label='is white'),
65
- ],
66
- outputs=gr.Text())
67
-
68
- demo.launch(debug=True)
 
1
  import gradio as gr
2
  import hopsworks
 
 
3
 
4
+ labels = ['Low', 'Medium', 'High']
 
 
 
 
 
 
 
 
 
 
 
 
5
 
6
  project = hopsworks.login()
7
  fs = project.get_feature_store()
8
 
9
+ dataset_api = project.get_dataset_api()
10
+ dataset_api.download("Resources/images/wine_df_recent.png")
11
+ dataset_api.download("Resources/images/wine_confusion_matrix.png")
12
+
13
+ monitor_fg = fs.get_or_create_feature_group(name="wine_predictions", version=1, primary_key=["datetime"],
14
+ description="Wine quality Prediction/Outcome Monitoring")
15
+
16
+ history_df = monitor_fg.read()
17
+ last_prediction = history_df.tail()
18
+ last_prediction = last_prediction[0] if len(last_prediction) > 0 else None
19
+
20
+ with gr.Blocks() as demo:
21
+ with gr.Row():
22
+ with gr.Column():
23
+ gr.Label("Today's Predicted")
24
+ gr.Label(f"{labels[last_prediction['prediction']] + ' quality' if last_prediction is not None else 'No predictions yet'}")
25
+ with gr.Column():
26
+ gr.Label("Today's Actual quality")
27
+ gr.Label(f"{labels[last_prediction['label']] + ' quality' if last_prediction is not None else 'No predictions yet'}")
28
+ with gr.Row():
29
+ with gr.Column():
30
+ gr.Label("Recent Prediction History")
31
+ gr.Image("wine_df_recent.png", elem_id="recent-predictions")
32
+ with gr.Column():
33
+ gr.Label("Confusion Maxtrix with Historical Prediction Performance")
34
+ gr.Image("wine_confusion_matrix.png", elem_id="confusion-matrix")
35
+
36
+ demo.launch()