MANIKANDAN A commited on
Commit
083d065
·
1 Parent(s): 7f7a61c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -40
app.py CHANGED
@@ -100,22 +100,58 @@ def login_section():
100
 
101
 
102
 
103
- def predict(cap_col):
104
  captions = []
105
  pred_caption = generate_caption('tmp.jpg', caption_model)
106
 
107
  cap_col.markdown('#### Predicted Captions:')
108
- captions.append(pred_caption)
 
109
 
110
  for _ in range(4):
111
  pred_caption = generate_caption('tmp.jpg', caption_model, add_noise=True)
112
  if pred_caption not in captions:
113
- captions.append(pred_caption)
 
114
 
115
  cap_col.markdown('<div class="caption-container">', unsafe_allow_html=True)
116
  for c in captions:
117
  cap_col.markdown(f'<div class="cap-line" style="color: black; background-color: light grey; padding: 5px; margin-bottom: 5px; font-family: \'Palatino Linotype\', \'Book Antiqua\', Palatino, serif;">{c}</div>', unsafe_allow_html=True)
118
  cap_col.markdown('</div>', unsafe_allow_html=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
119
 
120
 
121
 
@@ -135,45 +171,9 @@ def main():
135
  elif selected_tab == "Login":
136
  login_section()
137
  elif selected_tab == "Generate Caption":
138
- # Check if a user is logged in before accessing the caption generation feature
139
- if hasattr(st.session_state, "username"):
140
- st.markdown(f"<p style='{heading_style}'>Generate Caption</p>", unsafe_allow_html=True)
141
- st.markdown("Upload an image to generate a caption:")
142
-
143
- with st.sidebar:
144
- st.title("Options")
145
- selected_languages = st.multiselect("Select languages for translation:", ['en', 'ta', 'hi', 'zh-cn', 'es', 'fr', 'de', 'it', 'ja'])
146
- img_url = st.text_input("Enter Image URL:")
147
- img_upload = st.file_uploader("Upload Image:", type=['jpg', 'png', 'jpeg'])
148
-
149
- col1, col2 = st.columns(2)
150
- if img_url or img_upload:
151
- if img_url:
152
- img = Image.open(requests.get(img_url, stream=True).raw)
153
- else:
154
- img = Image.open(img_upload)
155
-
156
- img = img.convert('RGB')
157
- col1.image(img, caption="Input Image", use_column_width=True)
158
- img.save('tmp.jpg')
159
- predict(col2)
160
-
161
- for lang in selected_languages:
162
- if lang != "en":
163
- translated_caption = translator.translate(generate_caption, src="en", dest=lang)
164
- st.markdown(f"<p style='font-size: 24px; font-weight: bold; margin-bottom: 20px;'>{lang.upper()} Translation:</p>", unsafe_allow_html=True)
165
- st.write(translated_caption.text)
166
-
167
- # Update the caption in the database
168
-
169
-
170
-
171
-
172
  else:
173
  st.write("Please login to access this feature.")
174
 
175
-
176
-
177
-
178
  if __name__ == "__main__":
179
  main()
 
100
 
101
 
102
 
103
+ def predict():
104
  captions = []
105
  pred_caption = generate_caption('tmp.jpg', caption_model)
106
 
107
  cap_col.markdown('#### Predicted Captions:')
108
+ translated_caption = translate_caption(pred_caption, target_language)
109
+ captions.append(translated_caption)
110
 
111
  for _ in range(4):
112
  pred_caption = generate_caption('tmp.jpg', caption_model, add_noise=True)
113
  if pred_caption not in captions:
114
+ translated_caption = translate_caption(pred_caption, target_language)
115
+ captions.append(translated_caption)
116
 
117
  cap_col.markdown('<div class="caption-container">', unsafe_allow_html=True)
118
  for c in captions:
119
  cap_col.markdown(f'<div class="cap-line" style="color: black; background-color: light grey; padding: 5px; margin-bottom: 5px; font-family: \'Palatino Linotype\', \'Book Antiqua\', Palatino, serif;">{c}</div>', unsafe_allow_html=True)
120
  cap_col.markdown('</div>', unsafe_allow_html=True)
121
+
122
+ st.markdown('<h1 style="text-align:center; font-family:Arial; width:fit-content; font-size:3em; color:black; text-shadow: 2px 2px 4px #000000;">IMAGE CAPTION GENERATOR</h1>', unsafe_allow_html=True)
123
+ col1, col2 = st.columns(2)
124
+
125
+ # Image URL input
126
+ img_url = st.text_input(label='Enter Image URL')
127
+
128
+ # Image upload input
129
+ img_upload = st.file_uploader(label='Upload Image', type=['jpg', 'png', 'jpeg'])
130
+
131
+ # Language selection dropdown
132
+ target_language = st.selectbox('Select Target Language', ['en', 'ta', 'hi', 'es', 'fr', 'zh-cn'], index=0)
133
+
134
+ # Process image and generate captions
135
+ if img_url:
136
+ img = Image.open(requests.get(img_url, stream=True).raw)
137
+ img = img.convert('RGB')
138
+ col1.image(img, caption="Input Image", use_column_width=True)
139
+ img.save('tmp.jpg')
140
+ predict(col2)
141
+
142
+ st.markdown('<center style="opacity: 70%">OR</center>', unsafe_allow_html=True)
143
+
144
+ elif img_upload:
145
+ img = img_upload.read()
146
+ img = Image.open(io.BytesIO(img))
147
+ img = img.convert('RGB')
148
+ col1.image(img, caption="Input Image", use_column_width=True)
149
+ img.save('tmp.jpg')
150
+ predict(col2)
151
+
152
+ # Remove temporary image file
153
+ if img_url or img_upload:
154
+ os.remove('tmp.jpg')
155
 
156
 
157
 
 
171
  elif selected_tab == "Login":
172
  login_section()
173
  elif selected_tab == "Generate Caption":
174
+ predict()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
175
  else:
176
  st.write("Please login to access this feature.")
177
 
 
 
 
178
  if __name__ == "__main__":
179
  main()