MANIKANDAN A
commited on
Commit
·
7d12ca3
1
Parent(s):
fd46d84
Update app.py
Browse files
app.py
CHANGED
|
@@ -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,19 +42,18 @@ def create_table():
|
|
| 42 |
role TEXT NOT NULL
|
| 43 |
)
|
| 44 |
''')
|
| 45 |
-
|
| 46 |
-
# Function for signup section
|
| 47 |
-
def signup_section():
|
| 48 |
|
| 49 |
-
|
|
|
|
|
|
|
| 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("
|
| 56 |
if not new_username or not new_password or not new_email:
|
| 57 |
-
st.error("All fields are required for
|
| 58 |
return
|
| 59 |
|
| 60 |
role = "user"
|
|
@@ -69,9 +68,8 @@ def signup_section():
|
|
| 69 |
except sqlite3.IntegrityError:
|
| 70 |
st.error(SIGNUP_ERROR_EXISTING_USER)
|
| 71 |
|
| 72 |
-
#
|
| 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,17 +87,16 @@ def login_section():
|
|
| 89 |
|
| 90 |
if user and user[2] == password:
|
| 91 |
st.success(LOGIN_SUCCESS_MSG)
|
| 92 |
-
st.write(f"
|
| 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
|
| 100 |
-
|
| 101 |
-
|
| 102 |
|
|
|
|
| 103 |
def predict(cap_col):
|
| 104 |
captions = []
|
| 105 |
pred_caption = generate_caption('tmp.jpg', caption_model)
|
|
@@ -116,21 +113,20 @@ def predict(cap_col):
|
|
| 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 = ["
|
| 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 == "
|
| 134 |
signup_section()
|
| 135 |
elif selected_tab == "Login":
|
| 136 |
login_section()
|
|
@@ -158,43 +154,19 @@ def main():
|
|
| 158 |
img.save('tmp.jpg')
|
| 159 |
predict(col2)
|
| 160 |
|
| 161 |
-
|
| 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 |
-
|
| 175 |
-
|
| 176 |
-
st.write(edited_caption)
|
| 177 |
|
| 178 |
-
|
| 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 |
-
else:
|
| 189 |
-
st.info("Caption editing is only available for English language captions.")
|
| 190 |
else:
|
| 191 |
-
st.write("Please
|
| 192 |
|
| 193 |
# Remove temporary image file
|
| 194 |
if img_url or img_upload:
|
| 195 |
-
|
| 196 |
|
| 197 |
if __name__ == "__main__":
|
| 198 |
-
main()
|
| 199 |
-
|
| 200 |
-
|
|
|
|
| 19 |
caption_model = get_model()
|
| 20 |
|
| 21 |
# Constants
|
| 22 |
+
SIGNUP_SUCCESS_MSG = "Registration successful! You can now log in."
|
| 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 |
+
# Signup section
|
| 47 |
+
def signup_section():
|
| 48 |
+
st.markdown(f"<p style='{heading_style}'>Register</p>", unsafe_allow_html=True)
|
| 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("Register"):
|
| 55 |
if not new_username or not new_password or not new_email:
|
| 56 |
+
st.error("All fields are required for registration.")
|
| 57 |
return
|
| 58 |
|
| 59 |
role = "user"
|
|
|
|
| 68 |
except sqlite3.IntegrityError:
|
| 69 |
st.error(SIGNUP_ERROR_EXISTING_USER)
|
| 70 |
|
| 71 |
+
# Login section
|
| 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 |
|
| 88 |
if user and user[2] == password:
|
| 89 |
st.success(LOGIN_SUCCESS_MSG)
|
| 90 |
+
st.write(f"Logged in as: {user[1]}")
|
| 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 during login: {e}")
|
|
|
|
|
|
|
| 98 |
|
| 99 |
+
# Predict the caption
|
| 100 |
def predict(cap_col):
|
| 101 |
captions = []
|
| 102 |
pred_caption = generate_caption('tmp.jpg', caption_model)
|
|
|
|
| 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 = ["Register", "Login", "Generate Caption"]
|
| 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 == "Register":
|
| 130 |
signup_section()
|
| 131 |
elif selected_tab == "Login":
|
| 132 |
login_section()
|
|
|
|
| 154 |
img.save('tmp.jpg')
|
| 155 |
predict(col2)
|
| 156 |
|
| 157 |
+
# Rest of the code for displaying the generated caption and translations, and editing the caption
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 158 |
|
| 159 |
+
st.markdown("<p style='font-size: 24px; font-weight: bold; margin-bottom: 20px;'>Generated Caption:</p>", unsafe_allow_html=True)
|
| 160 |
+
st.write(generated_caption)
|
|
|
|
| 161 |
|
| 162 |
+
# Rest of the code for displaying and editing the caption in different languages
|
|
|
|
|
|
|
|
|
|
|
|
|
| 163 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 164 |
else:
|
| 165 |
+
st.write("Please log in to access this feature.")
|
| 166 |
|
| 167 |
# Remove temporary image file
|
| 168 |
if img_url or img_upload:
|
| 169 |
+
os.remove('tmp.jpg')
|
| 170 |
|
| 171 |
if __name__ == "__main__":
|
| 172 |
+
main()
|
|
|
|
|
|