Commit ·
c6cd2bc
1
Parent(s): 53843a5
Save processed files and add download support
Browse files
app.py
CHANGED
|
@@ -1,4 +1,5 @@
|
|
| 1 |
import json
|
|
|
|
| 2 |
from pathlib import Path
|
| 3 |
|
| 4 |
import gradio as gr
|
|
@@ -7,10 +8,12 @@ import spaces
|
|
| 7 |
from document_processor import DocumentProcessor
|
| 8 |
from normalizer import DocumentNormalizer
|
| 9 |
|
| 10 |
-
|
| 11 |
processor = DocumentProcessor()
|
| 12 |
normalizer = DocumentNormalizer()
|
| 13 |
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
@spaces.GPU(duration=120)
|
| 16 |
def process_document(file):
|
|
@@ -36,6 +39,27 @@ def process_document(file):
|
|
| 36 |
|
| 37 |
output = normalized.model_dump()
|
| 38 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
return (
|
| 40 |
json.dumps(
|
| 41 |
output,
|
|
@@ -48,6 +72,7 @@ def process_document(file):
|
|
| 48 |
indent=2,
|
| 49 |
ensure_ascii=False,
|
| 50 |
),
|
|
|
|
| 51 |
)
|
| 52 |
|
| 53 |
except Exception as e:
|
|
@@ -99,6 +124,13 @@ with gr.Blocks(
|
|
| 99 |
language="json",
|
| 100 |
)
|
| 101 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 102 |
with gr.Tabs():
|
| 103 |
|
| 104 |
with gr.Tab("AI Tutor JSON"):
|
|
@@ -119,6 +151,7 @@ with gr.Blocks(
|
|
| 119 |
json_output,
|
| 120 |
markdown_output,
|
| 121 |
statistics_output,
|
|
|
|
| 122 |
],
|
| 123 |
api_name="process_document",
|
| 124 |
)
|
|
|
|
| 1 |
import json
|
| 2 |
+
import tempfile
|
| 3 |
from pathlib import Path
|
| 4 |
|
| 5 |
import gradio as gr
|
|
|
|
| 8 |
from document_processor import DocumentProcessor
|
| 9 |
from normalizer import DocumentNormalizer
|
| 10 |
|
|
|
|
| 11 |
processor = DocumentProcessor()
|
| 12 |
normalizer = DocumentNormalizer()
|
| 13 |
|
| 14 |
+
OUTPUT_DIR = Path(tempfile.gettempdir()) / "ai-tutor-output"
|
| 15 |
+
OUTPUT_DIR.mkdir(parents=True, exist_ok=True)
|
| 16 |
+
|
| 17 |
|
| 18 |
@spaces.GPU(duration=120)
|
| 19 |
def process_document(file):
|
|
|
|
| 39 |
|
| 40 |
output = normalized.model_dump()
|
| 41 |
|
| 42 |
+
base_name = path.stem
|
| 43 |
+
json_path = OUTPUT_DIR / f"{base_name}_normalized.json"
|
| 44 |
+
markdown_path = OUTPUT_DIR / f"{base_name}_markdown.md"
|
| 45 |
+
stats_path = OUTPUT_DIR / f"{base_name}_statistics.json"
|
| 46 |
+
|
| 47 |
+
json_path.write_text(
|
| 48 |
+
json.dumps(output, indent=2, ensure_ascii=False),
|
| 49 |
+
encoding="utf-8",
|
| 50 |
+
)
|
| 51 |
+
markdown_path.write_text(result["markdown"], encoding="utf-8")
|
| 52 |
+
stats_path.write_text(
|
| 53 |
+
json.dumps(normalized.statistics, indent=2, ensure_ascii=False),
|
| 54 |
+
encoding="utf-8",
|
| 55 |
+
)
|
| 56 |
+
|
| 57 |
+
download_files = [
|
| 58 |
+
(str(json_path), json_path.name),
|
| 59 |
+
(str(markdown_path), markdown_path.name),
|
| 60 |
+
(str(stats_path), stats_path.name),
|
| 61 |
+
]
|
| 62 |
+
|
| 63 |
return (
|
| 64 |
json.dumps(
|
| 65 |
output,
|
|
|
|
| 72 |
indent=2,
|
| 73 |
ensure_ascii=False,
|
| 74 |
),
|
| 75 |
+
download_files,
|
| 76 |
)
|
| 77 |
|
| 78 |
except Exception as e:
|
|
|
|
| 124 |
language="json",
|
| 125 |
)
|
| 126 |
|
| 127 |
+
gr.Markdown("## Download")
|
| 128 |
+
|
| 129 |
+
download_output = gr.File(
|
| 130 |
+
label="Download Processed Files",
|
| 131 |
+
file_count="multiple",
|
| 132 |
+
)
|
| 133 |
+
|
| 134 |
with gr.Tabs():
|
| 135 |
|
| 136 |
with gr.Tab("AI Tutor JSON"):
|
|
|
|
| 151 |
json_output,
|
| 152 |
markdown_output,
|
| 153 |
statistics_output,
|
| 154 |
+
download_output,
|
| 155 |
],
|
| 156 |
api_name="process_document",
|
| 157 |
)
|