File size: 24,614 Bytes
7b2d644
 
 
 
 
 
c71160b
7b2d644
 
fc3603e
7b2d644
 
 
8aeaf55
c71160b
9e9dd03
1464198
d8ac28c
dc0e308
7b2d644
 
 
 
 
 
 
 
 
 
 
 
565358e
 
 
 
7b2d644
 
 
 
 
 
 
 
 
 
 
 
 
 
565358e
 
 
7b2d644
565358e
 
 
 
 
7b2d644
565358e
 
7b2d644
565358e
 
 
 
 
 
7b2d644
 
 
 
 
 
 
 
 
 
 
 
 
565358e
 
 
7b2d644
 
 
 
 
c71160b
 
 
1464198
 
 
565358e
 
 
 
 
 
 
 
 
 
9b68d5b
 
 
 
 
 
 
8540c54
 
9b68d5b
 
 
9e9dd03
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9b68d5b
ddab809
7b2d644
9b68d5b
 
1464198
 
 
 
 
 
 
 
 
c71160b
565358e
4fa66cd
 
 
 
 
 
 
 
ea9f6d5
 
 
9b68d5b
888452e
ea9f6d5
 
34f7b22
 
 
 
 
 
 
 
 
 
 
 
 
87e0a53
 
 
 
 
 
 
ea9f6d5
87e0a53
565358e
34f7b22
 
 
 
 
 
 
 
 
ea9f6d5
565358e
db6a123
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9b68d5b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
565358e
 
1464198
 
 
565358e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7b2d644
1464198
c71160b
1464198
c71160b
1464198
 
 
 
 
 
 
565358e
7b2d644
1464198
 
7b2d644
1464198
565358e
 
 
 
 
 
 
 
 
 
 
7281a3e
565358e
 
7b2d644
565358e
 
 
 
 
7b2d644
565358e
 
 
 
2fe6bbf
565358e
7b2d644
565358e
 
 
7b2d644
 
565358e
7b2d644
565358e
7b2d644
1464198
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
565358e
 
 
 
 
 
 
 
7b2d644
 
 
 
565358e
 
 
 
 
 
d8ac28c
565358e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7b2d644
d8ac28c
 
 
 
 
 
 
 
 
 
 
565358e
 
 
 
 
 
 
 
 
7b2d644
 
 
565358e
 
 
 
 
 
 
 
 
c71160b
565358e
 
 
c71160b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
565358e
c71160b
 
 
 
 
 
 
 
 
 
 
 
565358e
 
 
c71160b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
565358e
 
 
7b2d644
565358e
7b2d644
 
c71160b
565358e
 
c71160b
565358e
 
c71160b
db6a123
 
 
 
 
 
 
d8ac28c
db6a123
 
 
d8ac28c
db6a123
1464198
d8ac28c
 
c71160b
 
1464198
 
 
 
ddab809
 
1464198
 
 
ddab809
 
1464198
 
 
 
 
 
ddab809
 
1464198
7b2d644
 
1464198
c71160b
565358e
 
7b2d644
 
 
 
565358e
7b2d644
1464198
 
 
 
 
 
 
 
 
 
 
 
ddab809
 
1464198
 
 
ddab809
 
1464198
 
 
 
 
 
 
ddab809
1464198
 
 
 
ddab809
1464198
 
7b2d644
 
628cefb
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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
from fastapi import FastAPI, File, UploadFile, HTTPException
from fastapi.middleware.cors import CORSMiddleware
import torch
import torch.nn as nn
import torchvision.models as models
import torchvision.transforms as transforms
from PIL import Image
import json
import io
import os
import cv2
import numpy as np
import base64
import math
import contextlib
import requests
import unicodedata
import time
from huggingface_hub import login

app = FastAPI()

# Allow CORS for React development
app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

# ============================================================
# Model Architecture β€” SYNCED with Training8.ipynb
# ResNet50 + Transformer Decoder (d_model=384, nhead=6)
# ============================================================

