import streamlit as st from PIL import Image # Custom CSS for enhanced styling st.markdown(""" """, unsafe_allow_html=True) # Main UI container st.markdown('
', unsafe_allow_html=True) st.markdown('
Florence-2 Image Captioning Demo
', unsafe_allow_html=True) # Task prompt input task_prompt = st.text_input("Task Prompt", value="Describe the image in detail:") # Image upload area st.markdown('
', unsafe_allow_html=True) uploaded_image = st.file_uploader("Upload your image here", type=["jpg", "jpeg", "png"]) st.markdown('
', unsafe_allow_html=True) # Additional text input (optional) text_input = st.text_area("Additional Text Input (Optional)", height=150) # 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) # Generate Caption button if st.button("Generate Caption", key="generate"): # Assuming `run_example` function is defined result = run_example(task_prompt, image, text_input) # Display the generated caption st.markdown('
', unsafe_allow_html=True) st.markdown('
Generated Caption
', unsafe_allow_html=True) st.markdown(f'
{result["text"]}
', unsafe_allow_html=True) # Display bounding boxes or polygons if available if "bboxes" in result: st.markdown("### Detected Objects") fig = plot_bbox(image, result) st.pyplot(fig) if "polygons" in result: st.markdown("### Image with Polygons") processed_image = draw_polygons(image.copy(), result) st.image(processed_image, caption="Image with Polygons", use_column_width=True) st.markdown('
', unsafe_allow_html=True)