Spaces:
Runtime error
Runtime error
ZipeiZhang commited on
Commit ·
ee808f5
1
Parent(s): 3f58107
Add application file
Browse files
app.py
CHANGED
|
@@ -9,7 +9,7 @@ import numpy as np
|
|
| 9 |
from io import BytesIO
|
| 10 |
from great_expectations.dataset import PandasDataset
|
| 11 |
from great_expectations.core import ExpectationSuite, ExpectationConfiguration
|
| 12 |
-
from sklearn.metrics import confusion_matrix
|
| 13 |
import seaborn as sns
|
| 14 |
import matplotlib.pyplot as plt
|
| 15 |
api = '151p8WWCoctBzBeg.wRj1VwLA6wwjCS2aG7A51NsbhEbqVZ35wLl5g03b85EeetLKtpsO9bDOjy8DR2O3'
|
|
@@ -125,6 +125,13 @@ def wine_predict(fixed_acidity, volatile_acidity, citric_acid, residual_sugar,
|
|
| 125 |
y_test = y_test[:num_samples_matrix]
|
| 126 |
y_pred = model.predict(X_test)
|
| 127 |
confusion_matrix_plot = plot_confusion_matrix(y_test, y_pred)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 128 |
|
| 129 |
if num_samples == -1:
|
| 130 |
df = pd.DataFrame([[fixed_acidity, volatile_acidity, citric_acid, residual_sugar,
|
|
@@ -147,27 +154,13 @@ def wine_predict(fixed_acidity, volatile_acidity, citric_acid, residual_sugar,
|
|
| 147 |
synthetic_wines_df = generate_samples(num_samples) # This will also insert the data into the feature store
|
| 148 |
synthetic_wines_df = synthetic_wines_df.drop('quality', axis=1)
|
| 149 |
y_pred_new = model.predict(synthetic_wines_df)
|
| 150 |
-
synthetic_wines_df = synthetic_wines_df[:
|
| 151 |
-
wine_fg_50 = fs.get_feature_group(name="wine_2", version=1)
|
| 152 |
-
query = wine_fg_50.select_all()
|
| 153 |
-
feature_view = fs.get_or_create_feature_view(name="wine_2",
|
| 154 |
-
version=1,
|
| 155 |
-
description="Read from Wine Quality dataset",
|
| 156 |
-
labels=["quality"],
|
| 157 |
-
query=query)
|
| 158 |
-
|
| 159 |
-
X_train, X_test, y_train, y_test = feature_view.train_test_split(test_size = 0.2)
|
| 160 |
-
num_samples_matrix = int(num_samples_matrix)
|
| 161 |
-
X_test = X_test[:num_samples_matrix]
|
| 162 |
-
y_test = y_test[:num_samples_matrix]
|
| 163 |
-
y_pred = model.predict(X_test)
|
| 164 |
-
confusion_matrix_plot = plot_confusion_matrix(y_test, y_pred)
|
| 165 |
res = y_pred_new.mean()
|
| 166 |
|
| 167 |
if isinstance(synthetic_wines_df, pd.DataFrame):
|
| 168 |
-
return res, synthetic_wines_df, confusion_matrix_plot
|
| 169 |
else:
|
| 170 |
-
return res, pd.DataFrame(synthetic_wines_df), confusion_matrix_plot
|
| 171 |
|
| 172 |
|
| 173 |
def update_dashboard(fixed_acidity, volatile_acidity, citric_acid, residual_sugar,
|
|
@@ -175,13 +168,13 @@ def update_dashboard(fixed_acidity, volatile_acidity, citric_acid, residual_suga
|
|
| 175 |
ph, sulphates, alcohol, num_samples, num_samples_matrix):
|
| 176 |
|
| 177 |
|
| 178 |
-
predction ,generated_samples,confusion_matrix_plot
|
| 179 |
volatile_acidity, citric_acid, residual_sugar,
|
| 180 |
chlorides, free_sulfur_dioxide, total_sulfur_dioxide, density,
|
| 181 |
ph, sulphates, alcohol, num_samples, num_samples_matrix)
|
| 182 |
|
| 183 |
|
| 184 |
-
return predction, confusion_matrix_plot, generated_samples
|
| 185 |
demo = gr.Interface(
|
| 186 |
fn=update_dashboard,
|
| 187 |
title="Wine Quality Predictive Analytics",
|
|
@@ -205,6 +198,7 @@ demo = gr.Interface(
|
|
| 205 |
outputs=[
|
| 206 |
gr.outputs.Textbox(label="Predicted Quality"),
|
| 207 |
gr.outputs.Image(label="Confusion Matrix",type="pil"),
|
|
|
|
| 208 |
gr.outputs.Dataframe(label="Most Recent Generated Wine Data",type='pandas')
|
| 209 |
],
|
| 210 |
)
|
|
|
|
| 9 |
from io import BytesIO
|
| 10 |
from great_expectations.dataset import PandasDataset
|
| 11 |
from great_expectations.core import ExpectationSuite, ExpectationConfiguration
|
| 12 |
+
from sklearn.metrics import confusion_matrix, accuracy_score, f1_score, precision_score, recall_score
|
| 13 |
import seaborn as sns
|
| 14 |
import matplotlib.pyplot as plt
|
| 15 |
api = '151p8WWCoctBzBeg.wRj1VwLA6wwjCS2aG7A51NsbhEbqVZ35wLl5g03b85EeetLKtpsO9bDOjy8DR2O3'
|
|
|
|
| 125 |
y_test = y_test[:num_samples_matrix]
|
| 126 |
y_pred = model.predict(X_test)
|
| 127 |
confusion_matrix_plot = plot_confusion_matrix(y_test, y_pred)
|
| 128 |
+
acc = accuracy_score(y_test, y_pred)
|
| 129 |
+
prec = precision_score(y_test, y_pred, average='weighted')
|
| 130 |
+
rec = recall_score(y_test, y_pred, average='weighted')
|
| 131 |
+
f1 = f1_score(y_test, y_pred, average='weighted')
|
| 132 |
+
|
| 133 |
+
pred_results = pd.DataFrame([['Random Forest', acc, prec, rec, f1]],
|
| 134 |
+
columns = ['Model', 'Accuracy', 'Precision', 'Recall', 'F1 Score'])
|
| 135 |
|
| 136 |
if num_samples == -1:
|
| 137 |
df = pd.DataFrame([[fixed_acidity, volatile_acidity, citric_acid, residual_sugar,
|
|
|
|
| 154 |
synthetic_wines_df = generate_samples(num_samples) # This will also insert the data into the feature store
|
| 155 |
synthetic_wines_df = synthetic_wines_df.drop('quality', axis=1)
|
| 156 |
y_pred_new = model.predict(synthetic_wines_df)
|
| 157 |
+
synthetic_wines_df = synthetic_wines_df[:50]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 158 |
res = y_pred_new.mean()
|
| 159 |
|
| 160 |
if isinstance(synthetic_wines_df, pd.DataFrame):
|
| 161 |
+
return res, synthetic_wines_df, confusion_matrix_plot,pred_results
|
| 162 |
else:
|
| 163 |
+
return res, pd.DataFrame(synthetic_wines_df), confusion_matrix_plot,pred_results
|
| 164 |
|
| 165 |
|
| 166 |
def update_dashboard(fixed_acidity, volatile_acidity, citric_acid, residual_sugar,
|
|
|
|
| 168 |
ph, sulphates, alcohol, num_samples, num_samples_matrix):
|
| 169 |
|
| 170 |
|
| 171 |
+
predction ,generated_samples,confusion_matrix_plot,pred_results= wine_predict(fixed_acidity,
|
| 172 |
volatile_acidity, citric_acid, residual_sugar,
|
| 173 |
chlorides, free_sulfur_dioxide, total_sulfur_dioxide, density,
|
| 174 |
ph, sulphates, alcohol, num_samples, num_samples_matrix)
|
| 175 |
|
| 176 |
|
| 177 |
+
return predction, confusion_matrix_plot, pred_results,generated_samples
|
| 178 |
demo = gr.Interface(
|
| 179 |
fn=update_dashboard,
|
| 180 |
title="Wine Quality Predictive Analytics",
|
|
|
|
| 198 |
outputs=[
|
| 199 |
gr.outputs.Textbox(label="Predicted Quality"),
|
| 200 |
gr.outputs.Image(label="Confusion Matrix",type="pil"),
|
| 201 |
+
gr.outputs.Dataframe(label="Most Recent Prediction results",type='pandas'),
|
| 202 |
gr.outputs.Dataframe(label="Most Recent Generated Wine Data",type='pandas')
|
| 203 |
],
|
| 204 |
)
|