[email protected] commited on
Commit
1d4c573
·
1 Parent(s): a9a644b

Include PDF generation

Browse files
Files changed (4) hide show
  1. app.py +20 -3
  2. pages/chatbot.py +11 -8
  3. requirements.txt +3 -2
  4. utils/document.py +56 -0
app.py CHANGED
@@ -1,12 +1,12 @@
1
  import streamlit as st
2
  import os
 
3
 
4
  from dotenv import load_dotenv
5
  from rag import Rag
6
- from vectore_store.PineconeConnector import PineconeConnector
7
- from vectore_store.VectoreStoreManager import VectoreStoreManager
8
 
9
  from util import getYamlConfig
 
10
 
11
  load_dotenv()
12
 
@@ -35,10 +35,11 @@ def init_app():
35
 
36
  for chapter in st.session_state["chapters"]:
37
  st.session_state[f"chapter_{chapter['num']}"] = {
 
38
  "num": chapter['num'],
39
  "messages": [],
40
  "prompt_system": chapter['prompt_system'],
41
- "prompt": chapter['prompts'],
42
  }
43
 
44
 
@@ -86,6 +87,22 @@ def main():
86
  }
87
  )
88
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
89
  st.sidebar.markdown("### Documentation")
90
  st.sidebar.markdown("*Documentation :* [*Lien*]({})".format("https://docs.google.com/presentation/d/1b-cnCKge1qRBS26U5FfMScQPco4gZTn7tN1idhccsso/edit?usp=sharing"))
91
  st.sidebar.markdown("*Evaluation App. IA :*")
 
1
  import streamlit as st
2
  import os
3
+ import base64
4
 
5
  from dotenv import load_dotenv
6
  from rag import Rag
 
 
7
 
8
  from util import getYamlConfig
9
+ from utils.document import generate_pdf
10
 
11
  load_dotenv()
12
 
 
35
 
36
  for chapter in st.session_state["chapters"]:
37
  st.session_state[f"chapter_{chapter['num']}"] = {
38
+ "title": chapter['name'],
39
  "num": chapter['num'],
40
  "messages": [],
41
  "prompt_system": chapter['prompt_system'],
42
+ "prompts": chapter['prompts'],
43
  }
44
 
45
 
 
87
  }
88
  )
89
 
90
+ if st.sidebar.button("Générer le PDF"):
91
+
92
+ chapters = []
93
+
94
+ for chapter in st.session_state["chapters"]:
95
+ chapters.append(st.session_state[f"chapter_{chapter['num']}"])
96
+
97
+ pdf_buffer = generate_pdf(chapters)
98
+
99
+ st.sidebar.download_button(
100
+ label="📥 Télécharger le PDF",
101
+ data=pdf_buffer,
102
+ file_name="diagnostique_CEGARA.pdf",
103
+ mime="application/pdf"
104
+ )
105
+
106
  st.sidebar.markdown("### Documentation")
107
  st.sidebar.markdown("*Documentation :* [*Lien*]({})".format("https://docs.google.com/presentation/d/1b-cnCKge1qRBS26U5FfMScQPco4gZTn7tN1idhccsso/edit?usp=sharing"))
108
  st.sidebar.markdown("*Evaluation App. IA :*")
pages/chatbot.py CHANGED
@@ -23,7 +23,7 @@ def display_messages():
23
  model = message.kwargs.get("model", "Unknown Model") # Get the model, default to "Unknown Model"
24
  st.write(f"**Model :** {model}")
25
  st.markdown(message.content)
26
- st_copy_to_clipboard(message.content,key=f"message_{chapter_num}_{i}")
27
 
28
  elif isinstance(message, HumanMessage):
29
  with st.chat_message("Moi"):
@@ -54,13 +54,15 @@ def show_prompts(prompts: list[str]):
54
  expander = st.expander("Prompts pré-définis")
55
 
56
  for i, p in enumerate(prompts):
57
- if expander.button(p, key=f"button_{chapter_num}_{i}"):
58
  launchQuery(p)
59
 
60
 
61
  def page():
62
- yaml_data = getYamlConfig()["chapters"]
63
-
 
 
64
 
65
  ###############################################
66
  # Récupération du chapitre depuis YAML Config #
@@ -72,7 +74,7 @@ def page():
72
 
73
  setChapter(int(chapter_num))
74
 
75
- chapter = next((ch for ch in yaml_data if ch['num'] == chapter_num), None)
76
  if not chapter:
77
  st.text("Chapitre non trouvé")
78
  return
@@ -88,7 +90,7 @@ def page():
88
  if "messages" not in st.session_state[chapter_session_key]:
89
  st.session_state[chapter_session_key]["messages"] = [ ]
90
 
91
- if len(st.session_state[chapter_session_key]["messages"]) == 0 :
92
  st.session_state[chapter_session_key]["messages"] = [ SystemMessage(content=chapter["prompt_system"]) ]
93
 
94
 
@@ -96,7 +98,7 @@ def page():
96
  #### UI ####
97
  ############
98
 
99
- st.subheader(chapter['name'])
100
 
