# -*- coding: utf-8 -*- import gradio as gr import PyPDF2 import re # ----------------------------- # 📍 Calcul emplacement (11 boĂźtes / Ă©tage) # ----------------------------- def calculer_emplacement(n): try: if n is None or n < 1: return "### ⚠ Entrez un numĂ©ro valide" n = int(n) etage = ((n - 1) // 11) + 1 rayon = "A" if n <= 11 else "B" case = ((n - 1) % 11) + 1 return f""" # 📍 EMPLACEMENT BoĂźte : {n} Rayon : {rayon} Étage : {etage} Position : {case} / 11 """ except: return "❌ Erreur" # ----------------------------- # 📄 RĂ©sumĂ© simple stable (arabe + français) # ----------------------------- def summarize_offline(text, n_sentences=5): # DĂ©coupage phrases arabe + français sentences = re.split(r'[.!۟\n]', text) # Nettoyage sentences = [s.strip() for s in sentences if len(s.strip()) > 30] if len(sentences) == 0: return "⚠ Texte insuffisant pour rĂ©sumĂ©" summary = sentences[:n_sentences] return ". ".join(summary) + "." # ----------------------------- # 📄 Analyse PDF (SANS OCR) # ----------------------------- def analyser_pdf(file, longueur): if file is None: return "❌ Aucun fichier", "" try: reader = PyPDF2.PdfReader(file.name) text = "" for page in reader.pages[:5]: # max 5 pages pour stabilitĂ© content = page.extract_text() if content: text += content + " " clean_text = re.sub(r"\s+", " ", text).strip() if len(clean_text) < 50: return "❌ PDF vide ou scannĂ© (OCR dĂ©sactivĂ©)", "" n_sentences = 8 if longueur == "DĂ©taillĂ©" else 4 summary = summarize_offline(clean_text, n_sentences) return "✅ RĂ©sumĂ© gĂ©nĂ©rĂ© avec succĂšs", summary except Exception as e: return f"❌ Erreur : {str(e)}", "" # ----------------------------- # đŸ–„ Interface Gradio # ----------------------------- with gr.Blocks() as demo: gr.Markdown("# 📁 ArchivChat Stable Version") with gr.Tab("📍 Localisation"): input_num = gr.Number(label="NumĂ©ro de boĂźte", precision=0) output_loc = gr.Markdown() input_num.change(calculer_emplacement, input_num, output_loc) with gr.Tab("📄 RĂ©sumĂ© PDF"): file_in = gr.File(file_types=[".pdf"]) longueur_opt = gr.Radio(["Court", "DĂ©taillĂ©"], value="Court") btn = gr.Button("Analyser") statut = gr.Textbox(label="Statut") resume = gr.Textbox(label="RĂ©sumĂ©", lines=10) btn.click(analyser_pdf, [file_in, longueur_opt], [statut, resume]) demo.launch()