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