File size: 3,692 Bytes
53843a5
c6cd2bc
53843a5
 
221e41f
4c793dd
221e41f
cd7e591
53843a5
221e41f
cd7e591
53843a5
cd7e591
c6cd2bc
 
 
cd7e591
53843a5
cd7e591
 
 
53843a5
cd7e591
53843a5
ac57c76
53843a5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c6cd2bc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fab01c3
 
 
c6cd2bc
 
53843a5
 
 
 
 
 
 
 
 
 
 
 
c6cd2bc
53843a5
cd7e591
 
53843a5
 
 
ac57c76
cd7e591
53843a5
 
 
cd7e591
53843a5
 
 
4c793dd
53843a5
 
cd7e591
53843a5
 
 
cd7e591
53843a5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c6cd2bc
 
 
 
 
 
 
53843a5
 
 
cd7e591
53843a5
 
 
 
cd7e591
53843a5
 
 
 
 
 
 
 
 
 
 
c6cd2bc
53843a5
 
cd7e591
 
 
53843a5
e00a78c
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
import json
import tempfile
from pathlib import Path

import gradio as gr
import spaces

from document_processor import DocumentProcessor
from normalizer import DocumentNormalizer

processor = DocumentProcessor()
normalizer = DocumentNormalizer()

OUTPUT_DIR = Path(tempfile.gettempdir()) / "ai-tutor-output"
OUTPUT_DIR.mkdir(parents=True, exist_ok=True)


@spaces.GPU(duration=120)
def process_document(file):

    if file is None:
        raise gr.Error("Please upload a document.")

    file_path = file

    if hasattr(file, "name"):
        file_path = file.name

    path = Path(file_path)

    try:
        result = processor.process(str(path))

        normalized = normalizer.normalize(
            docling_document=result["document"],
            filename=path.name,
            raw_json=result["raw_json"],
        )

        output = normalized.model_dump()

        base_name = path.stem
        json_path = OUTPUT_DIR / f"{base_name}_normalized.json"
        markdown_path = OUTPUT_DIR / f"{base_name}_markdown.md"
        stats_path = OUTPUT_DIR / f"{base_name}_statistics.json"

        json_path.write_text(
            json.dumps(output, indent=2, ensure_ascii=False),
            encoding="utf-8",
        )
        markdown_path.write_text(result["markdown"], encoding="utf-8")
        stats_path.write_text(
            json.dumps(normalized.statistics, indent=2, ensure_ascii=False),
            encoding="utf-8",
        )

        download_files = [
            str(json_path),
            str(markdown_path),
            str(stats_path),
        ]

        return (
            json.dumps(
                output,
                indent=2,
                ensure_ascii=False,
            ),
            result["markdown"],
            json.dumps(
                normalized.statistics,
                indent=2,
                ensure_ascii=False,
            ),
            download_files,
        )

    except Exception as e:
        raise gr.Error(
            f"Document processing failed: {type(e).__name__}: {e}"
        )


with gr.Blocks(
    title="AI Tutor Document Processor"
) as demo:

    gr.Markdown(
        """
        # 📚 AI Tutor Document Processor

        Convert educational documents into a structured
        AI-Tutor document model.

        **Supported:** PDF, DOCX, PPTX, HTML, Markdown, TXT
        """
    )

    with gr.Row():

        file_input = gr.File(
            label="Upload Document",
            file_types=[
                ".pdf",
                ".docx",
                ".pptx",
                ".html",
                ".htm",
                ".md",
                ".txt",
            ],
            type="filepath",
        )

        process_button = gr.Button(
            "Process Document",
            variant="primary",
        )

    gr.Markdown("## Processing Statistics")

    statistics_output = gr.Code(
        label="Statistics",
        language="json",
    )

    gr.Markdown("## Download")

    download_output = gr.File(
        label="Download Processed Files",
        file_count="multiple",
    )

    with gr.Tabs():

        with gr.Tab("AI Tutor JSON"):

            json_output = gr.Code(
                label="Normalized Document",
                language="json",
            )

        with gr.Tab("Markdown"):

            markdown_output = gr.Markdown()

    process_button.click(
        fn=process_document,
        inputs=file_input,
        outputs=[
            json_output,
            markdown_output,
            statistics_output,
            download_output,
        ],
        api_name="process_document",
    )


if __name__ == "__main__":
    demo.launch(show_error=True)