class PositionalEncoding1D(nn.Module):
    def __init__(self, d_model, max_len=512):
        super().__init__()
        pe = torch.zeros(max_len, d_model)
        position = torch.arange(0, max_len, dtype=torch.float32).unsqueeze(1)
        div_term = torch.exp(torch.arange(0, d_model, 2).float() * (-math.log(10000.0) / d_model))
        pe[:, 0::2] = torch.sin(position * div_term)
        pe[:, 1::2] = torch.cos(position * div_term)
        self.register_buffer('pe', pe.unsqueeze(0))
        
    def forward(self, x):
        return x + self.pe[:, :x.size(1)]

class OCRTransformerModel(nn.Module):
    def __init__(self, vocab_size, d_model=384, nhead=6,
                 num_decoder_layers=4, dim_feedforward=1024, dropout=0.2):
        super().__init__()
        # ResNet50 encoder (matches Training8.ipynb exactly)
        backbone = models.resnet50(weights=None)
        self.encoder = nn.Sequential(*list(backbone.children())[:-2])
        # ResNet50 outputs 2048 channels β†’ project to d_model=384
        self.enc_proj = nn.Conv2d(2048, d_model, kernel_size=1)
        
        self.token_embed = nn.Embedding(vocab_size, d_model)
        self.pos_decoder = PositionalEncoding1D(d_model)
        
        decoder_layer = nn.TransformerDecoderLayer(
            d_model=d_model, nhead=nhead,
            dim_feedforward=dim_feedforward,
            dropout=dropout, batch_first=True)
        self.decoder = nn.TransformerDecoder(decoder_layer, num_layers=num_decoder_layers)
        self.output_layer = nn.Linear(d_model, vocab_size)

    def forward(self, images, tgt):
        feat = self.encoder(images)
        feat = self.enc_proj(feat)
        memory = feat.flatten(2).permute(0, 2, 1)
        
        tgt = self.token_embed(tgt)
        tgt = self.pos_decoder(tgt)
        
        mask = torch.triu(torch.ones(tgt.size(1), tgt.size(1), device=tgt.device), 1).bool()
        out = self.decoder(tgt, memory, tgt_mask=mask)
        return self.output_layer(out)

# ============================================================
# Global Resources
# ============================================================
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model = None
stoi = None
itos = None

# English Engine Resources (TrOCR)
model_eng = None
processor_eng = None
hf_token = None
lexicon = []
lexicon_set = set()

# ============================================================
# ImageNet Normalization β€” MUST match Training8.ipynb
# ============================================================
IMAGENET_MEAN = [0.485, 0.456, 0.406]
IMAGENET_STD  = [0.229, 0.224, 0.225]

# ============================================================
# Resource Loading
# ============================================================
def load_hf_token():
    """Load Hugging Face token from hf_token.txt or environment for faster authorized downloads."""
    token = os.getenv("HF_TOKEN")
    if not token and os.path.exists("hf_token.txt"):
        with open("hf_token.txt", "r") as f:
            token = f.read().strip()
    if token:
        # Set token directly as env var β€” avoids calling /whoami-v2 which hits rate limits on Docker restarts
        os.environ["HF_TOKEN"] = token
        print("[OK] Hugging Face Token Loaded! Authorized for faster downloads.")
    return token

async def query_inference_api(image_bytes, token):
    """
    Call the Hugging Face Inference API (Serverless) for English OCR.
    This offloads the 2.2GB model from local RAM/CPU to HF GPUs.
    """
    API_URL = "https://api-inference.huggingface.co/models/microsoft/trocr-large-handwritten"
    headers = {"Authorization": f"Bearer {token}"}
    
    # Retry logic for model loading (warm-up)
    for attempt in range(3):
        response = requests.post(API_URL, headers=headers, data=image_bytes)
        if response.status_code == 200:
            return response.json()[0].get("generated_text", "").strip()
        elif response.status_code == 503:
            print(f"[RETRY] Model is loading on HF Side: {response.json()}")
            import asyncio
            await asyncio.sleep(5)
        else:
            print(f"[FAIL] Inference API Error ({response.status_code}): {response.text}")
            break
    return "Error: Inference API failed"

