import streamlit as st import cv2 import numpy as np import os import textwrap from PIL import Image try: from local_ocr_engine import LocalOCREngine except Exception as e: # If it still fails, show error in streamlit st.error(f"Failed to load OCR Engine: {e}") st.exception(e) LocalOCREngine = None # Page Config st.set_page_config( page_title="PaddleOCR Local - Premium", page_icon="🔍", layout="wide", initial_sidebar_state="expanded" ) # Custom CSS for "Wow" factor st.markdown(""" """, unsafe_allow_html=True) # Sidebar with st.sidebar: st.image("https://github.com/PaddlePaddle/PaddleOCR/raw/release/2.6/doc/paddleocr_logo.png", use_container_width=True) st.title("Settings") use_gpu = st.toggle("Use GPU", value=False) lang = st.selectbox("Language", ["en", "ch", "fr", "german", "korean", "japan"], index=0) st.markdown("---") st.info("This tool runs locally on your machine. Text and tables are extracted using PaddleOCR PP-Structure.") # --- MAIN APPLICATION LOGIC --- if 'ocr_result' in st.session_state: # PHASE 1: FULL-SCREEN DESIGNER STUDIO data = st.session_state['ocr_result'] processed_output = data.get('processed_output', []) metadata = data.get('metadata', {}) img_base64 = data.get('img_base64', "") img_w = metadata.get('width', 800) img_h = metadata.get('height', 1000) # --- THE ULTIMATE PROFESSIONAL STUDIO (POLOTNO-GRADE) --- html_template = """

Designer Inspector

✅ Studio Engine: Konva 9.3

✅ Interactivity: Enabled

⌨️ Dbl-Click to Edit Text

🖱️ Drag to Reposition

📏 Select to Transform

""" import json studio_html = html_template.replace("REPLACEMENT_OCR_DATA", json.dumps(processed_output)) \ .replace("REPLACEMENT_IMG_BASE64", img_base64) \ .replace("REPLACEMENT_W", str(img_w)) \ .replace("REPLACEMENT_H", str(img_h)) st.markdown(studio_html, unsafe_allow_html=True) else: # PHASE 2: UPLOAD & ANALYSIS UI st.title("📄 Intelligent Document Extraction") st.markdown("Upload a document, receipt, or invoice for **high-fidelity** interactive extraction.") uploaded_file = st.file_uploader("Choose an image...", type=['png', 'jpg', 'jpeg', 'bmp', 'tiff']) if uploaded_file is not None: file_bytes = np.asarray(bytearray(uploaded_file.read()), dtype=np.uint8) image = cv2.imdecode(file_bytes, 1) st.markdown('

📄 Document Ready

', unsafe_allow_html=True) col_view = st.columns([1, 2, 1]) with col_view[1]: st.image(image, channels="BGR", use_container_width=True) if st.button("🚀 Run AI Carbon Copy Analysis", type="primary", use_container_width=True): with st.spinner("Analyzing document structure..."): try: engine = LocalOCREngine(use_gpu=use_gpu, lang=lang) result_data = engine.process_image(image, save_folder="output_results") # Fix: Store image base64 correctly for robust session recovery import base64 _, buffer = cv2.imencode('.jpg', image, [int(cv2.IMWRITE_JPEG_QUALITY), 70]) img_base64 = base64.b64encode(buffer).decode() result_data['img_base64'] = img_base64 st.session_state['ocr_result'] = result_data st.rerun() except Exception as e: st.error(f"Analysis Error: {e}") else: # Initial Welcome Screen st.markdown("""

👋 Welcome to Vizan Designer Studio

Please upload an image to begin your project.

""", unsafe_allow_html=True)