import streamlit as st import markdown2 import pdfkit from io import BytesIO from IPython.display import display, FileLink import base64 from langchain_core.messages import AIMessage, HumanMessage from datetime import datetime from download_chart import construct_plot from kaleido.scopes.plotly import PlotlyScope import pandas as pd import markdown from comparateur import get_table_empreintes_detailed from empreinte_export import get_carbon_footprint_html def colored_circle(color): return f'' def list_to_markdown(lst): return "\n".join([f'
{colored_circle(item["color"])} {item["name"]}: Pouvoir:{item["y"]}% Influence:{item["x"]}%
' for item in lst[:20]]) def categorize(row): if 50 <= row['pouvoir'] <= 100 and 0 <= row['influence'] < 50: return 'Rendre satisfait' elif 50 <= row['pouvoir'] <= 100 and 50 <= row['influence'] <= 100: return 'Gérer étroitement' elif 0 <= row['pouvoir'] < 50 and 0 <= row['influence'] < 50: return 'Suivre de près' elif 0 <= row['pouvoir'] < 50 and 50 <= row['influence'] <= 100: return 'Tenir informé' else: return 'Non catégorisé' @st.cache_data def convert_pp_to_csv(pp_grouped): if pp_grouped is None or len(pp_grouped) == 0: st.error("Aucune partie prenante n'a été définie") return None pp_df = pd.DataFrame(pp_grouped) pp_df.index.name = 'N°' pp_df.rename(columns={"name": "parties prenantes", "x": "influence", "y": "pouvoir"}, inplace=True) pp_df.drop(columns=['color'], inplace=True) # Apply the function to the DataFrame to create a new column 'categorie' pp_df['categorie'] = pp_df.apply(categorize, axis=1) pp_df = pp_df[["parties prenantes","categorie", "pouvoir", "influence"]] pp_df.rename_axis('N°', axis=1) return pp_df.to_csv(index=True,encoding="utf-8") @st.cache_data def create_pdf_from_markdown(logo_path, conversation,summary,brand_name,graph_html,app_url,list_pps,used_models=None): # Convertir la conversation en markdown markdown_text = "\n".join([f"### {entry['speaker']}:\n {entry['text']}\n ---" for entry in conversation]) if not used_models: used_models = ["Aucun modèle IA n'a été utilisé"] html_used_models = "".join([f"{model}
" for model in used_models]) markdown_summary = f"{summary}\n --- \n ---" markdown_list_pps = list_to_markdown(list_pps) # Convertir le markdown en HTML html_content = markdown.markdown(markdown_text,extensions=['markdown.extensions.tables']) html_summary = markdown2.markdown(markdown_summary) html_list_pps = markdown2.markdown(markdown_list_pps) analysis_date = datetime.now().strftime("%Y-%m-%d") # image_base64 = base64.b64encode(image_path).decode('utf-8') graph_html.update_layout(showlegend=False) img_bytes = PlotlyScope().transform( figure=graph_html, format="png", ) fig1 = f"data:image/png;base64,{base64.b64encode(img_bytes).decode('utf8')}" # Créer le HTML complet avec les images et le texte html_template = f"""Date de l'analyse IA RSE : {analysis_date}
IA utilisées :
{html_used_models}