| import logging |
| import re |
| from typing import List |
| from fpdf import FPDF |
| from datetime import datetime |
|
|
| |
| logging.basicConfig(level=logging.INFO) |
| logger = logging.getLogger(__name__) |
|
|
|
|
| def create_pdf_report(output_path: str, logo_path: str, report_text: str): |
| """ |
| Creates a PDF report with a logo, the current date, and a given text. |
| |
| Args: |
| output_path (str): The path where the generated PDF will be saved. |
| logo_path (str): The path to the logo image to include in the report. |
| report_text (str): The text content to include in the report. |
| |
| Returns |
| ------- |
| None: The function saves the PDF to the specified output path. |
| |
| Raises |
| ------ |
| FileNotFoundError: If the logo file does not exist. |
| ValueError: If the provided paths or text are invalid. |
| """ |
| pdf = FPDF() |
| pdf.add_page() |
|
|
| |
| pdf.set_font("Arial", size=12) |
|
|
| |
| try: |
| pdf.image(logo_path, x=10, y=8, w=30) |
| except FileNotFoundError: |
| raise FileNotFoundError(f"Logo file not found at: {logo_path}") |
|
|
| |
| pdf.set_font("Arial", style="B", size=16) |
| pdf.cell(200, 10, txt="Rapport de conversation avec Dis-ADEME", ln=True, align="C") |
|
|
| |
| pdf.set_font("Arial", size=12) |
| creation_date = datetime.now().strftime("%Y-%m-%d %H:%M:%S") |
| pdf.ln(10) |
| pdf.cell( |
| 200, |
| 10, |
| txt=f"Date de création : { |
| creation_date}", |
| ln=True, |
| align="R", |
| ) |
|
|
| |
| pdf.ln(20) |
| pdf.set_font("Arial", size=12) |
| pdf.multi_cell(0, 10, txt=report_text) |
|
|
| |
| try: |
| pdf.output(output_path) |
| logger.info(f"PDF report created successfully at: {output_path}") |
| except Exception as e: |
| raise ValueError(f"Failed to save PDF: {e}") |
|
|
|
|
| def extract_pdf_references(conversation: List[dict]) -> List[str]: |
| """ |
| Extract unique PDF references from the chatbot's responses in the conversation. |
| |
| Args: |
| conversation (List[dict]): List of dictionaries representing the conversation. |
| Each dictionary contains 'role' ('user' or 'assistant') |
| and 'content' (message string). |
| |
| Returns: |
| List[str]: A list of unique PDF references mentioned in the chatbot's responses. |
| """ |
| pdf_references = set() |
|
|
| for message in conversation: |
| if ( |
| message.get("role") == "assistant" |
| and "Consultez les documents suivants pour plus d'information:" |
| in message.get("content", "") |
| ): |
| |
| matches = re.findall(r"[\w\s-]+\.pdf", message["content"]) |
| pdf_references.update(matches) |
| return sorted(pdf_references) |
|
|