Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,41 +1,40 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
from config import index, client, langsmith_project
|
| 3 |
from pdf_processing import get_existing_pdf, load_and_preprocess_pdf, split_text
|
| 4 |
-
from
|
| 5 |
-
from
|
|
|
|
|
|
|
| 6 |
|
| 7 |
-
#
|
| 8 |
@st.cache_resource
|
| 9 |
def initialize_pdf_indexing():
|
| 10 |
pdf_path = get_existing_pdf()
|
| 11 |
if pdf_path:
|
| 12 |
text = load_and_preprocess_pdf(pdf_path)
|
| 13 |
texts = split_text(text)
|
| 14 |
-
|
|
|
|
| 15 |
|
| 16 |
-
#
|
| 17 |
initialize_pdf_indexing()
|
| 18 |
|
| 19 |
# 🔹 Initialisation de l'historique des interactions
|
| 20 |
if "chat_history" not in st.session_state:
|
| 21 |
st.session_state.chat_history = []
|
| 22 |
|
| 23 |
-
# 🔹 Fonction pour traiter la requête de l'utilisateur
|
| 24 |
-
def
|
| 25 |
-
"""
|
| 26 |
-
# Ajouter la question de l'utilisateur à l'historique
|
| 27 |
st.session_state.chat_history.append({"role": "user", "content": query})
|
| 28 |
|
| 29 |
-
# Exécuter l'agent avec un indicateur de chargement
|
| 30 |
with st.spinner("Recherche en cours..."):
|
| 31 |
initial_state = {"query": query, "messages": [], "relevant_docs": [], "response": ""}
|
| 32 |
-
result =
|
| 33 |
response = result["response"]
|
| 34 |
|
| 35 |
-
# Ajouter la réponse de l'assistant à l'historique
|
| 36 |
st.session_state.chat_history.append({"role": "assistant", "content": response})
|
| 37 |
|
| 38 |
-
# Log the query and response in LangSmith
|
| 39 |
client.create_run(
|
| 40 |
name="RAG_Query",
|
| 41 |
run_type="chain",
|
|
@@ -44,20 +43,44 @@ def process_query(query):
|
|
| 44 |
outputs={"response": response},
|
| 45 |
)
|
| 46 |
|
| 47 |
-
# 🔹
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
|
| 49 |
-
|
| 50 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
|
|
|
|
| 52 |
def display_sidebar():
|
| 53 |
-
"""
|
| 54 |
with st.sidebar:
|
| 55 |
-
st.
|
| 56 |
-
st.
|
|
|
|
| 57 |
|
| 58 |
# 🔹 Afficher l'historique de chat
|
| 59 |
def display_chat_history():
|
| 60 |
-
"""
|
| 61 |
for message in st.session_state.chat_history:
|
| 62 |
if message["role"] == "user":
|
| 63 |
with st.chat_message("user"):
|
|
@@ -68,8 +91,11 @@ def display_chat_history():
|
|
| 68 |
|
| 69 |
# 🔹 Point d'entrée de l'application
|
| 70 |
def main():
|
| 71 |
-
"""
|
| 72 |
-
st.title("Architecture A")
|
|
|
|
|
|
|
|
|
|
| 73 |
|
| 74 |
# Afficher la barre latérale
|
| 75 |
display_sidebar()
|
|
@@ -80,10 +106,11 @@ def main():
|
|
| 80 |
# Case d'entrée des questions en bas
|
| 81 |
query = st.chat_input("Posez votre question ici:")
|
| 82 |
if query:
|
| 83 |
-
|
|
|
|
|
|
|
|
|
|
| 84 |
st.rerun() # Rafraîchir la page pour afficher immédiatement les nouvelles réponses
|
| 85 |
|
| 86 |
if __name__ == "__main__":
|
| 87 |
-
main()
|
| 88 |
-
|
| 89 |
-
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
from config import index, client, langsmith_project
|
| 3 |
from pdf_processing import get_existing_pdf, load_and_preprocess_pdf, split_text
|
| 4 |
+
from pinecone_utilsA import index_pdf as index_pdf_A, retrieve_documents as retrieve_documents_A, decompress_text
|
| 5 |
+
from pinecone_utilsB import index_pdf as index_pdf_B, retrieve_documents as retrieve_documents_B
|
| 6 |
+
from graph_agentA import agent as agent_A
|
| 7 |
+
from graph_agentB import agent as agent_B
|
| 8 |
|
| 9 |
+
# 🔹 Initialisation de l'indexation PDF (commune aux deux architectures)
|
| 10 |
@st.cache_resource
|
| 11 |
def initialize_pdf_indexing():
|
| 12 |
pdf_path = get_existing_pdf()
|
| 13 |
if pdf_path:
|
| 14 |
text = load_and_preprocess_pdf(pdf_path)
|
| 15 |
texts = split_text(text)
|
| 16 |
+
index_pdf_A(texts) # Indexation pour l'architecture A
|
| 17 |
+
index_pdf_B(texts) # Indexation pour l'architecture B
|
| 18 |
|
| 19 |
+
# Initialiser l'indexation PDF
|
| 20 |
initialize_pdf_indexing()
|
| 21 |
|
| 22 |
# 🔹 Initialisation de l'historique des interactions
|
| 23 |
if "chat_history" not in st.session_state:
|
| 24 |
st.session_state.chat_history = []
|
| 25 |
|
| 26 |
+
# 🔹 Fonction pour traiter la requête de l'utilisateur (Architecture A)
|
| 27 |
+
def process_query_A(query):
|
| 28 |
+
"""Traitement de la requête avec l'architecture A."""
|
|
|
|
| 29 |
st.session_state.chat_history.append({"role": "user", "content": query})
|
| 30 |
|
|
|
|
| 31 |
with st.spinner("Recherche en cours..."):
|
| 32 |
initial_state = {"query": query, "messages": [], "relevant_docs": [], "response": ""}
|
| 33 |
+
result = agent_A.invoke(initial_state)
|
| 34 |
response = result["response"]
|
| 35 |
|
|
|
|
| 36 |
st.session_state.chat_history.append({"role": "assistant", "content": response})
|
| 37 |
|
|
|
|
| 38 |
client.create_run(
|
| 39 |
name="RAG_Query",
|
| 40 |
run_type="chain",
|
|
|
|
| 43 |
outputs={"response": response},
|
| 44 |
)
|
| 45 |
|
| 46 |
+
# 🔹 Fonction pour traiter la requête de l'utilisateur (Architecture B)
|
| 47 |
+
def process_query_B(query):
|
| 48 |
+
"""Traitement de la requête avec l'architecture B."""
|
| 49 |
+
st.session_state.chat_history.append({"role": "user", "content": query})
|
| 50 |
+
|
| 51 |
+
with st.spinner("Recherche en cours..."):
|
| 52 |
+
retrieved_docs = retrieve_documents_B(query)
|
| 53 |
+
|
| 54 |
+
initial_state = {
|
| 55 |
+
"query": query,
|
| 56 |
+
"messages": [],
|
| 57 |
+
"combined_docs": retrieved_docs, # 🔹 Fusion des résultats
|
| 58 |
+
"response": ""
|
| 59 |
+
}
|
| 60 |
+
result = agent_B.invoke(initial_state)
|
| 61 |
+
response = result["response"]
|
| 62 |
|
| 63 |
+
st.session_state.chat_history.append({"role": "assistant", "content": response})
|
| 64 |
+
|
| 65 |
+
client.create_run(
|
| 66 |
+
name="RAG_Query",
|
| 67 |
+
run_type="chain",
|
| 68 |
+
project_name=langsmith_project,
|
| 69 |
+
inputs={"query": query, "retrieved_docs": retrieved_docs},
|
| 70 |
+
outputs={"response": response},
|
| 71 |
+
)
|
| 72 |
|
| 73 |
+
# 🔹 Barre latérale améliorée
|
| 74 |
def display_sidebar():
|
| 75 |
+
"""Affiche la barre latérale avec des informations supplémentaires."""
|
| 76 |
with st.sidebar:
|
| 77 |
+
st.title("📄 La confession muette")
|
| 78 |
+
st.write("Posez vos questions sur le document.")
|
| 79 |
+
st.image(agent_A.get_graph().draw_mermaid_png(), caption="Workflow Graph")
|
| 80 |
|
| 81 |
# 🔹 Afficher l'historique de chat
|
| 82 |
def display_chat_history():
|
| 83 |
+
"""Affiche l'historique de chat dans un format conversationnel."""
|
| 84 |
for message in st.session_state.chat_history:
|
| 85 |
if message["role"] == "user":
|
| 86 |
with st.chat_message("user"):
|
|
|
|
| 91 |
|
| 92 |
# 🔹 Point d'entrée de l'application
|
| 93 |
def main():
|
| 94 |
+
"""Fonction principale pour exécuter l'application Streamlit."""
|
| 95 |
+
st.title("Architecture A et B")
|
| 96 |
+
|
| 97 |
+
# 🔹 Checkbox pour choisir l'architecture
|
| 98 |
+
use_architecture_B = st.checkbox("Utiliser l'architecture B (avancée)")
|
| 99 |
|
| 100 |
# Afficher la barre latérale
|
| 101 |
display_sidebar()
|
|
|
|
| 106 |
# Case d'entrée des questions en bas
|
| 107 |
query = st.chat_input("Posez votre question ici:")
|
| 108 |
if query:
|
| 109 |
+
if use_architecture_B:
|
| 110 |
+
process_query_B(query) # Utiliser l'architecture B
|
| 111 |
+
else:
|
| 112 |
+
process_query_A(query) # Utiliser l'architecture A
|
| 113 |
st.rerun() # Rafraîchir la page pour afficher immédiatement les nouvelles réponses
|
| 114 |
|
| 115 |
if __name__ == "__main__":
|
| 116 |
+
main()
|
|
|
|
|
|