witchEverly
commited on
Update pages/πΈ_InstaMuse.py
Browse files- pages/πΈ_InstaMuse.py +65 -87
pages/πΈ_InstaMuse.py
CHANGED
@@ -2,47 +2,15 @@ import os
|
|
2 |
import streamlit as st
|
3 |
from PIL import Image
|
4 |
import pyperclip
|
|
|
5 |
import app_utils as utils
|
6 |
-
from CaptionGenerator import CaptionGenerator, load_model
|
7 |
|
8 |
# Define Streamlit configurations
|
9 |
-
st.set_page_config(
|
10 |
-
page_title="Instamuse",
|
11 |
-
page_icon=":camera:",
|
12 |
-
layout='wide',
|
13 |
-
menu_items={
|
14 |
-
'Get Help': 'https://www.streamlit.io',
|
15 |
-
'Report a bug': "https://github.com/aditya-67/Instagram_Caption_Generator/issues",
|
16 |
-
'About': "# This is a Streamlit app that uses AI to generate captions for images."
|
17 |
-
}
|
18 |
-
)
|
19 |
|
20 |
# Initialize the caption generator
|
21 |
-
caption_generator =
|
22 |
-
|
23 |
-
# Define aesthetic enhancements, including CSS for the spinner
|
24 |
-
st.markdown(
|
25 |
-
"""
|
26 |
-
<style>
|
27 |
-
.big-font {
|
28 |
-
font-size:30px !important;
|
29 |
-
font-weight: bold;
|
30 |
-
}
|
31 |
-
.image-shadow {
|
32 |
-
box-shadow: 8px 8px 20px grey;
|
33 |
-
}
|
34 |
-
/* Centering the spinner */
|
35 |
-
.st-bq {
|
36 |
-
position: fixed;
|
37 |
-
top: 50%;
|
38 |
-
left: 50%;
|
39 |
-
transform: translate(-50%, -50%);
|
40 |
-
font-size: 20px; /* Make text larger */
|
41 |
-
}
|
42 |
-
</style>
|
43 |
-
""",
|
44 |
-
unsafe_allow_html=True
|
45 |
-
)
|
46 |
|
47 |
# Sidebar configuration
|
48 |
with st.sidebar:
|
@@ -53,78 +21,88 @@ with st.sidebar:
|
|
53 |
|
54 |
# Main page content
|
55 |
st.markdown('<p class="big-font">InstaMuse Photo Caption Generator</p>', unsafe_allow_html=True)
|
56 |
-
st.write("### Upload your photo below and spark some caption magic! ΰ¬(ΰ©Λα΅Λ)ΰ©* ΰ©β©β§β")
|
57 |
-
st.write(
|
58 |
-
'**Notice**: *The first time you upload an image, it may take longer to generate captions.*\n*Subsequent uploads will be faster.*')
|
59 |
|
|
|
|
|
|
|
|
|
60 |
|
61 |
# Upload image file and process image
|
62 |
-
|
63 |
-
"Upload your image here:",
|
64 |
-
type=["jpg", "png"],
|
65 |
-
help="Only jpg and png images are supported"
|
66 |
)
|
67 |
|
68 |
-
if file:
|
69 |
-
image = Image.open(file)
|
70 |
-
image.thumbnail((600, 600), Image.Resampling.LANCZOS)
|
71 |
|
72 |
-
|
73 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
74 |
|
75 |
-
|
76 |
-
|
|
|
|
|
77 |
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
desc = caption_generator.image_2_text(image)
|
83 |
captions, caption_list, img_description = caption_generator.text_2_caption(desc)
|
84 |
st.session_state['captions'] = captions
|
85 |
st.session_state['caption_list'] = caption_list
|
86 |
st.session_state['img_description'] = img_description
|
87 |
-
|
88 |
-
st.
|
89 |
-
|
|
|
|
|
90 |
|
91 |
-
|
92 |
-
if captions:
|
93 |
st.markdown("## π Generated Captions:")
|
94 |
-
for caption in caption_list:
|
95 |
-
if caption.strip():
|
96 |
st.info(f"##### {caption}")
|
97 |
|
98 |
-
|
99 |
-
if 'captions' in st.session_state and st.session_state['captions']:
|
100 |
-
col1, col2, col3, col4 = st.columns(4)
|
101 |
-
if col1.button("π Copy"):
|
102 |
-
pyperclip.copy(st.session_state['captions'])
|
103 |
-
st.success("Caption copied to clipboard!")
|
104 |
|
105 |
-
|
106 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
107 |
st.rerun()
|
108 |
|
109 |
-
if
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
if
|
120 |
-
st.
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
|
125 |
with st.expander("Need help?"):
|
126 |
-
st.write("Please contact us
|
127 |
|
128 |
-
# Footer
|
129 |
st.markdown("---")
|
|
|
130 |
st.caption("Thank you for using InstaMuse! Feel free to contact us for any suggestions or issues.")
|
|
|
2 |
import streamlit as st
|
3 |
from PIL import Image
|
4 |
import pyperclip
|
5 |
+
import CaptionGenerator
|
6 |
import app_utils as utils
|
|
|
7 |
|
8 |
# Define Streamlit configurations
|
9 |
+
st.set_page_config(page_title="Instamuse", page_icon=":camera:", layout='wide')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
|
11 |
# Initialize the caption generator
|
12 |
+
caption_generator = CaptionGenerator.CaptionGenerator()
|
13 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
|
15 |
# Sidebar configuration
|
16 |
with st.sidebar:
|
|
|
21 |
|
22 |
# Main page content
|
23 |
st.markdown('<p class="big-font">InstaMuse Photo Caption Generator</p>', unsafe_allow_html=True)
|
24 |
+
st.write("""### Upload your photo below and spark some caption magic! ΰ¬(ΰ©Λα΅Λ)ΰ©* ΰ©β©β§β""")
|
|
|
|
|
25 |
|
26 |
+
# Load the model for image description
|
27 |
+
with st.spinner('Loading Application, this make take a minute :hourglass_flowing_sand:'):
|
28 |
+
# Load the model for image captioning
|
29 |
+
model, processor = utils.init_model()
|
30 |
|
31 |
# Upload image file and process image
|
32 |
+
uploaded_file = st.file_uploader(
|
33 |
+
"Upload your image here:", type=["jpg", "png"], help="Only jpg and png images are supported"
|
|
|
|
|
34 |
)
|
35 |
|
|
|
|
|
|
|
36 |
|
37 |
+
if uploaded_file is not None:
|
38 |
+
if 'file' not in st.session_state or uploaded_file != st.session_state['file']:
|
39 |
+
st.session_state['file'] = uploaded_file
|
40 |
+
|
41 |
+
image = Image.open(st.session_state['file'])
|
42 |
+
image.thumbnail((400, 400), Image.Resampling.LANCZOS)
|
43 |
+
st.session_state['image'] = image
|
44 |
+
col1, col2 = st.columns(2)
|
45 |
|
46 |
+
with st.container():
|
47 |
+
with col1:
|
48 |
+
st.markdown("## πΈ Your Image:")
|
49 |
+
st.image(st.session_state['image'], caption='Uploaded Image', use_column_width=True)
|
50 |
|
51 |
+
with col2:
|
52 |
+
with st.spinner(r'#### :sparkles: :sparkles: Generating... please wait :hourglass_flowing_sand:'):
|
53 |
+
if 'captions' not in st.session_state:
|
54 |
+
desc = caption_generator.image_2_text(image, model, processor)
|
|
|
55 |
captions, caption_list, img_description = caption_generator.text_2_caption(desc)
|
56 |
st.session_state['captions'] = captions
|
57 |
st.session_state['caption_list'] = caption_list
|
58 |
st.session_state['img_description'] = img_description
|
59 |
+
st.empty()
|
60 |
+
st.markdown("## π Generated Captions:")
|
61 |
+
for caption in st.session_state['caption_list']:
|
62 |
+
if caption.strip() != "":
|
63 |
+
st.info(f"##### {caption}")
|
64 |
|
65 |
+
else:
|
|
|
66 |
st.markdown("## π Generated Captions:")
|
67 |
+
for caption in st.session_state['caption_list']:
|
68 |
+
if caption.strip() != "":
|
69 |
st.info(f"##### {caption}")
|
70 |
|
71 |
+
st.markdown("---")
|
|
|
|
|
|
|
|
|
|
|
72 |
|
73 |
+
col3, col4, col5, col6 = st.columns(4)
|
74 |
+
if col3.button("π Copy Captions"):
|
75 |
+
pyperclip.copy(st.session_state['captions'])
|
76 |
+
st.success("Captions copied to clipboard!")
|
77 |
+
|
78 |
+
if col4.button("π Regenerate Captions"):
|
79 |
+
# Forcefully clear file to trigger reprocessing
|
80 |
+
if 'file' in st.session_state:
|
81 |
+
del st.session_state['file']
|
82 |
+
del st.session_state['captions']
|
83 |
+
del st.session_state['caption_list']
|
84 |
+
del st.session_state['img_description']
|
85 |
st.rerun()
|
86 |
|
87 |
+
if col5.button("β¨ More Hashtags"):
|
88 |
+
if 'img_description' in st.session_state:
|
89 |
+
with st.spinner('Generating hashtags...'):
|
90 |
+
try:
|
91 |
+
hashtags = caption_generator.caption_2_hashtag(st.session_state['img_description'])
|
92 |
+
st.write("### Generated Hashtags:")
|
93 |
+
st.write(f"**{hashtags}**")
|
94 |
+
except Exception as e:
|
95 |
+
st.error(f"Error generating hashtags: {e}")
|
96 |
+
|
97 |
+
if col6.button(":x: Report Issue"):
|
98 |
+
st.write("You are beta testing this app. Please report any issues to the developer. Thank you")
|
99 |
+
|
100 |
+
|
101 |
+
st.markdown("---")
|
102 |
|
103 |
with st.expander("Need help?"):
|
104 |
+
st.write("Please contact us by [email](mailto:[email protected])") # Correct the email link
|
105 |
|
|
|
106 |
st.markdown("---")
|
107 |
+
|
108 |
st.caption("Thank you for using InstaMuse! Feel free to contact us for any suggestions or issues.")
|