Spaces:
Sleeping
Sleeping
Upload 7 files
Browse files- .gitattributes +3 -0
- Image1.jpg +3 -0
- Image2.png +3 -0
- Image3.png +3 -0
- app.py +36 -0
- functions.py +48 -0
- readme.txt +17 -0
- requirements.txt +12 -0
.gitattributes
CHANGED
|
@@ -33,3 +33,6 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
| 36 |
+
Image1.jpg filter=lfs diff=lfs merge=lfs -text
|
| 37 |
+
Image2.png filter=lfs diff=lfs merge=lfs -text
|
| 38 |
+
Image3.png filter=lfs diff=lfs merge=lfs -text
|
Image1.jpg
ADDED
|
Git LFS Details
|
Image2.png
ADDED
|
Git LFS Details
|
Image3.png
ADDED
|
Git LFS Details
|
app.py
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from functions import predict_step
|
| 3 |
+
from itertools import cycle
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
def image_uploader():
|
| 7 |
+
with st.form("uploader"):
|
| 8 |
+
images = st.file_uploader("Upload Images",accept_multiple_files=True,type=["png","jpg","jpeg"])
|
| 9 |
+
submitted = st.form_submit_button("Submit")
|
| 10 |
+
if submitted:
|
| 11 |
+
predicted_captions = predict_step(images,False)
|
| 12 |
+
for i,caption in enumerate(predicted_captions):
|
| 13 |
+
st.write(str(i+1)+'. '+caption)
|
| 14 |
+
|
| 15 |
+
def images_url():
|
| 16 |
+
with st.form("url"):
|
| 17 |
+
urls = st.text_input('Enter URL of Images followed by comma for multiple URLs')
|
| 18 |
+
images = urls.split(',')
|
| 19 |
+
submitted = st.form_submit_button("Submit")
|
| 20 |
+
if submitted:
|
| 21 |
+
predicted_captions = predict_step(images,True)
|
| 22 |
+
for i,caption in enumerate(predicted_captions):
|
| 23 |
+
st.write(str(i+1)+'. '+caption)
|
| 24 |
+
|
| 25 |
+
def main():
|
| 26 |
+
st.set_page_config(page_title="Image Captioning", page_icon="🖼️")
|
| 27 |
+
st.title("Image Caption")
|
| 28 |
+
|
| 29 |
+
st.subheader("Upload your own Images")
|
| 30 |
+
image_uploader()
|
| 31 |
+
|
| 32 |
+
st.subheader("Enter Image URLs")
|
| 33 |
+
images_url()
|
| 34 |
+
|
| 35 |
+
if __name__ == '__main__':
|
| 36 |
+
main()
|
functions.py
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from PIL import Image
|
| 2 |
+
from tqdm import tqdm
|
| 3 |
+
from transformers import VisionEncoderDecoderModel, ViTFeatureExtractor, AutoTokenizer
|
| 4 |
+
import torch
|
| 5 |
+
from PIL import Image
|
| 6 |
+
from tqdm import tqdm
|
| 7 |
+
import urllib.request
|
| 8 |
+
from itertools import cycle
|
| 9 |
+
import os
|
| 10 |
+
|
| 11 |
+
model = VisionEncoderDecoderModel.from_pretrained("nlpconnect/vit-gpt2-image-captioning")
|
| 12 |
+
feature_extractor = ViTFeatureExtractor.from_pretrained("nlpconnect/vit-gpt2-image-captioning")
|
| 13 |
+
tokenizer = AutoTokenizer.from_pretrained("nlpconnect/vit-gpt2-image-captioning")
|
| 14 |
+
|
| 15 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 16 |
+
model.to(device)
|
| 17 |
+
|
| 18 |
+
max_length = 16
|
| 19 |
+
num_beams = 4
|
| 20 |
+
num_return_sequences = 3 # Number of captions to generate for each image
|
| 21 |
+
gen_kwargs = {"max_length": max_length, "num_beams": num_beams, "num_return_sequences": num_return_sequences}
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
def predict_step(images_list,is_url):
|
| 25 |
+
images = []
|
| 26 |
+
for image in tqdm(images_list):
|
| 27 |
+
if is_url:
|
| 28 |
+
urllib.request.urlretrieve(image, "file.jpg")
|
| 29 |
+
i_image = Image.open("file.jpg")
|
| 30 |
+
|
| 31 |
+
else:
|
| 32 |
+
i_image = Image.open(image)
|
| 33 |
+
|
| 34 |
+
if i_image.mode != "RGB":
|
| 35 |
+
i_image = i_image.convert(mode="RGB")
|
| 36 |
+
|
| 37 |
+
images.append(i_image)
|
| 38 |
+
|
| 39 |
+
pixel_values = feature_extractor(images=images, return_tensors="pt").pixel_values
|
| 40 |
+
pixel_values = pixel_values.to(device)
|
| 41 |
+
|
| 42 |
+
output_ids = model.generate(pixel_values, **gen_kwargs)
|
| 43 |
+
|
| 44 |
+
preds = tokenizer.batch_decode(output_ids, skip_special_tokens=True, clean_up_tokenization_spaces=True)
|
| 45 |
+
preds = [pred.strip() for pred in preds]
|
| 46 |
+
if is_url:
|
| 47 |
+
os.remove('file.jpg')
|
| 48 |
+
return preds
|
readme.txt
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
TASK
|
| 2 |
+
● Create an AI tool that creates captions based on the image provided by the user. Should also have
|
| 3 |
+
the option to generate multiple captions based on the image.
|
| 4 |
+
● Provide an interface where the user can come and upload images and get AI generated captions. ●
|
| 5 |
+
You are to free use the library of your choice
|
| 6 |
+
● Use the following images as test cases - Link
|
| 7 |
+
Note - Try to use pre-trained models from websites like huggingface.
|
| 8 |
+
|
| 9 |
+
To Put it out there
|
| 10 |
+
Your assignment will also be evaluated on the following criteria
|
| 11 |
+
● Code quality, the less the code the better.
|
| 12 |
+
● Time taken to submit, the less the better.
|
| 13 |
+
● Response times, the faster the better.
|
| 14 |
+
|
| 15 |
+
Project Link:
|
| 16 |
+
|
| 17 |
+
https://drive.google.com/drive/folders/1Ekn8HzzHbo0oULYo6o8aSeju1hDdgJnV?usp=share_link
|
requirements.txt
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
fastapi==0.78.0
|
| 2 |
+
pandas==1.5.0
|
| 3 |
+
pydantic==1.10.2
|
| 4 |
+
scikit-learn==1.1.2
|
| 5 |
+
servicefoundry
|
| 6 |
+
mlfoundry
|
| 7 |
+
streamlit==1.13.0
|
| 8 |
+
uvicorn==0.18.3
|
| 9 |
+
xgboost==1.6.2
|
| 10 |
+
torch
|
| 11 |
+
transformers
|
| 12 |
+
tqdm
|