Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,65 +1,63 @@
|
|
1 |
import pandas as pd
|
2 |
import gradio as gr
|
3 |
-
# import joblib
|
4 |
import numpy as np
|
5 |
import plotly.express as px
|
6 |
-
from sklearn.preprocessing import StandardScaler
|
7 |
-
from sklearn.decomposition import PCA
|
8 |
-
from sklearn.cluster import KMeans
|
9 |
-
import sys
|
10 |
import pickle
|
|
|
11 |
|
12 |
-
# Charger les modèles
|
|
|
|
|
|
|
|
|
|
|
13 |
|
14 |
-
with open("lg.pkl", "rb") as f:
|
15 |
-
model = pickle.load(f)
|
16 |
with open("kmeans_model.pkl", "rb") as f:
|
17 |
model_cluster = pickle.load(f)
|
18 |
|
|
|
|
|
|
|
|
|
|
|
19 |
|
20 |
# Fonction de prédiction du diabète
|
21 |
def predict_diabetes(pregnancies, glucose, blood_pressure, skin_thickness, insulin, bmi, dpf, age):
|
22 |
input_data = np.array([[pregnancies, glucose, blood_pressure, skin_thickness, insulin, bmi, dpf, age]])
|
23 |
-
prediction =
|
24 |
return "Diabétique" if prediction == 1 else "Non diabétique"
|
25 |
|
26 |
-
# Fonction
|
27 |
def plot_clusters(selected_cluster):
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
if selected_data.empty:
|
41 |
return px.scatter(title="Aucun point à afficher")
|
42 |
|
|
|
43 |
fig = px.scatter(selected_data, x='PC1', y='PC2', color=selected_data['Cluster'].astype(str),
|
44 |
title=f"Visualisation du Cluster {selected_cluster}", labels={'color': 'Cluster'})
|
45 |
-
|
46 |
return fig
|
47 |
|
48 |
# Fonction pour télécharger les clusters en CSV
|
49 |
def download_clusters():
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
'Cluster': np.random.randint(0, 5, 100)
|
54 |
-
}
|
55 |
-
df_clusters = pd.DataFrame(cluster_data)
|
56 |
-
return df_clusters.to_csv(index=False), "clusters.csv"
|
57 |
-
|
58 |
-
# Interface utilisateur avec Gradio
|
59 |
with gr.Blocks() as app:
|
60 |
gr.Markdown("## Application Machine Learning : Classification et Clustering")
|
61 |
-
|
62 |
-
# Section Classification
|
63 |
gr.Markdown("### Prédiction du Diabète")
|
64 |
with gr.Row():
|
65 |
pregnancies = gr.Number(label="Grossesses")
|
@@ -72,23 +70,21 @@ with gr.Blocks() as app:
|
|
72 |
with gr.Row():
|
73 |
dpf = gr.Number(label="DPF")
|
74 |
age = gr.Number(label="Âge")
|
75 |
-
|
76 |
predict_button = gr.Button("Prédire")
|
77 |
output_label = gr.Textbox(label="Résultat")
|
78 |
-
|
79 |
-
predict_button.click(fn=predict_diabetes,
|
80 |
-
inputs=[pregnancies, glucose, blood_pressure, skin_thickness, insulin, bmi, dpf, age],
|
81 |
outputs=output_label)
|
82 |
-
|
83 |
-
# Section Clustering
|
84 |
gr.Markdown("### Visualisation des Clusters des Réactions en Ligne")
|
85 |
-
cluster_selector = gr.Dropdown(["Tous"] + [str(i) for i in range(
|
|
|
86 |
cluster_plot = gr.Plot()
|
87 |
|
88 |
-
|
89 |
-
return plot_clusters(selected_cluster)
|
90 |
-
|
91 |
-
cluster_selector.change(fn=update_plot, inputs=[cluster_selector], outputs=[cluster_plot])
|
92 |
|
93 |
# Téléchargement des clusters
|
94 |
download_button = gr.Button("Télécharger les clusters")
|
|
|
1 |
import pandas as pd
|
2 |
import gradio as gr
|
|
|
3 |
import numpy as np
|
4 |
import plotly.express as px
|
|
|
|
|
|
|
|
|
5 |
import pickle
|
6 |
+
from sklearn.preprocessing import StandardScaler
|
7 |
|
8 |
+
# Charger les modèles sauvegardés
|
9 |
+
with open("scaler.pkl", "rb") as f:
|
10 |
+
scaler = pickle.load(f)
|
11 |
+
|
12 |
+
with open("pca_model.pkl", "rb") as f:
|
13 |
+
pca_model = pickle.load(f)
|
14 |
|
|
|
|
|
15 |
with open("kmeans_model.pkl", "rb") as f:
|
16 |
model_cluster = pickle.load(f)
|
17 |
|
18 |
+
with open("lg.pkl", "rb") as f:
|
19 |
+
model_diabetes = pickle.load(f)
|
20 |
+
|
21 |
+
# Charger le dataframe (assurez-vous de mettre le bon chemin vers votre fichier)
|
22 |
+
df_scaled = pd.read_csv("data.csv") # Remplacez par le chemin réel du fichier
|
23 |
|
24 |
# Fonction de prédiction du diabète
|
25 |
def predict_diabetes(pregnancies, glucose, blood_pressure, skin_thickness, insulin, bmi, dpf, age):
|
26 |
input_data = np.array([[pregnancies, glucose, blood_pressure, skin_thickness, insulin, bmi, dpf, age]])
|
27 |
+
prediction = model_diabetes.predict(input_data)[0] # Utilise le modèle de régression logistique pour prédire
|
28 |
return "Diabétique" if prediction == 1 else "Non diabétique"
|
29 |
|
30 |
+
# Fonction pour afficher les clusters
|
31 |
def plot_clusters(selected_cluster):
|
32 |
+
# Appliquer PCA pour transformer les données en 2D
|
33 |
+
selected_data = pca_model.transform(df_scaled)
|
34 |
+
selected_data = pd.DataFrame(selected_data, columns=['PC1', 'PC2'])
|
35 |
+
|
36 |
+
# Prédire les clusters avec le modèle KMeans
|
37 |
+
selected_data['Cluster'] = model_cluster.predict(df_scaled)
|
38 |
+
|
39 |
+
# Filtrer par le cluster sélectionné (si "Tous" n'est pas choisi)
|
40 |
+
if selected_cluster != "Tous":
|
41 |
+
selected_data = selected_data[selected_data['Cluster'] == int(selected_cluster)]
|
42 |
+
|
|
|
43 |
if selected_data.empty:
|
44 |
return px.scatter(title="Aucun point à afficher")
|
45 |
|
46 |
+
# Créer le graphique avec Plotly
|
47 |
fig = px.scatter(selected_data, x='PC1', y='PC2', color=selected_data['Cluster'].astype(str),
|
48 |
title=f"Visualisation du Cluster {selected_cluster}", labels={'color': 'Cluster'})
|
49 |
+
|
50 |
return fig
|
51 |
|
52 |
# Fonction pour télécharger les clusters en CSV
|
53 |
def download_clusters():
|
54 |
+
return df.to_csv(index=False), "clusters.csv"
|
55 |
+
|
56 |
+
# Interface utilisateur Gradio
|
|
|
|
|
|
|
|
|
|
|
|
|
57 |
with gr.Blocks() as app:
|
58 |
gr.Markdown("## Application Machine Learning : Classification et Clustering")
|
59 |
+
|
60 |
+
# **Section Classification**
|
61 |
gr.Markdown("### Prédiction du Diabète")
|
62 |
with gr.Row():
|
63 |
pregnancies = gr.Number(label="Grossesses")
|
|
|
70 |
with gr.Row():
|
71 |
dpf = gr.Number(label="DPF")
|
72 |
age = gr.Number(label="Âge")
|
73 |
+
|
74 |
predict_button = gr.Button("Prédire")
|
75 |
output_label = gr.Textbox(label="Résultat")
|
76 |
+
|
77 |
+
predict_button.click(fn=predict_diabetes,
|
78 |
+
inputs=[pregnancies, glucose, blood_pressure, skin_thickness, insulin, bmi, dpf, age],
|
79 |
outputs=output_label)
|
80 |
+
|
81 |
+
# **Section Clustering**
|
82 |
gr.Markdown("### Visualisation des Clusters des Réactions en Ligne")
|
83 |
+
cluster_selector = gr.Dropdown(["Tous"] + [str(i) for i in range(len(df['Cluster'].unique()))],
|
84 |
+
label="Sélectionner un cluster")
|
85 |
cluster_plot = gr.Plot()
|
86 |
|
87 |
+
cluster_selector.change(fn=plot_clusters, inputs=[cluster_selector], outputs=[cluster_plot])
|
|
|
|
|
|
|
88 |
|
89 |
# Téléchargement des clusters
|
90 |
download_button = gr.Button("Télécharger les clusters")
|