File size: 2,567 Bytes
439e01f bc622d3 439e01f | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 | 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
def create_pdf_from_markdown(logo_path, image_path, conversation,summary):
# Convertir la conversation en markdown
markdown_text = "\n".join([f"### {entry['speaker']}:\n {entry['text']}\n ---" for entry in conversation])
markdown_summary = f"{summary}\n --- \n ---"
st.write(markdown_summary)
# Convertir le markdown en HTML
html_content = markdown2.markdown(markdown_text)
html_summary = markdown2.markdown(markdown_summary)
# image_base64 = base64.b64encode(image_path).decode('utf-8')
# Créer le HTML complet avec les images et le texte
html_template = f"""
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
</head>
<body>
<div style="text-align: center;">
<h1>Rapport de Conversation {st.session_state["Nom de la marque"]}</h1>
<img src="{logo_path}" alt="Logo" style="width: 150px;"/>
</div>
<div style="text-align: center; margin-top: 20px;">
<img src="data:image/png;base64" alt="Cartographie" style="width: 100%;"/>
</div>
<h2>RESUME</h2>
{html_summary}
<h2>Historique de la Conversation</h2>
{html_content}
</body>
</html>
"""
# Convertir le HTML en PDF
pdf = pdfkit.from_string(html_template, False)
return pdf
def get_conversation():
conversation = []
for message in st.session_state.chat_history:
if isinstance(message, AIMessage):
conversation.append({"speaker": "AI", "text": message.content})
elif isinstance(message, HumanMessage):
conversation.append({"speaker": "Moi", "text": message.content})
return conversation
def export_conversation(summary):
logo_path = "https://static.wixstatic.com/media/d7d3da_b69e03ae99224f7d8b6e358918e60071~mv2.png/v1/crop/x_173,y_0,w_1906,h_938/fill/w_242,h_119,al_c,q_85,usm_0.66_1.00_0.01,enc_auto/BZIIIT_LOGO-HORIZ-COULEUR.png" # Replace with your image path
conversation = get_conversation()
image_path = "newplot.png"
pdf = create_pdf_from_markdown(logo_path, image_path, conversation,summary)
st.success("PDF généré avec succès!")
if st.download_button("Télécharger le PDF", data=pdf, file_name=f"Cartographie {st.session_state['Nom de la marque']}.pdf", mime="application/pdf"):
st.rerun()
|