Update app.py
Browse files4 unmodified lines5678910112 unmodified lines141516171819202122232425262728294 unmodified linesprint("Loading TrOCR model...")device = "cuda" if torch.cuda.is_available() else "cpu"processor = TrOCRProcessor.from_pretrained('microsoft/trocr-base-handwritten')model = VisionEncoderDecoderModel.from_pretrained('microsoft/trocr-base-handwritten').to(device)print("Model loaded successfully!")2 unmodified linesif image is None:return ""# Process the image and send it to exactly where the model is currently livingpixel_values = processor(image, return_tensors="pt").pixel_values.to(model.device)# Generate the text extractiongenerated_ids = model.generate(pixel_values)generated_text = processor.batch_decode(generated_ids, skip_special_tokens=True)[0]return generated_text# The final interfacedemo = gr.Interface(fn=predict,inputs=gr.Image(type="pil"),4 unmodified lines56789101112132 unmodified lines161718192021222324252627284 unmodified linesprint("Loading TrOCR model...")device = "cuda" if torch.cuda.is_available() else "cpu"# use_fast=False completely bypasses the transformers tokenizer conversion bug!processor = TrOCRProcessor.from_pretrained('microsoft/trocr-base-handwritten', use_fast=False)model = VisionEncoderDecoderModel.from_pretrained('microsoft/trocr-base-handwritten').to(device)print("Model loaded successfully!")2 unmodified linesif image is None:return ""pixel_values = processor(image, return_tensors="pt").pixel_values.to(model.device)generated_ids = model.generate(pixel_values)generated_text = processor.batch_decode(generated_ids, skip_special_tokens=True)[0]return generated_textdemo = gr.Interface(fn=predict,inputs=gr.Image(type="pil"),
app.py+3-4