Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,3 +1,144 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
print("GRADIO VERSION:", gr.__version__)
|
| 3 |
import json
|
|
@@ -25,6 +166,24 @@ def process_file(uploaded_file, layoutlmv3_model_path=None):
|
|
| 25 |
if uploaded_file is None:
|
| 26 |
return "β Error: No file uploaded.", None
|
| 27 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
if not layoutlmv3_model_path:
|
| 29 |
layoutlmv3_model_path = DEFAULT_LAYOUTLMV3_MODEL_PATH
|
| 30 |
|
|
@@ -35,15 +194,12 @@ def process_file(uploaded_file, layoutlmv3_model_path=None):
|
|
| 35 |
return f"β Error: YOLO weights not found at {WEIGHTS_PATH}", None
|
| 36 |
|
| 37 |
try:
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
# Determine file type for logging
|
| 41 |
ext = Path(file_path).suffix.lower()
|
| 42 |
file_type = "Image" if ext in ['.jpg', '.jpeg', '.png', '.bmp', '.tiff', '.webp'] else "PDF"
|
| 43 |
print(f"π Starting pipeline for {file_type}: {file_path}")
|
| 44 |
|
| 45 |
-
# Call the pipeline
|
| 46 |
-
# Our modified working_yolo_pipeline now handles the branching internally.
|
| 47 |
result = run_document_pipeline(file_path, layoutlmv3_model_path)
|
| 48 |
|
| 49 |
if result is None:
|
|
@@ -61,6 +217,9 @@ def process_file(uploaded_file, layoutlmv3_model_path=None):
|
|
| 61 |
return json_display, temp_output.name
|
| 62 |
|
| 63 |
except Exception as e:
|
|
|
|
|
|
|
|
|
|
| 64 |
return f"β Error during processing: {str(e)}", None
|
| 65 |
|
| 66 |
|
|
@@ -71,16 +230,7 @@ with gr.Blocks(title="Document Analysis Pipeline") as demo:
|
|
| 71 |
|
| 72 |
gr.Markdown("""
|
| 73 |
# π Document & Image Analysis Pipeline
|
| 74 |
-
|
| 75 |
Upload a **PDF document** or an **Image (JPG/PNG)** to extract structured data.
|
| 76 |
-
|
| 77 |
-
**Supported Formats:** `.pdf`, `.jpg`, `.jpeg`, `.png`, `.bmp`, `.webp`
|
| 78 |
-
|
| 79 |
-
**Pipeline Steps:**
|
| 80 |
-
1. π **YOLO/OCR**: Word extraction + Figure/Equation detection
|
| 81 |
-
2. π€ **LayoutLMv3**: BIO tagging and structural analysis
|
| 82 |
-
3. π **Decoding**: Conversion to hierarchical JSON
|
| 83 |
-
4. πΌοΈ **Extraction**: Base64 embedding of detected visual elements
|
| 84 |
""")
|
| 85 |
|
| 86 |
with gr.Row():
|
|
@@ -88,7 +238,8 @@ with gr.Blocks(title="Document Analysis Pipeline") as demo:
|
|
| 88 |
file_input = gr.File(
|
| 89 |
label="Upload PDF or Image",
|
| 90 |
file_types=[".pdf", ".jpg", ".jpeg", ".png", ".bmp", ".webp", ".tiff"],
|
| 91 |
-
type="filepath"
|
|
|
|
| 92 |
)
|
| 93 |
|
| 94 |
model_path_input = gr.Textbox(
|
|
@@ -100,13 +251,6 @@ with gr.Blocks(title="Document Analysis Pipeline") as demo:
|
|
| 100 |
|
| 101 |
process_btn = gr.Button("π Process File", variant="primary", size="lg")
|
| 102 |
|
| 103 |
-
gr.Markdown("""
|
| 104 |
-
### βΉοΈ Notes:
|
| 105 |
-
- **Images** are treated as single-page documents.
|
| 106 |
-
- **PDFs** are processed page-by-page.
|
| 107 |
-
- High-resolution Tesseract OCR is used for all image content.
|
| 108 |
-
""")
|
| 109 |
-
|
| 110 |
with gr.Column(scale=2):
|
| 111 |
json_output = gr.Code(
|
| 112 |
label="Structured JSON Output",
|
|
@@ -133,4 +277,4 @@ if __name__ == "__main__":
|
|
| 133 |
server_port=7860,
|
| 134 |
share=False,
|
| 135 |
show_error=True
|
| 136 |
-
)
|
|
|
|
| 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 |
+
# # ==============================
|
| 9 |
+
# # PIPELINE IMPORT
|
| 10 |
+
# # ==============================
|
| 11 |
+
# try:
|
| 12 |
+
# from working_yolo_pipeline import run_document_pipeline, DEFAULT_LAYOUTLMV3_MODEL_PATH, WEIGHTS_PATH
|
| 13 |
+
# except ImportError:
|
| 14 |
+
# print("Warning: 'working_yolo_pipeline.py' not found. Using dummy paths.")
|
| 15 |
+
# def run_document_pipeline(*args):
|
| 16 |
+
# return {"error": "Placeholder pipeline function called."}
|
| 17 |
+
# DEFAULT_LAYOUTLMV3_MODEL_PATH = "./models/layoutlmv3_model"
|
| 18 |
+
# WEIGHTS_PATH = "./weights/yolo_weights.pt"
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
# def process_file(uploaded_file, layoutlmv3_model_path=None):
|
| 22 |
+
# """
|
| 23 |
+
# Handles both PDF and Image uploads and routes them to the YOLO/OCR pipeline.
|
| 24 |
+
# """
|
| 25 |
+
# if uploaded_file is None:
|
| 26 |
+
# return "β Error: No file uploaded.", None
|
| 27 |
+
|
| 28 |
+
# if not layoutlmv3_model_path:
|
| 29 |
+
# layoutlmv3_model_path = DEFAULT_LAYOUTLMV3_MODEL_PATH
|
| 30 |
+
|
| 31 |
+
# if not os.path.exists(layoutlmv3_model_path):
|
| 32 |
+
# return f"β Error: LayoutLMv3 model not found at {layoutlmv3_model_path}", None
|
| 33 |
+
|
| 34 |
+
# if not os.path.exists(WEIGHTS_PATH):
|
| 35 |
+
# return f"β Error: YOLO weights not found at {WEIGHTS_PATH}", None
|
| 36 |
+
|
| 37 |
+
# try:
|
| 38 |
+
# file_path = uploaded_file.name
|
| 39 |
+
|
| 40 |
+
# # Determine file type for logging
|
| 41 |
+
# ext = Path(file_path).suffix.lower()
|
| 42 |
+
# file_type = "Image" if ext in ['.jpg', '.jpeg', '.png', '.bmp', '.tiff', '.webp'] else "PDF"
|
| 43 |
+
# print(f"π Starting pipeline for {file_type}: {file_path}")
|
| 44 |
+
|
| 45 |
+
# # Call the pipeline exactly as before.
|
| 46 |
+
# # Our modified working_yolo_pipeline now handles the branching internally.
|
| 47 |
+
# result = run_document_pipeline(file_path, layoutlmv3_model_path)
|
| 48 |
+
|
| 49 |
+
# if result is None:
|
| 50 |
+
# return "β Error: Pipeline failed to process the document. Check console for details.", None
|
| 51 |
+
|
| 52 |
+
# # Prepare output file for download
|
| 53 |
+
# output_filename = f"{Path(file_path).stem}_analysis.json"
|
| 54 |
+
# temp_output = tempfile.NamedTemporaryFile(mode='w', delete=False, suffix='.json', prefix='analysis_')
|
| 55 |
+
|
| 56 |
+
# with open(temp_output.name, 'w', encoding='utf-8') as f:
|
| 57 |
+
# json.dump(result, f, indent=2, ensure_ascii=False)
|
| 58 |
+
|
| 59 |
+
# json_display = json.dumps(result, indent=2, ensure_ascii=False)
|
| 60 |
+
|
| 61 |
+
# return json_display, temp_output.name
|
| 62 |
+
|
| 63 |
+
# except Exception as e:
|
| 64 |
+
# return f"β Error during processing: {str(e)}", None
|
| 65 |
+
|
| 66 |
+
|
| 67 |
+
# # ==============================
|
| 68 |
+
# # GRADIO INTERFACE
|
| 69 |
+
# # ==============================
|
| 70 |
+
# with gr.Blocks(title="Document Analysis Pipeline") as demo:
|
| 71 |
+
|
| 72 |
+
# gr.Markdown("""
|
| 73 |
+
# # π Document & Image Analysis Pipeline
|
| 74 |
+
|
| 75 |
+
# Upload a **PDF document** or an **Image (JPG/PNG)** to extract structured data.
|
| 76 |
+
|
| 77 |
+
# **Supported Formats:** `.pdf`, `.jpg`, `.jpeg`, `.png`, `.bmp`, `.webp`
|
| 78 |
+
|
| 79 |
+
# **Pipeline Steps:**
|
| 80 |
+
# 1. π **YOLO/OCR**: Word extraction + Figure/Equation detection
|
| 81 |
+
# 2. π€ **LayoutLMv3**: BIO tagging and structural analysis
|
| 82 |
+
# 3. π **Decoding**: Conversion to hierarchical JSON
|
| 83 |
+
# 4. πΌοΈ **Extraction**: Base64 embedding of detected visual elements
|
| 84 |
+
# """)
|
| 85 |
+
|
| 86 |
+
# with gr.Row():
|
| 87 |
+
# with gr.Column(scale=1):
|
| 88 |
+
# file_input = gr.File(
|
| 89 |
+
# label="Upload PDF or Image",
|
| 90 |
+
# file_types=[".pdf", ".jpg", ".jpeg", ".png", ".bmp", ".webp", ".tiff"],
|
| 91 |
+
# type="filepath"
|
| 92 |
+
# )
|
| 93 |
+
|
| 94 |
+
# model_path_input = gr.Textbox(
|
| 95 |
+
# label="LayoutLMv3 Model Path (optional)",
|
| 96 |
+
# placeholder=DEFAULT_LAYOUTLMV3_MODEL_PATH,
|
| 97 |
+
# value=DEFAULT_LAYOUTLMV3_MODEL_PATH,
|
| 98 |
+
# interactive=True
|
| 99 |
+
# )
|
| 100 |
+
|
| 101 |
+
# process_btn = gr.Button("π Process File", variant="primary", size="lg")
|
| 102 |
+
|
| 103 |
+
# gr.Markdown("""
|
| 104 |
+
# ### βΉοΈ Notes:
|
| 105 |
+
# - **Images** are treated as single-page documents.
|
| 106 |
+
# - **PDFs** are processed page-by-page.
|
| 107 |
+
# - High-resolution Tesseract OCR is used for all image content.
|
| 108 |
+
# """)
|
| 109 |
+
|
| 110 |
+
# with gr.Column(scale=2):
|
| 111 |
+
# json_output = gr.Code(
|
| 112 |
+
# label="Structured JSON Output",
|
| 113 |
+
# language="json",
|
| 114 |
+
# lines=25
|
| 115 |
+
# )
|
| 116 |
+
|
| 117 |
+
# download_output = gr.File(
|
| 118 |
+
# label="Download Full JSON",
|
| 119 |
+
# interactive=False
|
| 120 |
+
# )
|
| 121 |
+
|
| 122 |
+
# # UI Logic
|
| 123 |
+
# process_btn.click(
|
| 124 |
+
# fn=process_file,
|
| 125 |
+
# inputs=[file_input, model_path_input],
|
| 126 |
+
# outputs=[json_output, download_output],
|
| 127 |
+
# api_name="process_document"
|
| 128 |
+
# )
|
| 129 |
+
|
| 130 |
+
# if __name__ == "__main__":
|
| 131 |
+
# demo.launch(
|
| 132 |
+
# server_name="0.0.0.0",
|
| 133 |
+
# server_port=7860,
|
| 134 |
+
# share=False,
|
| 135 |
+
# show_error=True
|
| 136 |
+
# )
|
| 137 |
+
|
| 138 |
+
|
| 139 |
+
|
| 140 |
+
|
| 141 |
+
|
| 142 |
import gradio as gr
|
| 143 |
print("GRADIO VERSION:", gr.__version__)
|
| 144 |
import json
|
|
|
|
| 166 |
if uploaded_file is None:
|
| 167 |
return "β Error: No file uploaded.", None
|
| 168 |
|
| 169 |
+
# --- FIX FOR GRADIO 6.x FILE HANDLING ---
|
| 170 |
+
# If multiple files were somehow uploaded or Gradio returned a list
|
| 171 |
+
if isinstance(uploaded_file, list):
|
| 172 |
+
uploaded_file = uploaded_file[0]
|
| 173 |
+
|
| 174 |
+
# Extract the actual file path string.
|
| 175 |
+
# Gradio File objects have a '.path' attribute for the temporary local location.
|
| 176 |
+
try:
|
| 177 |
+
if hasattr(uploaded_file, 'path'):
|
| 178 |
+
file_path = uploaded_file.path
|
| 179 |
+
elif isinstance(uploaded_file, dict):
|
| 180 |
+
file_path = uploaded_file.get("path")
|
| 181 |
+
else:
|
| 182 |
+
file_path = str(uploaded_file)
|
| 183 |
+
except Exception as e:
|
| 184 |
+
return f"β Error resolving file path: {str(e)}", None
|
| 185 |
+
# ---------------------------------------
|
| 186 |
+
|
| 187 |
if not layoutlmv3_model_path:
|
| 188 |
layoutlmv3_model_path = DEFAULT_LAYOUTLMV3_MODEL_PATH
|
| 189 |
|
|
|
|
| 194 |
return f"β Error: YOLO weights not found at {WEIGHTS_PATH}", None
|
| 195 |
|
| 196 |
try:
|
| 197 |
+
# Determine file type for logging safely
|
|
|
|
|
|
|
| 198 |
ext = Path(file_path).suffix.lower()
|
| 199 |
file_type = "Image" if ext in ['.jpg', '.jpeg', '.png', '.bmp', '.tiff', '.webp'] else "PDF"
|
| 200 |
print(f"π Starting pipeline for {file_type}: {file_path}")
|
| 201 |
|
| 202 |
+
# Call the pipeline
|
|
|
|
| 203 |
result = run_document_pipeline(file_path, layoutlmv3_model_path)
|
| 204 |
|
| 205 |
if result is None:
|
|
|
|
| 217 |
return json_display, temp_output.name
|
| 218 |
|
| 219 |
except Exception as e:
|
| 220 |
+
# This is where your previous error message was being caught and returned
|
| 221 |
+
import traceback
|
| 222 |
+
traceback.print_exc() # This prints the full error to your terminal for debugging
|
| 223 |
return f"β Error during processing: {str(e)}", None
|
| 224 |
|
| 225 |
|
|
|
|
| 230 |
|
| 231 |
gr.Markdown("""
|
| 232 |
# π Document & Image Analysis Pipeline
|
|
|
|
| 233 |
Upload a **PDF document** or an **Image (JPG/PNG)** to extract structured data.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 234 |
""")
|
| 235 |
|
| 236 |
with gr.Row():
|
|
|
|
| 238 |
file_input = gr.File(
|
| 239 |
label="Upload PDF or Image",
|
| 240 |
file_types=[".pdf", ".jpg", ".jpeg", ".png", ".bmp", ".webp", ".tiff"],
|
| 241 |
+
type="filepath",
|
| 242 |
+
file_count="single" # Force single file to avoid list/tuple issues
|
| 243 |
)
|
| 244 |
|
| 245 |
model_path_input = gr.Textbox(
|
|
|
|
| 251 |
|
| 252 |
process_btn = gr.Button("π Process File", variant="primary", size="lg")
|
| 253 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 254 |
with gr.Column(scale=2):
|
| 255 |
json_output = gr.Code(
|
| 256 |
label="Structured JSON Output",
|
|
|
|
| 277 |
server_port=7860,
|
| 278 |
share=False,
|
| 279 |
show_error=True
|
| 280 |
+
)
|