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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
print("GRADIO VERSION:", gr.__version__)
|
| 3 |
import json
|
|
@@ -5,13 +176,35 @@ import os
|
|
| 5 |
import tempfile
|
| 6 |
from pathlib import Path
|
| 7 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
# NOTE: You must ensure that 'working_yolo_pipeline.py' exists
|
| 9 |
# and defines the following items correctly:
|
| 10 |
from working_yolo_pipeline import run_document_pipeline, DEFAULT_LAYOUTLMV3_MODEL_PATH, WEIGHTS_PATH
|
| 11 |
-
# Since I don't have this file, I am assuming the imports are correct.
|
| 12 |
|
| 13 |
-
# Define placeholders for assumed constants if the pipeline file isn't present
|
| 14 |
-
# You should replace these with your actual definitions if they are missing
|
| 15 |
try:
|
| 16 |
from working_yolo_pipeline import run_document_pipeline, DEFAULT_LAYOUTLMV3_MODEL_PATH, WEIGHTS_PATH
|
| 17 |
except ImportError:
|
|
@@ -25,22 +218,13 @@ except ImportError:
|
|
| 25 |
def process_pdf(pdf_file, layoutlmv3_model_path=None):
|
| 26 |
"""
|
| 27 |
Wrapper function for Gradio interface.
|
| 28 |
-
|
| 29 |
-
Args:
|
| 30 |
-
pdf_file: Gradio UploadButton file object
|
| 31 |
-
layoutlmv3_model_path: Optional custom model path
|
| 32 |
-
|
| 33 |
-
Returns:
|
| 34 |
-
Tuple of (JSON string, download file path)
|
| 35 |
"""
|
| 36 |
if pdf_file is None:
|
| 37 |
return "β Error: No PDF file uploaded.", None
|
| 38 |
|
| 39 |
-
# Use default model path if not provided
|
| 40 |
if not layoutlmv3_model_path:
|
| 41 |
layoutlmv3_model_path = DEFAULT_LAYOUTLMV3_MODEL_PATH
|
| 42 |
|
| 43 |
-
# Verify model and weights exist
|
| 44 |
if not os.path.exists(layoutlmv3_model_path):
|
| 45 |
return f"β Error: LayoutLMv3 model not found at {layoutlmv3_model_path}", None
|
| 46 |
|
|
@@ -48,24 +232,19 @@ def process_pdf(pdf_file, layoutlmv3_model_path=None):
|
|
| 48 |
return f"β Error: YOLO weights not found at {WEIGHTS_PATH}", None
|
| 49 |
|
| 50 |
try:
|
| 51 |
-
# Get the uploaded PDF path
|
| 52 |
pdf_path = pdf_file.name
|
| 53 |
|
| 54 |
-
# Run the pipeline
|
| 55 |
result = run_document_pipeline(pdf_path, layoutlmv3_model_path, 'label_studio_import.json')
|
| 56 |
|
| 57 |
if result is None:
|
| 58 |
return "β Error: Pipeline failed to process the PDF. Check console for details.", None
|
| 59 |
|
| 60 |
-
# Create a temporary file for download
|
| 61 |
output_filename = f"{Path(pdf_path).stem}_analysis.json"
|
| 62 |
temp_output = tempfile.NamedTemporaryFile(mode='w', delete=False, suffix='.json', prefix='analysis_')
|
| 63 |
|
| 64 |
-
# Dump results to the temporary file
|
| 65 |
with open(temp_output.name, 'w', encoding='utf-8') as f:
|
| 66 |
json.dump(result, f, indent=2, ensure_ascii=False)
|
| 67 |
|
| 68 |
-
# Format JSON for display
|
| 69 |
json_display = json.dumps(result, indent=2, ensure_ascii=False)
|
| 70 |
|
| 71 |
return json_display, temp_output.name
|
|
@@ -74,10 +253,15 @@ def process_pdf(pdf_file, layoutlmv3_model_path=None):
|
|
| 74 |
return f"β Error during processing: {str(e)}", None
|
| 75 |
|
| 76 |
|
| 77 |
-
|
| 78 |
-
#
|
| 79 |
-
|
| 80 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
gr.Markdown("""
|
| 82 |
# π Document Analysis Pipeline
|
| 83 |
|
|
@@ -126,19 +310,17 @@ with gr.Blocks(title="Document Analysis Pipeline",
|
|
| 126 |
interactive=False
|
| 127 |
)
|
| 128 |
|
| 129 |
-
# Status/Examples section
|
| 130 |
with gr.Row():
|
| 131 |
gr.Markdown("""
|
| 132 |
### π Output Format
|
| 133 |
The pipeline generates JSON with the following structure:
|
| 134 |
- **Questions**: Extracted question text
|
| 135 |
-
- **Options**: Multiple choice options
|
| 136 |
- **Answers**: Correct answer(s)
|
| 137 |
- **Passages**: Associated reading passages
|
| 138 |
-
- **Images**: Base64-encoded figures and equations
|
| 139 |
""")
|
| 140 |
|
| 141 |
-
# Connect the button to the processing function
|
| 142 |
process_btn.click(
|
| 143 |
fn=process_pdf,
|
| 144 |
inputs=[pdf_input, model_path_input],
|
|
@@ -146,20 +328,11 @@ with gr.Blocks(title="Document Analysis Pipeline",
|
|
| 146 |
api_name="process_document"
|
| 147 |
)
|
| 148 |
|
| 149 |
-
# Example section (optional - add example PDFs if available)
|
| 150 |
-
# gr.Examples(
|
| 151 |
-
# examples=[
|
| 152 |
-
# ["examples/sample1.pdf"],
|
| 153 |
-
# ["examples/sample2.pdf"],
|
| 154 |
-
# ],
|
| 155 |
-
# inputs=pdf_input,
|
| 156 |
-
# )
|
| 157 |
|
| 158 |
-
# Launch the app
|
| 159 |
if __name__ == "__main__":
|
| 160 |
demo.launch(
|
| 161 |
server_name="0.0.0.0",
|
| 162 |
server_port=7860,
|
| 163 |
share=False,
|
| 164 |
show_error=True
|
| 165 |
-
)
|
|
|
|
| 1 |
+
# import gradio as gr
|
| 2 |
+
# print("GRADIO VERSION:", gr.__version__)
|
| 3 |
+
# import json
|
| 4 |
+
# import os
|
| 5 |
+
# import tempfile
|
| 6 |
+
# from pathlib import Path
|
| 7 |
+
|
| 8 |
+
# # NOTE: You must ensure that 'working_yolo_pipeline.py' exists
|
| 9 |
+
# # and defines the following items correctly:
|
| 10 |
+
# from working_yolo_pipeline import run_document_pipeline, DEFAULT_LAYOUTLMV3_MODEL_PATH, WEIGHTS_PATH
|
| 11 |
+
# # Since I don't have this file, I am assuming the imports are correct.
|
| 12 |
+
|
| 13 |
+
# # Define placeholders for assumed constants if the pipeline file isn't present
|
| 14 |
+
# # You should replace these with your actual definitions if they are missing
|
| 15 |
+
# try:
|
| 16 |
+
# from working_yolo_pipeline import run_document_pipeline, DEFAULT_LAYOUTLMV3_MODEL_PATH, WEIGHTS_PATH
|
| 17 |
+
# except ImportError:
|
| 18 |
+
# print("Warning: 'working_yolo_pipeline.py' not found. Using dummy paths.")
|
| 19 |
+
# def run_document_pipeline(*args):
|
| 20 |
+
# return {"error": "Placeholder pipeline function called."}
|
| 21 |
+
# DEFAULT_LAYOUTLMV3_MODEL_PATH = "./models/layoutlmv3_model"
|
| 22 |
+
# WEIGHTS_PATH = "./weights/yolo_weights.pt"
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
# def process_pdf(pdf_file, layoutlmv3_model_path=None):
|
| 26 |
+
# """
|
| 27 |
+
# Wrapper function for Gradio interface.
|
| 28 |
+
|
| 29 |
+
# Args:
|
| 30 |
+
# pdf_file: Gradio UploadButton file object
|
| 31 |
+
# layoutlmv3_model_path: Optional custom model path
|
| 32 |
+
|
| 33 |
+
# Returns:
|
| 34 |
+
# Tuple of (JSON string, download file path)
|
| 35 |
+
# """
|
| 36 |
+
# if pdf_file is None:
|
| 37 |
+
# return "β Error: No PDF file uploaded.", None
|
| 38 |
+
|
| 39 |
+
# # Use default model path if not provided
|
| 40 |
+
# if not layoutlmv3_model_path:
|
| 41 |
+
# layoutlmv3_model_path = DEFAULT_LAYOUTLMV3_MODEL_PATH
|
| 42 |
+
|
| 43 |
+
# # Verify model and weights exist
|
| 44 |
+
# if not os.path.exists(layoutlmv3_model_path):
|
| 45 |
+
# return f"β Error: LayoutLMv3 model not found at {layoutlmv3_model_path}", None
|
| 46 |
+
|
| 47 |
+
# if not os.path.exists(WEIGHTS_PATH):
|
| 48 |
+
# return f"β Error: YOLO weights not found at {WEIGHTS_PATH}", None
|
| 49 |
+
|
| 50 |
+
# try:
|
| 51 |
+
# # Get the uploaded PDF path
|
| 52 |
+
# pdf_path = pdf_file.name
|
| 53 |
+
|
| 54 |
+
# # Run the pipeline
|
| 55 |
+
# result = run_document_pipeline(pdf_path, layoutlmv3_model_path, 'label_studio_import.json')
|
| 56 |
+
|
| 57 |
+
# if result is None:
|
| 58 |
+
# return "β Error: Pipeline failed to process the PDF. Check console for details.", None
|
| 59 |
+
|
| 60 |
+
# # Create a temporary file for download
|
| 61 |
+
# output_filename = f"{Path(pdf_path).stem}_analysis.json"
|
| 62 |
+
# temp_output = tempfile.NamedTemporaryFile(mode='w', delete=False, suffix='.json', prefix='analysis_')
|
| 63 |
+
|
| 64 |
+
# # Dump results to the temporary file
|
| 65 |
+
# with open(temp_output.name, 'w', encoding='utf-8') as f:
|
| 66 |
+
# json.dump(result, f, indent=2, ensure_ascii=False)
|
| 67 |
+
|
| 68 |
+
# # Format JSON for display
|
| 69 |
+
# json_display = json.dumps(result, indent=2, ensure_ascii=False)
|
| 70 |
+
|
| 71 |
+
# return json_display, temp_output.name
|
| 72 |
+
|
| 73 |
+
# except Exception as e:
|
| 74 |
+
# return f"β Error during processing: {str(e)}", None
|
| 75 |
+
|
| 76 |
+
|
| 77 |
+
# # Create Gradio interface
|
| 78 |
+
# # FIX APPLIED: Removed 'theme=gr.themes.Soft()' which caused the TypeError
|
| 79 |
+
# with gr.Blocks(title="Document Analysis Pipeline") as demo:
|
| 80 |
+
# gr.Markdown("""
|
| 81 |
+
# # π Document Analysis Pipeline
|
| 82 |
+
|
| 83 |
+
# Upload a PDF document to extract structured data including questions, options, answers, passages, and embedded images.
|
| 84 |
+
|
| 85 |
+
# **Pipeline Steps:**
|
| 86 |
+
# 1. π YOLO/OCR Preprocessing (word extraction + figure/equation detection)
|
| 87 |
+
# 2. π€ LayoutLMv3 Inference (BIO tagging)
|
| 88 |
+
# 3. π Structured JSON Decoding
|
| 89 |
+
# 4. πΌοΈ Base64 Image Embedding
|
| 90 |
+
# """)
|
| 91 |
+
|
| 92 |
+
# with gr.Row():
|
| 93 |
+
# with gr.Column(scale=1):
|
| 94 |
+
# pdf_input = gr.File(
|
| 95 |
+
# label="Upload PDF Document",
|
| 96 |
+
# file_types=[".pdf"],
|
| 97 |
+
# type="filepath"
|
| 98 |
+
# )
|
| 99 |
+
|
| 100 |
+
# model_path_input = gr.Textbox(
|
| 101 |
+
# label="LayoutLMv3 Model Path (optional)",
|
| 102 |
+
# placeholder=DEFAULT_LAYOUTLMV3_MODEL_PATH,
|
| 103 |
+
# value=DEFAULT_LAYOUTLMV3_MODEL_PATH,
|
| 104 |
+
# interactive=True
|
| 105 |
+
# )
|
| 106 |
+
|
| 107 |
+
# process_btn = gr.Button("π Process Document", variant="primary", size="lg")
|
| 108 |
+
|
| 109 |
+
# gr.Markdown("""
|
| 110 |
+
# ### βΉοΈ Notes:
|
| 111 |
+
# - Processing may take several minutes depending on PDF size
|
| 112 |
+
# - Figures and equations will be extracted and embedded as Base64
|
| 113 |
+
# - The output JSON includes structured questions, options, and answers
|
| 114 |
+
# """)
|
| 115 |
+
|
| 116 |
+
# with gr.Column(scale=2):
|
| 117 |
+
# json_output = gr.Code(
|
| 118 |
+
# label="Structured JSON Output",
|
| 119 |
+
# language="json",
|
| 120 |
+
# lines=25
|
| 121 |
+
# )
|
| 122 |
+
|
| 123 |
+
# download_output = gr.File(
|
| 124 |
+
# label="Download Full JSON",
|
| 125 |
+
# interactive=False
|
| 126 |
+
# )
|
| 127 |
+
|
| 128 |
+
# # Status/Examples section
|
| 129 |
+
# with gr.Row():
|
| 130 |
+
# gr.Markdown("""
|
| 131 |
+
# ### π Output Format
|
| 132 |
+
# The pipeline generates JSON with the following structure:
|
| 133 |
+
# - **Questions**: Extracted question text
|
| 134 |
+
# - **Options**: Multiple choice options (A, B, C, D, etc.)
|
| 135 |
+
# - **Answers**: Correct answer(s)
|
| 136 |
+
# - **Passages**: Associated reading passages
|
| 137 |
+
# - **Images**: Base64-encoded figures and equations (embedded with keys like `figure1`, `equation2`)
|
| 138 |
+
# """)
|
| 139 |
+
|
| 140 |
+
# # Connect the button to the processing function
|
| 141 |
+
# process_btn.click(
|
| 142 |
+
# fn=process_pdf,
|
| 143 |
+
# inputs=[pdf_input, model_path_input],
|
| 144 |
+
# outputs=[json_output, download_output],
|
| 145 |
+
# api_name="process_document"
|
| 146 |
+
# )
|
| 147 |
+
|
| 148 |
+
# # Example section (optional - add example PDFs if available)
|
| 149 |
+
# # gr.Examples(
|
| 150 |
+
# # examples=[
|
| 151 |
+
# # ["examples/sample1.pdf"],
|
| 152 |
+
# # ["examples/sample2.pdf"],
|
| 153 |
+
# # ],
|
| 154 |
+
# # inputs=pdf_input,
|
| 155 |
+
# # )
|
| 156 |
+
|
| 157 |
+
# # Launch the app
|
| 158 |
+
# if __name__ == "__main__":
|
| 159 |
+
# demo.launch(
|
| 160 |
+
# server_name="0.0.0.0",
|
| 161 |
+
# server_port=7860,
|
| 162 |
+
# share=False,
|
| 163 |
+
# show_error=True
|
| 164 |
+
# )
|
| 165 |
+
|
| 166 |
+
|
| 167 |
+
|
| 168 |
+
|
| 169 |
+
|
| 170 |
+
|
| 171 |
+
|
| 172 |
import gradio as gr
|
| 173 |
print("GRADIO VERSION:", gr.__version__)
|
| 174 |
import json
|
|
|
|
| 176 |
import tempfile
|
| 177 |
from pathlib import Path
|
| 178 |
|
| 179 |
+
# ==============================
|
| 180 |
+
# WRITE CUSTOM CSS FOR FONTS
|
| 181 |
+
# ==============================
|
| 182 |
+
|
| 183 |
+
CUSTOM_CSS = """
|
| 184 |
+
@font-face {
|
| 185 |
+
font-family: 'NotoSansMath';
|
| 186 |
+
src: url('./NotoSansMath-Regular.ttf') format('truetype');
|
| 187 |
+
font-weight: normal;
|
| 188 |
+
font-style: normal;
|
| 189 |
+
}
|
| 190 |
+
|
| 191 |
+
html, body, * {
|
| 192 |
+
font-family: 'NotoSansMath', sans-serif !important;
|
| 193 |
+
}
|
| 194 |
+
"""
|
| 195 |
+
|
| 196 |
+
# Write the CSS file if not exists
|
| 197 |
+
if not os.path.exists("custom.css"):
|
| 198 |
+
with open("custom.css", "w") as f:
|
| 199 |
+
f.write(CUSTOM_CSS)
|
| 200 |
+
# ==============================
|
| 201 |
+
|
| 202 |
+
|
| 203 |
+
|
| 204 |
# NOTE: You must ensure that 'working_yolo_pipeline.py' exists
|
| 205 |
# and defines the following items correctly:
|
| 206 |
from working_yolo_pipeline import run_document_pipeline, DEFAULT_LAYOUTLMV3_MODEL_PATH, WEIGHTS_PATH
|
|
|
|
| 207 |
|
|
|
|
|
|
|
| 208 |
try:
|
| 209 |
from working_yolo_pipeline import run_document_pipeline, DEFAULT_LAYOUTLMV3_MODEL_PATH, WEIGHTS_PATH
|
| 210 |
except ImportError:
|
|
|
|
| 218 |
def process_pdf(pdf_file, layoutlmv3_model_path=None):
|
| 219 |
"""
|
| 220 |
Wrapper function for Gradio interface.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 221 |
"""
|
| 222 |
if pdf_file is None:
|
| 223 |
return "β Error: No PDF file uploaded.", None
|
| 224 |
|
|
|
|
| 225 |
if not layoutlmv3_model_path:
|
| 226 |
layoutlmv3_model_path = DEFAULT_LAYOUTLMV3_MODEL_PATH
|
| 227 |
|
|
|
|
| 228 |
if not os.path.exists(layoutlmv3_model_path):
|
| 229 |
return f"β Error: LayoutLMv3 model not found at {layoutlmv3_model_path}", None
|
| 230 |
|
|
|
|
| 232 |
return f"β Error: YOLO weights not found at {WEIGHTS_PATH}", None
|
| 233 |
|
| 234 |
try:
|
|
|
|
| 235 |
pdf_path = pdf_file.name
|
| 236 |
|
|
|
|
| 237 |
result = run_document_pipeline(pdf_path, layoutlmv3_model_path, 'label_studio_import.json')
|
| 238 |
|
| 239 |
if result is None:
|
| 240 |
return "β Error: Pipeline failed to process the PDF. Check console for details.", None
|
| 241 |
|
|
|
|
| 242 |
output_filename = f"{Path(pdf_path).stem}_analysis.json"
|
| 243 |
temp_output = tempfile.NamedTemporaryFile(mode='w', delete=False, suffix='.json', prefix='analysis_')
|
| 244 |
|
|
|
|
| 245 |
with open(temp_output.name, 'w', encoding='utf-8') as f:
|
| 246 |
json.dump(result, f, indent=2, ensure_ascii=False)
|
| 247 |
|
|
|
|
| 248 |
json_display = json.dumps(result, indent=2, ensure_ascii=False)
|
| 249 |
|
| 250 |
return json_display, temp_output.name
|
|
|
|
| 253 |
return f"β Error during processing: {str(e)}", None
|
| 254 |
|
| 255 |
|
| 256 |
+
|
| 257 |
+
# ======================
|
| 258 |
+
# CREATE THE GRADIO UI
|
| 259 |
+
# ======================
|
| 260 |
+
with gr.Blocks(
|
| 261 |
+
title="Document Analysis Pipeline",
|
| 262 |
+
css_paths=["custom.css"] # <ββ IMPORTANT FOR CUSTOM FONTS
|
| 263 |
+
) as demo:
|
| 264 |
+
|
| 265 |
gr.Markdown("""
|
| 266 |
# π Document Analysis Pipeline
|
| 267 |
|
|
|
|
| 310 |
interactive=False
|
| 311 |
)
|
| 312 |
|
|
|
|
| 313 |
with gr.Row():
|
| 314 |
gr.Markdown("""
|
| 315 |
### π Output Format
|
| 316 |
The pipeline generates JSON with the following structure:
|
| 317 |
- **Questions**: Extracted question text
|
| 318 |
+
- **Options**: Multiple choice options
|
| 319 |
- **Answers**: Correct answer(s)
|
| 320 |
- **Passages**: Associated reading passages
|
| 321 |
+
- **Images**: Base64-encoded figures and equations
|
| 322 |
""")
|
| 323 |
|
|
|
|
| 324 |
process_btn.click(
|
| 325 |
fn=process_pdf,
|
| 326 |
inputs=[pdf_input, model_path_input],
|
|
|
|
| 328 |
api_name="process_document"
|
| 329 |
)
|
| 330 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 331 |
|
|
|
|
| 332 |
if __name__ == "__main__":
|
| 333 |
demo.launch(
|
| 334 |
server_name="0.0.0.0",
|
| 335 |
server_port=7860,
|
| 336 |
share=False,
|
| 337 |
show_error=True
|
| 338 |
+
)
|