File size: 11,815 Bytes
8e8164a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
65734bb
8e8164a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
65734bb
 
8e8164a
 
 
 
 
 
 
 
 
c89ce4c
8e8164a
c89ce4c
 
8e8164a
 
 
 
 
 
 
 
 
 
 
 
c89ce4c
 
 
8e8164a
 
 
 
 
 
 
 
65734bb
8e8164a
 
 
 
 
 
 
 
 
 
 
 
 
c89ce4c
8e8164a
c89ce4c
 
8e8164a
 
c89ce4c
 
8e8164a
65734bb
 
 
 
 
 
 
 
c89ce4c
 
8e8164a
 
65734bb
 
 
 
 
 
 
c89ce4c
8e8164a
 
 
 
 
65734bb
 
8e8164a
 
c89ce4c
 
 
65734bb
 
 
 
 
8e8164a
 
 
c89ce4c
8e8164a
 
 
 
c89ce4c
8e8164a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c89ce4c
 
8e8164a
 
 
 
 
 
 
c89ce4c
8e8164a
 
 
 
65734bb
8e8164a
 
65734bb
 
c89ce4c
 
 
 
8e8164a
 
 
 
c89ce4c
8e8164a
 
 
 
 
 
 
 
c89ce4c
 
8e8164a
 
c89ce4c
8e8164a
 
 
 
c89ce4c
8e8164a
 
 
 
 
65734bb
 
 
8e8164a
 
65734bb
 
c89ce4c
8e8164a
65734bb
8e8164a
65734bb
 
c89ce4c
8e8164a
 
 
65734bb
 
 
 
 
 
 
8e8164a
65734bb
8e8164a
65734bb
8e8164a
 
 
 
 
 
 
 
 
c89ce4c
8e8164a
 
 
 
 
 
 
 
 
 
 
c89ce4c
 
8e8164a
 
 
 
c89ce4c
8e8164a
c89ce4c
 
8e8164a
 
 
 
 
 
c89ce4c
8e8164a
 
 
65734bb
 
8e8164a
 
 
 
 
 
 
 
 
 
 
 
c89ce4c
8e8164a
 
 
c89ce4c
 
 
 
 
8e8164a
 
 
 
 
 
 
 
 
c89ce4c
8e8164a
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
import streamlit as st
from PIL import Image, UnidentifiedImageError
import pytesseract
import os
import io
import zipfile
from pymongo import MongoClient
from bson import ObjectId
import base64
import datetime
import traceback
from dotenv import load_dotenv

# Load environment variables
load_dotenv()

# ========== Configuration ==========
DATABASE_NAME = "bengali_ocr_db"
COLLECTION_NAME = "documents"
POPPLER_PATH = "/usr/bin"  # Updated for Hugging Face
TESSERACT_CMD = "/usr/bin/tesseract"  # Updated for Hugging Face
MONGO_URI = os.getenv("MONGO_URI", "mongodb://localhost:27017")
MAX_FILE_SIZE = 200 * 1024 * 1024  # 200MB Hugging Face limit

# Configure Tesseract
pytesseract.pytesseract.tesseract_cmd = TESSERACT_CMD

# ========== Error Handling ==========
def handle_error(e, message="An error occurred"):
    st.error(f"🚨 {message}: {str(e)}")
    st.text(traceback.format_exc())
    st.session_state.update({
        'processed_images': [],
        'current_page': 0,
        'extracted_texts': [],
        'corrected_texts': []
    })

# ========== Database Connection ==========
def get_mongo_client():
    try:
        client = MongoClient(
            MONGO_URI,
            serverSelectionTimeoutMS=5000,
            socketTimeoutMS=10000,
            connectTimeoutMS=10000
        )
        client.admin.command('ping')
        return client
    except Exception as e:
        handle_error(e, "Database connection failed")
        return None

# ========== File Processing ==========
def process_pdf_to_images(file_bytes):
    try:
        from pdf2image import convert_from_bytes
        return convert_from_bytes(
            file_bytes, 
            poppler_path=POPPLER_PATH,
            fmt='jpeg',
            dpi=300
        ) or []
    except Exception as e:
        handle_error(e, "PDF processing failed")
        return []

