import gradio as gr from transformers import TrOCRProcessor, VisionEncoderDecoderModel from PIL import Image import requests # Load the TrOCR model and processor processor = TrOCRProcessor.from_pretrained('microsoft/trocr-large-printed') model = VisionEncoderDecoderModel.from_pretrained('microsoft/trocr-large-printed') def extract_text_from_image(image_url): image = Image.open(requests.get(image_url, stream=True).raw).convert("RGB") pixel_values = processor(images=image, return_tensors="pt").pixel_values generated_ids = model.generate(pixel_values) generated_text = processor.batch_decode(generated_ids, skip_special_tokens=True)[0] return generated_text # Create the Gradio interface gr.Interface( fn=extract_text_from_image, inputs=gr.Textbox(label="Enter image URL:"), outputs=gr.Textbox(label="Extracted Text"), title="Text Extraction from Images", description="Extract text from an image using optical character recognition (OCR).", ).launch()