101
  st.markdown("<style>iframe{height:50px;}</style>", unsafe_allow_html=True)
102
 
@@ -110,8 +112,9 @@ def page():
110
  display_messages()
111
 
112
 
113
- user_query = st.chat_input("", key=str(uuid.uuid4()))
114
 
 
115
  if user_query is not None and user_query != "":
116
  print(user_query)
117
 
 
23
  model = message.kwargs.get("model", "Unknown Model") # Get the model, default to "Unknown Model"
24
  st.write(f"**Model :** {model}")
25
  st.markdown(message.content)
26
+ st_copy_to_clipboard(message.content,key=f"message_{chapter_num}_{i}_{uuid.uuid4()}")
27
 
28
  elif isinstance(message, HumanMessage):
29
  with st.chat_message("Moi"):
 
54
  expander = st.expander("Prompts pré-définis")
55
 
56
  for i, p in enumerate(prompts):
57
+ if expander.button(p, key=f"button_{chapter_num}_{i}_{uuid.uuid4()}"):
58
  launchQuery(p)
59
 
60
 
61
  def page():
62
+
63
+ chapters = []
64
+ for chapter in st.session_state["chapters"]:
65
+ chapters.append(st.session_state[f"chapter_{chapter['num']}"])
66
 
67
  ###############################################
68
  # Récupération du chapitre depuis YAML Config #
 
74
 
75
  setChapter(int(chapter_num))
76
 
77
+ chapter = next((ch for ch in chapters if ch['num'] == chapter_num), None)
78
  if not chapter:
79
  st.text("Chapitre non trouvé")
80
  return
 
90
  if "messages" not in st.session_state[chapter_session_key]:
91
  st.session_state[chapter_session_key]["messages"] = [ ]
92
 
93
+ if len(st.session_state[chapter_session_key]["messages"]) < 2 :
94
  st.session_state[chapter_session_key]["messages"] = [ SystemMessage(content=chapter["prompt_system"]) ]
95
 
96
 
 
98
  #### UI ####
99
  ############
100
 
101
+ st.subheader(chapter['title'])
102
 
103
  st.markdown("<style>iframe{height:50px;}</style>", unsafe_allow_html=True)
104
 
 
112
  display_messages()
113
 
114
 
115
+ user_query = st.chat_input("", key=f"input_{chapter_session_key}")
116
 
117
+ print(f"submission : {user_query}")
118
  if user_query is not None and user_query != "":
119
  print(user_query)
120
 
requirements.txt CHANGED
@@ -19,6 +19,7 @@ langchain_anthropic
19
  llamaapi
20
  pyyaml
21
  st_copy_to_clipboard
22
- faiss-gpu
23
  faiss-cpu
24
- tiktoken
 
 
19
  llamaapi
20
  pyyaml
21
  st_copy_to_clipboard
22
+ # faiss-gpu
23
  faiss-cpu
24
+ tiktoken
25
+ reportlab
utils/document.py CHANGED
@@ -1,6 +1,12 @@
1
  import pymupdf
2
  import tiktoken
3
 
 
 
 
 
 
 
4
 
5
  def count_tokens(input_string: str) -> int:
6
  tokenizer = tiktoken.get_encoding("cl100k_base")
@@ -106,3 +112,53 @@ def audit_descriptif_pdf(file,max_img_width=500) -> dict:
106
  }
107
 
108
  return global_audit
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import pymupdf
2
  import tiktoken
3
 
4
+ from io import BytesIO
5
+ from reportlab.lib.pagesizes import A4
6
+ from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer
7
+ from reportlab.lib.styles import getSampleStyleSheet
8
+ from reportlab.lib.enums import TA_CENTER
9
+ from langchain_core.messages import AIMessage, HumanMessage, SystemMessage
10
 
11
  def count_tokens(input_string: str) -> int:
12
  tokenizer = tiktoken.get_encoding("cl100k_base")
 
112
  }
113
 
114
  return global_audit
115
+
116
+
117
+ def generate_pdf(chapter_data: list):
118
+ buffer = BytesIO()
119
+ doc = SimpleDocTemplate(buffer, pagesize=A4)
120
+
121
+ styles = getSampleStyleSheet()
122
+ style_title = styles["Title"]
123
+ style_title.alignment = TA_CENTER # Centrer le titre
124
+ style_header = styles["Heading2"]
125
+ style_message = styles["BodyText"]
126
+
127
+ elements = []
128
+
129
+ # Titre principal du document
130
+ elements.append(Paragraph("Conseiller augmenté CEGARA", style_title))
131
+ elements.append(Spacer(1, 20)) # Espacement après le titre
132
+
133
+ # Trier les chapitres par "num"
134
+ chapters = sorted(chapter_data, key=lambda x: x["num"])
135
+
136
+ for chapter in chapters:
137
+ # Ajouter le titre de la discussion
138
+ elements.append(Paragraph(f"Chapitre {chapter['num']}: {chapter['title']}", style_header))
139
+ elements.append(Spacer(1, 10))
140
+
141
+ if len(chapter["messages"]) > 1 :
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
+
162
+ doc.build(elements)
163
+ buffer.seek(0)
164
+ return buffer