witchEverly commited on
Commit
faa4567
Β·
verified Β·
1 Parent(s): b6f57ae

Update pages/πŸ“Έ_InstaMuse.py

Browse files
Files changed (1) hide show
  1. 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 = load_model()
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
- file = st.file_uploader(
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
- with st.spinner(r'#### :sparkles: :sparkles: Generating... please wait :hourglass_flowing_sand:'):
73
- st.session_state['file'] = file
 
 
 
 
 
 
74
 
75
- with st.container():
76
- col1, col2 = st.columns(2)
 
 
77
 
78
- with col1:
79
- st.markdown("## πŸ“Έ Your Image:")
80
- st.image(image, caption='Uploaded Image', use_column_width=True)
81
- try:
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
- except Exception as e:
88
- st.error(f"Error generating captions: {e}")
89
- captions = None
 
 
90
 
91
- with col2:
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
- # Action buttons with functionality
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
- if col2.button("πŸ”„ Regenerate"):
106
- with st.spinner('Regenerating captions...'):
 
 
 
 
 
 
 
 
 
 
107
  st.rerun()
108
 
109
- if col3.button("✨ More Hashtags"):
110
- with st.spinner('Generating hashtags...'):
111
- try:
112
- hashtags = caption_generator.caption_2_hashtag(st.session_state['img_description'])
113
- except Exception as e:
114
- st.error(f"Error generating hashtags: {e}")
115
- hashtags = None
116
- st.write("### Generated Hashtags:")
117
- st.write(f"**{hashtags}**")
118
-
119
- if col4.button(":window: Clear Screen"):
120
- st.session_state['file'] = None
121
- st.session_state['captions'] = ''
122
- st.session_state['caption_list'] = ''
123
- st.session_state['img_description'] = ''
124
 
125
  with st.expander("Need help?"):
126
- st.write("Please contact us at [email](mailto:[email protected])")
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.")