Votre Nom commited on
Commit ·
1965e4e
1
Parent(s): 43f2b89
gestion source
Browse files- src/streamlit_app.py +5 -60
src/streamlit_app.py
CHANGED
|
@@ -1,9 +1,7 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
import pandas as pd
|
| 3 |
from datetime import datetime
|
| 4 |
-
import json
|
| 5 |
from RAG import answer_question, retrieve_chunks, embed_question, build_context
|
| 6 |
-
import base64
|
| 7 |
|
| 8 |
# Configuration de la page
|
| 9 |
st.set_page_config(
|
|
@@ -89,61 +87,6 @@ if "messages" not in st.session_state:
|
|
| 89 |
if "sources" not in st.session_state:
|
| 90 |
st.session_state["sources"] = []
|
| 91 |
|
| 92 |
-
# Fonction pour exporter les conversations
|
| 93 |
-
def export_conversation(format_type="txt"):
|
| 94 |
-
if not st.session_state["messages"]:
|
| 95 |
-
return None
|
| 96 |
-
|
| 97 |
-
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
|
| 98 |
-
|
| 99 |
-
if format_type == "txt":
|
| 100 |
-
content = f"Conversation Tindle RAG - {datetime.now().strftime('%d/%m/%Y %H:%M')}\n"
|
| 101 |
-
content += "=" * 50 + "\n\n"
|
| 102 |
-
|
| 103 |
-
for i, msg in enumerate(st.session_state["messages"]):
|
| 104 |
-
content += f"{msg['role'].upper()}: {msg['content']}\n\n"
|
| 105 |
-
|
| 106 |
-
# Ajouter les sources si disponibles
|
| 107 |
-
if msg["role"] == "assistant" and i < len(st.session_state["sources"]):
|
| 108 |
-
content += "Sources utilisées:\n"
|
| 109 |
-
for j, source in enumerate(st.session_state["sources"][i], 1):
|
| 110 |
-
content += f"{j}. {source['id']}@{source['offset']} (score: {source['score']:.3f})\n"
|
| 111 |
-
content += f" {source['text'][:200]}...\n\n"
|
| 112 |
-
|
| 113 |
-
return content, f"conversation_tindle_{timestamp}.txt"
|
| 114 |
-
|
| 115 |
-
elif format_type == "json":
|
| 116 |
-
data = {
|
| 117 |
-
"timestamp": datetime.now().isoformat(),
|
| 118 |
-
"messages": st.session_state["messages"],
|
| 119 |
-
"sources": st.session_state["sources"]
|
| 120 |
-
}
|
| 121 |
-
return json.dumps(data, indent=2, ensure_ascii=False), f"conversation_tindle_{timestamp}.json"
|
| 122 |
-
|
| 123 |
-
# Barre d'export
|
| 124 |
-
col1, col2, col3 = st.columns([1, 1, 2])
|
| 125 |
-
with col1:
|
| 126 |
-
if st.button("📄 Exporter TXT"):
|
| 127 |
-
if st.session_state["messages"]:
|
| 128 |
-
content, filename = export_conversation("txt")
|
| 129 |
-
st.download_button(
|
| 130 |
-
label="💾 Télécharger TXT",
|
| 131 |
-
data=content,
|
| 132 |
-
file_name=filename,
|
| 133 |
-
mime="text/plain"
|
| 134 |
-
)
|
| 135 |
-
|
| 136 |
-
with col2:
|
| 137 |
-
if st.button("📊 Exporter JSON"):
|
| 138 |
-
if st.session_state["messages"]:
|
| 139 |
-
content, filename = export_conversation("json")
|
| 140 |
-
st.download_button(
|
| 141 |
-
label="💾 Télécharger JSON",
|
| 142 |
-
data=content,
|
| 143 |
-
file_name=filename,
|
| 144 |
-
mime="application/json"
|
| 145 |
-
)
|
| 146 |
-
|
| 147 |
# Affichage de l'historique du chat avec sources
|
| 148 |
for i, msg in enumerate(st.session_state["messages"]):
|
| 149 |
if msg["role"] == "user":
|
|
@@ -154,9 +97,11 @@ for i, msg in enumerate(st.session_state["messages"]):
|
|
| 154 |
st.markdown(msg["content"])
|
| 155 |
|
| 156 |
# Afficher les sources si disponibles et demandées
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
|
|
|
|
|
|
| 160 |
st.markdown(f"""
|
| 161 |
<div class="source-preview">
|
| 162 |
<strong>Source {j}:</strong> {source['id']}@{source['offset']}
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import pandas as pd
|
| 3 |
from datetime import datetime
|
|
|
|
| 4 |
from RAG import answer_question, retrieve_chunks, embed_question, build_context
|
|
|
|
| 5 |
|
| 6 |
# Configuration de la page
|
| 7 |
st.set_page_config(
|
|
|
|
| 87 |
if "sources" not in st.session_state:
|
| 88 |
st.session_state["sources"] = []
|
| 89 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
# Affichage de l'historique du chat avec sources
|
| 91 |
for i, msg in enumerate(st.session_state["messages"]):
|
| 92 |
if msg["role"] == "user":
|
|
|
|
| 97 |
st.markdown(msg["content"])
|
| 98 |
|
| 99 |
# Afficher les sources si disponibles et demandées
|
| 100 |
+
# Correction de l'index : on cherche les sources pour cette réponse spécifique
|
| 101 |
+
source_index = len([m for m in st.session_state["messages"][:i] if m["role"] == "assistant"])
|
| 102 |
+
if show_sources and source_index < len(st.session_state["sources"]) and st.session_state["sources"][source_index]:
|
| 103 |
+
with st.expander(f"📚 Sources utilisées ({len(st.session_state['sources'][source_index])} passages)"):
|
| 104 |
+
for j, source in enumerate(st.session_state["sources"][source_index], 1):
|
| 105 |
st.markdown(f"""
|
| 106 |
<div class="source-preview">
|
| 107 |
<strong>Source {j}:</strong> {source['id']}@{source['offset']}
|