async def load_resources():
    global model, stoi, itos, hf_token
    
    hf_token = load_hf_token()
    
    # --- Load Lexicon ---
    for lex_path in ["lexicon.txt", "Hindi-DS/lexicon.txt"]:
        if os.path.exists(lex_path):
            with open(lex_path, "r", encoding="utf-8") as f:
                lexicon = [unicodedata.normalize("NFC", l.strip()) for l in f if l.strip()]
            lexicon_set = set(lexicon)
            print(f"[OK] Lexicon Loaded ({len(lexicon)} words)")
            break
    
    # --- Load Hindi Engine ---
    checkpoint_path = "best_model_finetuned.pt"
    vocab_file = "vocab.json"
    stoi, itos = {}, {}

    if os.path.exists(vocab_file):
        with open(vocab_file, 'r', encoding='utf-8') as f:
            vlist = json.load(f)
            stoi = {val: i for i, val in enumerate(vlist)}
            itos = {i: val for i, val in enumerate(vlist)}
    
    if not os.path.exists(checkpoint_path):
        from huggingface_hub import hf_hub_download
        checkpoint_path = hf_hub_download(repo_id="Angstormy/parsify-ocr-weights", filename="best_model_finetuned.pt", token=hf_token)
        
    checkpoint = torch.load(checkpoint_path, map_location=device)
    
    # Smart Checkpoint Parsing (Supports various training formats)
    if isinstance(checkpoint, dict):
        state_dict = checkpoint.get('state_dict', checkpoint.get('model_state_dict', checkpoint))
        # Support d_model from checkpoint
        d_model = checkpoint.get('d_model', 384)
        # Attempt to load vocabulary from checkpoint if it exists
        if 'stoi' in checkpoint and not stoi:
            stoi = checkpoint['stoi']
            itos = {int(k): v for k, v in checkpoint.get('itos', {}).items()}
    else:
        state_dict = checkpoint
        d_model = 384

    # Dynamic Vocab Sizing (Ensures model matches checkpoint exactly)
    if 'output_layer.bias' in state_dict:
        vocab_size = state_dict['output_layer.bias'].size(0)
    elif 'token_embed.weight' in state_dict:
        vocab_size = state_dict['token_embed.weight'].size(0)
    else:
        vocab_size = len(stoi)

    print(f"[INFO] Initializing model with vocab_size={vocab_size} (Mapping has {len(stoi)} tokens)")
    model = OCRTransformerModel(vocab_size, d_model=d_model).to(device)
    
    # Load the weights
    try:
        model.load_state_dict(state_dict, strict=True)
    except RuntimeError:
        # If strict fails, try non-strict (handles minor version diffs)
        print("[WARN] Strict loading failed, attempting non-strict...")
        model.load_state_dict(state_dict, strict=False)

    model.eval()
    print(f"[OK] Hindi Engine loaded β€” ResNet50 + d_model={d_model} ({vocab_size} classes)")
    
    # --- Load English Engine (Local) ---
    print("\n" + "=" * 60)
    print("🧠 ALLOCATING MEMORY FOR LOCAL ENGLISH MODEL")
    print("=" * 60)
    from transformers import VisionEncoderDecoderModel, TrOCRProcessor
    
    eng_model_path = "trocr-large-english"
    if os.path.exists(eng_model_path):
        global processor_eng, model_eng
        
        start_load = time.time()
        
        print("⏳ [1/2] Fetching tiny processor config from Hugging Face...")
        processor_eng = TrOCRProcessor.from_pretrained("microsoft/trocr-large-handwritten")
        
        print(f"⏳ [2/2] Loading 2.5 GB model weights from '{eng_model_path}' into {str(device).upper()} RAM...")
        print("   -> Please wait. This blocks the server and usually takes 10-30 seconds...")
        model_eng = VisionEncoderDecoderModel.from_pretrained(eng_model_path).to(device)
        model_eng.eval()
        
        elapsed = time.time() - start_load
        print(f"βœ… SUCCESS! English Engine fully loaded into RAM in {elapsed:.1f} seconds!")
        print("=" * 60 + "\n")
    else:
        print(f"[WARN] English model folder '{eng_model_path}' not found! The /predict endpoint will fail for English.")
        print("=" * 60 + "\n")