def process_zip_to_images(file_bytes):
    images = []
    try:
        with zipfile.ZipFile(io.BytesIO(file_bytes)) as zip_ref:
            for f in zip_ref.namelist():
                if f.lower().endswith(('.jpg', '.jpeg', '.png', '.bmp')):
                    with zip_ref.open(f) as file:
                        try:
                            img_bytes = file.read()
                            images.append(Image.open(io.BytesIO(img_bytes)).convert('RGB'))
                        except (UnidentifiedImageError, IOError):
                            continue
        return images
    except Exception as e:
        handle_error(e, "ZIP processing failed")
        return []

def perform_ocr(image, lang='ben'):
    try:
        # Preprocess image for better OCR results
        processed_img = image.convert('L').point(lambda x: 0 if x < 128 else 255)
        
        # Perform OCR with Bengali language
        return pytesseract.image_to_string(
            processed_img,
            lang=lang,
            config='--oem 3 --psm 6'
        ).strip()
    except Exception as e:
        handle_error(e, "OCR failed")
        return ""

# ========== Main UI ==========
def main_ui():
    st.title("πŸ“œ Bengali Document OCR System")
    st.markdown("""
    **Upload documents, extract Bengali text, and save corrections to the database**
    """)
    
    # Initialize session state
    st.session_state.setdefault('processed_images', [])
    st.session_state.setdefault('extracted_texts', [])
    st.session_state.setdefault('corrected_texts', [])
    st.session_state.setdefault('current_page', 0)
    st.session_state.setdefault('current_doc_id', None)
    st.session_state.setdefault('connection_valid', False)
    st.session_state.setdefault('upload_error', None)

    with st.sidebar:
        st.header("βš™οΈ Controls")
        
        # Connection test
        if st.button("πŸ”Œ Test Database Connection"):
            client = get_mongo_client()
            if client:
                st.success("Connection successful!")
                st.session_state.connection_valid = True
                client.close()
            else:
                st.session_state.connection_valid = False
                st.error("Connection failed")

        # Document upload section
        st.subheader("πŸ“€ Upload Document")
        uploaded_file = st.file_uploader(
            "Choose document", 
            type=["pdf", "zip", "png", "jpg", "jpeg"],
            help="Upload PDFs, ZIP files, or images containing Bengali text"
        )
        
        # File size validation
        if uploaded_file and uploaded_file.size > MAX_FILE_SIZE:
            st.error(f"File too large! Max size is {MAX_FILE_SIZE//(1024*1024)}MB")
            st.session_state.upload_error = "FILE_TOO_LARGE"
        elif uploaded_file:
            st.session_state.upload_error = None
            
        doc_name = st.text_input("Document Name:", placeholder="Enter document name")
        doc_author = st.text_input("Author (optional):", placeholder="Document author")

        # Upload button
        upload_disabled = not (
            st.session_state.connection_valid and 
            uploaded_file and 
            doc_name and
            (st.session_state.upload_error is None)
        )
        
        if st.button("πŸš€ Upload", disabled=upload_disabled, help="Save document to database"):
            handle_file_upload(uploaded_file, doc_name, doc_author)

# ========== File Handling ==========
def handle_file_upload(file, name, author):
    client = get_mongo_client()
    if not client: 
        return

    try:
        # Get file bytes directly from uploader
        file_bytes = file.getvalue()
        
        # Validate file size
        if len(file_bytes) > MAX_FILE_SIZE:
            st.error(f"File size exceeds {MAX_FILE_SIZE//(1024*1024)}MB limit")
            return
            
        document = {
            "metadata": {
                "name": name,
                "author": author or "Unknown",
                "upload_date": datetime.datetime.utcnow()
            },
            "file_data": {
                "file_name": file.name,
                "content": base64.b64encode(file_bytes).decode(),
                "type": file.type
            },
            "pages": []
        }

        db = client[DATABASE_NAME]
        result = db[COLLECTION_NAME].insert_one(document)
        
        if result.acknowledged:
            st.session_state.update({
                'current_doc_id': str(result.inserted_id),
                'processed_images': [],
                'extracted_texts': [],
                'corrected_texts': [],
                'current_page': 0
            })
            st.success("Document uploaded successfully!")
            # Process file content from bytes
            process_file_content(file_bytes, file.type)
    except Exception as e:
        handle_error(e, "Upload failed")
    finally:
        client.close()

