Spaces:
Sleeping
Sleeping
| import streamlit as st | |
| import pandas as pd | |
| from joblib import load | |
| def load_model(): | |
| model_path = "model.joblib" | |
| try: | |
| model = load(model_path) | |
| return model | |
| except FileNotFoundError: | |
| st.error("Le fichier du modèle n'a pas été trouvé.") | |
| return None | |
| def show(): | |
| st.title("Prédiction d'Anémie") | |
| # Chargement du modèle | |
| model = load_model() | |
| if model is None: | |
| return | |
| # Saisie des données | |
| age = st.number_input("Âge", min_value=10, max_value=19) | |
| fer = st.selectbox("Avez-vous pris du fer sous quelque forme que ce soit au cours des 3 derniers mois ?", ["Oui", "Non"]) | |
| poids = st.number_input("Poids (en Kg)", min_value=10.0) | |
| taille = st.number_input("Taille (en cm)", min_value=50.0) | |
| # Calcul de l'IMC | |
| if poids > 0 and taille > 0: | |
| taille_m = taille / 100 | |
| imc = poids / (taille_m ** 2) | |
| else: | |
| imc = 0 | |
| # Affichage de l'IMC | |
| st.write(f"IMC Calculé: {imc:.2f}") | |
| menstruation = st.selectbox("Voyez-vous déjà vos règles ?", ["Oui", "Non"]) | |
| statut_matrimonial = st.selectbox("Quel est votre statut matrimonial?", ["Célibataire", "Mariée"]) | |
| zone = st.selectbox("Quelle est votre zone d'habitation ?", ["Urbaine", "Rurale"]) | |
| region = st.selectbox("Quelle est la région que vous habitez", ["Adamaoua", "Est", "Extrême Nord", "Nord"]) | |
| if st.button("Faire la Prédiction"): | |
| # Préparation des données pour la prédiction | |
| data = { | |
| 'Age': age, | |
| 'Fer': 1 if fer == "Oui" else 2, | |
| 'IMC_Calcule': imc, | |
| 'Menstruation': 1 if menstruation == "Oui" else 2, | |
| 'Statut_Matrimonial': 1 if statut_matrimonial == "Célibataire" else 2, | |
| 'Zone': 0 if zone == "Urbaine" else 1, | |
| 'Region': {"Adamaoua": 1, "Est": 2, "Extrême Nord": 3, "Nord": 4}[region], | |
| 'Menage': 850 | |
| } | |
| df = pd.DataFrame([data]) | |
| # Prédiction | |
| new_data_encoded = pd.get_dummies(df, drop_first=True) | |
| expected_columns = model.feature_names_in_ | |
| for column in expected_columns: | |
| if column not in new_data_encoded.columns: | |
| new_data_encoded[column] = 0 | |
| new_data_encoded = new_data_encoded[expected_columns] | |
| prediction = model.predict(new_data_encoded) | |
| prediction_proba = model.predict_proba(new_data_encoded) | |
| # Stockage des résultats dans st.session_state | |
| result_text = "Anémique" if prediction[0] == 1 else "Non Anémique" | |
| st.session_state.data = data | |
| st.session_state.prediction = { | |
| 'Prediction': result_text, | |
| 'Probability': prediction_proba[0][prediction[0]] # Probabilité à stocker pour le rapport | |
| } | |
| st.session_state.goto_report = True | |
| # Affichage des résultats de la prédiction | |
| st.markdown(f"<p style='color:red; text-align:center;'>Prédiction : {result_text}</p>", unsafe_allow_html=True) | |
| if __name__ == '__main__': | |
| show() | |