File size: 2,370 Bytes
af93bc7
ee29e30
 
 
 
 
4e51217
ee29e30
 
4e51217
af93bc7
4e51217
 
 
 
 
 
 
 
 
af93bc7
4e51217
229b554
af93bc7
4e51217
 
 
 
 
 
 
 
 
 
 
229b554
 
 
 
af93bc7
4e51217
 
 
229b554
 
 
 
 
4e51217
 
 
 
 
 
 
 
 
 
 
229b554
 
 
 
 
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
63
64
65
from transformers import pipeline
import streamlit as st
from PIL import Image
import requests
from io import BytesIO

# Initialize the pipeline
image_to_text = pipeline("image-to-text", model="nlpconnect/vit-gpt2-image-captioning")

st.title('Image Captioning Application')

# Function to load images from URL
def load_image_from_url(url):
    try:
        response = requests.get(url)
        img = Image.open(BytesIO(response.content))
        return img
    except Exception as e:
        st.error(f"Error loading image from URL: {e}")
        return None

# User option to select input type: Upload or URL
input_type = st.radio("Select input type:", ("Upload Image", "Image URL", "Text"))

if input_type == "Upload Image":
    uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
    if uploaded_file is not None:
        image = Image.open(uploaded_file)
        st.image(image, caption='Uploaded Image', use_column_width=True)
elif input_type == "Image URL":
    image_url = st.text_input("Enter the image URL here:", "")
    if image_url:
        image = load_image_from_url(image_url)
        if image:
            st.image(image, caption='Image from URL', use_column_width=True)
elif input_type == "Text":
    text_input = st.text_input("Enter text here:", "")
    if text_input:
        st.image(image, caption='Image from URL', use_column_width=True)

# Generate caption button
if st.button('Generate Caption'):
    if not image:
        if not text_input:
            st.warning("Please upload an image, enter an image URL or input text")
        else:
            result = text_input
            
    else:
        with st.spinner("Generating caption..."):
            # Process the image and generate caption
            if input_type == "Upload Image":
                # Save the uploaded image to a temporary file to pass its path to the model
                with open("temp_image.jpg", "wb") as f:
                    f.write(uploaded_file.getbuffer())
                result = image_to_text("temp_image.jpg")
            elif input_type == "Image URL" and image_url:
                result = image_to_text(image_url)
                
    if result:
        generated_text = result[0]['generated_text']
        st.success(f'Generated Caption: {generated_text}')
    else:
        st.error("Failed to generate caption.")