Update app.py
Browse files
app.py
CHANGED
@@ -1,20 +1,66 @@
|
|
1 |
import streamlit as st
|
2 |
from PIL import Image
|
3 |
|
4 |
-
# UI
|
5 |
-
st.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
|
7 |
# Sidebar for model selection
|
|
|
8 |
model_option = st.sidebar.selectbox(
|
9 |
"Select Model",
|
10 |
("microsoft/Florence-2-large-ft", "microsoft/Florence-2-large", "microsoft/Florence-2-base-ft", "microsoft/Florence-2-base")
|
11 |
)
|
12 |
|
13 |
# Task prompt selection
|
14 |
-
task_prompt = st.text_input("Task Prompt", value="
|
15 |
|
16 |
# Text input (optional)
|
17 |
-
text_input = st.
|
18 |
|
19 |
# Image upload
|
20 |
uploaded_image = st.file_uploader("Upload an Image", type=["jpg", "jpeg", "png"])
|
@@ -23,22 +69,29 @@ uploaded_image = st.file_uploader("Upload an Image", type=["jpg", "jpeg", "png"]
|
|
23 |
if uploaded_image is not None:
|
24 |
image = Image.open(uploaded_image)
|
25 |
st.image(image, caption="Uploaded Image", use_column_width=True)
|
26 |
-
|
27 |
-
#
|
28 |
-
|
29 |
-
|
|
|
|
|
|
|
30 |
result = run_example(task_prompt, image, text_input, model_option)
|
31 |
-
|
32 |
# Display results
|
33 |
-
st.
|
34 |
-
st.
|
35 |
-
|
36 |
# Display bounding boxes if available
|
37 |
if "bboxes" in result:
|
|
|
38 |
fig = plot_bbox(image, result)
|
39 |
st.pyplot(fig)
|
40 |
-
|
41 |
# Display polygons if available
|
42 |
if "polygons" in result:
|
|
|
43 |
processed_image = draw_polygons(image.copy(), result)
|
44 |
st.image(processed_image, caption="Image with Polygons", use_column_width=True)
|
|
|
|
|
|
1 |
import streamlit as st
|
2 |
from PIL import Image
|
3 |
|
4 |
+
# Custom CSS to style the UI
|
5 |
+
st.markdown("""
|
6 |
+
<style>
|
7 |
+
.main {
|
8 |
+
background-color: #f0f2f6;
|
9 |
+
padding: 20px;
|
10 |
+
border-radius: 10px;
|
11 |
+
box-shadow: 2px 2px 10px rgba(0, 0, 0, 0.1);
|
12 |
+
}
|
13 |
+
.sidebar .sidebar-content {
|
14 |
+
background-color: #2c3e50;
|
15 |
+
color: white;
|
16 |
+
}
|
17 |
+
.sidebar .sidebar-content a {
|
18 |
+
color: #f39c12;
|
19 |
+
}
|
20 |
+
.title {
|
21 |
+
color: #34495e;
|
22 |
+
font-family: 'Arial', sans-serif;
|
23 |
+
font-weight: bold;
|
24 |
+
font-size: 36px;
|
25 |
+
text-align: center;
|
26 |
+
margin-bottom: 30px;
|
27 |
+
}
|
28 |
+
.upload-button {
|
29 |
+
background-color: #1abc9c;
|
30 |
+
color: white;
|
31 |
+
border-radius: 5px;
|
32 |
+
padding: 10px;
|
33 |
+
text-align: center;
|
34 |
+
font-weight: bold;
|
35 |
+
}
|
36 |
+
.run-button {
|
37 |
+
background-color: #2980b9;
|
38 |
+
color: white;
|
39 |
+
border-radius: 5px;
|
40 |
+
padding: 10px;
|
41 |
+
font-weight: bold;
|
42 |
+
text-align: center;
|
43 |
+
cursor: pointer;
|
44 |
+
}
|
45 |
+
</style>
|
46 |
+
""", unsafe_allow_html=True)
|
47 |
+
|
48 |
+
# Main UI layout
|
49 |
+
st.markdown('<div class="main">', unsafe_allow_html=True)
|
50 |
+
st.markdown('<div class="title">Florence-2 Image Captioning Demo</div>', unsafe_allow_html=True)
|
51 |
|
52 |
# Sidebar for model selection
|
53 |
+
st.sidebar.markdown("## Model Configuration")
|
54 |
model_option = st.sidebar.selectbox(
|
55 |
"Select Model",
|
56 |
("microsoft/Florence-2-large-ft", "microsoft/Florence-2-large", "microsoft/Florence-2-base-ft", "microsoft/Florence-2-base")
|
57 |
)
|
58 |
|
59 |
# Task prompt selection
|
60 |
+
task_prompt = st.text_input("Task Prompt", value="Describe the image in detail:")
|
61 |
|
62 |
# Text input (optional)
|
63 |
+
text_input = st.text_area("Additional Text Input (Optional)", height=150)
|
64 |
|
65 |
# Image upload
|
66 |
uploaded_image = st.file_uploader("Upload an Image", type=["jpg", "jpeg", "png"])
|
|
|
69 |
if uploaded_image is not None:
|
70 |
image = Image.open(uploaded_image)
|
71 |
st.image(image, caption="Uploaded Image", use_column_width=True)
|
72 |
+
|
73 |
+
# Space between the image and the button
|
74 |
+
st.markdown("<br>", unsafe_allow_html=True)
|
75 |
+
|
76 |
+
# Run button
|
77 |
+
if st.button("Generate Caption", key="run"):
|
78 |
+
# Assuming `run_example` function and others are defined
|
79 |
result = run_example(task_prompt, image, text_input, model_option)
|
80 |
+
|
81 |
# Display results
|
82 |
+
st.markdown("### Generated Caption")
|
83 |
+
st.success(result["text"]) # Display the generated text
|
84 |
+
|
85 |
# Display bounding boxes if available
|
86 |
if "bboxes" in result:
|
87 |
+
st.markdown("### Detected Objects")
|
88 |
fig = plot_bbox(image, result)
|
89 |
st.pyplot(fig)
|
90 |
+
|
91 |
# Display polygons if available
|
92 |
if "polygons" in result:
|
93 |
+
st.markdown("### Image with Polygons")
|
94 |
processed_image = draw_polygons(image.copy(), result)
|
95 |
st.image(processed_image, caption="Image with Polygons", use_column_width=True)
|
96 |
+
|
97 |
+
st.markdown('</div>', unsafe_allow_html=True)
|