import streamlit as st
from PIL import Image
# Custom CSS for enhanced styling
st.markdown("""
""", unsafe_allow_html=True)
# Initialize session state to block re-running
if 'has_run' not in st.session_state:
st.session_state.has_run = False
# Main UI container
st.markdown('
Florence-2 Image Captioning Demo
', unsafe_allow_html=True)
# Image upload area
st.sidebar.markdown('', unsafe_allow_html=True)
uploaded_image = st.sidebar.file_uploader("Upload your image here", type=["jpg", "jpeg", "png"])
st.sidebar.markdown('
', unsafe_allow_html=True)
# Display the uploaded image and process it if available
if uploaded_image is not None:
image = Image.open(uploaded_image)
st.image(image, caption="Uploaded Image", use_column_width=True)
# Task prompt input
task_prompt = st.sidebar.text_input("Task Prompt", value="Describe the image in detail:")
# Additional text input (optional)
text_input = st.text_area("Input Questions", height=50)
# Generate Caption button
if st.button("Generate Caption", key="Generate") and not st.session_state.has_run:
# Mark that the script has been run
st.session_state.has_run = True
st.write(task_prompt,"\n",text_input)
# Clear session state button
if st.session_state.has_run:
if st.button("Clear and Upload New Image"):
st.session_state.has_run = False
st.experimental_rerun()
st.markdown('', unsafe_allow_html=True)