@contextlib.asynccontextmanager
async def lifespan(app: FastAPI):
    await load_resources()
    yield

app = FastAPI(lifespan=lifespan)

# Allow CORS for React development
app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

# ============================================================
# Preprocessing & Robustness helpers
# ============================================================

# ============================================================

def ink_crop(img_gray, margin=2):
    """Crop to ink bounding box using Otsu (for bounding box only)."""
    _, binary = cv2.threshold(img_gray, 0, 255,
                               cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)
    coords = cv2.findNonZero(binary)
    if coords is not None:
        x, y, w, h = cv2.boundingRect(coords)
        x1 = max(0, x - margin)
        y1 = max(0, y - margin)
        x2 = min(img_gray.shape[1], x + w + margin)
        y2 = min(img_gray.shape[0], y + h + margin)
        return y1, y2, x1, x2
    return 0, img_gray.shape[0], 0, img_gray.shape[1]

def preprocess_image(image_input):
    """
    Preprocessing pipeline β€” Handles both bytes and pre-decoded BGR images.
    """
    if isinstance(image_input, bytes):
        nparr = np.frombuffer(image_input, np.uint8)
        img_bgr = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
    else:
        img_bgr = image_input
        
    if img_bgr is None: return None, None
    img_gray = cv2.cvtColor(img_bgr, cv2.COLOR_BGR2GRAY)
    
    # Use the crop directly (do not apply another destructive ink_crop)
    img_gray_cropped = img_gray
    
    # 2. CLAHE (clipLimit=2.0, tileGridSize=4Γ—4) BEFORE PADDING
    clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(4, 4))
    img_enhanced = clahe.apply(img_gray_cropped)
    
    # 3. Aspect-ratio resize (height=64, max_width=400)
    IMG_HEIGHT = 64
    MAX_WIDTH = 400
    h, w = img_enhanced.shape
    new_w = min(int(w * (IMG_HEIGHT / h)), MAX_WIDTH)
    pil_img = Image.fromarray(img_enhanced).convert("L")
    resample_method = Image.Resampling.LANCZOS if hasattr(Image, "Resampling") else Image.LANCZOS
    pil_img = pil_img.resize((new_w, IMG_HEIGHT), resample_method)
    
    # 4. Grayscale β†’ 3-channel
    pil_img_rgb = Image.merge('RGB', [pil_img, pil_img, pil_img])
    
    # 5. ToTensor + ImageNet Normalize
    img_tensor = transforms.Compose([
        transforms.ToTensor(),
        transforms.Normalize(mean=IMAGENET_MEAN, std=IMAGENET_STD),
    ])(pil_img_rgb)
    
    # 6. Black pad to MAX_WIDTH (value=0.0 β€” matches Training8)
    if img_tensor.shape[2] < MAX_WIDTH:
        img_tensor = torch.nn.functional.pad(
            img_tensor, (0, MAX_WIDTH - img_tensor.shape[2], 0, 0), value=0.0)
        
    img_tensor = img_tensor.unsqueeze(0).to(device)
    
    # Debug View (Denormalized)
    debug_arr = (pil_img_rgb).convert("RGB")
    debug_arr = np.array(debug_arr)
    debug_arr = cv2.cvtColor(debug_arr, cv2.COLOR_RGB2BGR)
    _, buffer = cv2.imencode('.png', debug_arr)
    debug_raw_b64 = base64.b64encode(buffer).decode()
    
    return img_tensor, debug_raw_b64

