File size: 2,311 Bytes
45296db
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
149da67
45296db
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5b2cffc
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#imported all required libraries
import streamlit as st
import torch
import requests
from PIL import Image
from io import BytesIO
from transformers import ViTFeatureExtractor, AutoTokenizer, VisionEncoderDecoderModel


#used a pretrained model hosted on huggingface
loc = "ydshieh/vit-gpt2-coco-en"

feature_extractor = ViTFeatureExtractor.from_pretrained(loc)
tokenizer = AutoTokenizer.from_pretrained(loc)
model = VisionEncoderDecoderModel.from_pretrained(loc)
model.eval()

#defined a function for prediction

def predict(image):
    pixel_values = feature_extractor(images=image, return_tensors="pt").pixel_values

    with torch.no_grad():
        output_ids = model.generate(pixel_values, max_length=16, num_beams=4, return_dict_in_generate=True).sequences

    preds = tokenizer.batch_decode(output_ids, skip_special_tokens=True)
    preds = [pred.strip() for pred in preds]

    return preds

#defined a function for Streamlit App
def app():
    st.title("ImaginateAI")
    st.write("ViT and GPT2 are used to generate Image Caption for the uploaded image. COCO Dataset was used for training. This image captioning model might have some biases that I couldn’t figure during testing")
    st.write("Upload an image or paste a URL to get predicted captions.")

    upload_option = st.selectbox("Choose an option:", ("Upload Image", "Paste URL"))

    if upload_option == "Upload Image":
        uploaded_file = st.file_uploader("Upload an image", type=["jpg", "jpeg"])

        if uploaded_file is not None:
            image = Image.open(uploaded_file)
            preds = predict(image)
            st.image(image, caption="Uploaded Image", use_column_width=True)
            st.write("Predicted Caption:", preds)


    elif upload_option == "Paste URL":
        image_url = st.text_input("Enter Image URL")
        if st.button("Submit") and image_url:
            try:
                response = requests.get(image_url, stream=True)
                image = Image.open(BytesIO(response.content))
                preds = predict(image)
                st.image(image, caption="Image from URL", use_column_width=True)
                st.write("Predicted Caption:", preds)
            except:
                st.write("Error: Invalid URL or unable to fetch image.")

if __name__ == "__main__":
    app()