Update app.py
Browse files123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325# import gradio as gr# print("GRADIO VERSION:", gr.__version__)# import json# import os# import tempfile# from pathlib import Path# # NOTE: You must ensure that 'working_yolo_pipeline.py' exists# # and defines the following items correctly:# from working_yolo_pipeline import run_document_pipeline, DEFAULT_LAYOUTLMV3_MODEL_PATH, WEIGHTS_PATH# # Since I don't have this file, I am assuming the imports are correct.# # Define placeholders for assumed constants if the pipeline file isn't present# # You should replace these with your actual definitions if they are missing# try:# from working_yolo_pipeline import run_document_pipeline, DEFAULT_LAYOUTLMV3_MODEL_PATH, WEIGHTS_PATH# except ImportError:# print("Warning: 'working_yolo_pipeline.py' not found. Using dummy paths.")# def run_document_pipeline(*args):# return {"error": "Placeholder pipeline function called."}# DEFAULT_LAYOUTLMV3_MODEL_PATH = "./models/layoutlmv3_model"# WEIGHTS_PATH = "./weights/yolo_weights.pt"# def process_pdf(pdf_file, layoutlmv3_model_path=None):# """# Wrapper function for Gradio interface.# Args:# pdf_file: Gradio UploadButton file object# layoutlmv3_model_path: Optional custom model path# Returns:# Tuple of (JSON string, download file path)# """# if pdf_file is None:# return "β Error: No PDF file uploaded.", None# # Use default model path if not provided# if not layoutlmv3_model_path:# layoutlmv3_model_path = DEFAULT_LAYOUTLMV3_MODEL_PATH# # Verify model and weights exist# if not os.path.exists(layoutlmv3_model_path):# return f"β Error: LayoutLMv3 model not found at {layoutlmv3_model_path}", None# if not os.path.exists(WEIGHTS_PATH):# return f"β Error: YOLO weights not found at {WEIGHTS_PATH}", None# try:# # Get the uploaded PDF path# pdf_path = pdf_file.name# # Run the pipeline# result = run_document_pipeline(pdf_path, layoutlmv3_model_path, 'label_studio_import.json')# if result is None:# return "β Error: Pipeline failed to process the PDF. Check console for details.", None# # Create a temporary file for download# output_filename = f"{Path(pdf_path).stem}_analysis.json"# temp_output = tempfile.NamedTemporaryFile(mode='w', delete=False, suffix='.json', prefix='analysis_')# # Dump results to the temporary file# with open(temp_output.name, 'w', encoding='utf-8') as f:# json.dump(result, f, indent=2, ensure_ascii=False)# # Format JSON for display# json_display = json.dumps(result, indent=2, ensure_ascii=False)# return json_display, temp_output.name# except Exception as e:# return f"β Error during processing: {str(e)}", None# # Create Gradio interface# # FIX APPLIED: Removed 'theme=gr.themes.Soft()' which caused the TypeError# with gr.Blocks(title="Document Analysis Pipeline") as demo:# gr.Markdown("""# # π Document Analysis Pipeline# Upload a PDF document to extract structured data including questions, options, answers, passages, and embedded images.# **Pipeline Steps:**# 1. π YOLO/OCR Preprocessing (word extraction + figure/equation detection)# 2. π€ LayoutLMv3 Inference (BIO tagging)# 3. π Structured JSON Decoding# 4. πΌοΈ Base64 Image Embedding# """)# with gr.Row():# with gr.Column(scale=1):# pdf_input = gr.File(# label="Upload PDF Document",# file_types=[".pdf"],# type="filepath"# )# model_path_input = gr.Textbox(# label="LayoutLMv3 Model Path (optional)",# placeholder=DEFAULT_LAYOUTLMV3_MODEL_PATH,# value=DEFAULT_LAYOUTLMV3_MODEL_PATH,# interactive=True# )# process_btn = gr.Button("π Process Document", variant="primary", size="lg")# gr.Markdown("""# ### βΉοΈ Notes:# - Processing may take several minutes depending on PDF size# - Figures and equations will be extracted and embedded as Base64# - The output JSON includes structured questions, options, and answers# """)# with gr.Column(scale=2):# json_output = gr.Code(# label="Structured JSON Output",# language="json",# lines=25# )# download_output = gr.File(# label="Download Full JSON",# interactive=False# )# # Status/Examples section# with gr.Row():# gr.Markdown("""# ### π Output Format# The pipeline generates JSON with the following structure:# - **Questions**: Extracted question text# - **Options**: Multiple choice options (A, B, C, D, etc.)# - **Answers**: Correct answer(s)# - **Passages**: Associated reading passages# - **Images**: Base64-encoded figures and equations (embedded with keys like `figure1`, `equation2`)# """)# # Connect the button to the processing function# process_btn.click(# fn=process_pdf,# inputs=[pdf_input, model_path_input],# outputs=[json_output, download_output],# api_name="process_document"# )# # Example section (optional - add example PDFs if available)# # gr.Examples(# # examples=[# # ["examples/sample1.pdf"],# # ["examples/sample2.pdf"],# # ],# # inputs=pdf_input,# # )# # Launch the app# if __name__ == "__main__":# demo.launch(# server_name="0.0.0.0",# server_port=7860,# share=False,# show_error=True# )import gradio as grprint("GRADIO VERSION:", gr.__version__)import jsonimport osimport tempfilefrom pathlib import Path# ==============================# WRITE CUSTOM CSS FOR FONTS# ==============================# CUSTOM_CSS = """# @font-face {# font-family: 'NotoSansMath';# src: url('./NotoSansMath-Regular.ttf') format('truetype');# font-weight: normal;# font-style: normal;# }# html, body, * {# font-family: 'NotoSansMath', sans-serif !important;# }# """# # Optionally write the CSS file if needed (not required for inline css)# if not os.path.exists("custom.css"):# with open("custom.css", "w") as f:# f.write(CUSTOM_CSS)# ==============================try:from working_yolo_pipeline import run_document_pipeline, DEFAULT_LAYOUTLMV3_MODEL_PATH, WEIGHTS_PATHexcept ImportError:print("Warning: 'working_yolo_pipeline.py' not found. Using dummy paths.")def run_document_pipeline(*args):return {"error": "Placeholder pipeline function called."}DEFAULT_LAYOUTLMV3_MODEL_PATH = "./models/layoutlmv3_model"WEIGHTS_PATH = "./weights/yolo_weights.pt"def process_pdf(pdf_file, layoutlmv3_model_path=None):if pdf_file is None:return "β Error: No PDF file uploaded.", Noneif not layoutlmv3_model_path:layoutlmv3_model_path = DEFAULT_LAYOUTLMV3_MODEL_PATHif not os.path.exists(layoutlmv3_model_path):return f"β Error: LayoutLMv3 model not found at {layoutlmv3_model_path}", Noneif not os.path.exists(WEIGHTS_PATH):return f"β Error: YOLO weights not found at {WEIGHTS_PATH}", Nonetry:pdf_path = pdf_file.nameresult = run_document_pipeline(pdf_path, layoutlmv3_model_path, 'label_studio_import.json')if result is None:return "β Error: Pipeline failed to process the PDF. Check console for details.", Noneoutput_filename = f"{Path(pdf_path).stem}_analysis.json"temp_output = tempfile.NamedTemporaryFile(mode='w', delete=False, suffix='.json', prefix='analysis_')with open(temp_output.name, 'w', encoding='utf-8') as f:json.dump(result, f, indent=2, ensure_ascii=False)json_display = json.dumps(result, indent=2, ensure_ascii=False)return json_display, temp_output.nameexcept Exception as e:return f"β Error during processing: {str(e)}", Nonewith gr.Blocks(title="Document Analysis Pipeline") as demo:gr.HTML()gr.Markdown("""# π Document Analysis PipelineUpload a PDF document to extract structured data including questions, options, answers, passages, and embedded images.**Pipeline Steps:**1. π YOLO/OCR Preprocessing (word extraction + figure/equation detection)2. π€ LayoutLMv3 Inference (BIO tagging)3. π Structured JSON Decoding4. πΌοΈ Base64 Image Embedding""")with gr.Row():with gr.Column(scale=1):pdf_input = gr.File(label="Upload PDF Document",file_types=[".pdf"],type="filepath")model_path_input = gr.Textbox(label="LayoutLMv3 Model Path (optional)",placeholder=DEFAULT_LAYOUTLMV3_MODEL_PATH,value=DEFAULT_LAYOUTLMV3_MODEL_PATH,interactive=True)process_btn = gr.Button("π Process Document", variant="primary", size="lg")gr.Markdown("""### βΉοΈ Notes:- Processing may take several minutes depending on PDF size- Figures and equations will be extracted and embedded as Base64- The output JSON includes structured questions, options, and answers""")with gr.Column(scale=2):json_output = gr.Code(label="Structured JSON Output",language="json",lines=25)download_output = gr.File(label="Download Full JSON",interactive=False)with gr.Row():gr.Markdown("""### π Output FormatThe pipeline generates JSON with the following structure:- **Questions**: Extracted question text- **Options**: Multiple choice options- **Answers**: Correct answer(s)- **Passages**: Associated reading passages- **Images**: Base64-encoded figures and equations""")process_btn.click(fn=process_pdf,inputs=[pdf_input, model_path_input],outputs=[json_output, download_output],api_name="process_document")if __name__ == "__main__":demo.launch(server_name="0.0.0.0",server_port=7860,share=False,show_error=True)12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182import gradio as grimport cv2import numpy as npfrom paddleocr import PPStructurefrom huggingface_hub import snapshot_downloadimport os# --- STEP 1: Download the Model from Hugging Face ---# We download the 'main' branch which contains the Paddle inference weightsprint("Downloading PP-DocLayoutV3 from Hugging Face...")model_path = snapshot_download(repo_id="PaddlePaddle/PP-DocLayoutV3", allow_patterns=["*.pdiparams", "*.pdmodel", "*.yml", "*.json"])print(f"Model downloaded to: {model_path}")# --- STEP 2: Initialize the Layout Engine ---# We use PPStructure, which is PaddleOCR's layout analysis module.# We point it to the downloaded model folder.layout_engine = PPStructure(layout_model_dir=model_path,table=False, # Disable table structure recognition for speedocr=False, # Disable OCR for now (we just want to see layout)show_log=True,use_angle_cls=True, # Helps with orientationenable_mkldnn=False # CRITICAL: Fixes the CPU crash)def analyze_layout(input_image):if input_image is None:return None, "No image uploaded"image_np = np.array(input_image)# Run Inference# result is a list of dictionaries, one per detected regionresult = layout_engine(image_np)viz_image = image_np.copy()detections_text = []# --- STEP 3: Visualize Results ---for region in result:# Extract Box (4 points)box = region['layout_bbox']label = region['label']# Convert to numpy format for drawing# layout_bbox is usually [x1, y1, x2, y2]x1, y1, x2, y2 = int(box[0]), int(box[1]), int(box[2]), int(box[3])# Color coding based on typecolor = (0, 255, 0) # Green for Textif label == 'title': color = (0, 0, 255) # Red for Titleelif label == 'figure': color = (255, 0, 0) # Blue for Figureselif label == 'table': color = (255, 255, 0) # Cyan for Tables# Draw Rectanglecv2.rectangle(viz_image, (x1, y1), (x2, y2), color, 3)# Draw Labelcv2.putText(viz_image, label, (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.8, color, 2)detections_text.append(f"Found {label} at {box}")return viz_image, "\n".join(detections_text)# --- Gradio UI ---with gr.Blocks(title="PP-DocLayoutV3 Demo") as demo:gr.Markdown("## π PP-DocLayoutV3 Explorer")gr.Markdown("This model detects **layout regions** (Text, Tables, Titles) instead of reading characters. It is excellent for de-warping and segmenting messy documents.")with gr.Row():with gr.Column():input_img = gr.Image(type="pil", label="Input Document")submit_btn = gr.Button("Analyze Layout", variant="primary")with gr.Column():output_img = gr.Image(label="Layout Visualization")output_log = gr.Textbox(label="Detected Regions", lines=10)submit_btn.click(fn=analyze_layout, inputs=input_img, outputs=[output_img, output_log])if __name__ == "__main__":demo.launch(server_name="0.0.0.0", server_port=7860)
app.py+75-318