MANIKANDAN A
commited on
Commit
·
7f1c645
1
Parent(s):
cf11161
Update app.py
Browse files
app.py
CHANGED
|
@@ -18,8 +18,6 @@ def get_model():
|
|
| 18 |
|
| 19 |
caption_model = get_model()
|
| 20 |
|
| 21 |
-
|
| 22 |
-
|
| 23 |
# Constants
|
| 24 |
SIGNUP_SUCCESS_MSG = "Signup successful! You can now login."
|
| 25 |
SIGNUP_ERROR_EXISTING_USER = "Username already exists. Please choose a different username."
|
|
@@ -44,12 +42,10 @@ def create_table():
|
|
| 44 |
role TEXT NOT NULL
|
| 45 |
)
|
| 46 |
''')
|
| 47 |
-
|
| 48 |
# Function for signup section
|
| 49 |
def signup_section():
|
| 50 |
-
|
| 51 |
st.markdown(f"<p style='{heading_style}'>Signup</p>", unsafe_allow_html=True)
|
| 52 |
-
|
| 53 |
new_username = st.text_input("New Username", key="new_username", help="Choose a unique username")
|
| 54 |
new_password = st.text_input("New Password", type="password", key="new_password", help="Password should be at least 8 characters long")
|
| 55 |
new_email = st.text_input("Email", key="new_email", help="Enter a valid email address")
|
|
@@ -68,12 +64,13 @@ def signup_section():
|
|
| 68 |
(new_username, new_password, new_email, role))
|
| 69 |
st.success(SIGNUP_SUCCESS_MSG)
|
| 70 |
st.balloons()
|
|
|
|
|
|
|
| 71 |
except sqlite3.IntegrityError:
|
| 72 |
st.error(SIGNUP_ERROR_EXISTING_USER)
|
| 73 |
|
| 74 |
# Function for login section
|
| 75 |
def login_section():
|
| 76 |
-
|
| 77 |
st.markdown(f"<p style='{heading_style}'>Login</p>", unsafe_allow_html=True)
|
| 78 |
username = st.text_input("Username", key="login_username", help="Enter your username")
|
| 79 |
password = st.text_input("Password", type="password", key="login_password",help="Enter your password")
|
|
@@ -82,7 +79,7 @@ def login_section():
|
|
| 82 |
if not username or not password:
|
| 83 |
st.error("Username and password are required for login.")
|
| 84 |
return
|
| 85 |
-
|
| 86 |
try:
|
| 87 |
with sqlite3.connect("login.db") as conn:
|
| 88 |
cursor = conn.cursor()
|
|
@@ -95,16 +92,17 @@ def login_section():
|
|
| 95 |
st.session_state.username = username
|
| 96 |
st.session_state.selected_tab = "Generate Caption"
|
| 97 |
st.balloons()
|
|
|
|
|
|
|
| 98 |
else:
|
| 99 |
st.error(LOGIN_ERROR_INVALID_CREDENTIALS)
|
| 100 |
except sqlite3.OperationalError as e:
|
| 101 |
-
st.error(f"An error occurred while trying to log in: {e}")
|
| 102 |
-
|
| 103 |
|
| 104 |
def translate_caption(caption, target_language):
|
| 105 |
translated = translator.translate(caption, dest=target_language)
|
| 106 |
return translated.text
|
| 107 |
-
|
| 108 |
def predict(cap_col):
|
| 109 |
captions = []
|
| 110 |
pred_caption = generate_caption('tmp.jpg', caption_model)
|
|
@@ -118,12 +116,46 @@ def predict(cap_col):
|
|
| 118 |
if pred_caption not in captions:
|
| 119 |
translated_caption = translate_caption(pred_caption, target_language)
|
| 120 |
captions.append(translated_caption)
|
| 121 |
-
|
| 122 |
cap_col.markdown('<div class="caption-container">', unsafe_allow_html=True)
|
| 123 |
for c in captions:
|
| 124 |
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)
|
| 125 |
cap_col.markdown('</div>', unsafe_allow_html=True)
|
| 126 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 127 |
|
| 128 |
def main():
|
| 129 |
# Create the database table if it doesn't exist
|
|
@@ -141,42 +173,11 @@ def main():
|
|
| 141 |
elif selected_tab == "Login":
|
| 142 |
login_section()
|
| 143 |
elif selected_tab == "Generate Caption":
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
# Image upload input
|
| 151 |
-
img_upload = st.file_uploader(label='Upload Image', type=['jpg', 'png', 'jpeg'])
|
| 152 |
-
|
| 153 |
-
# Language selection dropdown
|
| 154 |
-
target_language = st.selectbox('Select Target Language', ['en', 'ta', 'hi', 'es', 'fr', 'zh-cn'], index=0)
|
| 155 |
-
|
| 156 |
-
# Process image and generate captions
|
| 157 |
-
if img_url:
|
| 158 |
-
img = Image.open(requests.get(img_url, stream=True).raw)
|
| 159 |
-
img = img.convert('RGB')
|
| 160 |
-
col1.image(img, caption="Input Image", use_column_width=True)
|
| 161 |
-
img.save('tmp.jpg')
|
| 162 |
-
predict(col2)
|
| 163 |
-
|
| 164 |
-
st.markdown('<center style="opacity: 70%">OR</center>', unsafe_allow_html=True)
|
| 165 |
-
|
| 166 |
-
elif img_upload:
|
| 167 |
-
img = img_upload.read()
|
| 168 |
-
img = Image.open(io.BytesIO(img))
|
| 169 |
-
img = img.convert('RGB')
|
| 170 |
-
col1.image(img, caption="Input Image", use_column_width=True)
|
| 171 |
-
img.save('tmp.jpg')
|
| 172 |
-
predict(col2)
|
| 173 |
-
|
| 174 |
-
# Remove temporary image file
|
| 175 |
-
if img_url or img_upload:
|
| 176 |
-
os.remove('tmp.jpg')
|
| 177 |
-
|
| 178 |
-
else:
|
| 179 |
-
st.write("Please login to access this feature.")
|
| 180 |
|
| 181 |
if __name__ == "__main__":
|
| 182 |
-
main()
|
|
|
|
| 18 |
|
| 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."
|
|
|
|
| 42 |
role TEXT NOT NULL
|
| 43 |
)
|
| 44 |
''')
|
| 45 |
+
|
| 46 |
# Function for signup section
|
| 47 |
def signup_section():
|
|
|
|
| 48 |
st.markdown(f"<p style='{heading_style}'>Signup</p>", unsafe_allow_html=True)
|
|
|
|
| 49 |
new_username = st.text_input("New Username", key="new_username", help="Choose a unique username")
|
| 50 |
new_password = st.text_input("New Password", type="password", key="new_password", help="Password should be at least 8 characters long")
|
| 51 |
new_email = st.text_input("Email", key="new_email", help="Enter a valid email address")
|
|
|
|
| 64 |
(new_username, new_password, new_email, role))
|
| 65 |
st.success(SIGNUP_SUCCESS_MSG)
|
| 66 |
st.balloons()
|
| 67 |
+
# After successful signup, direct the user to the login page
|
| 68 |
+
login_section()
|
| 69 |
except sqlite3.IntegrityError:
|
| 70 |
st.error(SIGNUP_ERROR_EXISTING_USER)
|
| 71 |
|
| 72 |
# Function for login section
|
| 73 |
def login_section():
|
|
|
|
| 74 |
st.markdown(f"<p style='{heading_style}'>Login</p>", unsafe_allow_html=True)
|
| 75 |
username = st.text_input("Username", key="login_username", help="Enter your username")
|
| 76 |
password = st.text_input("Password", type="password", key="login_password",help="Enter your password")
|
|
|
|
| 79 |
if not username or not password:
|
| 80 |
st.error("Username and password are required for login.")
|
| 81 |
return
|
| 82 |
+
|
| 83 |
try:
|
| 84 |
with sqlite3.connect("login.db") as conn:
|
| 85 |
cursor = conn.cursor()
|
|
|
|
| 92 |
st.session_state.username = username
|
| 93 |
st.session_state.selected_tab = "Generate Caption"
|
| 94 |
st.balloons()
|
| 95 |
+
# After successful login, direct the user to the image caption prediction function
|
| 96 |
+
generate_caption_section()
|
| 97 |
else:
|
| 98 |
st.error(LOGIN_ERROR_INVALID_CREDENTIALS)
|
| 99 |
except sqlite3.OperationalError as e:
|
| 100 |
+
st.error(f"An error occurred while trying to log in: {e}")
|
|
|
|
| 101 |
|
| 102 |
def translate_caption(caption, target_language):
|
| 103 |
translated = translator.translate(caption, dest=target_language)
|
| 104 |
return translated.text
|
| 105 |
+
|
| 106 |
def predict(cap_col):
|
| 107 |
captions = []
|
| 108 |
pred_caption = generate_caption('tmp.jpg', caption_model)
|
|
|
|
| 116 |
if pred_caption not in captions:
|
| 117 |
translated_caption = translate_caption(pred_caption, target_language)
|
| 118 |
captions.append(translated_caption)
|
| 119 |
+
|
| 120 |
cap_col.markdown('<div class="caption-container">', unsafe_allow_html=True)
|
| 121 |
for c in captions:
|
| 122 |
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)
|
| 123 |
cap_col.markdown('</div>', unsafe_allow_html=True)
|
| 124 |
+
|
| 125 |
+
def generate_caption_section():
|
| 126 |
+
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)
|
| 127 |
+
col1, col2 = st.columns(2)
|
| 128 |
+
|
| 129 |
+
# Image URL input
|
| 130 |
+
img_url = st.text_input(label='Enter Image URL')
|
| 131 |
+
|
| 132 |
+
# Image upload input
|
| 133 |
+
img_upload = st.file_uploader(label='Upload Image', type=['jpg', 'png', 'jpeg'])
|
| 134 |
+
|
| 135 |
+
# Language selection dropdown
|
| 136 |
+
target_language = st.selectbox('Select Target Language', ['en', 'ta', 'hi', 'es', 'fr', 'zh-cn'], index=0)
|
| 137 |
+
|
| 138 |
+
# Process image and generate captions
|
| 139 |
+
if img_url:
|
| 140 |
+
img = Image.open(requests.get(img_url, stream=True).raw)
|
| 141 |
+
img = img.convert('RGB')
|
| 142 |
+
col1.image(img, caption="Input Image", use_column_width=True)
|
| 143 |
+
img.save('tmp.jpg')
|
| 144 |
+
predict(col2)
|
| 145 |
+
|
| 146 |
+
st.markdown('<center style="opacity: 70%">OR</center>', unsafe_allow_html=True)
|
| 147 |
+
|
| 148 |
+
elif img_upload:
|
| 149 |
+
img = img_upload.read()
|
| 150 |
+
img = Image.open(io.BytesIO(img))
|
| 151 |
+
img = img.convert('RGB')
|
| 152 |
+
col1.image(img, caption="Input Image", use_column_width=True)
|
| 153 |
+
img.save('tmp.jpg')
|
| 154 |
+
predict(col2)
|
| 155 |
+
|
| 156 |
+
# Remove temporary image file
|
| 157 |
+
if img_url or img_upload:
|
| 158 |
+
os.remove('tmp.jpg')
|
| 159 |
|
| 160 |
def main():
|
| 161 |
# Create the database table if it doesn't exist
|
|
|
|
| 173 |
elif selected_tab == "Login":
|
| 174 |
login_section()
|
| 175 |
elif selected_tab == "Generate Caption":
|
| 176 |
+
# Check if the user is logged in before allowing access to the image caption generation
|
| 177 |
+
if 'username' in st.session_state:
|
| 178 |
+
generate_caption_section()
|
| 179 |
+
else:
|
| 180 |
+
st.write("Please login to access this feature.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 181 |
|
| 182 |
if __name__ == "__main__":
|
| 183 |
+
main()
|