Spaces:
Build error
Build error
new update
Browse files- app.py +96 -0
- kmeans_model.joblib +3 -0
- lg.joblib +3 -0
- requirements.txt +26 -0
app.py
ADDED
@@ -0,0 +1,96 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
|
10 |
+
# Charger les modèles
|
11 |
+
try:
|
12 |
+
model = joblib.load("C:\\Users\\karballah\\Documents\\APP_Class_Clustering_ML_L3\\lg.joblib")
|
13 |
+
model_cluster = joblib.load("C:\\Users\\karballah\\Documents\\APP_Class_Clustering_ML_L3\\kmeans_model.joblib")
|
14 |
+
print("Modèles chargés avec succès.")
|
15 |
+
except FileNotFoundError:
|
16 |
+
print("Erreur: Le fichier n'a pas été trouvé.")
|
17 |
+
exit(1)
|
18 |
+
|
19 |
+
# Fonction de prédiction du diabète
|
20 |
+
def predict_diabetes(pregnancies, glucose, blood_pressure, skin_thickness, insulin, bmi, dpf, age):
|
21 |
+
input_data = np.array([[pregnancies, glucose, blood_pressure, skin_thickness, insulin, bmi, dpf, age]])
|
22 |
+
prediction = model.predict(input_data)[0]
|
23 |
+
return "Diabétique" if prediction == 1 else "Non diabétique"
|
24 |
+
|
25 |
+
# Fonction de visualisation des clusters
|
26 |
+
def plot_clusters(selected_cluster):
|
27 |
+
np.random.seed(42)
|
28 |
+
pca_features = np.random.randn(100, 2)
|
29 |
+
clusters = np.random.randint(0, 5, size=100)
|
30 |
+
|
31 |
+
pca_df = pd.DataFrame(pca_features, columns=['PC1', 'PC2'])
|
32 |
+
pca_df['Cluster'] = clusters
|
33 |
+
|
34 |
+
if selected_cluster == "Tous":
|
35 |
+
selected_data = pca_df
|
36 |
+
else:
|
37 |
+
selected_data = pca_df[pca_df['Cluster'] == int(selected_cluster)]
|
38 |
+
|
39 |
+
if selected_data.empty:
|
40 |
+
return px.scatter(title="Aucun point à afficher")
|
41 |
+
|
42 |
+
fig = px.scatter(selected_data, x='PC1', y='PC2', color=selected_data['Cluster'].astype(str),
|
43 |
+
title=f"Visualisation du Cluster {selected_cluster}", labels={'color': 'Cluster'})
|
44 |
+
|
45 |
+
return fig
|
46 |
+
|
47 |
+
# Fonction pour télécharger les clusters en CSV
|
48 |
+
def download_clusters():
|
49 |
+
cluster_data = {
|
50 |
+
'PC1': np.random.randn(100),
|
51 |
+
'PC2': np.random.randn(100),
|
52 |
+
'Cluster': np.random.randint(0, 5, 100)
|
53 |
+
}
|
54 |
+
df_clusters = pd.DataFrame(cluster_data)
|
55 |
+
return df_clusters.to_csv(index=False), "clusters.csv"
|
56 |
+
|
57 |
+
# Interface utilisateur avec Gradio
|
58 |
+
with gr.Blocks() as app:
|
59 |
+
gr.Markdown("## Application Machine Learning : Classification et Clustering")
|
60 |
+
|
61 |
+
# Section Classification
|
62 |
+
gr.Markdown("### Prédiction du Diabète")
|
63 |
+
with gr.Row():
|
64 |
+
pregnancies = gr.Number(label="Grossesses")
|
65 |
+
glucose = gr.Number(label="Glucose")
|
66 |
+
blood_pressure = gr.Number(label="Pression artérielle")
|
67 |
+
with gr.Row():
|
68 |
+
skin_thickness = gr.Number(label="Épaisseur de peau")
|
69 |
+
insulin = gr.Number(label="Insuline")
|
70 |
+
bmi = gr.Number(label="IMC")
|
71 |
+
with gr.Row():
|
72 |
+
dpf = gr.Number(label="DPF")
|
73 |
+
age = gr.Number(label="Âge")
|
74 |
+
|
75 |
+
predict_button = gr.Button("Prédire")
|
76 |
+
output_label = gr.Textbox(label="Résultat")
|
77 |
+
|
78 |
+
predict_button.click(fn=predict_diabetes,
|
79 |
+
inputs=[pregnancies, glucose, blood_pressure, skin_thickness, insulin, bmi, dpf, age],
|
80 |
+
outputs=output_label)
|
81 |
+
|
82 |
+
# Section Clustering
|
83 |
+
gr.Markdown("### Visualisation des Clusters des Réactions en Ligne")
|
84 |
+
cluster_selector = gr.Dropdown(["Tous"] + [str(i) for i in range(5)], label="Sélectionner un cluster")
|
85 |
+
cluster_plot = gr.Plot()
|
86 |
+
|
87 |
+
def update_plot(selected_cluster):
|
88 |
+
return plot_clusters(selected_cluster)
|
89 |
+
|
90 |
+
cluster_selector.change(fn=update_plot, inputs=[cluster_selector], outputs=[cluster_plot])
|
91 |
+
|
92 |
+
# Téléchargement des clusters
|
93 |
+
download_button = gr.Button("Télécharger les clusters")
|
94 |
+
download_button.click(fn=download_clusters, outputs=gr.File())
|
95 |
+
|
96 |
+
app.launch()
|
kmeans_model.joblib
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:98fc30ca548f15395c506815965c7efe98d09c95306d7ce8d6abde4229584721
|
3 |
+
size 29791
|
lg.joblib
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:3044d65ebb0fcff67f20e7610636909c9a5a73f6b6a2e95504f48a47b0eccc6c
|
3 |
+
size 927
|
requirements.txt
ADDED
@@ -0,0 +1,26 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
gradio==5.10.0
|
2 |
+
Flask==3.1.0
|
3 |
+
Flask-SQLAlchemy==3.1.1
|
4 |
+
Flask-Bootstrap==3.3.7.1
|
5 |
+
Flask-WTF==1.2.1
|
6 |
+
joblib==1.4.2
|
7 |
+
scikit-learn==1.5.1
|
8 |
+
numpy==2.2.2
|
9 |
+
pandas==2.2.3
|
10 |
+
matplotlib==3.8.4
|
11 |
+
seaborn==0.13.2
|
12 |
+
scipy==1.13.0
|
13 |
+
statsmodels==0.14.4
|
14 |
+
streamlit==1.41.1
|
15 |
+
google-api-core==2.24.0
|
16 |
+
google-auth==2.37.0
|
17 |
+
google-auth-httplib2==0.2.0
|
18 |
+
huggingface-hub==0.27.1
|
19 |
+
gunicorn==22.0.0
|
20 |
+
uvicorn==0.34.0
|
21 |
+
Werkzeug==3.1.3
|
22 |
+
requests==2.32.3
|
23 |
+
beautifulsoup4==4.12.3
|
24 |
+
pytest==7.2.2
|
25 |
+
pytest-flask==1.2.0
|
26 |
+
pytest-gradio==0.0.1
|