# -*- coding: utf-8 -*- """OCR Web Application Prototype.ipynb Automatically generated by Colab. Original file is located at https://colab.research.google.com/drive/1vzsQ17-W1Vy6yJ60XUwFy0QRkOR_SIg7 """ from transformers import Qwen2VLForConditionalGeneration, AutoProcessor from qwen_vl_utils import process_vision_info import torch import gradio as gr from PIL import Image processor = AutoProcessor.from_pretrained("Qwen/Qwen2-VL-2B-Instruct") # Initialize the model with float16 precision and handle fallback to CPU # Simplified model loading function for CPU def load_model(): return Qwen2VLForConditionalGeneration.from_pretrained( "Qwen/Qwen2-VL-2B-Instruct", torch_dtype=torch.float32, # Use float32 for CPU ) # Load the model vlm = load_model() # OCR function to extract text from an image def ocr_image(image, query="Extract text from the image"): messages = [ { "role": "user", "content": [ { "type": "image", "image": image, }, {"type": "text", "text": query}, ], } ] # Prepare inputs for the model text = processor.apply_chat_template(messages, tokenize=False, add_generation_prompt=True) image_inputs, video_inputs = process_vision_info(messages) inputs = processor( text=[text], images=image_inputs, videos=video_inputs, padding=True, return_tensors="pt", ) inputs = inputs.to("cpu") # Generate the output text using the model generated_ids = vlm.generate(**inputs, max_new_tokens=512) generated_ids_trimmed = [ out_ids[len(in_ids):] for in_ids, out_ids in zip(inputs.input_ids, generated_ids) ] output_text = processor.batch_decode( generated_ids_trimmed, skip_special_tokens=True, clean_up_tokenization_spaces=False ) return output_text[0] # Gradio interface def process_image(image): return ocr_image(image) # Create Gradio interface for uploading an image interface = gr.Interface( fn=process_image, inputs=gr.Image(type="pil"), outputs="text", title="Hindi & English OCR", description="Upload an image containing text in Hindi or English to extract the text using OCR." ) # Launch Gradio interface in Colab interface.launch()