Spaces:
Sleeping
Sleeping
Upload 4 files
Browse files- app.py +110 -0
- best_rf.joblib +3 -0
- requirements.txt +10 -0
- scaler.joblib +3 -0
app.py
ADDED
|
@@ -0,0 +1,110 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import joblib
|
| 3 |
+
import numpy as np
|
| 4 |
+
import pandas as pd
|
| 5 |
+
import plotly.express as px
|
| 6 |
+
|
| 7 |
+
# Load the models
|
| 8 |
+
scaler = joblib.load('scaler.joblib')
|
| 9 |
+
model = joblib.load('best_rf.joblib')
|
| 10 |
+
|
| 11 |
+
sensory_feature_column = ['sweet',
|
| 12 |
+
'sour',
|
| 13 |
+
'bitter',
|
| 14 |
+
'aromatic_impact',
|
| 15 |
+
'fruity_impact',
|
| 16 |
+
'art_sweetener_chewing',
|
| 17 |
+
'chews',
|
| 18 |
+
'art_sweetener_after',
|
| 19 |
+
'stickiness_with_fingers',
|
| 20 |
+
'springiness_with_fingers',
|
| 21 |
+
'abrasive',
|
| 22 |
+
'hardness_with_molars',
|
| 23 |
+
'uniformity_of_bite',
|
| 24 |
+
'gritty',
|
| 25 |
+
'springiness_during_chew',
|
| 26 |
+
'cohesiveness_of_mass',
|
| 27 |
+
'moistness_of_mass',
|
| 28 |
+
'toothsticking',
|
| 29 |
+
'toothpacking',
|
| 30 |
+
'adhesiveness_to_molars',
|
| 31 |
+
'oily_mouthcoating']
|
| 32 |
+
|
| 33 |
+
def scale_output(prediction_matrix):
|
| 34 |
+
# Calculate the sum for each row
|
| 35 |
+
row_sums = np.sum(prediction_matrix, axis=1)
|
| 36 |
+
|
| 37 |
+
# Calculate the surplus or lack from 1 for each row
|
| 38 |
+
delta = 1 - row_sums
|
| 39 |
+
|
| 40 |
+
# Adjust each row proportionally
|
| 41 |
+
adjusted_matrix = prediction_matrix + delta[:, np.newaxis] * (prediction_matrix / row_sums[:, np.newaxis])
|
| 42 |
+
print(f'scaled probability: {adjusted_matrix.sum(axis=1)}')
|
| 43 |
+
return adjusted_matrix
|
| 44 |
+
|
| 45 |
+
# Streamlit App
|
| 46 |
+
def main():
|
| 47 |
+
st.title("User distribution prediction")
|
| 48 |
+
|
| 49 |
+
col1, col2, col3 = st.columns(3)
|
| 50 |
+
|
| 51 |
+
with col1:
|
| 52 |
+
sweet = st.slider('sweet', 0.00, 10.00, 0.01)
|
| 53 |
+
aromatic_impact = st.slider('aromatic impact', 0.00, 10.00, 0.01)
|
| 54 |
+
chew = st.slider('chew', 0.00, 10.00, 0.01)
|
| 55 |
+
springiness_with_fingers = st.slider('springiness_with_fingers', 0.00, 10.00, 0.01)
|
| 56 |
+
uniformity_of_bite = st.slider('uniformity_of_bite', 0.00, 10.00, 0.01)
|
| 57 |
+
cohesiveness_of_mass = st.slider('cohesiveness_of_mass', 0.00, 10.00, 0.01)
|
| 58 |
+
toothpacking = st.slider('toothpacking', 0.00, 10.00, 0.01)
|
| 59 |
+
|
| 60 |
+
with col2:
|
| 61 |
+
sour = st.slider('sour', 0.00, 10.00, 0.01)
|
| 62 |
+
fruity_impact = st.slider('fruity impact',0.00, 10.00, 0.01)
|
| 63 |
+
art_sweetener_after = st.slider('art_sweetener_after', 0.00, 10.00, 0.01)
|
| 64 |
+
abrasiveness = st.slider('abrasiveness', 0.00, 10.00, 0.01)
|
| 65 |
+
gritty = st.slider('gritty', 0.00, 10.00, 0.01)
|
| 66 |
+
moisture_of_mass = st.slider('moisture_of_mass', 0.00, 10.00, 0.01)
|
| 67 |
+
adhesiveness_to_molars = st.slider('adhesiveness_to_molars', 0.00, 10.00, 0.01)
|
| 68 |
+
|
| 69 |
+
|
| 70 |
+
with col3:
|
| 71 |
+
bitter = st.slider('bitter', 0.00, 10.00, 0.01)
|
| 72 |
+
art_sweetener_chewing = st.slider('art_sweetener_chewing',0.00, 10.00, 0.01)
|
| 73 |
+
stickiness_with_fingers = st.slider('stickiness_with_fingers', 0.00, 10.00, 0.01)
|
| 74 |
+
hardness_with_molars = st.slider('hardness_with_molars', 0.00, 10.00, 0.01)
|
| 75 |
+
springiness_during_chew = st.slider('springiness_during_chew', 0.00, 10.00, 0.01)
|
| 76 |
+
toothsticking = st.slider('toothsticking', 0.00, 10.00, 0.01)
|
| 77 |
+
oily_mouthcoating = st.slider('oily_mouthcoating', 0.00, 10.00, 0.01)
|
| 78 |
+
|
| 79 |
+
|
| 80 |
+
data_input = [sweet, sour, bitter, aromatic_impact, fruity_impact, art_sweetener_chewing,chew,
|
| 81 |
+
art_sweetener_after, stickiness_with_fingers, springiness_with_fingers , abrasiveness, hardness_with_molars , uniformity_of_bite, gritty,
|
| 82 |
+
springiness_during_chew, cohesiveness_of_mass, moisture_of_mass , toothsticking , toothpacking, adhesiveness_to_molars, oily_mouthcoating]
|
| 83 |
+
|
| 84 |
+
test_df = pd.DataFrame(np.array(data_input).reshape(1, 21))
|
| 85 |
+
test_df.columns = sensory_feature_column
|
| 86 |
+
|
| 87 |
+
scaled_data_input = scaler.transform(test_df)
|
| 88 |
+
handout_prediction = scale_output(model.predict(scaled_data_input))
|
| 89 |
+
|
| 90 |
+
handout_pred_df = pd.DataFrame(handout_prediction)
|
| 91 |
+
handout_pred_df.columns = [str(i) for i in range(1,10)]
|
| 92 |
+
handout_pred_df['prod'] = 'prediction'
|
| 93 |
+
handout_pred_df = handout_pred_df.melt('prod')
|
| 94 |
+
# handout_pred_df['type'] = 'handout_test'
|
| 95 |
+
|
| 96 |
+
# st.write(f"select data: {data_input }")
|
| 97 |
+
# st.write(f"scaled select data: {scaled_data_input }")
|
| 98 |
+
st.write(f"prediction: {handout_prediction }")
|
| 99 |
+
st.write(f"sum of probability: {handout_prediction.sum()}")
|
| 100 |
+
|
| 101 |
+
fig = px.histogram(data_frame=handout_pred_df, x= 'variable',y='value', nbins= 9,
|
| 102 |
+
text_auto=True, title="Probability prediction of liking score from sensory attributes",
|
| 103 |
+
labels={"value": "probability",
|
| 104 |
+
"variable": "liking score"})
|
| 105 |
+
|
| 106 |
+
st.plotly_chart(fig, theme='streamlit')
|
| 107 |
+
|
| 108 |
+
|
| 109 |
+
if __name__ == "__main__":
|
| 110 |
+
main()
|
best_rf.joblib
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:c9628ee04279903b0435981063ef25122a1a573ba8d79a34f2f801cdfcf96713
|
| 3 |
+
size 1775335
|
requirements.txt
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
pandas
|
| 2 |
+
pyxlsb
|
| 3 |
+
seaborn
|
| 4 |
+
plotly
|
| 5 |
+
nbformat
|
| 6 |
+
ipykernel
|
| 7 |
+
scikit-learn
|
| 8 |
+
statsmodels
|
| 9 |
+
streamlit
|
| 10 |
+
shap
|
scaler.joblib
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:b292c973e27df674137f95577e6a1283ff70daf9e50965eff31a174962314235
|
| 3 |
+
size 2277
|