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(""" """, unsafe_allow_html=True) def main(): set_custom_style() st.markdown('

Image Captioning App

', 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('
', unsafe_allow_html=True) st.markdown("### Generated Caption:") st.markdown(f'

{caption}

', unsafe_allow_html=True) st.markdown('
', 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()