Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,124 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
print(test_data.columns)
|
76 |
+
y_test = encoder.fit_transform(test_data["prdtypecode"])
|
77 |
+
class_labels = encoder.classes_
|
78 |
+
label_size = 27
|
79 |
+
|
80 |
+
|
81 |
+
|
82 |
+
####### Page principale
|
83 |
+
st.set_page_config(layout="wide")
|
84 |
+
st.markdown('<style>' + open('./style.css').read() + '</style>', unsafe_allow_html=True)
|
85 |
+
|
86 |
+
st.title("Mon Application")
|
87 |
+
|
88 |
+
with st.sidebar:
|
89 |
+
tabs = on_hover_tabs(tabName=['Introduction', "Analyse", "Preprocessing", "Modèle", "Pistes exploratoires"],
|
90 |
+
iconName=['apps', 'bar_chart', "sync", "memory", "topic"], default_choice=0)
|
91 |
+
|
92 |
+
st.markdown("""
|
93 |
+
<style>
|
94 |
+
.rounded-border-parent {
|
95 |
+
border-radius: 15px !important;
|
96 |
+
border: 1px solid blue !important;
|
97 |
+
background-color: lightgray !important;
|
98 |
+
}
|
99 |
+
</style>
|
100 |
+
""", unsafe_allow_html=True)
|
101 |
+
|
102 |
+
|
103 |
+
if tabs == "Introduction":
|
104 |
+
st.write("# Introduction")
|
105 |
+
st.write("Ici")
|
106 |
+
|
107 |
+
elif tabs == "Analyse":
|
108 |
+
st.write("# Analyse")
|
109 |
+
|
110 |
+
st.dataframe(data.head(30))
|
111 |
+
st.write("")
|
112 |
+
|
113 |
+
repartition_par_categorie(st, data)
|
114 |
+
repartition_longueur_categorie(st, data)
|
115 |
+
|
116 |
+
elif tabs == "Preprocessing":
|
117 |
+
detection_langage_et_traduction(st, extract_data, sum_data)
|
118 |
+
|
119 |
+
elif tabs == "Modèle":
|
120 |
+
presentation_modele(st, test_data, model,class_labels,y_test)
|
121 |
+
|
122 |
+
elif tabs == "Pistes exploratoires":
|
123 |
+
st.write("# Pistes exploratoires")
|
124 |
+
st.write("Ici")
|