Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,130 +1,156 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import easyocr
|
| 3 |
-
from transformers import pipeline
|
| 4 |
import numpy as np
|
| 5 |
from PIL import Image
|
|
|
|
| 6 |
from docx import Document
|
| 7 |
from pptx import Presentation
|
|
|
|
|
|
|
|
|
|
| 8 |
import os
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
# RΓ©sumΓ© BART
|
| 14 |
-
summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
|
| 15 |
-
|
| 16 |
-
# ---------- IMAGE (OCR) ----------
|
| 17 |
-
def process_image(image):
|
| 18 |
-
try:
|
| 19 |
-
if isinstance(image, Image.Image):
|
| 20 |
-
image = np.array(image)
|
| 21 |
-
|
| 22 |
-
results = reader.readtext(image, paragraph=True)
|
| 23 |
-
text = " ".join([r[1] for r in results])
|
| 24 |
-
|
| 25 |
-
if not text.strip():
|
| 26 |
-
return "Aucun texte dΓ©tectΓ©.", "RΓ©sumΓ© impossible."
|
| 27 |
|
| 28 |
-
|
| 29 |
-
|
| 30 |
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
min_length=min(30, wc // 2),
|
| 36 |
-
do_sample=False
|
| 37 |
)
|
| 38 |
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
except Exception as e:
|
| 42 |
-
return f"Erreur : {e}", "Erreur."
|
| 43 |
|
| 44 |
-
|
| 45 |
-
def extract_text_from_file(file_path):
|
| 46 |
-
ext = os.path.splitext(file_path)[1].lower()
|
| 47 |
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
return f.read()
|
| 51 |
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
return "\n".join(p.text for p in doc.paragraphs)
|
| 55 |
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
text = []
|
| 59 |
-
for slide in prs.slides:
|
| 60 |
-
for shape in slide.shapes:
|
| 61 |
-
if hasattr(shape, "text"):
|
| 62 |
-
text.append(shape.text)
|
| 63 |
-
return "\n".join(text)
|
| 64 |
-
|
| 65 |
-
else:
|
| 66 |
-
return ""
|
| 67 |
-
|
| 68 |
-
def process_file(file):
|
| 69 |
-
try:
|
| 70 |
-
text = extract_text_from_file(file.name)
|
| 71 |
-
|
| 72 |
-
if not text.strip():
|
| 73 |
-
return "Aucun texte dΓ©tectΓ©.", "RΓ©sumΓ© impossible."
|
| 74 |
-
|
| 75 |
-
if len(text.split()) < 30:
|
| 76 |
-
return text, "Texte trop court pour Γͺtre rΓ©sumΓ©."
|
| 77 |
-
|
| 78 |
-
wc = len(text.split())
|
| 79 |
-
summary = summarizer(
|
| 80 |
-
text,
|
| 81 |
-
max_length=min(150, wc),
|
| 82 |
-
min_length=min(30, wc // 2),
|
| 83 |
-
do_sample=False
|
| 84 |
-
)
|
| 85 |
|
| 86 |
-
|
|
|
|
| 87 |
|
| 88 |
-
|
| 89 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
|
| 91 |
-
# --------
|
| 92 |
-
with gr.Blocks(title="OCR & RΓ©sumΓ©
|
| 93 |
gr.Markdown("""
|
| 94 |
-
# π§ OCR & RΓ©sumΓ©
|
| 95 |
-
|
| 96 |
-
|
|
|
|
|
|
|
| 97 |
""")
|
| 98 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 99 |
with gr.Tabs():
|
| 100 |
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
|
| 108 |
-
|
| 109 |
process_image,
|
| 110 |
-
inputs=
|
| 111 |
-
outputs=[
|
| 112 |
)
|
| 113 |
|
| 114 |
-
# ----- TAB FILE -----
|
| 115 |
with gr.Tab("π Fichier"):
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
)
|
| 120 |
-
|
| 121 |
-
file_text = gr.Textbox(label="Texte extrait", lines=10)
|
| 122 |
-
file_summary = gr.Textbox(label="RΓ©sumΓ©", lines=5)
|
| 123 |
|
| 124 |
-
|
| 125 |
process_file,
|
| 126 |
-
inputs=
|
| 127 |
-
outputs=[
|
| 128 |
)
|
| 129 |
|
| 130 |
if __name__ == "__main__":
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import easyocr
|
|
|
|
| 3 |
import numpy as np
|
| 4 |
from PIL import Image
|
| 5 |
+
from transformers import pipeline
|
| 6 |
from docx import Document
|
| 7 |
from pptx import Presentation
|
| 8 |
+
from spellchecker import SpellChecker
|
| 9 |
+
from langdetect import detect
|
| 10 |
+
import fitz # PyMuPDF
|
| 11 |
import os
|
| 12 |
+
import tempfile
|
| 13 |
+
|
| 14 |
+
# -------- OCR --------
|
| 15 |
+
reader = easyocr.Reader(["fr", "en", "ar"], gpu=False)
|
| 16 |
+
|
| 17 |
+
# -------- RΓ©sumΓ©s par langue --------
|
| 18 |
+
summarizers = {
|
| 19 |
+
"fr": pipeline("summarization", model="facebook/bart-large-cnn"),
|
| 20 |
+
"en": pipeline("summarization", model="facebook/bart-large-cnn"),
|
| 21 |
+
"ar": pipeline("summarization", model="csebuetnlp/mT5_multilingual_XLSum")
|
| 22 |
+
}
|
| 23 |
+
|
| 24 |
+
# -------- Correcteur OCR --------
|
| 25 |
+
spell = SpellChecker(language="fr")
|
| 26 |
+
|
| 27 |
+
def correct_ocr(text):
|
| 28 |
+
words = text.split()
|
| 29 |
+
corrected = []
|
| 30 |
+
for w in words:
|
| 31 |
+
if w.isalpha():
|
| 32 |
+
corrected.append(spell.correction(w) or w)
|
| 33 |
+
else:
|
| 34 |
+
corrected.append(w)
|
| 35 |
+
return " ".join(corrected)
|
| 36 |
+
|
| 37 |
+
# -------- RΓ©sumΓ© --------
|
| 38 |
+
def summarize_text(text, lang):
|
| 39 |
+
wc = len(text.split())
|
| 40 |
+
if wc < 30:
|
| 41 |
+
return "Texte trop court pour Γͺtre rΓ©sumΓ©."
|
| 42 |
+
|
| 43 |
+
model = summarizers.get(lang, summarizers["fr"])
|
| 44 |
+
result = model(
|
| 45 |
+
text,
|
| 46 |
+
max_length=min(150, wc),
|
| 47 |
+
min_length=min(30, wc // 2),
|
| 48 |
+
do_sample=False
|
| 49 |
+
)
|
| 50 |
+
return result[0]["summary_text"]
|
| 51 |
+
|
| 52 |
+
# -------- IMAGE --------
|
| 53 |
+
def process_image(image, lang):
|
| 54 |
+
image = np.array(image)
|
| 55 |
+
results = reader.readtext(image, paragraph=True)
|
| 56 |
+
text = " ".join([r[1] for r in results])
|
| 57 |
+
|
| 58 |
+
if not text.strip():
|
| 59 |
+
return "Aucun texte dΓ©tectΓ©.", "", None
|
| 60 |
+
|
| 61 |
+
corrected = correct_ocr(text)
|
| 62 |
+
summary = summarize_text(corrected, lang)
|
| 63 |
+
|
| 64 |
+
file_path = save_summary(summary)
|
| 65 |
+
return corrected, summary, file_path
|
| 66 |
+
|
| 67 |
+
# -------- PDF --------
|
| 68 |
+
def extract_text_pdf(path):
|
| 69 |
+
doc = fitz.open(path)
|
| 70 |
+
return "\n".join(page.get_text() for page in doc)
|
| 71 |
+
|
| 72 |
+
# -------- FICHIER --------
|
| 73 |
+
def extract_text(file_path):
|
| 74 |
+
ext = os.path.splitext(file_path)[1].lower()
|
| 75 |
|
| 76 |
+
if ext == ".txt":
|
| 77 |
+
return open(file_path, encoding="utf-8", errors="ignore").read()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
|
| 79 |
+
if ext == ".docx":
|
| 80 |
+
return "\n".join(p.text for p in Document(file_path).paragraphs)
|
| 81 |
|
| 82 |
+
if ext == ".pptx":
|
| 83 |
+
prs = Presentation(file_path)
|
| 84 |
+
return "\n".join(
|
| 85 |
+
shape.text for slide in prs.slides for shape in slide.shapes if hasattr(shape, "text")
|
|
|
|
|
|
|
| 86 |
)
|
| 87 |
|
| 88 |
+
if ext == ".pdf":
|
| 89 |
+
return extract_text_pdf(file_path)
|
|
|
|
|
|
|
| 90 |
|
| 91 |
+
return ""
|
|
|
|
|
|
|
| 92 |
|
| 93 |
+
def process_file(file, lang):
|
| 94 |
+
text = extract_text(file.name)
|
|
|
|
| 95 |
|
| 96 |
+
if not text.strip():
|
| 97 |
+
return "Aucun texte dΓ©tectΓ©.", "", None
|
|
|
|
| 98 |
|
| 99 |
+
corrected = correct_ocr(text)
|
| 100 |
+
summary = summarize_text(corrected, lang)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 101 |
|
| 102 |
+
file_path = save_summary(summary)
|
| 103 |
+
return corrected, summary, file_path
|
| 104 |
|
| 105 |
+
# -------- TΓ©lΓ©charger --------
|
| 106 |
+
def save_summary(summary):
|
| 107 |
+
tmp = tempfile.NamedTemporaryFile(delete=False, suffix=".txt")
|
| 108 |
+
tmp.write(summary.encode("utf-8"))
|
| 109 |
+
tmp.close()
|
| 110 |
+
return tmp.name
|
| 111 |
|
| 112 |
+
# -------- INTERFACE --------
|
| 113 |
+
with gr.Blocks(title="OCR & RΓ©sumΓ© Intelligent") as demo:
|
| 114 |
gr.Markdown("""
|
| 115 |
+
# π§ OCR & RΓ©sumΓ© Intelligent
|
| 116 |
+
β Image | TXT | DOCX | PPTX | PDF
|
| 117 |
+
β RΓ©sumΓ© FR / AR / EN
|
| 118 |
+
β Correction OCR
|
| 119 |
+
β TΓ©lΓ©chargement du rΓ©sumΓ©
|
| 120 |
""")
|
| 121 |
|
| 122 |
+
lang = gr.Radio(
|
| 123 |
+
choices=[("FranΓ§ais", "fr"), ("Ψ§ΩΨΉΨ±Ψ¨ΩΨ©", "ar"), ("English", "en")],
|
| 124 |
+
value="fr",
|
| 125 |
+
label="Langue du rΓ©sumΓ©"
|
| 126 |
+
)
|
| 127 |
+
|
| 128 |
with gr.Tabs():
|
| 129 |
|
| 130 |
+
with gr.Tab("πΌοΈ Image"):
|
| 131 |
+
img = gr.Image(type="pil")
|
| 132 |
+
btn_img = gr.Button("Extraire & RΓ©sumer", variant="primary")
|
| 133 |
+
txt_img = gr.Textbox(label="Texte corrigΓ©", lines=10)
|
| 134 |
+
sum_img = gr.Textbox(label="RΓ©sumΓ©", lines=5)
|
| 135 |
+
dl_img = gr.File(label="π₯ TΓ©lΓ©charger le rΓ©sumΓ©")
|
| 136 |
|
| 137 |
+
btn_img.click(
|
| 138 |
process_image,
|
| 139 |
+
inputs=[img, lang],
|
| 140 |
+
outputs=[txt_img, sum_img, dl_img]
|
| 141 |
)
|
| 142 |
|
|
|
|
| 143 |
with gr.Tab("π Fichier"):
|
| 144 |
+
file = gr.File(file_types=[".txt", ".docx", ".pptx", ".pdf"])
|
| 145 |
+
btn_file = gr.Button("Extraire & RΓ©sumer", variant="primary")
|
| 146 |
+
txt_file = gr.Textbox(label="Texte corrigΓ©", lines=10)
|
| 147 |
+
sum_file = gr.Textbox(label="RΓ©sumΓ©", lines=5)
|
| 148 |
+
dl_file = gr.File(label="π₯ TΓ©lΓ©charger le rΓ©sumΓ©")
|
|
|
|
|
|
|
| 149 |
|
| 150 |
+
btn_file.click(
|
| 151 |
process_file,
|
| 152 |
+
inputs=[file, lang],
|
| 153 |
+
outputs=[txt_file, sum_file, dl_file]
|
| 154 |
)
|
| 155 |
|
| 156 |
if __name__ == "__main__":
|