File size: 12,519 Bytes
ad6d0d0
 
 
 
 
 
6bd2c76
7198231
 
ad6d0d0
 
 
 
 
 
6bd2c76
 
 
 
b3273c1
6bd2c76
 
b3273c1
6bd2c76
 
 
 
 
b3273c1
 
 
 
6bd2c76
 
b3273c1
6bd2c76
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7198231
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6bd2c76
b3273c1
6bd2c76
 
 
 
 
 
ad6d0d0
6bd2c76
ad6d0d0
18f46da
6bd2c76
ad6d0d0
6bd2c76
ad6d0d0
 
 
6bd2c76
 
 
 
 
 
 
 
b3273c1
6bd2c76
 
 
 
 
 
ad6d0d0
 
 
 
 
 
 
 
7198231
ad6d0d0
 
b3273c1
ad6d0d0
 
 
 
 
 
 
7198231
ad6d0d0
 
7198231
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b3273c1
7198231
b3273c1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ad6d0d0
 
 
 
 
 
 
 
 
 
b3273c1
 
6bd2c76
b3273c1
 
ad6d0d0
b3273c1
 
 
ad6d0d0
 
b3273c1
 
ad6d0d0
 
 
 
 
 
 
448f55b
18f46da
7198231
448f55b
ad6d0d0
 
b3273c1
 
7198231
 
 
6bd2c76
b3273c1
 
 
ad6d0d0
 
 
 
b3273c1
 
 
 
ad6d0d0
 
b3273c1
 
 
 
ad6d0d0
 
 
b3273c1
 
 
ad6d0d0
 
 
b3273c1
 
ad6d0d0
 
 
6bd2c76
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
import gradio as gr
import os
import tempfile
from datetime import datetime
import pandas as pd
import json
import unicodedata
import pytesseract
from PIL import Image

# Import DocLing and necessary configuration classes
from docling.document_converter import DocumentConverter, PdfFormatOption
from docling.datamodel.pipeline_options import PdfPipelineOptions
from docling.datamodel.base_models import InputFormat

# --- Language detection and normalization helpers ---
try:
    from ftfy import fix_text
    def _fix_text(s: str) -> str:
        return fix_text(s)
except ImportError:
    def _fix_text(s: str) -> str:
        return s

try:
    from langdetect import detect, DetectorFactory
    DetectorFactory.seed = 0  # deterministic
    def _detect_lang(text: str) -> str | None:
        try:
            return detect(text)
        except Exception:
            return None
except ImportError:
    def _detect_lang(text: str) -> str | None:
        return None

def normalize_text(s: str) -> str:
    s = _fix_text(s)
    return unicodedata.normalize("NFC", s)

# Map ISO-ish lang codes to Tesseract codes
LANG_MAP = {
    "pt": "por", "es": "spa", "en": "eng", "fr": "fra", "de": "deu", "it": "ita",
    "nl": "nld", "pl": "pol", "tr": "tur", "cs": "ces", "ru": "rus", "uk": "ukr",
    "el": "ell", "ro": "ron", "hu": "hun", "sv": "swe", "da": "dan", "fi": "fin",
    "no": "nor", "ca": "cat", "gl": "glg"
}

def guess_lang_code(text: str) -> str | None:
    lang = _detect_lang(text) if text and text.strip() else None
    return LANG_MAP.get(lang) if lang else None

def process_image_with_ocr(image_path: str) -> str:
    """
    Extract text from image using OCR (Tesseract)
    """
    try:
        img = Image.open(image_path)
        # Detect language from image content
        text = pytesseract.image_to_string(img, lang='por+eng')
        detected_lang = guess_lang_code(text) or "por"
        # Re-extract with detected language for better accuracy
        text = pytesseract.image_to_string(img, lang=detected_lang)
        return normalize_text(text)
    except Exception as e:
        raise Exception(f"OCR processing failed: {str(e)}")

def looks_garbled(text: str) -> bool:
    if not text or len(text.strip()) < 100:
        return True
    # Common mojibake signs
    bad_patterns = ["Γƒ", "Γ‚", "οΏ½", "Βͺ"]
    return sum(text.count(p) for p in bad_patterns) > 5
# --- End helpers ---

# --- START OF OCR CONFIGURATION ---
# Default: do_ocr=False (use native text layer). When OCR is needed, we'll build options dynamically.
pdf_options = PdfPipelineOptions(
    do_ocr=False,
    ocr_model="tesseract"
)
format_options = {InputFormat.PDF: PdfFormatOption(pipeline_options=pdf_options)}
docling_converter = DocumentConverter(format_options=format_options)
# --- END OF OCR CONFIGURATION ---