# ============================================================
# Inference Logic & Post-Processing
# ============================================================

def levenshtein(a, b):
    """Standard Levenshtein distance for word correction."""
    m, n = len(a), len(b)
    dp = [[0] * (n + 1) for _ in range(m + 1)]
    for i in range(m + 1): dp[i][0] = i
    for j in range(n + 1): dp[0][j] = j
    for i in range(1, m + 1):
        for j in range(1, n + 1):
            cost = 0 if a[i-1] == b[j-1] else 1
            dp[i][j] = min(dp[i-1][j]+1, dp[i][j-1]+1, dp[i-1][j-1]+cost)
    return dp[m][n]

def lexicon_correct(pred, max_edit_dist=1):
    """Corrects OCR predictions using the loaded lexicon."""
    
    # We have removed manual grammatical rules to ensure 100% transparency 
    # of the raw tensor predictions for pure ML evaluation.
    
    if not lexicon_set: return pred
    pred = unicodedata.normalize("NFC", pred.strip())
    if not pred or pred in lexicon_set: return pred
    best_word, best_dist = pred, max_edit_dist + 1
    for word in lexicon:
        if abs(len(word) - len(pred)) > max_edit_dist: continue
        dist = levenshtein(pred, word)
        if dist < best_dist:
            best_dist, best_word = dist, word
    return best_word if best_dist <= max_edit_dist else pred

# ============================================================
# Beam Search Decoder
# ============================================================
def beam_search_decode(model, images, k=3, max_len=25):
    """
    Ultra-Accuracy Beam Search Decoder.
    Optimized for high-precision diagnostic output and memory efficiency.
    """
    B = images.size(0)
    BOS_VAL = stoi.get("<bos>", 1)
    EOS_VAL = stoi.get("<eos>", 2)
    PAD_VAL = stoi.get("<pad>", 0)

    # Initial beam: (sequence_tensor, score, diagnostic_history)
    # History is tracked per beam to ensure the matrix stays in sync with the winning path
    beams = [(torch.full((1, 1), BOS_VAL, dtype=torch.long, device=device), 0.0, [])]

    for step_idx in range(max_len):
        step_start_time = time.time()
        candidates = []
        for seq, score, history in beams:
            # Skip beams that reached EOS
            if seq[0, -1].item() == EOS_VAL:
                candidates.append((seq, score, history))
                continue
                
            # Single forward pass for the current beam
            with (torch.amp.autocast('cuda') if device.type == 'cuda' else contextlib.nullcontext()):
                logits = model(images, seq)
            
            # Extract log-probabilities for the next token
            log_probs = torch.log_softmax(logits[:, -1, :], dim=-1)
            top_lp, top_i = log_probs[0].topk(k)
            
            # --- Vector Diagnostic Generation ---
            # We calculate this once per active beam to avoid redundant loops
            current_diagnostics = [
                {
                    "char": itos.get(idx.item(), '<?>'),
                    "confidence": round(torch.exp(lp).item(), 4)
                } 
                for lp, idx in zip(top_lp, top_i)
            ]

            # Branch into k candidates
            for lp, idx in zip(top_lp, top_i):
                new_seq = torch.cat([seq, idx.unsqueeze(0).unsqueeze(0)], dim=1)
                new_score = score + lp.item()
                
                # Append diagnostic data only to the relevant path
                new_history = history + [{"step": step_idx + 1, "top_candidates": current_diagnostics}]
                candidates.append((new_seq, new_score, new_history))
        
        # Sort by cumulative score and prune to keep top K beams
        beams = sorted(candidates, key=lambda x: x[1], reverse=True)[:k]
        
        # Calculate step duration in seconds
        step_duration_sec = time.time() - step_start_time
        
        # Update history with duration for each beam
        new_beams = []
        for seq, score, history in beams:
            if history:
                history[-1]["duration_sec"] = round(step_duration_sec, 4)
            new_beams.append((seq, score, history))
        beams = new_beams

        # Stop if all surviving beams have reached EOS
        if all(b[0][0, -1].item() == EOS_VAL for b in beams):
            break

    # Pick the absolute best path
    best_seq, _, best_history = beams[0]
    
    ids = best_seq[0].tolist()
    out_chars = []
    for i in ids:
        if i == EOS_VAL: break
        if i in [PAD_VAL, BOS_VAL]: continue
        out_chars.append(itos.get(i, ""))
    
    prediction = "".join(out_chars)
    return prediction, best_history

