File size: 788 Bytes
11bb2ca
 
 
 
 
 
 
 
 
cc2dff7
11bb2ca
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
from transformers import ViTFeatureExtractor, BertTokenizer, VisionEncoderDecoderModel, AutoTokenizer
import gradio as gr

model=VisionEncoderDecoderModel.from_pretrained("priyank-m/vit-bert-OCR")
tokenizer =  AutoTokenizer.from_pretrained("bert-base-multilingual-cased")
feature_extractor = ViTFeatureExtractor.from_pretrained("google/vit-large-patch32-384")

def run_ocr(image):
  pixel_values = feature_extractor(image, return_tensors="pt").pixel_values
  # autoregressively generate caption (uses greedy decoding by default )
  generated_ids = model.generate(pixel_values, max_new_tokens=50)
  generated_text = tokenizer.batch_decode(generated_ids, skip_special_tokens=True)[0]
  return generated_text
  
 
demo = gr.Interface(fn=run_ocr, inputs="image", outputs="text")
demo.launch()