File size: 771 Bytes
79a9c3d
d089c9a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
79a9c3d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
from transformers import ViTFeatureExtractor, 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()