File size: 3,234 Bytes
d8c6f85
 
644820b
d8c6f85
 
5171dd5
d8c6f85
 
d462fc5
d8c6f85
644820b
d8c6f85
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9077c63
 
 
 
 
 
 
 
 
 
 
d8c6f85
 
 
 
4400787
d8c6f85
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import modal
from PIL import Image
import gradio as gr

MODEL_VERSION = 6
LOCAL = True

if LOCAL == False:
   hopsworks_image = modal.Image.debian_slim(python_version='3.9').pip_install(["gradio", "requests", "hopsworks", "joblib", "pandas", "scikit-learn==1.1.1"])
   stub = modal.Stub("wine_prediction_user_interface")
   @stub.function(image=hopsworks_image, secret=modal.Secret.from_name("HOPSWORKS_API_KEY"))
   def f():
       g()

def g():
    import requests
    import hopsworks
    import joblib
    import pandas as pd

    project = hopsworks.login()
    fs = project.get_feature_store()

    mr = project.get_model_registry()
    model = mr.get_model("wine_model", version=MODEL_VERSION)
    model_dir = model.download()
    model = joblib.load(model_dir + "/wine_model.pkl")
    print("Model downloaded")

    def wine(fixed_acidity, volatile_acidity, citric_acid, residual_sugar, chlorides, free_sulfur_dioxide,
            total_sulfar_dioxide, density, ph, sulphates, alcohol, color):
        print("Calling function")
    #     df = pd.DataFrame([[sepal_length],[sepal_width],[petal_length],[petal_width]], 
        df = pd.DataFrame([[fixed_acidity, volatile_acidity, citric_acid, residual_sugar, chlorides, free_sulfur_dioxide,
                            total_sulfar_dioxide, density, ph, sulphates, alcohol, color]], 
                        columns=['fixed_acidity', 'volatile_acidity', 'citric_aicd', 'residual_sugar', 'chlorides', 'free_sulfur_dioxide',
                            'total_sulfur_dioxide', 'density', 'ph', 'sulphates', 'alcohol', 'color'])
        print("Predicting")
        print(df)
        # 'res' is a list of predictions returned as the label.
        res = model.predict(df)
        # We add '[0]' to the result of the transformed 'res', because 'res' is a list, and we only want 
        # the first element.
    #     print("Res: {0}").format(res)
        print(res)
        return res
            
    demo = gr.Interface(
        fn=wine,
        title="Wine Quality Predictive Analytics",
        description="Experiment with several parameters to predict the quality of wine.",
        allow_flagging="never",
        inputs=[
            gr.Number(value=1.0, label="fixed_acidity [3.8,15.9]"),
            gr.Number(value=1.0, label="volatile_acidity [0.1,1.6]"),
            gr.Number(value=1.0, label="citric_aicd [0.0,1.6]"),
            gr.Number(value=1.0, label="residual_sugar [0.6,65.8]"),
            gr.Number(value=1.0, label="chlorides [0.0,0.6]"),
            gr.Number(value=1.0, label="free_sulfur_dioxide [1.0,289.0]"),
            gr.Number(value=1.0, label="total_sulfur_dioxide [6.0,440.0]"),
            gr.Number(value=1.0, label="density [0.9,1.0]"),
            gr.Number(value=1.0, label="ph [2.7,4.0]"),
            gr.Number(value=1.0, label="sulphates [0.2,2.0]"),
            gr.Number(value=1.0, label="alcohol [8.0,14.9]"),
            gr.Number(value=1.0, label="color (0:red, 1:white)"),
            ],
        outputs=gr.Number(label="predicted quality"))

    demo.launch(debug=True)

if __name__ == "__main__":
    if LOCAL == True :
        g()
    else:
        modal.runner.deploy_stub(stub)
        with stub.run():
            f.remote()