File size: 2,790 Bytes
8b4c31a
 
ad666e1
502f248
8b4c31a
2906402
 
 
 
 
 
 
3af10b0
 
 
 
 
 
9ce2d62
3af10b0
 
 
 
 
9ce2d62
3af10b0
 
 
 
 
 
 
 
 
 
 
 
 
 
9ce2d62
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3af10b0
 
 
8b4c31a
3af10b0
 
 
8b4c31a
 
 
 
3feef57
 
 
 
 
 
 
 
9ce2d62
3af10b0
 
 
3feef57
 
502f248
 
3feef57
 
502f248
 
8b4c31a
 
 
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import streamlit as st
from PIL import Image
import io
import traceback

try:
    from image_to_text import generate_caption
except ImportError as e:
    st.error(f"Error importing generate_caption function: {str(e)}")
    st.error("Please make sure all required libraries are installed.")
    st.stop()

def set_custom_style():
    st.markdown("""
    <style>
    .stApp {
        max-width: 800px;
        margin: 0 auto;
        padding: 1rem;
    }
    .title {
        color: #1e3a8a;
        font-size: 2.5rem;
        font-weight: bold;
        margin-bottom: 1rem;
        text-align: center;
    }
    .stButton>button {
        background-color: #3b82f6;
        color: white;
        font-weight: bold;
        padding: 0.5rem 2rem;
        border-radius: 5px;
        border: none;
        transition: background-color 0.3s ease;
    }
    .stButton>button:hover {
        background-color: #2563eb;
    }
    .caption-container {
        background-color: #f3f4f6;
        padding: 1rem;
        border-radius: 5px;
        margin-top: 1rem;
    }
    .caption-text {
        font-size: 1.2rem;
        font-style: italic;
        color: #4a5568;
    }
    .stAlert {
        background-color: #fee2e2;
        color: #991b1b;
        padding: 0.5rem;
        border-radius: 5px;
        margin-top: 1rem;
    }
    </style>
    """, unsafe_allow_html=True)

def main():
    set_custom_style()
    
    st.markdown('<h1 class="title">Image Captioning App</h1>', unsafe_allow_html=True)

    uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
    
    if uploaded_file is not None:
        try:
            image = Image.open(uploaded_file)
            st.image(image, caption='Uploaded Image', use_column_width=True)
            
            if st.button('Generate Caption'):
                with st.spinner('Generating caption...'):
                    try:
                        caption = generate_caption(image)
                        st.markdown('<div class="caption-container">', unsafe_allow_html=True)
                        st.markdown("### Generated Caption:")
                        st.markdown(f'<p class="caption-text">{caption}</p>', unsafe_allow_html=True)
                        st.markdown('</div>', unsafe_allow_html=True)
                    except Exception as e:
                        st.error(f"An error occurred while generating the caption: {str(e)}")
                        st.error("Detailed error traceback:")
                        st.code(traceback.format_exc())
        except Exception as e:
            st.error(f"An error occurred while processing the image: {str(e)}")
            st.error("Detailed error traceback:")
            st.code(traceback.format_exc())

if __name__ == "__main__":
    main()