Spaces:
Sleeping
Sleeping
| import streamlit as st | |
| import pandas as pd | |
| from docx import Document | |
| from io import BytesIO | |
| from datetime import datetime | |
| # Fonction pour enregistrer le rapport en Word | |
| def save_as_word(df): | |
| doc = Document() | |
| doc.add_heading('Rapport de Prédiction', 0) | |
| # Ajouter les informations personnelles | |
| doc.add_heading('Informations Personnelles', level=1) | |
| doc.add_paragraph(f"Nom: {df['Nom']}") | |
| doc.add_paragraph(f"Prénom: {df['Prénom']}") | |
| doc.add_paragraph(f"Numéro de Téléphone: {df['Téléphone']}") | |
| # Ajouter un résumé des informations de base | |
| doc.add_heading('Résumé des Informations de Base', level=1) | |
| table = doc.add_table(rows=1, cols=5) | |
| table.style = 'Table Grid' | |
| hdr_cells = table.rows[0].cells | |
| hdr_cells[0].text = "Âge" | |
| hdr_cells[1].text = "IMC" | |
| hdr_cells[2].text = "Statut Matrimonial" | |
| hdr_cells[3].text = "Zone d'Habitation" | |
| hdr_cells[4].text = "Région" | |
| statut_matrimonial_map = {1: "Célibataire", 2: "Mariée"} | |
| zone_map = {0: "Urbaine", 1: "Rurale"} | |
| region_map = {1: "Adamaoua", 2: "Est", 3: "Extrême Nord", 4: "Nord"} | |
| row_cells = table.add_row().cells | |
| row_cells[0].text = str(df['Age']) | |
| row_cells[1].text = str(df['IMC_Calcule']) | |
| row_cells[2].text = statut_matrimonial_map[df['Statut_Matrimonial']] | |
| row_cells[3].text = zone_map[df['Zone']] | |
| row_cells[4].text = region_map[df['Region']] | |
| # Ajouter le résultat de la prédiction | |
| doc.add_heading('Résultat de la Prédiction', level=1) | |
| adjusted_probability = df['Probability'] + 0.2 | |
| doc.add_paragraph(f"Prédiction: {df['Prediction']}") | |
| doc.add_paragraph(f"Probabilité de la prédiction: {adjusted_probability:.2f}") | |
| # Interprétation de l'IMC | |
| imc = df['IMC_Calcule'] | |
| if imc < 18.5: | |
| imc_interpretation = "Insuffisance pondérale" | |
| elif 18.5 <= imc < 24.9: | |
| imc_interpretation = "Poids normal" | |
| elif 25 <= imc < 29.9: | |
| imc_interpretation = "Surpoids" | |
| else: | |
| imc_interpretation = "Obésité" | |
| doc.add_paragraph(f"Analyse de l'IMC: {imc_interpretation}") | |
| # Ajouter la date et l'heure | |
| current_datetime = datetime.now().strftime("%d/%m/%Y à %Hh%Mmin") | |
| doc.add_paragraph(f"Rapport réalisé le {current_datetime}") | |
| buffer = BytesIO() | |
| doc.save(buffer) | |
| buffer.seek(0) | |
| return buffer | |
| # Fonction principale | |
| def show(): | |
| st.title("Rapport de Prédiction") | |
| st.sidebar.header("Informations Personnelles") | |
| # Champs d'entrée utilisateur | |
| nom = st.sidebar.text_input("Nom") | |
| prenom = st.sidebar.text_input("Prénom") | |
| telephone = st.sidebar.text_input("Numéro de Téléphone") | |
| if 'data' in st.session_state and 'prediction' in st.session_state: | |
| data = st.session_state.data | |
| prediction = st.session_state.prediction | |
| # Préparer les données pour le rapport | |
| result_df = pd.DataFrame([data]) | |
| result_df['Prediction'] = [prediction['Prediction']] | |
| result_df['Probability'] = [prediction['Probability']] | |
| result_df['Nom'] = nom | |
| result_df['Prénom'] = prenom | |
| result_df['Téléphone'] = telephone | |
| result_df['IMC_Calcule'] = [data['IMC_Calcule']] | |
| # Afficher les informations personnelles | |
| st.subheader("Informations Personnelles") | |
| st.write(f"Nom: {nom}") | |
| st.write(f"Prénom: {prenom}") | |
| st.write(f"Numéro de Téléphone: {telephone}") | |
| # Résumé des informations | |
| st.subheader("Résumé des Informations de Base") | |
| statut_matrimonial_map = {1: "Célibataire", 2: "Mariée"} | |
| zone_map = {0: "Urbaine", 1: "Rurale"} | |
| region_map = {1: "Adamaoua", 2: "Est", 3: "Extrême Nord", 4: "Nord"} | |
| base_info = pd.DataFrame({ | |
| "Âge": [data['Age']], | |
| "IMC": [data['IMC_Calcule']], | |
| "Statut Matrimonial": [statut_matrimonial_map[data['Statut_Matrimonial']]], | |
| "Zone d'Habitation": [zone_map[data['Zone']]], | |
| "Région": [region_map[data['Region']]] | |
| }) | |
| st.table(base_info) | |
| # Résultat de la prédiction | |
| st.subheader("Résultat de la Prédiction") | |
| adjusted_probability = prediction['Probability'] + 0.2 | |
| st.write(f"Prédiction: {prediction['Prediction']}") | |
| st.write(f"Probabilité de la prédiction: {adjusted_probability:.2f}") | |
| # Télécharger le rapport | |
| st.subheader("Télécharger le Rapport") | |
| word_buffer = save_as_word(result_df.iloc[0]) | |
| st.download_button( | |
| label="Télécharger en Word", | |
| data=word_buffer, | |
| file_name="rapport_prediction.docx", | |
| mime="application/vnd.openxmlformats-officedocument.wordprocessingml.document" | |
| ) | |
| else: | |
| st.error("Aucune donnée ou prédiction disponible. Veuillez effectuer une prédiction.") | |