ArchivChat / app.py
NOBODY204's picture
Update app.py
8bf9737 verified
# -*- 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()