zahrakh98 commited on
Commit
770d0cd
·
1 Parent(s): 1476b0c

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +77 -0
app.py ADDED
@@ -0,0 +1,77 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import modal
2
+ from PIL import Image
3
+
4
+ MODEL_VERSION = 1
5
+ LOCAL = False
6
+
7
+ if LOCAL == False:
8
+ hopsworks_image = modal.Image.debian_slim(python_version='3.9').pip_install(["gradio", "requests", "hopsworks", "joblib", "pandas", "scikit-learn==1.1.1"])
9
+ stub = modal.Stub("wine_prediction_user_interface")
10
+ @stub.function(image=hopsworks_image, secret=modal.Secret.from_name("HOPSWORKS_API_KEY"))
11
+ def f():
12
+ g()
13
+
14
+ def g():
15
+ import gradio as gr
16
+ import requests
17
+ import hopsworks
18
+ import joblib
19
+ import pandas as pd
20
+
21
+ project = hopsworks.login()
22
+ fs = project.get_feature_store()
23
+
24
+ mr = project.get_model_registry()
25
+ model = mr.get_model("wine_model", version=MODEL_VERSION)
26
+ model_dir = model.download()
27
+ model = joblib.load(model_dir + "/wine_model.pkl")
28
+ print("Model downloaded")
29
+
30
+ def wine(fixed_acidity, volatile_acidity, citric_acid, residual_sugar, chlorides, free_sulfur_dioxide,
31
+ total_sulfar_dioxide, density, ph, sulphates, alcohol, color):
32
+ print("Calling function")
33
+ # df = pd.DataFrame([[sepal_length],[sepal_width],[petal_length],[petal_width]],
34
+ df = pd.DataFrame([[fixed_acidity, volatile_acidity, citric_acid, residual_sugar, chlorides, free_sulfur_dioxide,
35
+ total_sulfar_dioxide, density, ph, sulphates, alcohol, color]],
36
+ columns=['fixed_acidity', 'volatile_acidity', 'citric_aicd', 'residual_sugar', 'chlorides', 'free_sulfur_dioxide',
37
+ 'total_sulfur_dioxide', 'density', 'ph', 'sulphates', 'alcohol', 'color'])
38
+ print("Predicting")
39
+ print(df)
40
+ # 'res' is a list of predictions returned as the label.
41
+ res = model.predict(df)
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
+ # print("Res: {0}").format(res)
45
+ print(res)
46
+ return res
47
+
48
+ demo = gr.Interface(
49
+ fn=wine,
50
+ title="Wine Quality Predictive Analytics",
51
+ description="Experiment with several parameters to predict the quality of wine.",
52
+ allow_flagging="never",
53
+ inputs=[
54
+ gr.Number(value=1.0, label="fixed_acidity"),
55
+ gr.Number(value=1.0, label="volatile_acidity"),
56
+ gr.Number(value=1.0, label="citric_aicd"),
57
+ gr.Number(value=1.0, label="residual_sugar"),
58
+ gr.Number(value=1.0, label="chlorides"),
59
+ gr.Number(value=1.0, label="free_sulfur_dioxide"),
60
+ gr.Number(value=1.0, label="total_sulfur_dioxide"),
61
+ gr.Number(value=1.0, label="density"),
62
+ gr.Number(value=1.0, label="ph"),
63
+ gr.Number(value=1.0, label="sulphates"),
64
+ gr.Number(value=1.0, label="alcohol"),
65
+ gr.Number(value=1.0, label="color (0:red, 1:white)"),
66
+ ],
67
+ outputs=gr.Number(label="predicted quality"))
68
+
69
+ demo.launch(debug=True, share=True)
70
+
71
+ if __name__ == "__main__":
72
+ if LOCAL == True :
73
+ g()
74
+ else:
75
+ modal.runner.deploy_stub(stub)
76
+ with stub.run():
77
+ f.remote()