File size: 6,994 Bytes
53d9817
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import json
import os
import sys
import time
from dataclasses import asdict
from pathlib import Path

# Cap BLAS/ONNX worker threads to prevent full CPU pegging / system stutter
os.environ.setdefault("OMP_NUM_THREADS", "4")
os.environ.setdefault("OPENBLAS_NUM_THREADS", "4")
os.environ.setdefault("MKL_NUM_THREADS", "4")

# Ensure workspace root is always in Python search path
sys.path.insert(0, str(Path(__file__).parent.resolve()))

import gradio as gr

from universal_parser import parse, to_chunks, to_graph, to_markdown

# Hugging Face ZeroGPU compatibility handler (startup check only)
try:
    import spaces

    @spaces.GPU(duration=1)
    def _zero_gpu_startup_guard():
        """Satisfies ZeroGPU startup check without consuming user GPU quota."""
        return None

except (ImportError, Exception):
    pass


def process_document(file_obj, max_chunk_tokens, progress=gr.Progress(track_tqdm=True)):
    if file_obj is None:
        return (
            "### Please upload a document to begin parsing.",
            "[]",
            "{}",
            "{}",
            "Zero input provided.",
        )

    file_path = file_obj.name
    start_time = time.perf_counter()

    try:
        progress(0.1, desc="Reading file & extracting document structure...")
        doc = parse(file_path)
        elapsed_ms = round((time.perf_counter() - start_time) * 1000, 2)

        # 1. Render Markdown
        progress(0.6, desc="Rendering Markdown & RAG chunks...")
        markdown_output = to_markdown(doc)

        # 2. Render Hierarchical RAG Chunks (capped in UI preview to prevent browser lag)
        chunks = to_chunks(doc, max_tokens=int(max_chunk_tokens))
        chunk_data = [
            {
                "chunk_id": c.chunk_id,
                "headings": c.headings,
                "estimated_tokens": c.estimated_tokens,
                "page_numbers": c.page_numbers,
                "text": c.text,
            }
            for c in chunks
        ]
        display_chunks = chunk_data[:100]
        if len(chunk_data) > 100:
            display_chunks.append(
                {"_ui_note": f"Showing first 100 of {len(chunk_data)} total chunks for UI speed."}
            )
        chunks_json = json.dumps(display_chunks, indent=2)

        # 3. Render Knowledge Graph
        progress(0.8, desc="Building Knowledge Graph & Schemas...")
        graph = to_graph(doc)
        graph_dict = asdict(graph)
        if len(graph_dict.get("nodes", [])) > 200:
            total_nodes = len(graph_dict["nodes"])
            graph_dict["nodes"] = graph_dict["nodes"][:200]
            graph_dict["_ui_note"] = f"Displaying first 200 of {total_nodes} nodes in UI."
        graph_json = json.dumps(graph_dict, indent=2)

        # 4. Render Raw Schema JSON (capped in browser code viewer for smoothness)
        doc_dump = doc.model_dump()
        if len(doc_dump.get("content_tree", [])) > 200:
            total_elements = len(doc_dump["content_tree"])
            doc_dump["content_tree"] = doc_dump["content_tree"][:200]
            doc_dump["_ui_note"] = (
                f"Displaying first 200 of {total_elements} elements in UI. Document is 100% parsed."
            )
        schema_json = json.dumps(doc_dump, indent=2, default=str)

        # 5. Telemetry & Metrics Summary
        element_types = {}
        for elem in doc.content_tree:
            element_types[elem.type] = element_types.get(elem.type, 0) + 1

        metrics_summary = f"""### Extraction Telemetry
- **File Name:** `{doc.metadata.file_name}`
- **Format:** `{doc.metadata.file_type.upper()}`
- **Parsing Latency:** `{elapsed_ms} ms`
- **Total Pages:** `{doc.metadata.page_count}`
- **Total Elements:** `{len(doc.content_tree)}`
- **Generated Chunks:** `{len(chunks)}`

#### Element Distribution
""" + "\n".join([f"- **{k.capitalize()}:** `{v}`" for k, v in element_types.items()])

        progress(1.0, desc="Complete!")
        return (
            markdown_output,
            chunks_json,
            graph_json,
            schema_json,
            metrics_summary,
        )

    except Exception as e:
        error_msg = f"### Extraction Error\nAn error occurred while processing the file: `{str(e)}`"
        return error_msg, "[]", "{}", "{}", "Processing failed."


# Build Gradio UI
with gr.Blocks(
    title="Universal Document Parser — Zero-GPU RAG Ingestion",
    theme=gr.themes.Soft(primary_hue="blue", neutral_hue="slate"),
) as demo:
    gr.Markdown(
        """
        # Universal Document Parser
        ### Zero-GPU, CPU-Only Engineered Ingestion Engine for RAG Pipelines & AI Agents

        Upload any document below (PDF, DOCX, XLSX, PPTX, HTML, EPUB, CSV, Parquet, JSON, XML, EML, MSG, Image) to extract structured Markdown, hierarchical RAG chunks, knowledge graph entities, and schema telemetry in real-time.
        """
    )

    with gr.Row():
        with gr.Column(scale=1):
            file_input = gr.File(
                label="Upload Document",
                file_types=[
                    ".pdf",
                    ".docx",
                    ".xlsx",
                    ".pptx",
                    ".html",
                    ".epub",
                    ".csv",
                    ".tsv",
                    ".parquet",
                    ".json",
                    ".xml",
                    ".eml",
                    ".msg",
                    ".mbox",
                    ".png",
                    ".jpg",
                    ".jpeg",
                    ".webp",
                ],
            )
            chunk_token_slider = gr.Slider(
                minimum=128,
                maximum=2048,
                value=512,
                step=64,
                label="RAG Chunk Max Tokens",
                info="Token budget per chunk for hierarchical breadcrumbs.",
            )
            parse_btn = gr.Button("Parse Document", variant="primary", size="lg")
            metrics_output = gr.Markdown(label="Extraction Telemetry")

        with gr.Column(scale=2):
            with gr.Tabs():
                with gr.TabItem("Markdown Output"):
                    markdown_output = gr.Markdown(label="Extracted Markdown")
                with gr.TabItem("RAG Chunks"):
                    chunks_output = gr.Code(label="Hierarchical Chunks JSON", language="json")
                with gr.TabItem("Knowledge Graph"):
                    graph_output = gr.Code(label="Graph Nodes & Edges JSON", language="json")
                with gr.TabItem("Full Pydantic Schema"):
                    schema_output = gr.Code(label="Document Schema JSON", language="json")

    parse_btn.click(
        fn=process_document,
        inputs=[file_input, chunk_token_slider],
        outputs=[
            markdown_output,
            chunks_output,
            graph_output,
            schema_output,
            metrics_output,
        ],
        api_name=False,
    )

if __name__ == "__main__":
    demo.launch(ssr_mode=False)