def greedy_decode(model, images, max_len=25):
    """Legacy Greedy Decode (Backup/English logic)."""
    return beam_search_decode(model, images, k=1, max_len=max_len)


# ============================================================
# English Preprocessing
# ============================================================
def preprocess_english(image_bytes):
    """
    Absolute Raw Vision: Crop and Pass.
    No padding, no aspect-ratio manipulation. Let TrOCR processor handle it.
    """
    nparr = np.frombuffer(image_bytes, np.uint8)
    img_bgr = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
    img_gray = cv2.cvtColor(img_bgr, cv2.COLOR_BGR2GRAY)
    
    # 1. Natural Laser Crop (Removes excess background)
    _, thresh = cv2.threshold(img_gray, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)
    coords = cv2.findNonZero(thresh)
    if coords is not None:
        x, y, w, h = cv2.boundingRect(coords)
        pad = 20
        y1, y2 = max(0, y-pad), min(img_bgr.shape[0], y+h+pad)
        x1, x2 = max(0, x-pad), min(img_bgr.shape[1], x+w+pad)
        cropped = img_bgr[y1:y2, x1:x2]
    else:
        cropped = img_bgr

    # 2. Raw PIL Conversion
    pil_img = Image.fromarray(cv2.cvtColor(cropped, cv2.COLOR_BGR2RGB))
    
    pixel_values = processor_eng(images=pil_img, return_tensors="pt").pixel_values.to(device)
    
    # Debug View
    debug_arr = pixel_values.squeeze(0).cpu().numpy().transpose(1, 2, 0)
    debug_arr = (debug_arr * 0.5 + 0.5) * 255
    debug_arr = debug_arr.clip(0, 255).astype(np.uint8)
    debug_arr = cv2.cvtColor(debug_arr, cv2.COLOR_RGB2BGR)
    _, buffer = cv2.imencode('.png', debug_arr)
    return pixel_values, base64.b64encode(buffer).decode('utf-8')

