|
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): |
|
|
|
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) |
|
|
|
html_content = markdown2.markdown(markdown_text) |
|
html_summary = markdown2.markdown(markdown_summary) |
|
|
|
|
|
|
|
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> |
|
""" |
|
|
|
|
|
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" |
|
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() |
|
|
|
|