# Load model directly from transformers import AutoModelForImageTextToText, TrOCRProcessor import torch from PIL import Image processor = TrOCRProcessor.from_pretrained("microsoft/trocr-base-printed") model = AutoModelForImageTextToText.from_pretrained("ChronoStellar/TrOCR_IndonesianLPR") device = torch.device("cuda" if torch.cuda.is_available() else "cpu") model.to(device) import gradio as gr from PIL import Image import torch # Assuming model, processor, and device are already defined def OCR(pil_image, model=model, processor=processor, device=device): # Prepare image for the model pixel_values = processor(pil_image, return_tensors="pt").pixel_values # Move the input to the appropriate device (CPU/GPU) pixel_values = pixel_values.to(device) # Generate prediction model.eval() # Set the model to evaluation mode with torch.no_grad(): # Disable gradient calculation for inference generated_ids = model.generate(pixel_values) # Decode the predicted IDs to get the text predicted_text = processor.batch_decode(generated_ids, skip_special_tokens=True)[0] return predicted_text # Create Gradio interface interface = gr.Interface( fn=OCR, inputs=gr.Image(type="pil", label="Upload License Plate Image"), outputs=gr.Textbox(label="Predicted License Plate"), title="Automatic License Plate Recognition", description="Upload an image of a license plate, and the system will predict the text on it.", ) # Launch the Gradio app interface.launch()