Spaces:
Runtime error
Runtime error
Laurine Sottani
commited on
Commit
·
3550f03
1
Parent(s):
cf99660
fix file support
Browse files- file_cleaning_ui.py +15 -13
- requirements.txt +1 -0
file_cleaning_ui.py
CHANGED
|
@@ -6,6 +6,7 @@ import tempfile
|
|
| 6 |
from pathlib import Path
|
| 7 |
|
| 8 |
import pdfplumber
|
|
|
|
| 9 |
import gradio as gr
|
| 10 |
|
| 11 |
def clean_text_for_rag(text: str) -> str:
|
|
@@ -41,6 +42,17 @@ def extract_and_clean_pdf(pdf_path: str) -> str:
|
|
| 41 |
return clean_text_for_rag(" ".join(all_pages))
|
| 42 |
|
| 43 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
def extract_and_clean_txt(txt_path: str) -> str:
|
| 45 |
"""Lit un fichier texte (txt, md, …) et le nettoie."""
|
| 46 |
print(f"[+] Lecture du fichier texte : {txt_path}")
|
|
@@ -53,18 +65,6 @@ def extract_and_clean_txt(txt_path: str) -> str:
|
|
| 53 |
]
|
| 54 |
return "\n".join(cleaned)
|
| 55 |
|
| 56 |
-
def extract_and_clean_docx(docx_path: str) -> str:
|
| 57 |
-
"""Lit un fichier type Word et le nettoie."""
|
| 58 |
-
print(f"[+] Lecture du fichier docx : {docx_path}")
|
| 59 |
-
with open(docx_path, "r", encoding="utf-8") as f:
|
| 60 |
-
lines = f.readlines()
|
| 61 |
-
cleaned = [
|
| 62 |
-
clean_text_for_rag(line.strip())
|
| 63 |
-
for line in lines
|
| 64 |
-
if line.strip()
|
| 65 |
-
]
|
| 66 |
-
return "\n".join(cleaned)
|
| 67 |
-
|
| 68 |
def process_file(input_file: gr.File, output_name: str) -> str:
|
| 69 |
"""
|
| 70 |
- Detecte le type (PDF ou texte)
|
|
@@ -77,6 +77,8 @@ def process_file(input_file: gr.File, output_name: str) -> str:
|
|
| 77 |
|
| 78 |
if ext == ".pdf":
|
| 79 |
cleaned_text = extract_and_clean_pdf(input_path)
|
|
|
|
|
|
|
| 80 |
else:
|
| 81 |
cleaned_text = extract_and_clean_txt(input_path)
|
| 82 |
|
|
@@ -103,7 +105,7 @@ with gr.Blocks(title="Nettoyage de texte pour RAG") as demo:
|
|
| 103 |
with gr.Column(scale=1):
|
| 104 |
input_file = gr.File(
|
| 105 |
label="Déposez votre fichier ici",
|
| 106 |
-
file_types=["pdf", "txt", "md", "
|
| 107 |
)
|
| 108 |
output_name = gr.Textbox(
|
| 109 |
value="output.md",
|
|
|
|
| 6 |
from pathlib import Path
|
| 7 |
|
| 8 |
import pdfplumber
|
| 9 |
+
import docx
|
| 10 |
import gradio as gr
|
| 11 |
|
| 12 |
def clean_text_for_rag(text: str) -> str:
|
|
|
|
| 42 |
return clean_text_for_rag(" ".join(all_pages))
|
| 43 |
|
| 44 |
|
| 45 |
+
def extract_and_clean_docx(docx_path: str) -> str:
|
| 46 |
+
"""Lit un fichier DOCX et le nettoie."""
|
| 47 |
+
print(f"[+] Extraction du DOCX : {docx_path}")
|
| 48 |
+
doc = docx.Document(docx_path)
|
| 49 |
+
paragraphs = []
|
| 50 |
+
for para in doc.paragraphs:
|
| 51 |
+
text = para.text.strip()
|
| 52 |
+
if text:
|
| 53 |
+
paragraphs.append(text)
|
| 54 |
+
return clean_text_for_rag(" ".join(paragraphs))
|
| 55 |
+
|
| 56 |
def extract_and_clean_txt(txt_path: str) -> str:
|
| 57 |
"""Lit un fichier texte (txt, md, …) et le nettoie."""
|
| 58 |
print(f"[+] Lecture du fichier texte : {txt_path}")
|
|
|
|
| 65 |
]
|
| 66 |
return "\n".join(cleaned)
|
| 67 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
def process_file(input_file: gr.File, output_name: str) -> str:
|
| 69 |
"""
|
| 70 |
- Detecte le type (PDF ou texte)
|
|
|
|
| 77 |
|
| 78 |
if ext == ".pdf":
|
| 79 |
cleaned_text = extract_and_clean_pdf(input_path)
|
| 80 |
+
elif ext == ".docx":
|
| 81 |
+
cleaned_text = extract_and_clean_docx(input_path)
|
| 82 |
else:
|
| 83 |
cleaned_text = extract_and_clean_txt(input_path)
|
| 84 |
|
|
|
|
| 105 |
with gr.Column(scale=1):
|
| 106 |
input_file = gr.File(
|
| 107 |
label="Déposez votre fichier ici",
|
| 108 |
+
file_types=["pdf", "txt", "md", "docx"],
|
| 109 |
)
|
| 110 |
output_name = gr.Textbox(
|
| 111 |
value="output.md",
|
requirements.txt
CHANGED
|
@@ -1 +1,2 @@
|
|
| 1 |
pdfplumber
|
|
|
|
|
|
| 1 |
pdfplumber
|
| 2 |
+
python-docx
|