File size: 1,615 Bytes
246df6f
 
 
bd01478
ba44e57
246df6f
0738976
4878f59
 
ba44e57
246df6f
4878f59
 
ba44e57
 
ffd6c60
ba44e57
 
 
 
 
 
 
 
 
 
 
246df6f
45f8c9c
ba44e57
7d12a4b
ba44e57
 
7d12a4b
246df6f
 
2a3202e
 
 
 
ddbd464
2a3202e
 
 
246df6f
2a3202e
 
1
2
3
4
5
6
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
import torch 
import re 
import gradio as gr
from transformers import AutoTokenizer, ViTFeatureExtractor, VisionEncoderDecoderModel, ViTImageProcessor
'''
device='cpu'
encoder_checkpoint = "Thibalte/captionning_project"
decoder_checkpoint = "Thibalte/captionning_project"
model_checkpoint = "Thibalte/captionning_project"
feature_extractor= ViTImageProcessor.from_pretrained(model_path)
feature_extractor = ViTFeatureExtractor.from_pretrained(encoder_checkpoint)
tokenizer = AutoTokenizer.from_pretrained(decoder_checkpoint)
model = VisionEncoderDecoderModel.from_pretrained(model_checkpoint).to(device)
'''
# Load the trained model
model_path = "Thibalte/captionning_project"

# Load tokenizer
tokenizer = AutoTokenizer.from_pretrained(model_path)

#Load ImageProcessor
feature_extractor= ViTImageProcessor.from_pretrained(model_path)

# Load model
model = VisionEncoderDecoderModel.from_pretrained(model_path)



def predict(image,max_length=24, num_beams=4):
    image = image.convert('RGB')
    pixel_values = feature_extractor(images=image, return_tensors="pt").pixel_values
    sequences = model.generate(pixel_values, num_beams=4, max_length=25)
    captions = tokenizer.batch_decode(sequences, skip_special_tokens=True)
    return captions


# Gradio Interface
gradio_app = gr.Interface(
    fn=predict,
    inputs=gr.Image(label="Select image for captioning", sources=['upload', 'webcam'], type="pil"),
    outputs=[gr.Textbox(label="Image Caption")],
    examples = [f"example{i}.jpg" for i in range(1,7)],
    title="Image Captioning with our model",
)

if __name__ == "__main__":
    gradio_app.launch()