[email protected] commited on
Commit
328456e
·
1 Parent(s): 06eeb91

feat: Add Markdown to HTML conversion for PDF generation

Browse files

- Implemented markdown_to_html function to convert Markdown text to HTML format.
- Enhanced PDF generation process to include formatted messages with appropriate styling.
- Updated config.yaml to streamline context and objective presentation.

Files changed (2) hide show
  1. config.yaml +6 -8
  2. utils/document.py +77 -7
config.yaml CHANGED
@@ -21,21 +21,19 @@ chapters:
21
  - "Quels sont les objectifs principaux ?"
22
  prompt_system: "ROLE : Tu es conseiller d'entreprise agricole\n
23
 
24
- CONTEXTE / RESSOURCES :\nTu as accès à l'entretien que le conseiller a eu avec l'exploitant agricole. Cet entretien est fourni sous forme de transcript à cette conversation (voir pièce jointe)\n
25
 
26
- OBJECTIF :\n
27
- Tu dois réaliser le paragraphe ' 1 Contexte et objectifs ' du DIAGNOSTIC PRE INSALLATION\n
 
28
 
29
- CONTRAINTE :\n
30
- Tu dois le présenter rigoureusement de la façon suivante :
31
- \n\n
32
  1. CONTEXTE ET OBJECTIFS\n
33
 
34
  * Présentation de l’exploitation à reprendre (Historique/ situation actuelle)
35
- [Section 01 : à compléter avec maximum 100 mots]
36
 
37
  * Présentation du candidat / Parcours de vie
38
- [Section 02 : à compléter avec maximum 100 mots]
39
 
40
  * Motivation et présentation du projet du candidat
41
  [Section 03 : à compléter avec maximum 60 mots]"
 
21
  - "Quels sont les objectifs principaux ?"
22
  prompt_system: "ROLE : Tu es conseiller d'entreprise agricole\n
23
 
24
+ CONTEXTE / RESSOURCES : Tu as accès à l'entretien que le conseiller a eu avec l'exploitant agricole. Cet entretien est fourni sous forme de transcript à cette conversation (voir pièce jointe)\n
25
 
26
+ OBJECTIF : Tu dois réaliser le paragraphe ' 1 Contexte et objectifs ' du DIAGNOSTIC PRE INSALLATION\n
27
+
28
+ CONTRAINTE : Tu dois le présenter rigoureusement de la façon suivante :
29
 
 
 
 
30
  1. CONTEXTE ET OBJECTIFS\n
31
 
32
  * Présentation de l’exploitation à reprendre (Historique/ situation actuelle)
33
+ [Section 01 : à compléter avec maximum 100 mots]\n
34
 
35
  * Présentation du candidat / Parcours de vie
36
+ [Section 02 : à compléter avec maximum 100 mots]\n
37
 
38
  * Motivation et présentation du projet du candidat
39
  [Section 03 : à compléter avec maximum 60 mots]"
utils/document.py CHANGED
@@ -1,5 +1,7 @@
1
  import pymupdf
2
  import tiktoken
 
 
3
 
4
  from io import BytesIO
5
  from reportlab.lib.pagesizes import A4
@@ -113,6 +115,69 @@ def audit_descriptif_pdf(file,max_img_width=500) -> dict:
113
 
114
  return global_audit
115
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
116
 
117
  def generate_pdf(chapter_data: list):
118
  buffer = BytesIO()
@@ -142,20 +207,25 @@ def generate_pdf(chapter_data: list):
142
  for msg in chapter["messages"]:
143
  if isinstance(msg, HumanMessage):
144
  color = "blue"
145
- sender = "👤 Utilisateur"
146
  elif isinstance(msg, AIMessage):
147
  color = "green"
148
- sender = "🤖 IA"
149
  elif isinstance(msg, SystemMessage):
150
  color = "red"
151
- sender = "⚙️ Système"
152
  else:
153
  color = "black"
154
- sender = "🔹 Message"
 
 
 
 
 
 
155
 
156
- print(msg.content)
157
- elements.append(Paragraph(f"<b><font color='{color}'>{sender} :</font></b> {msg.content}", style_message))
158
- elements.append(Spacer(1, 5))
159
 
160
  elements.append(Spacer(1, 15)) # Espacement entre discussions
