Update pages/📸_InstaMuse.py
Browse files- pages/📸_InstaMuse.py +37 -12
pages/📸_InstaMuse.py
CHANGED
|
@@ -20,34 +20,48 @@ with st.sidebar:
|
|
| 20 |
st.write("Start turning heads with your posts. Use InstaMuse now and watch your likes soar!")
|
| 21 |
|
| 22 |
# Main page content
|
| 23 |
-
st.markdown('
|
| 24 |
st.write("""### Upload your photo below and spark some caption magic! ଘ(੭ˊᵕˋ)੭* ੈ✩‧₊""")
|
| 25 |
|
| 26 |
st.write(
|
| 27 |
-
'**Notice**: *Due to model load time, it may take a moment to load the application. This only occurs once.*
|
| 28 |
-
|
| 29 |
-
|
| 30 |
|
| 31 |
# Load the model for image description
|
| 32 |
with st.spinner('Loading Application, this make take a minute :hourglass_flowing_sand:'):
|
| 33 |
# Load the model for image captioning
|
| 34 |
model, processor = utils.init_model()
|
| 35 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
# Upload image file and process image
|
| 37 |
uploaded_file = st.file_uploader(
|
| 38 |
-
"Upload your image here:", type=["jpg", "png"],
|
|
|
|
| 39 |
)
|
| 40 |
|
| 41 |
|
| 42 |
-
if uploaded_file
|
| 43 |
-
if 'file' not in st.session_state
|
| 44 |
st.session_state['file'] = uploaded_file
|
|
|
|
|
|
|
|
|
|
| 45 |
|
| 46 |
-
image = Image.open(st.session_state['file'])
|
| 47 |
-
image.thumbnail((400, 400), Image.Resampling.LANCZOS)
|
| 48 |
-
st.session_state['image'] = image
|
| 49 |
col1, col2 = st.columns(2)
|
| 50 |
-
|
| 51 |
with st.container():
|
| 52 |
with col1:
|
| 53 |
st.markdown("## 📸 Your Image:")
|
|
@@ -61,7 +75,18 @@ if uploaded_file is not None:
|
|
| 61 |
st.session_state['captions'] = captions
|
| 62 |
st.session_state['caption_list'] = caption_list
|
| 63 |
st.session_state['img_description'] = img_description
|
| 64 |
-
st.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
st.markdown("## 📝 Generated Captions:")
|
| 66 |
for caption in st.session_state['caption_list']:
|
| 67 |
if caption.strip() != "":
|
|
|
|
| 20 |
st.write("Start turning heads with your posts. Use InstaMuse now and watch your likes soar!")
|
| 21 |
|
| 22 |
# Main page content
|
| 23 |
+
st.markdown('## InstaMuse Photo Caption Generator')
|
| 24 |
st.write("""### Upload your photo below and spark some caption magic! ଘ(੭ˊᵕˋ)੭* ੈ✩‧₊""")
|
| 25 |
|
| 26 |
st.write(
|
| 27 |
+
f'**Notice**: *Due to model load time, it may take a moment to load the application. This only occurs once.* \n*Subsequent uploads will be faster.*'
|
| 28 |
+
)
|
|
|
|
| 29 |
|
| 30 |
# Load the model for image description
|
| 31 |
with st.spinner('Loading Application, this make take a minute :hourglass_flowing_sand:'):
|
| 32 |
# Load the model for image captioning
|
| 33 |
model, processor = utils.init_model()
|
| 34 |
|
| 35 |
+
|
| 36 |
+
def clear_cache():
|
| 37 |
+
"""
|
| 38 |
+
Function to clear the session state cache.
|
| 39 |
+
"""
|
| 40 |
+
if 'file' in st.session_state:
|
| 41 |
+
del st.session_state['file']
|
| 42 |
+
if 'captions' in st.session_state:
|
| 43 |
+
del st.session_state['captions']
|
| 44 |
+
if 'caption_list' in st.session_state:
|
| 45 |
+
del st.session_state['caption_list']
|
| 46 |
+
if 'img_description' in st.session_state:
|
| 47 |
+
del st.session_state['img_description']
|
| 48 |
+
st.rerun()
|
| 49 |
+
|
| 50 |
# Upload image file and process image
|
| 51 |
uploaded_file = st.file_uploader(
|
| 52 |
+
"Upload your image here:", type=["jpg", "png"],
|
| 53 |
+
help="Only jpg and png images are supported"
|
| 54 |
)
|
| 55 |
|
| 56 |
|
| 57 |
+
if uploaded_file:
|
| 58 |
+
if 'file' not in st.session_state:
|
| 59 |
st.session_state['file'] = uploaded_file
|
| 60 |
+
image = Image.open(uploaded_file)
|
| 61 |
+
image.thumbnail((400, 400), Image.Resampling.LANCZOS)
|
| 62 |
+
st.session_state['image'] = image
|
| 63 |
|
|
|
|
|
|
|
|
|
|
| 64 |
col1, col2 = st.columns(2)
|
|
|
|
| 65 |
with st.container():
|
| 66 |
with col1:
|
| 67 |
st.markdown("## 📸 Your Image:")
|
|
|
|
| 75 |
st.session_state['captions'] = captions
|
| 76 |
st.session_state['caption_list'] = caption_list
|
| 77 |
st.session_state['img_description'] = img_description
|
| 78 |
+
st.markdown("## 📝 Generated Captions:")
|
| 79 |
+
for caption in st.session_state['caption_list']:
|
| 80 |
+
if caption.strip() != "":
|
| 81 |
+
st.info(f"##### {caption}")
|
| 82 |
+
|
| 83 |
+
elif st.session_state['file'] != uploaded_file:
|
| 84 |
+
clear_cache()
|
| 85 |
+
desc = caption_generator.image_2_text(image, model, processor)
|
| 86 |
+
captions, caption_list, img_description = caption_generator.text_2_caption(desc)
|
| 87 |
+
st.session_state['captions'] = captions
|
| 88 |
+
st.session_state['caption_list'] = caption_list
|
| 89 |
+
st.session_state['img_description'] = img_description
|
| 90 |
st.markdown("## 📝 Generated Captions:")
|
| 91 |
for caption in st.session_state['caption_list']:
|
| 92 |
if caption.strip() != "":
|