Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
@@ -1,118 +1,44 @@
|
|
1 |
-
import
|
2 |
-
import
|
3 |
-
from PIL import Image
|
4 |
-
from PIL import ImageDraw
|
5 |
import gradio as gr
|
6 |
-
import
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
#['27-Books.jpg',['en']],
|
48 |
-
#['28-Books.jpg',['en']],
|
49 |
-
#['27-Games.jpg',['en']],
|
50 |
-
#['28-Games.jpg',['en']]]
|
51 |
-
|
52 |
-
#['29-Books-Science-Fiction.jpg',['en']],
|
53 |
-
#['30-Manga-Books.jpg',['en']],
|
54 |
-
#['31-Books.jpg',['en']],
|
55 |
-
#['32-Books.jpg',['en']],
|
56 |
-
#['33-Game-Night.jpg',['en']],
|
57 |
-
#['34-Games.jpg',['en']]
|
58 |
-
#['35-Favorite-Games.jpg',['en']],
|
59 |
-
#['36-Game-Night.jpg',['en']],
|
60 |
-
#['38-Strategy-Games.jpg',['en']],
|
61 |
-
#['39-Games.jpg',['en']],
|
62 |
-
#['40-Games.jpg',['en']],
|
63 |
-
#['41-Game-Rules.jpg',['en']],
|
64 |
-
#['42-Game-Rules.jpg',['en']],
|
65 |
-
#['43-Games-Pieces.jpg',['en']],
|
66 |
-
#['44-Game-Pieces.jpg',['en']],
|
67 |
-
#['45-Choose-Your-Own-Adventure.jpg',['en']],
|
68 |
-
#['46-Choose-Your-Own-Adventure.jpg',['en']]
|
69 |
-
#]
|
70 |
-
|
71 |
-
# Comment
|
72 |
-
#['english.png',['en']],
|
73 |
-
#['chinese.jpg',['ch_sim', 'en']],
|
74 |
-
#['japanese.jpg',['ja', 'en']],
|
75 |
-
#['Hindi.jpeg',['hi', 'en']]
|
76 |
-
|
77 |
-
def draw_boxes(image, bounds, color='yellow', width=2):
|
78 |
-
draw = ImageDraw.Draw(image)
|
79 |
-
for bound in bounds:
|
80 |
-
p0, p1, p2, p3 = bound[0]
|
81 |
-
draw.line([*p0, *p1, *p2, *p3, *p0], fill=color, width=width)
|
82 |
-
return image
|
83 |
-
|
84 |
-
def inference(img, lang):
|
85 |
-
reader = easyocr.Reader(lang)
|
86 |
-
bounds = reader.readtext(img.name)
|
87 |
-
im = PIL.Image.open(img.name)
|
88 |
-
draw_boxes(im, bounds)
|
89 |
-
im.save('result.jpg')
|
90 |
-
return ['result.jpg', pd.DataFrame(bounds).iloc[: , 1:]]
|
91 |
-
|
92 |
-
title = 'Image To Optical Character Recognition'
|
93 |
-
description = 'Multilingual OCR which works conveniently on all devices in multiple languages.'
|
94 |
-
article = "<p style='text-align: center'></p>"
|
95 |
-
css = ".output_image, .input_image {height: 40rem !important; width: 100% !important;}"
|
96 |
-
examples = [['20-Books.jpg',['en']],['21-Books.jpg',['en']],['22-Magazines.jpg',['en']],['23-Magazines.jpg',['en']]]
|
97 |
-
|
98 |
-
choices = [
|
99 |
-
"ch_sim",
|
100 |
-
"ch_tra",
|
101 |
-
"de",
|
102 |
-
"en",
|
103 |
-
"es",
|
104 |
-
"ja",
|
105 |
-
"hi",
|
106 |
-
"ru"
|
107 |
-
]
|
108 |
-
gr.Interface(
|
109 |
-
inference,
|
110 |
-
[gr.inputs.Image(type='file', label='Input'),gr.inputs.CheckboxGroup(choices, type="value", default=['en'], label='language')],
|
111 |
-
[gr.outputs.Image(type='file', label='Output'), gr.outputs.Dataframe(headers=['text', 'confidence'])],
|
112 |
-
title=title,
|
113 |
-
description=description,
|
114 |
-
article=article,
|
115 |
-
examples=examples,
|
116 |
-
css=css,
|
117 |
-
enable_queue=True
|
118 |
-
).launch(debug=True)
|
|
|
1 |
+
import torch
|
2 |
+
import re
|
|
|
|
|
3 |
import gradio as gr
|
4 |
+
from transformers import AutoTokenizer, ViTFeatureExtractor, VisionEncoderDecoderModel
|
5 |
+
|
6 |
+
device='cpu'
|
7 |
+
encoder_checkpoint = "nlpconnect/vit-gpt2-image-captioning"
|
8 |
+
decoder_checkpoint = "nlpconnect/vit-gpt2-image-captioning"
|
9 |
+
model_checkpoint = "nlpconnect/vit-gpt2-image-captioning"
|
10 |
+
feature_extractor = ViTFeatureExtractor.from_pretrained(encoder_checkpoint)
|
11 |
+
tokenizer = AutoTokenizer.from_pretrained(decoder_checkpoint)
|
12 |
+
model = VisionEncoderDecoderModel.from_pretrained(model_checkpoint).to(device)
|
13 |
+
|
14 |
+
|
15 |
+
def predict(image,max_length=64, num_beams=4):
|
16 |
+
image = image.convert('RGB')
|
17 |
+
image = feature_extractor(image, return_tensors="pt").pixel_values.to(device)
|
18 |
+
clean_text = lambda x: x.replace('<|endoftext|>','').split('\n')[0]
|
19 |
+
caption_ids = model.generate(image, max_length = max_length)[0]
|
20 |
+
caption_text = clean_text(tokenizer.decode(caption_ids))
|
21 |
+
return caption_text
|
22 |
+
|
23 |
+
|
24 |
+
|
25 |
+
input = gr.inputs.Image(label="Upload your Image", type = 'pil', optional=True)
|
26 |
+
output = gr.outputs.Textbox(type="auto",label="Captions")
|
27 |
+
examples = [f"example{i}.jpg" for i in range(1,7)]
|
28 |
+
|
29 |
+
description= "Image captioning application made using transformers"
|
30 |
+
title = "Image Captioning 🖼️"
|
31 |
+
|
32 |
+
article = "Created By : Shreyas Dixit "
|
33 |
+
|
34 |
+
interface = gr.Interface(
|
35 |
+
fn=predict,
|
36 |
+
inputs = input,
|
37 |
+
theme="grass",
|
38 |
+
outputs=output,
|
39 |
+
examples = examples,
|
40 |
+
title=title,
|
41 |
+
description=description,
|
42 |
+
article = article,
|
43 |
+
)
|
44 |
+
interface.launch(debug=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|