main.py 2.5 KB

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