Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -0,0 +1,88 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
|
| 3 |
+
import streamlit as st
|
| 4 |
+
from config import index, client, langsmith_project
|
| 5 |
+
from pdf_processing import get_existing_pdf, load_and_preprocess_pdf, split_text
|
| 6 |
+
from pinecone_utils import index_pdf, retrieve_documents
|
| 7 |
+
from graph_agent import agent
|
| 8 |
+
|
| 9 |
+
# Load and index PDF (only once)
|
| 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(texts)
|
| 17 |
+
|
| 18 |
+
# Initialize PDF indexing
|
| 19 |
+
initialize_pdf_indexing()
|
| 20 |
+
|
| 21 |
+
# 🔹 Initialisation de l'historique des interactions
|
| 22 |
+
if "chat_history" not in st.session_state:
|
| 23 |
+
st.session_state.chat_history = []
|
| 24 |
+
|
| 25 |
+
# 🔹 Fonction pour traiter la requête de l'utilisateur
|
| 26 |
+
def process_query(query):
|
| 27 |
+
"""Process the user query and update the chat history."""
|
| 28 |
+
# Ajouter la question de l'utilisateur à l'historique
|
| 29 |
+
st.session_state.chat_history.append({"role": "user", "content": query})
|
| 30 |
+
|
| 31 |
+
# Exécuter l'agent avec un indicateur de chargement
|
| 32 |
+
with st.spinner("Recherche en cours..."):
|
| 33 |
+
initial_state = {"query": query, "messages": [], "relevant_docs": [], "response": ""}
|
| 34 |
+
result = agent.invoke(initial_state)
|
| 35 |
+
response = result["response"]
|
| 36 |
+
|
| 37 |
+
# Ajouter la réponse de l'assistant à l'historique
|
| 38 |
+
st.session_state.chat_history.append({"role": "assistant", "content": response})
|
| 39 |
+
|
| 40 |
+
# Log the query and response in LangSmith
|
| 41 |
+
client.create_run(
|
| 42 |
+
name="RAG_Query",
|
| 43 |
+
run_type="chain",
|
| 44 |
+
project_name=langsmith_project,
|
| 45 |
+
inputs={"query": query},
|
| 46 |
+
outputs={"response": response},
|
| 47 |
+
)
|
| 48 |
+
|
| 49 |
+
# 🔹 Barre latérale améliorée
|
| 50 |
+
def display_sidebar():
|
| 51 |
+
"""Display the sidebar with additional information and options."""
|
| 52 |
+
with st.sidebar:
|
| 53 |
+
st.title("📄 La confession muette")
|
| 54 |
+
st.write("Posez vos questions sur le document.")
|
| 55 |
+
st.image(agent.get_graph().draw_mermaid_png(), caption="Workflow Graph")
|
| 56 |
+
|
| 57 |
+
# 🔹 Afficher l'historique de chat
|
| 58 |
+
def display_chat_history():
|
| 59 |
+
"""Display the chat history in a conversational format."""
|
| 60 |
+
for message in st.session_state.chat_history:
|
| 61 |
+
if message["role"] == "user":
|
| 62 |
+
with st.chat_message("user"):
|
| 63 |
+
st.markdown(message["content"])
|
| 64 |
+
elif message["role"] == "assistant":
|
| 65 |
+
with st.chat_message("assistant"):
|
| 66 |
+
st.markdown(message["content"])
|
| 67 |
+
|
| 68 |
+
# 🔹 Point d'entrée de l'application
|
| 69 |
+
def main():
|
| 70 |
+
"""Main function to run the Streamlit app."""
|
| 71 |
+
st.title("Architecture A")
|
| 72 |
+
|
| 73 |
+
# Afficher la barre latérale
|
| 74 |
+
display_sidebar()
|
| 75 |
+
|
| 76 |
+
# Afficher l'historique de chat
|
| 77 |
+
display_chat_history()
|
| 78 |
+
|
| 79 |
+
# Case d'entrée des questions en bas
|
| 80 |
+
query = st.chat_input("Posez votre question ici:")
|
| 81 |
+
if query:
|
| 82 |
+
process_query(query)
|
| 83 |
+
st.rerun() # Rafraîchir la page pour afficher immédiatement les nouvelles réponses
|
| 84 |
+
|
| 85 |
+
if __name__ == "__main__":
|
| 86 |
+
main()
|
| 87 |
+
|
| 88 |
+
|