Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,36 +1,172 @@
|
|
|
|
|
| 1 |
import numpy as np
|
| 2 |
-
import seaborn as sns
|
| 3 |
import matplotlib.pyplot as plt
|
|
|
|
| 4 |
|
| 5 |
|
| 6 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
st.write("")
|
| 16 |
-
st.
|
| 17 |
st.write("")
|
| 18 |
-
st.write("Certaines descriptions dépassent notre limitation en terme de tokens d'entrée du modèle, aussi, plutôt de couper le texte à l'aveugle, nous choisissons de résumer les descriptions.")
|
| 19 |
st.write("")
|
| 20 |
-
st.
|
| 21 |
-
|
| 22 |
-
st
|
| 23 |
-
st.write("
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
-
data_sum = sum_data[["description","tr_description_sum"]]
|
| 26 |
-
st.dataframe(data_sum)
|
| 27 |
st.write("")
|
| 28 |
-
st.markdown("---")
|
| 29 |
st.write("")
|
| 30 |
-
st
|
| 31 |
st.write("")
|
| 32 |
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
st
|
| 36 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pandas as pd
|
| 2 |
import numpy as np
|
|
|
|
| 3 |
import matplotlib.pyplot as plt
|
| 4 |
+
import importlib
|
| 5 |
|
| 6 |
|
| 7 |
+
from st_on_hover_tabs import on_hover_tabs
|
| 8 |
+
|
| 9 |
+
import streamlit as st
|
| 10 |
+
|
| 11 |
+
import streamlit_presentation
|
| 12 |
+
import streamlit_presentation.analyse
|
| 13 |
+
importlib.reload(streamlit_presentation.analyse)
|
| 14 |
+
from streamlit_presentation.analyse import repartition_par_categorie
|
| 15 |
+
from streamlit_presentation.analyse import repartition_longueur_categorie
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
import streamlit_presentation.preprocessing
|
| 19 |
+
importlib.reload(streamlit_presentation.preprocessing)
|
| 20 |
+
from streamlit_presentation.preprocessing import detection_langage_et_traduction
|
| 21 |
+
|
| 22 |
+
import streamlit_presentation.modele
|
| 23 |
+
importlib.reload(streamlit_presentation.modele)
|
| 24 |
+
from streamlit_presentation.modele import presentation_modele
|
| 25 |
+
from sklearn.metrics import f1_score
|
| 26 |
+
|
| 27 |
+
plt.rcParams['font.size'] = 12
|
| 28 |
+
plt.rcParams['axes.labelsize'] = 10
|
| 29 |
+
plt.rcParams['axes.titlesize'] = 12
|
| 30 |
+
plt.rcParams['xtick.labelsize'] = 8
|
| 31 |
+
plt.rcParams['ytick.labelsize'] = 8
|
| 32 |
+
plt.rcParams['legend.fontsize'] = 8
|
| 33 |
+
plt.rcParams['lines.linewidth'] = 1
|
| 34 |
+
|
| 35 |
+
#on charge les donnees utilisees
|
| 36 |
+
data = pd.read_csv( 'data.csv')
|
| 37 |
+
extract_data = pd.read_csv( 'data_tr_extract.csv')
|
| 38 |
+
sum_data = pd.read_csv( 'data_sum_extract.csv')
|
| 39 |
+
test_data = pd.read_pickle( 'data_test.pkl')
|
| 40 |
+
|
| 41 |
+
from keras.models import load_model
|
| 42 |
+
import tensorflow as tf
|
| 43 |
+
from tensorflow.keras import backend as K
|
| 44 |
+
import ast
|
| 45 |
+
|
| 46 |
+
|
| 47 |
+
def f1_weighted(true, pred):
|
| 48 |
+
|
| 49 |
+
# Classes
|
| 50 |
+
classes = K.arange(0, 27)
|
| 51 |
+
true = K.one_hot(K.cast(true, 'int32'), 27)
|
| 52 |
|
| 53 |
+
# Calcule les TP, FP, FN pour chaque classe
|
| 54 |
+
tp = K.dot(K.transpose(true), K.round(pred))
|
| 55 |
+
fp = K.dot(K.transpose(1-true), K.round(pred))
|
| 56 |
+
fn = K.dot(K.transpose(true), 1-K.round(pred))
|
| 57 |
+
|
| 58 |
+
# Calcule le score F1 pour chaque classe
|
| 59 |
+
p = tp / (tp + fp + K.epsilon())
|
| 60 |
+
r = tp / (tp + fn + K.epsilon())
|
| 61 |
+
f1 = 2*p*r / (p+r+K.epsilon())
|
| 62 |
+
|
| 63 |
|
| 64 |
+
weighted_f1 = K.sum(f1 * K.sum(true, axis=0) / K.sum(true))
|
| 65 |
+
return weighted_f1
|
| 66 |
+
|
| 67 |
+
model = load_model("final_model_kfold.h5", custom_objects={'f1_weighted': f1_weighted})
|
| 68 |
+
|
| 69 |
+
|
| 70 |
+
|
| 71 |
+
|
| 72 |
+
|
| 73 |
+
from sklearn.preprocessing import LabelEncoder
|
| 74 |
+
encoder = LabelEncoder()
|
| 75 |
+
y_test = encoder.fit_transform(test_data["prdtypecode"])
|
| 76 |
+
class_labels = encoder.classes_
|
| 77 |
+
label_size = 27
|
| 78 |
+
|
| 79 |
+
|
| 80 |
+
|
| 81 |
+
####### Page principale
|
| 82 |
+
st.set_page_config(layout="wide",page_title="Rakuten Challenge")
|
| 83 |
+
hide_default_format = """
|
| 84 |
+
<style>
|
| 85 |
+
#MainMenu {visibility: hidden; }
|
| 86 |
+
footer {visibility: hidden;}
|
| 87 |
+
</style>
|
| 88 |
+
"""
|
| 89 |
+
st.markdown(hide_default_format, unsafe_allow_html=True)
|
| 90 |
+
|
| 91 |
+
st.markdown('<style>' + open('./style.css').read() + '</style>', unsafe_allow_html=True)
|
| 92 |
+
|
| 93 |
+
st.title("Rakuten Challenge")
|
| 94 |
+
|
| 95 |
+
with st.sidebar:
|
| 96 |
+
tabs = on_hover_tabs(tabName=['Introduction', "Analyse", "Preprocessing", "Modèle", "Pistes exploratoires"],
|
| 97 |
+
iconName=['apps', 'bar_chart', "sync", "memory", "topic"],
|
| 98 |
+
styles = {'navtab': {'background-color':'RGB(55,71,79)',
|
| 99 |
+
'color': 'RGB(180,180,180)',
|
| 100 |
+
'font-size': '18px',
|
| 101 |
+
'transition': '.3s',
|
| 102 |
+
'white-space': 'nowrap',
|
| 103 |
+
'text-transform': 'uppercase'},
|
| 104 |
+
'tabOptionsStyle': {':hover :hover': {'color': 'RGB(235,197,82)',
|
| 105 |
+
'cursor': 'pointer'}},
|
| 106 |
+
'iconStyle':{'position':'fixed',
|
| 107 |
+
'left':'7.5px',
|
| 108 |
+
'text-align': 'left'},
|
| 109 |
+
'tabStyle' : {'list-style-type': 'none',
|
| 110 |
+
'margin-bottom': '30px',
|
| 111 |
+
'padding-left': '30px'}},
|
| 112 |
+
default_choice=0)
|
| 113 |
+
|
| 114 |
+
st.markdown("""
|
| 115 |
+
<style>
|
| 116 |
+
.rounded-border-parent {
|
| 117 |
+
border-radius: 15px !important;
|
| 118 |
+
border: 1px solid blue !important;
|
| 119 |
+
background-color: lightgray !important;
|
| 120 |
+
}
|
| 121 |
+
</style>
|
| 122 |
+
""", unsafe_allow_html=True)
|
| 123 |
+
|
| 124 |
+
|
| 125 |
+
if tabs == "Introduction":
|
| 126 |
+
st.write("### Introduction")
|
| 127 |
+
st.write("""
|
| 128 |
+
Le catalogue de l’ecommerce Rakuten comporte des centaines de milliers d’articles mis à jour régulièrement. Le besoin de l’entreprise est de les classer automatiquement dans leur catégorie.
|
| 129 |
+
|
| 130 |
+
L’objectif de notre projet est de de prédire le code type d’un produit à partir de données textes décrivant des produits ainsi que leurs images associées.
|
| 131 |
+
|
| 132 |
+
Notre étude doit déterminer la qualité et la pertinence des données, d’évaluer un prétraitement possible et proposer une solution de classification l’exploitation de ces dernières
|
| 133 |
+
""")
|
| 134 |
+
|
| 135 |
+
elif tabs == "Analyse":
|
| 136 |
+
st.write("### Analyse")
|
| 137 |
+
st.write("Extrait de la base de données fournie par Rakuten:")
|
| 138 |
+
st.dataframe(data.head(30))
|
| 139 |
st.write("")
|
| 140 |
+
st.write("### Regard sur les données:")
|
| 141 |
st.write("")
|
|
|
|
| 142 |
st.write("")
|
| 143 |
+
st.write("Une distribution des observations par code produit non balancée:")
|
| 144 |
+
st.write("")
|
| 145 |
+
repartition_par_categorie(st, data)
|
| 146 |
+
st.write("")
|
| 147 |
+
st.divider()
|
| 148 |
+
st.write("")
|
| 149 |
+
st.write("### Variabilité de la taille des champs textes:")
|
| 150 |
+
st.write("")
|
| 151 |
+
st.text("""
|
| 152 |
+
Pourcentage de valeurs manquantes pour la description : 35.09%
|
| 153 |
+
Pourcentage de valeurs manquantes pour la designation : 0.00%
|
| 154 |
+
""")
|
| 155 |
|
|
|
|
|
|
|
| 156 |
st.write("")
|
|
|
|
| 157 |
st.write("")
|
| 158 |
+
repartition_longueur_categorie(st, data)
|
| 159 |
st.write("")
|
| 160 |
|
| 161 |
+
|
| 162 |
+
elif tabs == "Preprocessing":
|
| 163 |
+
detection_langage_et_traduction(st, extract_data, sum_data)
|
| 164 |
+
|
| 165 |
+
elif tabs == "Modèle":
|
| 166 |
+
presentation_modele(st, test_data, model,class_labels,y_test,encoder)
|
| 167 |
+
|
| 168 |
+
elif tabs == "Pistes exploratoires":
|
| 169 |
+
st.write("# Pistes exploratoires")
|
| 170 |
+
st.write("Ici")
|
| 171 |
+
|
| 172 |
+
|