def process_file_content(file_bytes, file_type):
    try:
        images = []
        if file_type == "application/pdf":
            images = process_pdf_to_images(file_bytes)
        elif file_type == "application/zip":
            images = process_zip_to_images(file_bytes)
        elif file_type.startswith("image/"):
            images = [Image.open(io.BytesIO(file_bytes))]
        else:
            st.error(f"Unsupported file type: {file_type}")
            return

        if not images:
            st.error("No valid images found in the document")
            return

        st.session_state.processed_images = images
        st.session_state.extracted_texts = [''] * len(images)
        st.session_state.corrected_texts = [''] * len(images)
        st.experimental_rerun()  # Refresh UI to show document
    except Exception as e:
        handle_error(e, "File processing failed")

# ========== Document Display ==========
def display_document():
    if not st.session_state.processed_images:
        return

    st.subheader(f"Editing: {st.session_state.get('doc_name', 'Document')}")
    
    col1, col2 = st.columns([1, 1])
    with col1:
        st.header("πŸ“„ Document Preview")
        display_navigation()
        display_image()
    
    with col2:
        st.header("✏️ Text Editor")
        process_ocr_page()
        display_editor()
        handle_save()

def display_navigation():
    current_page = st.session_state.current_page
    total_pages = len(st.session_state.processed_images)
    
    cols = st.columns([1, 3, 1])
    with cols[0]:
        if st.button("β—€ Previous", disabled=current_page == 0):
            st.session_state.current_page = max(0, current_page - 1)
            st.experimental_rerun()
    with cols[1]:
        st.markdown(f"**Page {current_page + 1} of {total_pages}**")
    with cols[2]:
        if st.button("Next β–Ά", disabled=current_page >= total_pages - 1):
            st.session_state.current_page = min(total_pages - 1, current_page + 1)
            st.experimental_rerun()

def display_image():
    try:
        current_page = st.session_state.current_page
        img = st.session_state.processed_images[current_page]
        
        # Resize for better display
        max_size = (800, 800)
        img.thumbnail(max_size, Image.LANCZOS)
        
        st.image(
            img,
            use_container_width=True,
            caption=f"Page {current_page + 1}"
        )
    except Exception as e:
        st.error(f"Error displaying image: {e}")

def display_editor():
    current_page = st.session_state.current_page
    text = st.session_state.corrected_texts[current_page] or st.session_state.extracted_texts[current_page]
    
    new_text = st.text_area(
        "Edit Extracted Text",
        value=text,
        height=400,
        key=f"editor_{current_page}",
        placeholder="OCR results will appear here..."
    )
    
    if new_text != text:
        st.session_state.corrected_texts[current_page] = new_text

# ========== OCR Processing ==========
def process_ocr_page():
    current_page = st.session_state.current_page
    if st.session_state.extracted_texts[current_page]:
        return
    
    with st.spinner("Performing OCR..."):
        try:
            image = st.session_state.processed_images[current_page]
            text = perform_ocr(image)
            st.session_state.extracted_texts[current_page] = text
            if not st.session_state.corrected_texts[current_page]:
                st.session_state.corrected_texts[current_page] = text
        except Exception as e:
            handle_error(e, "OCR processing failed")

# ========== Save Handling ==========
def handle_save():
    if not st.button("πŸ’Ύ Save Document", help="Save all pages to database"):
        return
    
    client = get_mongo_client()
    if not client: 
        return

    try:
        db = client[DATABASE_NAME]
        doc_id = ObjectId(st.session_state.current_doc_id)
        
        pages = [{
            "page_number": i+1,
            "extracted_text": st.session_state.extracted_texts[i],
            "corrected_text": st.session_state.corrected_texts[i],
            "last_updated": datetime.datetime.utcnow()
        } for i in range(len(st.session_state.extracted_texts))]

        result = db[COLLECTION_NAME].update_one(
            {"_id": doc_id},
            {"$set": {"pages": pages}}
        )
        
        if result.modified_count > 0:
            st.success("Document saved successfully!")
        else:
            st.warning("Document was not updated")
    except Exception as e:
        handle_error(e, "Save failed")
    finally:
        client.close()

# ========== Run Application ==========
if __name__ == "__main__":
    try:
        main_ui()
        if st.session_state.get('current_doc_id') and st.session_state.get('processed_images'):
            display_document()
    except Exception as e:
        handle_error(e, "Application error")