Nissan commited on
Commit ·
95fe60f
1
Parent(s): 61d7b4c
Upload app.py
Browse filesAdded app.py file to run Image Captioning AI application
app.py
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
from PIL import ImageChops
|
| 3 |
+
from transformers import VisionEncoderDecoderModel, ViTFeatureExtractor, PreTrainedTokenizerFast
|
| 4 |
+
import requests
|
| 5 |
+
|
| 6 |
+
model = VisionEncoderDecoderModel.from_pretrained("zaynnissan/VitGPT2")
|
| 7 |
+
vit_feature_extractor = ViTFeatureExtractor.from_pretrained("google/vit-base-patch16-224-in21k")
|
| 8 |
+
tokenizer = PreTrainedTokenizerFast.from_pretrained("distilgpt2")
|
| 9 |
+
|
| 10 |
+
def VitGPT2(img):
|
| 11 |
+
pixel_values = vit_feature_extractor(images=img, return_tensors="pt").pixel_values
|
| 12 |
+
encoder_outputs = model.generate(pixel_values.to('çpu'),num_beams=5)
|
| 13 |
+
generated_sentences = tokenizer.batch_decode(encoder_outputs, skip_special_tokens =True)
|
| 14 |
+
|
| 15 |
+
return(generated_sentences[0].split('.')[0])
|
| 16 |
+
|
| 17 |
+
import gradio as gr
|
| 18 |
+
|
| 19 |
+
inputs = [
|
| 20 |
+
gr.inputs.Image(type="pil", label = "Original Image")
|
| 21 |
+
]
|
| 22 |
+
|
| 23 |
+
outputs = [
|
| 24 |
+
gr.outputs.Textbox(label = 'Caption')
|
| 25 |
+
]
|
| 26 |
+
title = "Image Captioning using Vision Transformer and GPT-2"
|
| 27 |
+
description = "Developed by Zayn"
|
| 28 |
+
article = "< a href='https://huggingface.co/'>Hugging Face AI Community</a>"
|
| 29 |
+
examples = [
|
| 30 |
+
["car.jpg"]
|
| 31 |
+
]
|
| 32 |
+
gr.Interfacce(
|
| 33 |
+
VitGPT2,
|
| 34 |
+
inputs,
|
| 35 |
+
outputs,
|
| 36 |
+
title = title,
|
| 37 |
+
description = description,
|
| 38 |
+
article = article,
|
| 39 |
+
examples = examples,
|
| 40 |
+
theme = "huggingface",
|
| 41 |
+
).launch(debug=True,enable_queue=True)
|