161
 
 
1
  import pymupdf
2
  import tiktoken
3
+ import markdown
4
+ import re
5
 
6
  from io import BytesIO
7
  from reportlab.lib.pagesizes import A4
 
115
 
116
  return global_audit
117
 
118
+ # Fonction pour convertir le Markdown en HTML pour le PDF
119
+ def markdown_to_html(md_text):
120
+ return markdown.markdown(md_text, output_format='html' )
121
+ html = """
122
+ <html>
123
+ <head>
124
+ <style>
125
+ body { font-family: Arial, sans-serif; margin: 40px; }
126
+ h1 { text-align: center; color: #333; }
127
+ h2 { border-bottom: 2px solid #666; padding-bottom: 5px; margin-top: 30px; }
128
+ .message { margin-bottom: 10px; padding: 10px; border-radius: 5px; }
129
+ .human { background-color: #e1f5fe; }
130
+ .ai { background-color: #e8f5e9; }
131
+ .system { background-color: #ffebee; }
132
+ </style>
133
+ </head>
134
+ <body>
135
+ <h1>Conseiller augmenté CEGARA</h1>
136
+ """
137
+
138
+ # Trier les chapitres par "num"
139
+ chapters = sorted(chapter_data, key=lambda x: x["num"])
140
+
141
+ for chapter in chapters:
142
+ html += f"<h2>Chapitre {chapter['num']}: {chapter['title']}</h2>"
143
+
144
+ if len(chapter["messages"]) > 1 :
145
+ # Affichage des messages
146
+ for msg in chapter["messages"]:
147
+
148
+ if isinstance(msg, HumanMessage):
149
+ sender = "Utilisateur"
150
+ css_class = "human"
151
+ elif isinstance(msg, AIMessage):
152
+ sender = "IA"
153
+ css_class = "ai"
154
+ elif isinstance(msg, SystemMessage):
155
+ sender = "Système"
156
+ css_class = "system"
157
+ else:
158
+ sender = "Message"
159
+ css_class = ""
160
+
161
+ html += f"""
162
+ <div class="message {css_class}">
163
+ <b>{sender} :</b> {markdown_to_html(msg.content)}
164
+ </div>
165
+ """
166
+
167
+ html += "</body></html>"
168
+
169
+ return generate_pdf_from_html(html)
170
+
171
+ # Fonction pour convertir le Markdown en texte enrichi compatible ReportLab
172
+ def markdown_to_reportlab(text):
173
+ # text = text.replace("**", "<b>").replace("__", "<b>") # Gras
174
+ # text = text.replace("*", "<i>").replace("_", "<i>") # Italique
175
+ text = text.replace("\n", "<br/>") # Italique
176
+ # text = re.sub(r"\n- (.+)", r"\n• \1", text) # Listes à puces
177
+ # text = re.sub(r"^# (.+)", r"<b><font size='16'>\1</font></b>", text, flags=re.MULTILINE) # Titre H1
178
+ # text = re.sub(r"^## (.+)", r"<b><font size='14'>\1</font></b>", text, flags=re.MULTILINE) # Titre H2
179
+ # text = re.sub(r"^### (.+)", r"<b><font size='12'>\1</font></b>", text, flags=re.MULTILINE) # Titre H3
180
+ return text
181
 
182
  def generate_pdf(chapter_data: list):
183
  buffer = BytesIO()
 
207
  for msg in chapter["messages"]:
208
  if isinstance(msg, HumanMessage):
209
  color = "blue"
210
+ sender = "Utilisateur"
211
  elif isinstance(msg, AIMessage):
212
  color = "green"
213
+ sender = "Conseiller augmenté CEGARA"
214
  elif isinstance(msg, SystemMessage):
215
  color = "red"
216
+ sender = "Système"
217
  else:
218
  color = "black"
219
+ sender = "Message"
220
+
221
+ elements.append(Paragraph(f"<b><font color='{color}'>{sender}</font></b>", style_message))
222
+
223
+ content = msg.content
224
+ content = markdown_to_html(content)
225
+ content = markdown_to_reportlab(content)
226
 
227
+ elements.append(Paragraph(content, style_message))
228
+ elements.append(Spacer(1, 10))
 
229
 
230
  elements.append(Spacer(1, 15)) # Espacement entre discussions
231