Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,156 +1,109 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
import os
|
| 3 |
-
import shutil
|
| 4 |
-
import zipfile
|
| 5 |
-
import pytesseract
|
| 6 |
-
from pdf2image import convert_from_path
|
| 7 |
-
from PIL import Image
|
| 8 |
import re
|
| 9 |
-
|
|
|
|
|
|
|
|
|
|
| 10 |
import pandas as pd
|
|
|
|
| 11 |
|
| 12 |
-
#
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
Ce service vous permet de :
|
| 18 |
-
- Extraire nom, prénom, référence (DI), et date de séance à partir de fichiers PDF ou ZIP.
|
| 19 |
-
- Renommer les fichiers selon un format structuré.
|
| 20 |
-
- Organiser les fichiers dans des dossiers par date.
|
| 21 |
-
- Générer un fichier Excel avec les informations extraites.
|
| 22 |
|
| 23 |
-
|
| 24 |
-
""
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
-
def process_pdfs(
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
output_excel = "rename_log.xlsx"
|
| 30 |
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
shutil.rmtree(pdf_folder)
|
| 34 |
os.makedirs(pdf_folder, exist_ok=True)
|
| 35 |
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
if input_file.name.endswith(".zip"):
|
| 39 |
-
with zipfile.ZipFile(input_file.name, 'r') as zip_ref:
|
| 40 |
-
zip_ref.extractall(pdf_folder)
|
| 41 |
-
input_paths = [os.path.join(pdf_folder, f) for f in os.listdir(pdf_folder) if f.lower().endswith(".pdf")]
|
| 42 |
-
elif input_file.name.endswith(".pdf"):
|
| 43 |
-
dest_path = os.path.join(pdf_folder, os.path.basename(input_file.name))
|
| 44 |
-
shutil.copy(input_file.name, dest_path)
|
| 45 |
-
input_paths = [dest_path]
|
| 46 |
-
else:
|
| 47 |
-
return None, None, "❌ Format de fichier non supporté."
|
| 48 |
-
|
| 49 |
-
# Patterns Regex
|
| 50 |
-
title_pattern = re.compile(r"(?:Monsieur|Madame|Morveuwer|De heer)\s+((?:[\w\-éèêëàâäîïôöùûüç']+\s+){1,3}[\w\-éèêëàâäîïôöùûüç']+)", re.IGNORECASE)
|
| 51 |
-
seance_pattern = re.compile(r"SEANCE\s+du\s+(\d{2})[\/\-](\d{2})[\/\-](\d{4})", re.IGNORECASE)
|
| 52 |
-
ref_pattern = re.compile(r"n\.réf\s*[:\-]?\s*das\/(?:[\w]+\/)*(\d{4})\/(\d+)", re.IGNORECASE)
|
| 53 |
-
|
| 54 |
-
log_data = []
|
| 55 |
-
processed_files = []
|
| 56 |
-
|
| 57 |
-
for filepath in input_paths:
|
| 58 |
-
filename = os.path.basename(filepath)
|
| 59 |
-
try:
|
| 60 |
-
images = convert_from_path(filepath, first_page=1, last_page=1)
|
| 61 |
-
image = images[0]
|
| 62 |
-
width, height = image.size
|
| 63 |
-
cropped_image = image.crop((0, 0, width, height // 2))
|
| 64 |
-
text = pytesseract.image_to_string(cropped_image, lang='fra+eng')
|
| 65 |
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
|
| 71 |
-
|
| 72 |
-
|
|
|
|
| 73 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
date_match = seance_pattern.search(text)
|
| 75 |
date_str = "NO_DATE"
|
| 76 |
-
folder_path = pdf_folder
|
| 77 |
if date_match:
|
| 78 |
day, month, year = date_match.groups()
|
| 79 |
date_str = f"{year}{month}{day}"
|
| 80 |
folder_path = os.path.join(pdf_folder, date_str)
|
| 81 |
os.makedirs(folder_path, exist_ok=True)
|
|
|
|
|
|
|
| 82 |
|
|
|
|
| 83 |
ref_match = ref_pattern.search(text)
|
| 84 |
-
ref_code =
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
})
|
| 110 |
-
processed_files.append(f"- {filename} → {new_filename}")
|
| 111 |
-
|
| 112 |
-
except Exception as e:
|
| 113 |
-
processed_files.append(f"❌ {filename} (Erreur: {str(e)})")
|
| 114 |
-
|
| 115 |
-
# Excel export
|
| 116 |
-
df = pd.DataFrame(log_data)
|
| 117 |
df.to_excel(output_excel, index=False)
|
| 118 |
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
last_files = "\n".join(processed_files[-5:]) if processed_files else "Aucun fichier traité"
|
| 122 |
-
report = f"""
|
| 123 |
-
✅ **Traitement terminé**
|
| 124 |
-
- Fichiers traités : {len(log_data)}
|
| 125 |
-
- Derniers fichiers :
|
| 126 |
-
{last_files}
|
| 127 |
-
"""
|
| 128 |
-
|
| 129 |
-
return "renamed_pdfs.zip", output_excel, report
|
| 130 |
-
|
| 131 |
-
# Interface Gradio
|
| 132 |
-
with gr.Blocks(title="Assistant PDF Notifications CPAS") as demo:
|
| 133 |
-
gr.Markdown("# 📄 Assistant Notifications – Conseil de l’Action Sociale")
|
| 134 |
-
gr.Markdown("Téléchargez un PDF ou un ZIP contenant des notifications à traiter.")
|
| 135 |
-
|
| 136 |
-
btn_doc = gr.Button("📘 Voir la documentation")
|
| 137 |
-
output_doc = gr.Markdown()
|
| 138 |
-
btn_doc.click(fn=afficher_doc, inputs=[], outputs=output_doc)
|
| 139 |
-
|
| 140 |
-
with gr.Row():
|
| 141 |
-
input_file = gr.File(label="1. PDF ou ZIP", file_types=[".zip", ".pdf"])
|
| 142 |
-
btn_process = gr.Button("🚀 Traiter")
|
| 143 |
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
output_excel = gr.File(label="3. Rapport Excel")
|
| 147 |
|
| 148 |
-
|
|
|
|
|
|
|
| 149 |
|
| 150 |
-
btn_process.click(
|
| 151 |
-
fn=process_pdfs,
|
| 152 |
-
inputs=[input_file],
|
| 153 |
-
outputs=[output_zip, output_excel, output_report]
|
| 154 |
-
)
|
| 155 |
|
| 156 |
-
|
|
|
|
|
|
|
|
|
| 1 |
import os
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
import re
|
| 3 |
+
import zipfile
|
| 4 |
+
import shutil
|
| 5 |
+
import tempfile
|
| 6 |
+
import fitz # PyMuPDF
|
| 7 |
import pandas as pd
|
| 8 |
+
import gradio as gr
|
| 9 |
|
| 10 |
+
# Regex
|
| 11 |
+
title_pattern = re.compile(r"(M\.|Mme|Mlle)\s+[A-ZÉÈÊËÀÂÄÎÏÔÖÙÛÜÇa-zéèêëàâäîïôöùûüç\-'\s]+")
|
| 12 |
+
ref_pattern = re.compile(r"DI\s*([0-9]{4})\s*/\s*([0-9]+)")
|
| 13 |
+
seance_pattern = re.compile(r"(\d{2})[/-](\d{2})[/-](\d{4})")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
+
def extract_text_from_pdf(pdf_path):
|
| 16 |
+
text = ""
|
| 17 |
+
with fitz.open(pdf_path) as doc:
|
| 18 |
+
for page in doc:
|
| 19 |
+
text += page.get_text()
|
| 20 |
+
return text
|
| 21 |
|
| 22 |
+
def process_pdfs(files):
|
| 23 |
+
if not isinstance(files, list):
|
| 24 |
+
files = [files]
|
|
|
|
| 25 |
|
| 26 |
+
temp_dir = tempfile.mkdtemp()
|
| 27 |
+
pdf_folder = os.path.join(temp_dir, "results")
|
|
|
|
| 28 |
os.makedirs(pdf_folder, exist_ok=True)
|
| 29 |
|
| 30 |
+
excel_data = []
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
+
for file in files:
|
| 33 |
+
if zipfile.is_zipfile(file.name):
|
| 34 |
+
with zipfile.ZipFile(file.name, 'r') as zip_ref:
|
| 35 |
+
zip_ref.extractall(temp_dir)
|
| 36 |
+
pdf_files = [os.path.join(dp, f) for dp, dn, filenames in os.walk(temp_dir) for f in filenames if f.lower().endswith(".pdf")]
|
| 37 |
+
else:
|
| 38 |
+
output_path = os.path.join(temp_dir, os.path.basename(file.name))
|
| 39 |
+
shutil.copy(file.name, output_path)
|
| 40 |
+
pdf_files = [output_path]
|
| 41 |
|
| 42 |
+
for pdf_path in pdf_files:
|
| 43 |
+
filename = os.path.basename(pdf_path)
|
| 44 |
+
text = extract_text_from_pdf(pdf_path)
|
| 45 |
|
| 46 |
+
# Extraction nom
|
| 47 |
+
name_match = title_pattern.search(text)
|
| 48 |
+
nom_prenom = "NO_NAME"
|
| 49 |
+
safe_name = "NO_NAME"
|
| 50 |
+
if name_match:
|
| 51 |
+
nom_prenom = name_match.group(0).strip()
|
| 52 |
+
safe_name = re.sub(r"[^\w]", "_", nom_prenom)
|
| 53 |
+
safe_name = re.sub(r"_+", "_", safe_name)
|
| 54 |
+
safe_name = re.sub(r"(_?DEPARTEMENT|_?ACTION|_?DIRECTION|_?SERVICE|_?UNITE|_?DIVISION)+", "", safe_name, flags=re.IGNORECASE)
|
| 55 |
+
safe_name = safe_name.strip("_")
|
| 56 |
+
|
| 57 |
+
# Date
|
| 58 |
date_match = seance_pattern.search(text)
|
| 59 |
date_str = "NO_DATE"
|
|
|
|
| 60 |
if date_match:
|
| 61 |
day, month, year = date_match.groups()
|
| 62 |
date_str = f"{year}{month}{day}"
|
| 63 |
folder_path = os.path.join(pdf_folder, date_str)
|
| 64 |
os.makedirs(folder_path, exist_ok=True)
|
| 65 |
+
else:
|
| 66 |
+
folder_path = pdf_folder
|
| 67 |
|
| 68 |
+
# Référence DI
|
| 69 |
ref_match = ref_pattern.search(text)
|
| 70 |
+
ref_code = "NO_REF"
|
| 71 |
+
ref_excel = "NO_REF"
|
| 72 |
+
if ref_match:
|
| 73 |
+
ref_year, ref_number = ref_match.groups()
|
| 74 |
+
ref_code = f"{ref_year}-{ref_number}"
|
| 75 |
+
ref_excel = f"{ref_year}/{ref_number}"
|
| 76 |
+
|
| 77 |
+
# Nom de fichier
|
| 78 |
+
final_filename = f"{safe_name}_{ref_code}.pdf"
|
| 79 |
+
final_path = os.path.join(folder_path, final_filename)
|
| 80 |
+
|
| 81 |
+
shutil.copy(pdf_path, final_path)
|
| 82 |
+
|
| 83 |
+
# Pour Excel
|
| 84 |
+
excel_data.append([
|
| 85 |
+
filename,
|
| 86 |
+
final_filename,
|
| 87 |
+
nom_prenom,
|
| 88 |
+
ref_excel,
|
| 89 |
+
date_str
|
| 90 |
+
])
|
| 91 |
+
|
| 92 |
+
# Générer Excel
|
| 93 |
+
output_excel = os.path.join(temp_dir, "résultats.xlsx")
|
| 94 |
+
df = pd.DataFrame(excel_data, columns=["Fichier_Base", "FichierNouveau", "Nom et Prénom", "DI", "Date de séance"])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 95 |
df.to_excel(output_excel, index=False)
|
| 96 |
|
| 97 |
+
return output_excel
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 98 |
|
| 99 |
+
with gr.Blocks() as demo:
|
| 100 |
+
gr.Markdown("## 🧾 Traitement de PDFs ou ZIPs - Extraction DI")
|
|
|
|
| 101 |
|
| 102 |
+
input_files = gr.File(label="1. Fichiers PDF ou un fichier ZIP", file_types=[".zip", ".pdf"], file_count="multiple")
|
| 103 |
+
btn_process = gr.Button("2. Lancer le traitement")
|
| 104 |
+
output_excel = gr.File(label="3. Télécharger l'Excel", file_types=[".xlsx"])
|
| 105 |
|
| 106 |
+
btn_process.click(fn=process_pdfs, inputs=input_files, outputs=output_excel)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 107 |
|
| 108 |
+
if __name__ == "__main__":
|
| 109 |
+
demo.launch(server_name="0.0.0.0", server_port=7860)
|