Spaces:
Runtime error
Runtime error
| import gradio as gr | |
| from PIL import Image | |
| import requests | |
| import hopsworks | |
| import joblib | |
| import pandas as pd | |
| from sklearn.preprocessing import StandardScaler , MinMaxScaler | |
| project = hopsworks.login() | |
| fs = project.get_feature_store() | |
| mr = project.get_model_registry() | |
| model = mr.get_model("wine_model", version=5) | |
| model_dir = model.download() | |
| model = joblib.load(model_dir + "/wine_model.pkl") | |
| print("Model downloaded") | |
| def wine_quality(type, fixed_acidity, volatile_acidity, citric_acid, residual_sugar, chlorides, free_sulfur_dioxide, total_sulfur_dioxide, density, pH, sulphates, alcohol): | |
| print("Calling function") | |
| if(type == "red"): | |
| type = 0 | |
| else: | |
| type = 1 | |
| fixed_acidity = float(fixed_acidity) | |
| volatile_acidity = float(volatile_acidity) | |
| citric_acid = float(citric_acid) | |
| residual_sugar = float(residual_sugar) | |
| chlorides = float(chlorides) | |
| free_sulfur_dioxide = float(free_sulfur_dioxide) | |
| total_sulfur_dioxide = float(total_sulfur_dioxide) | |
| density = float(density) | |
| pH = float(pH) | |
| sulphates = float(sulphates) | |
| alcohol = float(alcohol) | |
| df = pd.DataFrame([[type, fixed_acidity, volatile_acidity, citric_acid, residual_sugar, chlorides, free_sulfur_dioxide, total_sulfur_dioxide, density, pH, sulphates, alcohol]], | |
| columns=['type', 'fixed_acidity', 'volatile_acidity', 'citric_acid', 'residual_sugar', 'chlorides', 'free_sulfur_dioxide', 'total_sulfur_dioxide', 'density', 'ph', 'sulphates', 'alcohol']) | |
| df['ratio_sulfur_dioxide'] = df['free_sulfur_dioxide']/df['total_sulfur_dioxide'] | |
| df['skewness'] = df.skew(axis = 1, skipna = True) | |
| df['kurtosis'] = df.kurtosis(axis = 1, skipna = True) | |
| df['mean'] = df.mean(axis = 1, skipna = True) | |
| df['median'] = df.median(axis = 1, skipna = True) | |
| df['variance'] = df.var(axis = 1, skipna = True) | |
| df['std'] = df.std(axis = 1, skipna = True) | |
| df['cv'] = df.std(axis = 1, skipna = True)/df.mean(axis = 1, skipna = True) | |
| df['range'] = df.max(axis = 1, skipna = True) - df.min(axis = 1, skipna = True) | |
| df['iqr'] = df.quantile(q=0.75, axis=1) - df.quantile(q=0.25, axis=1) | |
| mean = [4.30745506e-01,1.32617810e+00,1.64965628e-01,1.45381413e-01, | |
| 4.75798474e+00,3.50417288e-02,1.77480338e+01,5.65175045e+01, | |
| 2.99844222e-03,2.00322077e-01,1.49339305e-01,1.19261996e+00, | |
| 1.24635133e-01,5.33233574e-01,3.10338694e+00,4.96250939e+00, | |
| 5.59999753e-01,8.11946649e+02,1.90343823e+02,5.51748235e-01, | |
| 8.11930146e+02,3.76404974e+00] | |
| std= [7.53886409e-01,7.20547176e+00,3.39272741e-01,3.18574727e-01, | |
| 5.44265045e+00,5.60243189e-02,3.05253194e+01,1.15744574e+02, | |
| 9.94696634e-01,3.21393720e+00,5.30888102e-01,1.04918008e+01, | |
| 2.86767940e-01,3.00198372e+00,1.03539209e+01,1.26173180e+01, | |
| 2.70951027e+00,9.71837196e+02,2.28237885e+02,2.73221508e+00, | |
| 9.71806016e+02,1.29150916e+01] | |
| df = (df-mean)/std | |
| print("Predicting") | |
| print(df) | |
| # 'res' is a list of predictions returned as the label. | |
| res = model.predict(df) | |
| print(res) | |
| return res[0] | |
| demo = gr.Interface( | |
| fn=wine_quality, | |
| title="Wine Quality Prediction", | |
| description="Predict the quality of wine based on its features.", | |
| allow_flagging="never", | |
| inputs=[ | |
| gr.Dropdown(["red", "white"], label="type"), | |
| gr.inputs.Number(default=5.0, label="fixed acidity"), | |
| gr.inputs.Number(default=5.0, label="volatile acidity"), | |
| gr.inputs.Number(default=5.0, label="citric acid"), | |
| gr.inputs.Number(default=5.0, label="residual sugar"), | |
| gr.inputs.Number(default=5.0, label="chlorides"), | |
| gr.inputs.Number(default=5.0, label="free sulfur dioxide"), | |
| gr.inputs.Number(default=5.0, label="total sulfur dioxide"), | |
| gr.inputs.Number(default=5.0, label="density"), | |
| gr.inputs.Number(default=5.0, label="pH"), | |
| gr.inputs.Number(default=5.0, label="sulphates"), | |
| gr.inputs.Number(default=5.0, label="alcohol") | |
| ], | |
| outputs=gr.outputs.Label(label="Quality") | |
| ) | |
| demo.launch(debug=True) | |