# ============================================================
# Script Detection
# ============================================================
def detect_script(image_bytes):
    """
    Intelligent Script Identification v4: Peak Prominence + Sentence Awareness.
    """
    nparr = np.frombuffer(image_bytes, np.uint8)
    img_gray = cv2.imdecode(nparr, cv2.IMREAD_GRAYSCALE)
    if img_gray is None: return "hindi"
    
    _, thresh = cv2.threshold(img_gray, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)
    coords = cv2.findNonZero(thresh)
    
    if coords is not None:
        x, y, w, h = cv2.boundingRect(coords)
        cropped_thresh = thresh[y:y+h, x:x+w]
        
        kernel = np.ones((1, 10), np.uint8) 
        dilated = cv2.dilate(cropped_thresh, kernel, iterations=1)
        
        h_proj = np.sum(dilated, axis=1)
        search_range = int(h * 0.45) 
        
        if search_range > 0:
            top_h_proj = h_proj[:search_range]
            max_density = np.max(top_h_proj)
            avg_density = np.mean(h_proj)
            
            density_ratio = max_density / (w * 255) if w > 0 else 0
            prominence = max_density / (avg_density + 1e-6)
            
            top_half_ink = np.sum(cropped_thresh[:h//2])
            bottom_half_ink = np.sum(cropped_thresh[h//2:])
            weight_ratio = top_half_ink / (bottom_half_ink + 1e-6)
            
            print(f"DEBUG [Detect v4]: Ratio={density_ratio:.2f}, Prominence={prominence:.2f}, Weight={weight_ratio:.2f}")
            
            if (density_ratio > 0.35 and prominence > 1.8) or weight_ratio > 1.5:
                print("DEBUG [Detect]: HINDI Identified.")
                return "hindi"
    
    print("DEBUG [Detect]: ENGLISH Identified.")
    return "english"

# ============================================================
# Prediction Endpoint
# ============================================================
@app.post("/predict")
async def predict_ocr(file: UploadFile = File(...), lang: str = "hindi"):
    try:
        image_bytes = await file.read()
        
        # --- Language Selection ---
        final_lang = lang
            
        inference_steps = []
        pretrained_prediction = ""
        if final_lang == "english":
            if model_eng is None or processor_eng is None:
                return {"error": "English local model not loaded. Run download_model.py first."}
            
            # Preprocess and prepare for TrOCR
            pixel_values, debug_b64 = preprocess_english(image_bytes)
            
            # Local Inference
            start_eng = time.time()
            with torch.no_grad():
                generated_ids = model_eng.generate(pixel_values)
                prediction = processor_eng.batch_decode(generated_ids, skip_special_tokens=True)[0]
            eng_duration_sec = time.time() - start_eng
                
            final_prediction = prediction
            inference_steps = [{"word": prediction, "steps": [{"step": "Total", "top_candidates": [{"char": "Full Sequence", "confidence": 1.0}], "duration_sec": round(eng_duration_sec, 3)}]}]
            print(f"ROUTING TO '{final_lang}': Local Inference -> FINAL: '{final_prediction}' ({eng_duration_sec:.3f}s)")
        else:
            if model is None: return {"error": "Hindi model not loaded"}
            
            img_bgr = cv2.imdecode(np.frombuffer(image_bytes, np.uint8), cv2.IMREAD_COLOR)
            if img_bgr is None: return {"error": "Could not decode image"}
            
            # Process entire image as a single sequence
            images, debug_b64 = preprocess_image(img_bgr)
            
            results = []
            all_steps = []
            if images is not None:
                pred, steps = beam_search_decode(model, images, k=3)
                if pred:
                    results.append(pred)
                    all_steps.append({"word": pred, "steps": steps})
            
            final_prediction = " ".join(results)
            inference_steps = all_steps
            
            print(f"ROUTING TO '{final_lang}': Full-Sequence -> FINAL: '{final_prediction}'")

        return {
            "prediction": final_prediction,
            "raw_model_prediction": final_prediction,
            "engine_view": f"data:image/png;base64,{debug_b64}",
            "detected_lang": final_lang,
            "inference_steps": inference_steps
        }
    except Exception as e:
        import traceback
        traceback.print_exc()
        raise HTTPException(status_code=500, detail=str(e))


@app.post("/predict_sentence")
async def predict_sentence(file: UploadFile = File(...), 
                           text_threshold: float = 0.7, 
                           link_threshold: float = 0.4, 
                           low_text: float = 0.4):
    try:
        if model is None: return {"error": "Hindi model not loaded"}
        image_bytes = await file.read()
        img_bgr = cv2.imdecode(np.frombuffer(image_bytes, np.uint8), cv2.IMREAD_COLOR)
        if img_bgr is None: return {"error": "Could not decode image"}
        
        # Process entire image as a single sequence
        images, debug_b64 = preprocess_image(img_bgr)
        
        results = []
        all_steps = []
        if images is not None:
            pred, steps = beam_search_decode(model, images, k=3)
            if pred:
                results.append(pred)
                all_steps.append({"word": pred, "steps": steps})
        
        return {
            "prediction": " ".join(results),
            "words": results,
            "engine_view": f"data:image/png;base64,{debug_b64}",
            "inference_steps": all_steps,
        }
    except Exception as e:
        import traceback
        traceback.print_exc()
        raise HTTPException(status_code=500, detail=str(e))

if __name__ == "__main__":
    import uvicorn
    uvicorn.run(app, host="0.0.0.0", port=7860)