Update app.py
Browse files
app.py
CHANGED
@@ -1,35 +1,39 @@
|
|
1 |
-
import
|
2 |
-
|
3 |
-
import numpy as np #Image Processing
|
4 |
import gradio as gr
|
|
|
5 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
|
7 |
-
input = gr.inputs.Image(label="Upload your Image", type= 'pil', optional=True)
|
8 |
|
9 |
-
def
|
10 |
-
|
11 |
-
|
|
|
|
|
|
|
|
|
12 |
|
13 |
-
def text(image):
|
14 |
-
reader = load_model() #load model
|
15 |
-
print("Model Load")
|
16 |
-
input_image = image.convert('RGB') #read image
|
17 |
-
print("image read")
|
18 |
-
result = reader.readtext(np.array(input_image))
|
19 |
-
result_text = [] #empty list for results
|
20 |
-
for text in result:
|
21 |
-
result_text.append(text[1])
|
22 |
-
return result_text
|
23 |
|
24 |
-
output = gr.outputs.Textbox(type="text",label="Captions")
|
25 |
|
26 |
-
|
|
|
|
|
|
|
|
|
27 |
|
28 |
interface = gr.Interface(
|
29 |
-
fn=
|
30 |
inputs = input,
|
31 |
theme="grass",
|
32 |
outputs=output,
|
|
|
33 |
title=title,
|
34 |
)
|
35 |
interface.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 = "jaimin/image_caption"
|
8 |
+
decoder_checkpoint = "jaimin/image_caption"
|
9 |
+
model_checkpoint = "jaimin/image_caption"
|
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 |
+
title = "Image To Text"
|
30 |
|
31 |
interface = gr.Interface(
|
32 |
+
fn=predict,
|
33 |
inputs = input,
|
34 |
theme="grass",
|
35 |
outputs=output,
|
36 |
+
examples = examples,
|
37 |
title=title,
|
38 |
)
|
39 |
interface.launch(debug=True)
|