Spaces:
Sleeping
Sleeping
add application file
Browse files
app.py
ADDED
|
@@ -0,0 +1,121 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import numpy as np
|
| 4 |
+
from sklearn.ensemble import RandomForestClassifier
|
| 5 |
+
from sklearn.preprocessing import StandardScaler
|
| 6 |
+
import time
|
| 7 |
+
import matplotlib.pyplot as plt
|
| 8 |
+
import seaborn as sns
|
| 9 |
+
|
| 10 |
+
# Fonction pour charger un modèle existant (à adapter selon ton besoin)
|
| 11 |
+
def load_model():
|
| 12 |
+
# Création d'un modèle simple pour la démonstration.
|
| 13 |
+
model = RandomForestClassifier(n_estimators=100, random_state=42)
|
| 14 |
+
return model
|
| 15 |
+
|
| 16 |
+
# Fonction de pré-traitement des données
|
| 17 |
+
def preprocess_data(temperature, humidity, water_level, N, P, K):
|
| 18 |
+
data = np.array([[temperature, humidity, water_level, N, P, K]])
|
| 19 |
+
scaler = StandardScaler()
|
| 20 |
+
scaled_data = scaler.fit_transform(data)
|
| 21 |
+
return scaled_data
|
| 22 |
+
|
| 23 |
+
# Prédiction avec le modèle
|
| 24 |
+
def make_prediction(model, data):
|
| 25 |
+
prediction = model.predict(data)
|
| 26 |
+
return prediction[0]
|
| 27 |
+
|
| 28 |
+
# Interface Streamlit
|
| 29 |
+
st.set_page_config(page_title="Prédiction de la pompe à eau", layout="wide")
|
| 30 |
+
st.title("Prédiction de l'activation/désactivation de la pompe à eau")
|
| 31 |
+
st.write("Remplissez le formulaire ci-dessous pour prédire si la pompe doit être désactivée.")
|
| 32 |
+
|
| 33 |
+
# Utilisation de colonnes pour organiser les champs de saisie
|
| 34 |
+
col1, col2 = st.columns(2)
|
| 35 |
+
|
| 36 |
+
with col1:
|
| 37 |
+
temperature = st.number_input("Température (°C)", min_value=-50, max_value=50, value=25, step=1)
|
| 38 |
+
humidity = st.number_input("Humidité (%)", min_value=0, max_value=100, value=50, step=1)
|
| 39 |
+
water_level = st.number_input("Niveau d'eau (%)", min_value=0, max_value=100, value=50, step=1)
|
| 40 |
+
|
| 41 |
+
with col2:
|
| 42 |
+
N = st.number_input("Concentration en Azote (N)", min_value=0, max_value=500, value=100, step=1)
|
| 43 |
+
P = st.number_input("Concentration en Phosphore (P)", min_value=0, max_value=500, value=100, step=1)
|
| 44 |
+
K = st.number_input("Concentration en Potassium (K)", min_value=0, max_value=500, value=100, step=1)
|
| 45 |
+
fan_actuator_off = st.number_input("Fan Actuator OFF", min_value=0, max_value=1, value=0)
|
| 46 |
+
water_plant_pump_on = st.number_input("Water Plant Pump ON", min_value=0, max_value=1, value=1)
|
| 47 |
+
|
| 48 |
+
# Ajouter un bouton de prédiction avec animation
|
| 49 |
+
if st.button('Faire la prédiction', use_container_width=True):
|
| 50 |
+
with st.spinner('Traitement en cours...'):
|
| 51 |
+
time.sleep(2)
|
| 52 |
+
|
| 53 |
+
try:
|
| 54 |
+
# Prétraitement des données
|
| 55 |
+
data = preprocess_data(temperature, humidity, water_level, N, P, K)
|
| 56 |
+
|
| 57 |
+
# Charger le modèle (ici un modèle fictif pour la démo)
|
| 58 |
+
model = load_model()
|
| 59 |
+
|
| 60 |
+
# Entraîner le modèle avec des données fictives
|
| 61 |
+
X_train = np.random.rand(100, 6) # 100 exemples de données aléatoires
|
| 62 |
+
y_train = np.random.choice([0, 1], 100) # Cible binaire
|
| 63 |
+
model.fit(X_train, y_train)
|
| 64 |
+
|
| 65 |
+
# Faire la prédiction
|
| 66 |
+
prediction = make_prediction(model, data)
|
| 67 |
+
|
| 68 |
+
# Affichage de la prédiction
|
| 69 |
+
if prediction == 1:
|
| 70 |
+
st.success("La pompe **doit être désactivée**.")
|
| 71 |
+
else:
|
| 72 |
+
st.warning("La pompe **doit être activée**.")
|
| 73 |
+
|
| 74 |
+
# Animation des ballons après la prédiction
|
| 75 |
+
st.balloons()
|
| 76 |
+
|
| 77 |
+
except Exception as e:
|
| 78 |
+
st.error(f"Erreur pendant la prédiction : {e}")
|
| 79 |
+
|
| 80 |
+
# Section "Exemple de données"
|
| 81 |
+
st.markdown("""
|
| 82 |
+
### Exemple de données
|
| 83 |
+
- **Température** : 25°C
|
| 84 |
+
- **Humidité** : 50%
|
| 85 |
+
- **Niveau d'eau** : 50%
|
| 86 |
+
- **Azote (N)** : 100 mg/L
|
| 87 |
+
- **Phosphore (P)** : 100 mg/L
|
| 88 |
+
- **Potassium (K)** : 100 mg/L
|
| 89 |
+
- **Fan Actuator OFF** : 0
|
| 90 |
+
- **Water Plant Pump ON** : 1
|
| 91 |
+
""")
|
| 92 |
+
|
| 93 |
+
# Ajouter un peu de style pour rendre l'interface plus jolie
|
| 94 |
+
st.markdown("""
|
| 95 |
+
<style>
|
| 96 |
+
.css-1aumxhk {
|
| 97 |
+
text-align: center;
|
| 98 |
+
color: #2e7d32;
|
| 99 |
+
font-size: 22px;
|
| 100 |
+
font-weight: bold;
|
| 101 |
+
}
|
| 102 |
+
.stButton>button {
|
| 103 |
+
background-color: #43a047;
|
| 104 |
+
color: white;
|
| 105 |
+
font-size: 18px;
|
| 106 |
+
padding: 15px 30px;
|
| 107 |
+
border-radius: 8px;
|
| 108 |
+
}
|
| 109 |
+
.stButton>button:hover {
|
| 110 |
+
background-color: #388e3c;
|
| 111 |
+
}
|
| 112 |
+
</style>
|
| 113 |
+
""", unsafe_allow_html=True)
|
| 114 |
+
|
| 115 |
+
# Explication de l'objectif du projet
|
| 116 |
+
st.markdown("""
|
| 117 |
+
### Objectif du Projet
|
| 118 |
+
L'objectif de ce projet est de prédire l'état de la pompe à eau dans un système automatisé de gestion de l'irrigation.
|
| 119 |
+
En utilisant des informations telles que la température, l'humidité, le niveau d'eau, et d'autres paramètres environnementaux et opérationnels,
|
| 120 |
+
nous pouvons déterminer si la pompe doit être activée ou désactivée pour optimiser l'usage des ressources.
|
| 121 |
+
""")
|