import os import io import gradio as gr import torch import numpy as np import cv2 from PIL import Image from peft import PeftModel from transformers import TrOCRProcessor, VisionEncoderDecoderModel from cnn_model import CharacterClassifier from preprocessing import preprocess_for_ocr # --- ROBUST GLOBAL PATCH FOR GRADIO 4.x --- import gradio_client.utils def robust_get_type(schema): if isinstance(schema, bool): return "Any" if not isinstance(schema, dict): return "Any" if "const" in schema: return "Any" return original_get_type(schema) if hasattr(gradio_client.utils, "get_type"): original_get_type = gradio_client.utils.get_type gradio_client.utils.get_type = robust_get_type # ------------------------------------------ # --- CONFIGURATION --- BASE_MODEL_ID = "paudelanil/trocr-devanagari-2" ADAPTER_ID = "manishw10/devgen-trocr-devanagari-lora" CNN_MODEL_PATH = "devanagari-cnn-classifier.pt" device = "cuda" if torch.cuda.is_available() else "cpu" # --- ENGINE INITIALIZATION --- print("System: Initializing Full Combined Suite...") processor = TrOCRProcessor.from_pretrained(BASE_MODEL_ID) base_model = VisionEncoderDecoderModel.from_pretrained(BASE_MODEL_ID) base_model.config.decoder_start_token_id = processor.tokenizer.cls_token_id base_model.config.pad_token_id = processor.tokenizer.pad_token_id base_model.config.eos_token_id = processor.tokenizer.sep_token_id base_model.config.vocab_size = base_model.config.decoder.vocab_size peft_model = PeftModel.from_pretrained(base_model, ADAPTER_ID) try: model = peft_model.merge_and_unload() except Exception: model = peft_model model.to(device); model.eval() cnn_engine = CharacterClassifier(model_path=CNN_MODEL_PATH, device=device) # --- ORIGINAL ROUTING LOGIC --- def _flood_fill(binary, visited, start_y, start_x, h, w): stack = [(start_y, start_x)] size = 0 while stack: y, x = stack.pop() if y<0 or y>=h or x<0 or x>=w or visited[y,x] or not binary[y,x]: continue visited[y,x] = True; size += 1 stack.extend([(y+1,x),(y-1,x),(y,x+1),(y,x-1)]) return size def count_blobs(binary): h, w = binary.shape; visited = np.zeros_like(binary, dtype=bool); count = 0 for y in range(h): for x in range(w): if binary[y,x] and not visited[y,x]: size = _flood_fill(binary, visited, y, x, h, w) if size >= max(binary.size * 0.001, 10): count += 1 return count def original_classify_input(image): gray = image.convert("L"); arr = np.array(gray) threshold = min(arr.mean() * 0.75, 200) binary = (arr < threshold).astype(np.uint8) rows, cols = np.any(binary, axis=1), np.any(binary, axis=0) if not rows.any() or not cols.any(): return "character", 1.0, 1 y0, x0 = np.where(rows)[0][0], np.where(cols)[0][0] y1, x1 = np.where(rows)[0][-1], np.where(cols)[0][-1] w, h = x1-x0+1, y1-y0+1 ar, bc = w/h, count_blobs(binary) is_char = True if ar > 2.5: is_char = False elif ar > 1.8 and bc >= 3: is_char = False elif bc >= 4: is_char = False elif ar < 1.3 and bc <= 2: is_char = True elif bc == 1 and ar < 1.5: is_char = True elif ar < 1.75 and bc <= 2: is_char = True elif ar > 1.6: is_char = False return ("character" if is_char else "word"), ar, bc def get_confidence_html(confidence): color = "#10b981" if confidence > 0.9 else "#f59e0b" if confidence > 0.7 else "#ef4444" return f"""