def convert_with_strategy(path: str):
    # 1) No-OCR pass
    no_ocr_opts = PdfPipelineOptions(do_ocr=False, ocr_model="tesseract")
    converter = DocumentConverter(format_options={InputFormat.PDF: PdfFormatOption(pipeline_options=no_ocr_opts)})
    res = converter.convert(path)
    text_sample = normalize_text(res.document.export_to_text())

    if not looks_garbled(text_sample):
        return res

    # 2) OCR fallback with detected language (default to Portuguese)
    detected = guess_lang_code(text_sample) or "por"
    ocr_opts = PdfPipelineOptions(do_ocr=True, ocr_model="tesseract", ocr_languages=[detected])
    ocr_converter = DocumentConverter(format_options={InputFormat.PDF: PdfFormatOption(pipeline_options=ocr_opts)})
    return ocr_converter.convert(path)

def process_file(file):
    """
    Process an uploaded file and return 4 files:
    1. Docling document (JSON)
    2. Text file
    3. Markdown file
    4. HTML file
    Supports: PDF, DOCX, XLSX, XLS, CSV, PPTX, TXT, and IMAGE formats (PNG, JPG, JPEG, BMP, TIFF)
    """
    if file is None:
        return None, None, None, None, "❌ Error: Please upload a file."

    # Normalize to a filesystem path string
    path = file.name if hasattr(file, "name") else str(file)
    ext = os.path.splitext(path)[1].lower()

    docling_direct = {".pdf", ".docx", ".xlsx", ".pptx"}
    to_xlsx_first = {".csv", ".xls"}
    image_formats = {".png", ".jpg", ".jpeg", ".bmp", ".tiff", ".tif"}

    try:
        # Handle image files with OCR
        if ext in image_formats:
            text_content = process_image_with_ocr(path)
            
            timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
            base_filename = f"document_{timestamp}"

            # 1. Docling document (JSON)
            docling_json_path = f"{base_filename}_docling.json"
            docling_dict = {
                "type": "image_document_ocr",
                "content": text_content,
                "metadata": {
                    "source": os.path.basename(path),
                    "timestamp": timestamp,
                    "format": ext
                }
            }
            with open(docling_json_path, "w", encoding="utf-8") as f:
                json.dump(docling_dict, f, indent=2, ensure_ascii=False)

            # 2. Text file
            txt_path = f"{base_filename}.txt"
            with open(txt_path, "w", encoding="utf-8") as f:
                f.write(text_content)

            # 3. Markdown file
            md_path = f"{base_filename}.md"
            with open(md_path, "w", encoding="utf-8") as f:
                f.write(f"# Document (OCR from Image)\n\n{text_content}")

            # 4. HTML file
            html_path = f"{base_filename}.html"
            html_content = f"""<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <h1>Document (OCR from Image)</h1>
    <pre>{text_content}</pre>
</body>
</html>"""
            with open(html_path, "w", encoding="utf-8") as f:
                f.write(html_content)

            success_message = "βœ… Successfully processed image with OCR! 4 files generated."
            return docling_json_path, txt_path, md_path, html_path, success_message

        # Convert CSV/XLS to XLSX first if needed
        elif ext in to_xlsx_first:
            if ext == ".csv":
                df = pd.read_csv(path)
            else:  # .xls
                df = pd.read_excel(path)

            with tempfile.NamedTemporaryFile(delete=False, suffix=".xlsx") as tmp:
                df.to_excel(tmp.name, index=False)
                path = tmp.name

        # Process with DocLing
        if ext in docling_direct or ext in to_xlsx_first:
            result = convert_with_strategy(path)

            # Generate timestamp for filenames
            timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
            base_filename = f"document_{timestamp}"

            # 1. Docling document (JSON)
            docling_json_path = f"{base_filename}_docling.json"
            with open(docling_json_path, "w", encoding="utf-8") as f:
                json.dump(result.document.export_to_dict(), f, indent=2, ensure_ascii=False)

            # Normalize outputs
            text_out = normalize_text(result.document.export_to_text())
            md_out = normalize_text(result.document.export_to_markdown())
            html_out = normalize_text(result.document.export_to_html())

            # 2. Text file
            txt_path = f"{base_filename}.txt"
            with open(txt_path, "w", encoding="utf-8") as f:
                f.write(text_out)

            # 3. Markdown file
            md_path = f"{base_filename}.md"
            with open(md_path, "w", encoding="utf-8") as f:
                f.write(md_out)

            # 4. HTML file
            html_path = f"{base_filename}.html"
            with open(html_path, "w", encoding="utf-8") as f:
                f.write(html_out)

            success_message = "βœ… Successfully processed file! 4 files generated."
            return docling_json_path, txt_path, md_path, html_path, success_message

        elif ext == ".txt":
            # For plain text files, create all formats
            with open(path, "r", encoding="utf-8") as f:
                text_content = f.read()

            # Normalize input text as requested
            text_content = normalize_text(text_content)

            timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
            base_filename = f"document_{timestamp}"

            # 1. Docling document (JSON) - simple structure for text
            docling_json_path = f"{base_filename}_docling.json"
            docling_dict = {
                "type": "text_document",
                "content": text_content,
                "metadata": {
                    "source": os.path.basename(path),
                    "timestamp": timestamp
                }
            }
            with open(docling_json_path, "w", encoding="utf-8") as f:
                json.dump(docling_dict, f, indent=2, ensure_ascii=False)

            # 2. Text file
            txt_path = f"{base_filename}.txt"
            with open(txt_path, "w", encoding="utf-8") as f:
                f.write(text_content)

            # 3. Markdown file
            md_path = f"{base_filename}.md"
            with open(md_path, "w", encoding="utf-8") as f:
                f.write(f"# Document\n\n{text_content}")

            # 4. HTML file
            html_path = f"{base_filename}.html"
            html_content = f"""<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <pre>{text_content}</pre>
</body>
</html>"""
            with open(html_path, "w", encoding="utf-8") as f:
                f.write(html_content)

            success_message = "βœ… Successfully processed text file! 4 files generated."
            return docling_json_path, txt_path, md_path, html_path, success_message

        else:
            error_message = f"❌ Unsupported file format: {ext}"
            return None, None, None, None, error_message

    except Exception as e:
        error_message = f"❌ Error processing file: {str(e)}"
        return None, None, None, None, error_message

