Update app.py
Browse files10 unmodified lines111213141516171819202122232425264 unmodified lines31323334353637383940414243444546474849505152535455565758596061626364656667685 unmodified lines74757677787910 unmodified linesprint(f"Model downloaded to: {model_path}")# --- STEP 2: Initialize V3 Engine ---layout_engine = PPStructureV3(layout_model_dir=model_path,table=False,ocr=False,# show_log=True, <-- REMOVED (Caused the crash)# In V3, 'use_angle_cls' is often renamed for documents:use_doc_orientation_classify=True,enable_mkldnn=False # Keeps the crash fix)def analyze_layout(input_image):4 unmodified lines# Run Inferencetry:# V3 usually returns a generator or listresult = layout_engine(image_np)except Exception as e:return image_np, f"Error running layout analysis: {e}"viz_image = image_np.copy()detections_text = []if result is None:return viz_image, "No layout detected."# Iterate through resultsfor region in result:# V3 Output format usually includes 'layout_bbox' or 'bbox'if isinstance(region, dict):# Try specific v3 keys first, fallback to genericbox = region.get('layout_bbox') or region.get('bbox')label = region.get('label', 'unknown')else:continueif box is None: continue# Draw the boxtry:x1, y1, x2, y2 = int(box[0]), int(box[1]), int(box[2]), int(box[3])# Color codingcolor = (0, 255, 0) # Default Greenif label == 'title': color = (0, 0, 255) # Redelif label == 'figure': color = (255, 0, 0) # Blueelif label == 'table': color = (255, 255, 0) # Cyancv2.rectangle(viz_image, (x1, y1), (x2, y2), color, 3)cv2.putText(viz_image, label, (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.8, color, 2)5 unmodified lineswith gr.Blocks(title="PP-DocLayoutV3 Explorer") as demo:gr.Markdown("## ๐ PP-DocLayoutV3 Explorer")with gr.Row():with gr.Column():10 unmodified lines11121314151617181920212223244 unmodified lines29303132333435363738394041424344454647484950515253545556575859606162636465665 unmodified lines7273747576777810 unmodified linesprint(f"Model downloaded to: {model_path}")# --- STEP 2: Initialize V3 Engine ---# FIXES:# 1. Used 'model_dir' instead of 'layout_model_dir'# 2. Removed 'table=False' and 'ocr=False' (Invalid in V3)# 3. Kept 'enable_mkldnn=False' (Essential for CPU stability)layout_engine = PPStructureV3(model_dir=model_path,use_doc_orientation_classify=True,enable_mkldnn=False)def analyze_layout(input_image):4 unmodified lines# Run Inferencetry:# V3 inference often returns a generator or a list object# We convert to list to be saferesults = list(layout_engine(image_np))except Exception as e:return image_np, f"Error running layout analysis: {e}"viz_image = image_np.copy()detections_text = []if not results:return viz_image, "No layout detected."# --- STEP 3: Visualize V3 Results ---# The structure of V3 results is typically a list of dicts.# Each dict has 'layout_bbox' (or 'bbox') and 'label'.for region in results:if not isinstance(region, dict): continue# Try finding the box with supported keysbox = region.get('layout_bbox') or region.get('bbox')label = region.get('label', 'unknown')if box is None: continuetry:x1, y1, x2, y2 = int(box[0]), int(box[1]), int(box[2]), int(box[3])# Color coding for different regionscolor = (0, 255, 0) # Text (Green)if label == 'title': color = (0, 0, 255) # Title (Red)elif label == 'figure': color = (255, 0, 0) # Figure (Blue)elif label == 'table': color = (255, 255, 0)# Table (Cyan)cv2.rectangle(viz_image, (x1, y1), (x2, y2), color, 3)cv2.putText(viz_image, label, (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.8, color, 2)5 unmodified lineswith gr.Blocks(title="PP-DocLayoutV3 Explorer") as demo:gr.Markdown("## ๐ PP-DocLayoutV3 Explorer")gr.Markdown("Visualizes document structure (Title, Text, Table, Figure).")with gr.Row():with gr.Column():
app.py+26-27