main.py 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. INPUTS = {
  2. "Allianz Vie_2025.pdf": {
  3. "S.05": []
  4. }
  5. }
  6. from workflow_agents import app
  7. from function_exctract import extract_pages, extract_pdf_path
  8. from dotenv import load_dotenv
  9. from langfuse.langchain import CallbackHandler
  10. # Initialize Langfuse CallbackHandler for Langchain (tracing)
  11. langfuse_handler = CallbackHandler()
  12. load_dotenv()
  13. if __name__ == "__main__":
  14. # 1. Boucle sur chaque entreprise (Fichier PDF)
  15. for pdf_filename, sections_input in INPUTS.items():
  16. print("----------------------------------------")
  17. print(f"\n DÉMARRAGE ENTREPRISE : {pdf_filename}")
  18. entreprise_clean = pdf_filename.replace(".pdf", "")
  19. # Récupère le vrai chemin du PDF
  20. pdf_path = extract_pdf_path(pdf_filename)
  21. print(f" PDF trouvé : {pdf_path}")
  22. # Récupère les pages depuis Excel
  23. sections_with_pages = extract_pages(pdf_filename)
  24. # Fusionne : garde seulement les sections choisies dans INPUTS
  25. # et remplit les pages depuis Excel
  26. sections = {
  27. s: sections_with_pages.get(s, [])
  28. for s in sections_input.keys()
  29. }
  30. # 2. Boucle sur chaque section
  31. # 2. Boucle sur chaque section
  32. for section_name, pages in sections.items():
  33. # S'assurer que pages est une liste (ex: [45, 46])
  34. list_pages = pages if isinstance(pages, list) else [pages]
  35. # --- NOUVELLE BOUCLE PAR PAGE ---
  36. for p in list_pages:
  37. print(f" Traitement Section : {section_name} — PAGE INDIVIDUELLE : {p}")
  38. initial_state = {
  39. "messages": [],
  40. "pdf_path": pdf_path,
  41. "page": p, # On envoie le numéro unique
  42. "entreprise_name": entreprise_clean,
  43. "section_name": f"{section_name}_page_{p}" # On change le nom pour le fichier final
  44. }
  45. try:
  46. # L'agent est appelé pour CHAQUE page
  47. print("----------------------------------------")
  48. final_state = app.invoke(initial_state, config={"callbacks": [langfuse_handler]})
  49. print(f" Page {p} de la section {section_name} terminée.")
  50. except Exception as e:
  51. print(f" Erreur sur la page {p}: {e}")
  52. print("----------------------------------------")
  53. #print("\n✨ TOUTES LES ENTREPRISES ET SECTIONS ONT ÉTÉ TRAITÉES.")