def reset_form():
    """Reset the form"""
    return None, None, None, None, None, ""

# Gradio Interface
with gr.Blocks(title="LLM-Ready Document Converter") as app:
    
    gr.Markdown("# πŸ“„ LLM-Ready Document Converter")
    gr.Markdown("**HOWTO** : Upload a document or image and get 4 output files: Docling JSON, TXT, Markdown, and HTML")
    gr.Markdown("**EXPLANATION** : This app transforms various document formats (like TXT, standard and scanned PDFs, DOCX, PPT, CSV, XLS, XLSX) and **images (PNG, JPG, JPEG, BMP, TIFF)** into structured, machine-readable outputs optimized for Large Language Models (LLMs). For images, it uses OCR (Optical Character Recognition) to extract text. For all input documents, it extracts and converts content into clean formats such as DocLing JSON (for document structure), plain text, Markdown, and HTML making it easier for AI models to process, analyze, or generate responses from complex documents without losing key details like layout or formatting. Essentially, it's a bridge between raw files and AI-ready data.")

    with gr.Row():
        with gr.Column():
            file_input = gr.File(
    			label="Upload Document",
    			file_types=[".pdf", ".txt", ".docx", ".xlsx", ".xls", ".csv", ".pptx", ".png", ".jpg", ".jpeg", ".bmp", ".tiff", ".tif"]
			)

        with gr.Row():
            submit_btn = gr.Button("Convert Document", variant="primary")
            reset_btn = gr.Button("Reset")

    status_output = gr.Markdown(label="Status")

    with gr.Row():
        with gr.Column():
            docling_output = gr.File(label="Docling Document (JSON)")
        with gr.Column():
            txt_output = gr.File(label="Text File")

    with gr.Row():
        with gr.Column():
            md_output = gr.File(label="Markdown File")
        with gr.Column():
            html_output = gr.File(label="HTML File")

    # Events
    submit_btn.click(
        fn=process_file,
        inputs=[file_input],
        outputs=[docling_output, txt_output, md_output, html_output, status_output]
    )

    reset_btn.click(
        fn=reset_form,
        outputs=[file_input, docling_output, txt_output, md_output, html_output, status_output]
    )

if __name__ == "__main__":
    app.launch(share=True)