MANIKANDAN A commited on
Commit ·
db7d9e9
1
Parent(s): bd7b553
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
-
|
| 2 |
import os
|
| 3 |
import streamlit as st
|
| 4 |
import requests
|
|
@@ -19,7 +19,7 @@ def get_model():
|
|
| 19 |
caption_model = get_model()
|
| 20 |
|
| 21 |
# Constants
|
| 22 |
-
SIGNUP_SUCCESS_MSG = "
|
| 23 |
SIGNUP_ERROR_EXISTING_USER = "Username already exists. Please choose a different username."
|
| 24 |
LOGIN_SUCCESS_MSG = "Login successful!"
|
| 25 |
LOGIN_ERROR_INVALID_CREDENTIALS = "Login failed. Invalid username or password."
|
|
@@ -42,18 +42,19 @@ def create_table():
|
|
| 42 |
role TEXT NOT NULL
|
| 43 |
)
|
| 44 |
''')
|
| 45 |
-
|
| 46 |
-
#
|
| 47 |
def signup_section():
|
| 48 |
-
|
|
|
|
| 49 |
|
| 50 |
new_username = st.text_input("New Username", key="new_username", help="Choose a unique username")
|
| 51 |
new_password = st.text_input("New Password", type="password", key="new_password", help="Password should be at least 8 characters long")
|
| 52 |
new_email = st.text_input("Email", key="new_email", help="Enter a valid email address")
|
| 53 |
|
| 54 |
-
if st.button("
|
| 55 |
if not new_username or not new_password or not new_email:
|
| 56 |
-
st.error("All fields are required for
|
| 57 |
return
|
| 58 |
|
| 59 |
role = "user"
|
|
@@ -68,8 +69,9 @@ def signup_section():
|
|
| 68 |
except sqlite3.IntegrityError:
|
| 69 |
st.error(SIGNUP_ERROR_EXISTING_USER)
|
| 70 |
|
| 71 |
-
#
|
| 72 |
def login_section():
|
|
|
|
| 73 |
st.markdown(f"<p style='{heading_style}'>Login</p>", unsafe_allow_html=True)
|
| 74 |
username = st.text_input("Username", key="login_username", help="Enter your username")
|
| 75 |
password = st.text_input("Password", type="password", key="login_password",help="Enter your password")
|
|
@@ -87,16 +89,17 @@ def login_section():
|
|
| 87 |
|
| 88 |
if user and user[2] == password:
|
| 89 |
st.success(LOGIN_SUCCESS_MSG)
|
| 90 |
-
st.write(f"
|
| 91 |
st.session_state.username = username
|
| 92 |
st.session_state.selected_tab = "Generate Caption"
|
| 93 |
st.balloons()
|
| 94 |
else:
|
| 95 |
st.error(LOGIN_ERROR_INVALID_CREDENTIALS)
|
| 96 |
except sqlite3.OperationalError as e:
|
| 97 |
-
st.error(f"An error occurred
|
|
|
|
|
|
|
| 98 |
|
| 99 |
-
# Predict the caption
|
| 100 |
def predict(cap_col):
|
| 101 |
captions = []
|
| 102 |
pred_caption = generate_caption('tmp.jpg', caption_model)
|
|
@@ -113,20 +116,21 @@ def predict(cap_col):
|
|
| 113 |
for c in captions:
|
| 114 |
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)
|
| 115 |
cap_col.markdown('</div>', unsafe_allow_html=True)
|
|
|
|
|
|
|
| 116 |
|
| 117 |
-
# Main function
|
| 118 |
def main():
|
| 119 |
# Create the database table if it doesn't exist
|
| 120 |
create_table()
|
| 121 |
|
| 122 |
# Define the navigation tabs
|
| 123 |
-
tabs = ["
|
| 124 |
|
| 125 |
# Select the active tab based on user input
|
| 126 |
selected_tab = st.sidebar.selectbox("Navigation", tabs)
|
| 127 |
|
| 128 |
# Route to the appropriate section based on the selected tab
|
| 129 |
-
if selected_tab == "
|
| 130 |
signup_section()
|
| 131 |
elif selected_tab == "Login":
|
| 132 |
login_section()
|
|
@@ -143,7 +147,7 @@ def main():
|
|
| 143 |
img_upload = st.file_uploader("Upload Image:", type=['jpg', 'png', 'jpeg'])
|
| 144 |
|
| 145 |
col1, col2 = st.columns(2)
|
| 146 |
-
if
|
| 147 |
if img_url:
|
| 148 |
img = Image.open(requests.get(img_url, stream=True).raw)
|
| 149 |
else:
|
|
@@ -154,19 +158,41 @@ def main():
|
|
| 154 |
img.save('tmp.jpg')
|
| 155 |
predict(col2)
|
| 156 |
|
| 157 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 158 |
|
| 159 |
-
|
| 160 |
-
|
|
|
|
| 161 |
|
| 162 |
-
|
|
|
|
|
|
|
| 163 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 164 |
else:
|
| 165 |
-
st.write("Please
|
| 166 |
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
os.remove('tmp.jpg')
|
| 170 |
|
| 171 |
if __name__ == "__main__":
|
| 172 |
-
main()
|
|
|
|
| 1 |
+
mport io
|
| 2 |
import os
|
| 3 |
import streamlit as st
|
| 4 |
import requests
|
|
|
|
| 19 |
caption_model = get_model()
|
| 20 |
|
| 21 |
# Constants
|
| 22 |
+
SIGNUP_SUCCESS_MSG = "Signup successful! You can now login."
|
| 23 |
SIGNUP_ERROR_EXISTING_USER = "Username already exists. Please choose a different username."
|
| 24 |
LOGIN_SUCCESS_MSG = "Login successful!"
|
| 25 |
LOGIN_ERROR_INVALID_CREDENTIALS = "Login failed. Invalid username or password."
|
|
|
|
| 42 |
role TEXT NOT NULL
|
| 43 |
)
|
| 44 |
''')
|
| 45 |
+
|
| 46 |
+
# Function for signup section
|
| 47 |
def signup_section():
|
| 48 |
+
|
| 49 |
+
st.markdown(f"<p style='{heading_style}'>Signup</p>", unsafe_allow_html=True)
|
| 50 |
|
| 51 |
new_username = st.text_input("New Username", key="new_username", help="Choose a unique username")
|
| 52 |
new_password = st.text_input("New Password", type="password", key="new_password", help="Password should be at least 8 characters long")
|
| 53 |
new_email = st.text_input("Email", key="new_email", help="Enter a valid email address")
|
| 54 |
|
| 55 |
+
if st.button("Signup"):
|
| 56 |
if not new_username or not new_password or not new_email:
|
| 57 |
+
st.error("All fields are required for signup.")
|
| 58 |
return
|
| 59 |
|
| 60 |
role = "user"
|
|
|
|
| 69 |
except sqlite3.IntegrityError:
|
| 70 |
st.error(SIGNUP_ERROR_EXISTING_USER)
|
| 71 |
|
| 72 |
+
# Function for login section
|
| 73 |
def login_section():
|
| 74 |
+
|
| 75 |
st.markdown(f"<p style='{heading_style}'>Login</p>", unsafe_allow_html=True)
|
| 76 |
username = st.text_input("Username", key="login_username", help="Enter your username")
|
| 77 |
password = st.text_input("Password", type="password", key="login_password",help="Enter your password")
|
|
|
|
| 89 |
|
| 90 |
if user and user[2] == password:
|
| 91 |
st.success(LOGIN_SUCCESS_MSG)
|
| 92 |
+
st.write(f"You are logged in as: {user[1]}")
|
| 93 |
st.session_state.username = username
|
| 94 |
st.session_state.selected_tab = "Generate Caption"
|
| 95 |
st.balloons()
|
| 96 |
else:
|
| 97 |
st.error(LOGIN_ERROR_INVALID_CREDENTIALS)
|
| 98 |
except sqlite3.OperationalError as e:
|
| 99 |
+
st.error(f"An error occurred while trying to log in: {e}")
|
| 100 |
+
|
| 101 |
+
|
| 102 |
|
|
|
|
| 103 |
def predict(cap_col):
|
| 104 |
captions = []
|
| 105 |
pred_caption = generate_caption('tmp.jpg', caption_model)
|
|
|
|
| 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 |
|
|
|
|
| 122 |
def main():
|
| 123 |
# Create the database table if it doesn't exist
|
| 124 |
create_table()
|
| 125 |
|
| 126 |
# Define the navigation tabs
|
| 127 |
+
tabs = ["Signup", "Login", "Generate Caption"]
|
| 128 |
|
| 129 |
# Select the active tab based on user input
|
| 130 |
selected_tab = st.sidebar.selectbox("Navigation", tabs)
|
| 131 |
|
| 132 |
# Route to the appropriate section based on the selected tab
|
| 133 |
+
if selected_tab == "Signup":
|
| 134 |
signup_section()
|
| 135 |
elif selected_tab == "Login":
|
| 136 |
login_section()
|
|
|
|
| 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:
|
|
|
|
| 158 |
img.save('tmp.jpg')
|
| 159 |
predict(col2)
|
| 160 |
|
| 161 |
+
if generated_caption:
|
| 162 |
+
col2.markdown('<div style="margin-top: 15px; padding: 10px; background-color: #e6f7ff; border-radius: 5px;">' + generated_caption + '</div>', unsafe_allow_html=True)
|
| 163 |
+
else:
|
| 164 |
+
col2.markdown('<div style="margin-top: 15px; padding: 10px; background-color: #e6f7ff; border-radius: 5px;">Caption generation failed.</div>', unsafe_allow_html=True)
|
| 165 |
+
|
| 166 |
+
if generated_caption:
|
| 167 |
+
st.markdown("<p style='font-size: 24px; font-weight: bold; margin-bottom: 20px;'>Generated Caption:</p>", unsafe_allow_html=True)
|
| 168 |
+
st.write(generated_caption)
|
| 169 |
|
| 170 |
+
if "en" in selected_languages:
|
| 171 |
+
st.markdown("<p style='font-size: 24px; font-weight: bold; margin-bottom: 20px;'>Edit Caption:</p>", unsafe_allow_html=True)
|
| 172 |
+
edited_caption = st.text_area("Edit the caption", value=generated_caption)
|
| 173 |
|
| 174 |
+
if edited_caption:
|
| 175 |
+
st.markdown("<p style='font-size: 24px; font-weight: bold; margin-bottom: 20px;'>Edited Caption:</p>", unsafe_allow_html=True)
|
| 176 |
+
st.write(edited_caption)
|
| 177 |
|
| 178 |
+
for lang in selected_languages:
|
| 179 |
+
if lang != "en":
|
| 180 |
+
translated_caption = translator.translate(edited_caption, src="en", dest=lang)
|
| 181 |
+
st.markdown(f"<p style='font-size: 24px; font-weight: bold; margin-bottom: 20px;'>{lang.upper()} Translation:</p>", unsafe_allow_html=True)
|
| 182 |
+
st.write(translated_caption.text)
|
| 183 |
+
|
| 184 |
+
username = st.session_state.username
|
| 185 |
+
update_caption(username, edited_caption) # Update the caption in the database
|
| 186 |
+
|
| 187 |
+
st.success("Caption updated and saved successfully!")
|
| 188 |
+
os.remove('tmp.jpg')
|
| 189 |
+
else:
|
| 190 |
+
st.info("Caption editing is only available for English language captions.")
|
| 191 |
else:
|
| 192 |
+
st.write("Please login to access this feature.")
|
| 193 |
|
| 194 |
+
|
| 195 |
+
|
|
|
|
| 196 |
|
| 197 |
if __name__ == "__main__":
|
| 198 |
+
main()
|