# import all required libraries after doing research import gradio as gr from PIL import Image from surya.ocr import run_ocr # dedicated GOT_OCR_2.0 for hindi languages from surya.model.detection.model import load_model as load_det_model, load_processor as load_det_processor from surya.model.recognition.model import load_model as load_rec_model from surya.model.recognition.processor import load_processor as load_rec_processor import re # recognized hindi encoded pattern from transformers import AutoModel, AutoTokenizer import torch import tempfile import os # device = "cuda" device = 'cuda' if torch.cuda.is_available() else 'cpu' print(device) # load_desirable_model got_model_name = 'tdnathmlenthusiast/tester' det_processor, det_model = load_det_processor(), load_det_model() det_model.to(device) rec_model, rec_processor = load_rec_model(), load_rec_processor() rec_model.to(device) # tokenized to extract individual character tokenizer = AutoTokenizer.from_pretrained( got_model_name, trust_remote_code=True, device_map=device, revision = 'main') got_model = AutoModel.from_pretrained( got_model_name, trust_remote_code=True, low_cpu_mem_usage=True, device_map=device, use_safetensors=True) got_model = got_model.eval().to(device) # function to extract hindi & english def extract_hindi(text): # Unicode range for Devanagari script hindi_pattern = re.compile(r'[\u0900-\u097F]+') hindi_words = hindi_pattern.findall(text) return ' '.join(hindi_words) def process_image(image): with tempfile.NamedTemporaryFile(delete=False, suffix=".png") as temp_file: image.save(temp_file.name) temp_file_path = temp_file.name image = Image.open(temp_file_path) image = image.convert("RGB") langs = ["hi"] surya_predictions = run_ocr( [image], [langs], det_model, det_processor, rec_model, rec_processor) surya_text_list = re.findall(r"text='(.*?)'", str(surya_predictions[0])) surya_text = '\n'.join(surya_text_list) surya_text = extract_hindi(surya_text) got_res = got_model.chat(tokenizer, temp_file_path, ocr_type='ocr') combined_text = f"