Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,12 +2,9 @@
|
|
| 2 |
import gradio as gr
|
| 3 |
import PyPDF2
|
| 4 |
import re
|
| 5 |
-
from sumy.parsers.plaintext import PlaintextParser
|
| 6 |
-
from sumy.nlp.tokenizers import Tokenizer
|
| 7 |
-
from sumy.summarizers.lex_rank import LexRankSummarizer
|
| 8 |
|
| 9 |
# -----------------------------
|
| 10 |
-
# Calcul emplacement (11 boîtes / étage)
|
| 11 |
# -----------------------------
|
| 12 |
def calculer_emplacement(n):
|
| 13 |
try:
|
|
@@ -28,24 +25,36 @@ Position : {case} / 11
|
|
| 28 |
return "❌ Erreur"
|
| 29 |
|
| 30 |
# -----------------------------
|
| 31 |
-
# Résumé
|
| 32 |
# -----------------------------
|
| 33 |
def summarize_offline(text, n_sentences=5):
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
# -----------------------------
|
| 40 |
-
# Analyse PDF (SANS OCR)
|
| 41 |
# -----------------------------
|
| 42 |
def analyser_pdf(file, longueur):
|
|
|
|
| 43 |
if file is None:
|
| 44 |
return "❌ Aucun fichier", ""
|
|
|
|
| 45 |
try:
|
| 46 |
reader = PyPDF2.PdfReader(file.name)
|
|
|
|
| 47 |
text = ""
|
| 48 |
-
for page in reader.pages[:5]:
|
| 49 |
content = page.extract_text()
|
| 50 |
if content:
|
| 51 |
text += content + " "
|
|
@@ -53,21 +62,23 @@ def analyser_pdf(file, longueur):
|
|
| 53 |
clean_text = re.sub(r"\s+", " ", text).strip()
|
| 54 |
|
| 55 |
if len(clean_text) < 50:
|
| 56 |
-
return "❌ PDF scanné (OCR désactivé
|
|
|
|
|
|
|
| 57 |
|
| 58 |
-
n_sentences = 10 if longueur == "Détaillé" else 5
|
| 59 |
summary = summarize_offline(clean_text, n_sentences)
|
| 60 |
|
| 61 |
-
return "✅ Résumé
|
| 62 |
|
| 63 |
except Exception as e:
|
| 64 |
return f"❌ Erreur : {str(e)}", ""
|
| 65 |
|
| 66 |
# -----------------------------
|
| 67 |
-
# Interface Gradio
|
| 68 |
# -----------------------------
|
| 69 |
with gr.Blocks() as demo:
|
| 70 |
-
|
|
|
|
| 71 |
|
| 72 |
with gr.Tab("📍 Localisation"):
|
| 73 |
input_num = gr.Number(label="Numéro de boîte", precision=0)
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
import PyPDF2
|
| 4 |
import re
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
# -----------------------------
|
| 7 |
+
# 📍 Calcul emplacement (11 boîtes / étage)
|
| 8 |
# -----------------------------
|
| 9 |
def calculer_emplacement(n):
|
| 10 |
try:
|
|
|
|
| 25 |
return "❌ Erreur"
|
| 26 |
|
| 27 |
# -----------------------------
|
| 28 |
+
# 📄 Résumé simple stable (arabe + français)
|
| 29 |
# -----------------------------
|
| 30 |
def summarize_offline(text, n_sentences=5):
|
| 31 |
+
|
| 32 |
+
# Découpage phrases arabe + français
|
| 33 |
+
sentences = re.split(r'[.!؟\n]', text)
|
| 34 |
+
|
| 35 |
+
# Nettoyage
|
| 36 |
+
sentences = [s.strip() for s in sentences if len(s.strip()) > 30]
|
| 37 |
+
|
| 38 |
+
if len(sentences) == 0:
|
| 39 |
+
return "⚠️ Texte insuffisant pour résumé"
|
| 40 |
+
|
| 41 |
+
summary = sentences[:n_sentences]
|
| 42 |
+
|
| 43 |
+
return ". ".join(summary) + "."
|
| 44 |
|
| 45 |
# -----------------------------
|
| 46 |
+
# 📄 Analyse PDF (SANS OCR)
|
| 47 |
# -----------------------------
|
| 48 |
def analyser_pdf(file, longueur):
|
| 49 |
+
|
| 50 |
if file is None:
|
| 51 |
return "❌ Aucun fichier", ""
|
| 52 |
+
|
| 53 |
try:
|
| 54 |
reader = PyPDF2.PdfReader(file.name)
|
| 55 |
+
|
| 56 |
text = ""
|
| 57 |
+
for page in reader.pages[:5]: # max 5 pages pour stabilité
|
| 58 |
content = page.extract_text()
|
| 59 |
if content:
|
| 60 |
text += content + " "
|
|
|
|
| 62 |
clean_text = re.sub(r"\s+", " ", text).strip()
|
| 63 |
|
| 64 |
if len(clean_text) < 50:
|
| 65 |
+
return "❌ PDF vide ou scanné (OCR désactivé)", ""
|
| 66 |
+
|
| 67 |
+
n_sentences = 8 if longueur == "Détaillé" else 4
|
| 68 |
|
|
|
|
| 69 |
summary = summarize_offline(clean_text, n_sentences)
|
| 70 |
|
| 71 |
+
return "✅ Résumé généré avec succès", summary
|
| 72 |
|
| 73 |
except Exception as e:
|
| 74 |
return f"❌ Erreur : {str(e)}", ""
|
| 75 |
|
| 76 |
# -----------------------------
|
| 77 |
+
# 🖥 Interface Gradio
|
| 78 |
# -----------------------------
|
| 79 |
with gr.Blocks() as demo:
|
| 80 |
+
|
| 81 |
+
gr.Markdown("# 📁 ArchivChat Stable Version")
|
| 82 |
|
| 83 |
with gr.Tab("📍 Localisation"):
|
| 84 |
input_num = gr.Number(label="Numéro de boîte", precision=0)
|