Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,57 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import gradio as gr
|
3 |
+
|
4 |
+
|
5 |
+
from transformers import VisionEncoderDecoderModel
|
6 |
+
|
7 |
+
# initialize a vit-bert from a pretrained ViT and a pretrained BERT model. Note that the cross-attention layers will be randomly initialized
|
8 |
+
model = VisionEncoderDecoderModel.from_encoder_decoder_pretrained(
|
9 |
+
"google/vit-base-patch16-224-in21k", "bert-base-uncased"
|
10 |
+
)
|
11 |
+
# saving model after fine-tuning
|
12 |
+
model.save_pretrained("./vit-bert")
|
13 |
+
# load fine-tuned model
|
14 |
+
model = VisionEncoderDecoderModel.from_pretrained("./vit-bert")
|
15 |
+
|
16 |
+
#####################
|
17 |
+
from transformers import AutoTokenizer
|
18 |
+
repo_name = "ydshieh/vit-gpt2-coco-en"
|
19 |
+
feature_extractor = ViTFeatureExtractor.from_pretrained(repo_name)
|
20 |
+
tokenizer = AutoTokenizer.from_pretrained(repo_name)
|
21 |
+
model = VisionEncoderDecoderModel.from_pretrained(repo_name)
|
22 |
+
|
23 |
+
def get_quote(image_1):
|
24 |
+
#reader = easyocr.Reader(['en'])
|
25 |
+
image = Image.open(image_1,mode = 'r')
|
26 |
+
|
27 |
+
|
28 |
+
|
29 |
+
##############
|
30 |
+
pixel_values = feature_extractor(image, return_tensors="pt").pixel_values
|
31 |
+
# autoregressively generate text (using beam search or other decoding strategy)
|
32 |
+
generated_ids = model.generate(pixel_values, max_length=16, num_beams=4, return_dict_in_generate=True)
|
33 |
+
|
34 |
+
################
|
35 |
+
# decode into text
|
36 |
+
preds = tokenizer.batch_decode(generated_ids[0], skip_special_tokens=True)
|
37 |
+
preds = [pred.strip() for pred in preds]
|
38 |
+
#print(preds)
|
39 |
+
|
40 |
+
return preds
|
41 |
+
|
42 |
+
|
43 |
+
#1: Text to Speech
|
44 |
+
#import gradio as gr
|
45 |
+
title = "Image to text generation"
|
46 |
+
|
47 |
+
demo = gr.Interface.load(
|
48 |
+
fn=get_quote,
|
49 |
+
inputs = "image",
|
50 |
+
outputs=['text'],
|
51 |
+
title = title,
|
52 |
+
description = "Import an image file and get text from it" ,
|
53 |
+
cache_examples=False
|
54 |
+
|
55 |
+
)
|
56 |
+
if __name__ == "__main__":
|
57 |
+
demo.launch()
|