MANIKANDAN A
commited on
Commit
·
fec7940
1
Parent(s):
56a534d
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,6 +1,5 @@
|
|
| 1 |
import sqlite3
|
| 2 |
import streamlit as st
|
| 3 |
-
from passlib.hash import bcrypt
|
| 4 |
from PIL import Image
|
| 5 |
from model import get_caption_model, generate_caption
|
| 6 |
from googletrans import Translator
|
|
@@ -12,8 +11,18 @@ st.set_page_config(page_title="Image Caption Generator", layout="wide")
|
|
| 12 |
# Initialize Translator
|
| 13 |
translator = Translator()
|
| 14 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
# Function to create the SQLite table if it doesn't exist
|
| 16 |
-
@st.
|
| 17 |
def create_table():
|
| 18 |
with sqlite3.connect("login.db") as conn:
|
| 19 |
cursor = conn.cursor()
|
|
@@ -27,41 +36,43 @@ def create_table():
|
|
| 27 |
)
|
| 28 |
''')
|
| 29 |
|
| 30 |
-
# Function
|
| 31 |
-
def
|
| 32 |
-
st.
|
|
|
|
| 33 |
st.markdown("<p style='text-align: center;'>Please fill in the details to sign up:</p>", unsafe_allow_html=True)
|
| 34 |
-
|
| 35 |
-
new_username = st.text_input("New Username")
|
| 36 |
-
new_password = st.text_input("New Password", type="password")
|
| 37 |
-
new_email = st.text_input("Email")
|
| 38 |
|
| 39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
if not new_username or not new_password or not new_email:
|
| 41 |
st.error("All fields are required for signup.")
|
| 42 |
return
|
| 43 |
|
| 44 |
role = "user"
|
| 45 |
-
hashed_password = new_password
|
| 46 |
|
| 47 |
try:
|
| 48 |
with sqlite3.connect("login.db") as conn:
|
| 49 |
cursor = conn.cursor()
|
| 50 |
cursor.execute("INSERT INTO users (username, password, email, role) VALUES (?, ?, ?, ?)",
|
| 51 |
-
(new_username,
|
| 52 |
-
st.success(
|
|
|
|
| 53 |
except sqlite3.IntegrityError:
|
| 54 |
-
st.error(
|
| 55 |
|
| 56 |
-
# Function
|
| 57 |
-
def
|
| 58 |
-
st.
|
|
|
|
| 59 |
st.markdown("<p style='text-align: center;'>Please enter your login details:</p>", unsafe_allow_html=True)
|
| 60 |
-
|
| 61 |
-
username = st.text_input("Username")
|
| 62 |
-
password = st.text_input("Password", type="password")
|
| 63 |
|
| 64 |
-
|
|
|
|
|
|
|
|
|
|
| 65 |
if not username or not password:
|
| 66 |
st.error("Username and password are required for login.")
|
| 67 |
return
|
|
@@ -72,83 +83,91 @@ def login():
|
|
| 72 |
cursor.execute("SELECT * FROM users WHERE username = ?", (username,))
|
| 73 |
user = cursor.fetchone()
|
| 74 |
|
| 75 |
-
if user and password:
|
| 76 |
-
st.success(
|
| 77 |
st.write(f"You are logged in as: {user[1]}")
|
|
|
|
|
|
|
| 78 |
st.session_state.username = username
|
| 79 |
st.session_state.selected_tab = "Generate Caption"
|
|
|
|
| 80 |
else:
|
| 81 |
-
st.error(
|
| 82 |
except sqlite3.OperationalError as e:
|
| 83 |
st.error(f"An error occurred while trying to log in: {e}")
|
| 84 |
|
| 85 |
-
# Function to generate image caption and edit captions
|
| 86 |
-
@st.cache_resource
|
| 87 |
-
def generate_image_caption(img, caption_model):
|
| 88 |
-
return generate_caption(img, caption_model)
|
| 89 |
-
|
| 90 |
-
def display_edit_caption(selected_languages, generated_caption):
|
| 91 |
-
st.markdown("<p style='font-size: 24px; font-weight: bold; margin-bottom: 20px;'>Edit Caption:</p>", unsafe_allow_html=True)
|
| 92 |
-
edited_caption = st.text_area("Edit the caption", value=generated_caption, key="edited_caption")
|
| 93 |
-
|
| 94 |
-
if edited_caption:
|
| 95 |
-
st.markdown("<p style='font-size: 24px; font-weight: bold; margin-bottom: 20px;'>Edited Caption:</p>", unsafe_allow_html=True)
|
| 96 |
-
st.write(edited_caption)
|
| 97 |
-
|
| 98 |
-
for lang in selected_languages:
|
| 99 |
-
if lang != "en":
|
| 100 |
-
translated_caption = translator.translate(edited_caption, src="en", dest=lang)
|
| 101 |
-
st.markdown(f"<p style='font-size: 24px; font-weight: bold; margin-bottom: 20px;'>{lang.upper()} Translation:</p>", unsafe_allow_html=True)
|
| 102 |
-
st.write(translated_caption.text)
|
| 103 |
-
|
| 104 |
-
username = st.session_state.username
|
| 105 |
-
st.balloons()
|
| 106 |
-
st.success("Caption editing complete!")
|
| 107 |
-
|
| 108 |
-
# Main function to control the application flow
|
| 109 |
def main():
|
|
|
|
| 110 |
create_table()
|
| 111 |
|
|
|
|
| 112 |
tabs = ["Signup", "Login", "Generate Caption"]
|
|
|
|
|
|
|
| 113 |
selected_tab = st.sidebar.selectbox("Navigation", tabs)
|
| 114 |
|
|
|
|
| 115 |
if selected_tab == "Signup":
|
| 116 |
-
|
| 117 |
elif selected_tab == "Login":
|
| 118 |
-
|
| 119 |
elif selected_tab == "Generate Caption":
|
|
|
|
| 120 |
if hasattr(st.session_state, "username"):
|
| 121 |
-
st.
|
| 122 |
-
st.
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
st.
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 150 |
else:
|
| 151 |
st.write("Please login to access this feature.")
|
| 152 |
|
| 153 |
if __name__ == "__main__":
|
| 154 |
-
main()
|
|
|
|
| 1 |
import sqlite3
|
| 2 |
import streamlit as st
|
|
|
|
| 3 |
from PIL import Image
|
| 4 |
from model import get_caption_model, generate_caption
|
| 5 |
from googletrans import Translator
|
|
|
|
| 11 |
# Initialize Translator
|
| 12 |
translator = Translator()
|
| 13 |
|
| 14 |
+
# Constants
|
| 15 |
+
SIGNUP_SUCCESS_MSG = "Signup successful! You can now login."
|
| 16 |
+
SIGNUP_ERROR_EXISTING_USER = "Username already exists. Please choose a different username."
|
| 17 |
+
LOGIN_SUCCESS_MSG = "Login successful!"
|
| 18 |
+
LOGIN_ERROR_INVALID_CREDENTIALS = "Login failed. Invalid username or password."
|
| 19 |
+
|
| 20 |
+
# Define CSS styles
|
| 21 |
+
heading_style = "font-size: 24px; font-weight: bold; text-align: center;"
|
| 22 |
+
input_style = "margin-top: 10px; padding: 5px; width: 100%;"
|
| 23 |
+
|
| 24 |
# Function to create the SQLite table if it doesn't exist
|
| 25 |
+
@st.cache(allow_output_mutation=True)
|
| 26 |
def create_table():
|
| 27 |
with sqlite3.connect("login.db") as conn:
|
| 28 |
cursor = conn.cursor()
|
|
|
|
| 36 |
)
|
| 37 |
''')
|
| 38 |
|
| 39 |
+
# Function for signup section
|
| 40 |
+
def signup_section():
|
| 41 |
+
st.title("Signup")
|
| 42 |
+
st.markdown(f"<p style='{heading_style}'>Signup</p>", unsafe_allow_html=True)
|
| 43 |
st.markdown("<p style='text-align: center;'>Please fill in the details to sign up:</p>", unsafe_allow_html=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
|
| 45 |
+
new_username = st.text_input("New Username", key="new_username", style=input_style, help="Choose a unique username")
|
| 46 |
+
new_password = st.text_input("New Password", type="password", key="new_password", style=input_style, help="Password should be at least 8 characters long")
|
| 47 |
+
new_email = st.text_input("Email", key="new_email", style=input_style, help="Enter a valid email address")
|
| 48 |
+
|
| 49 |
+
if st.button("Signup", style="margin-top: 10px;"):
|
| 50 |
if not new_username or not new_password or not new_email:
|
| 51 |
st.error("All fields are required for signup.")
|
| 52 |
return
|
| 53 |
|
| 54 |
role = "user"
|
|
|
|
| 55 |
|
| 56 |
try:
|
| 57 |
with sqlite3.connect("login.db") as conn:
|
| 58 |
cursor = conn.cursor()
|
| 59 |
cursor.execute("INSERT INTO users (username, password, email, role) VALUES (?, ?, ?, ?)",
|
| 60 |
+
(new_username, new_password, new_email, role))
|
| 61 |
+
st.success(SIGNUP_SUCCESS_MSG)
|
| 62 |
+
st.balloons()
|
| 63 |
except sqlite3.IntegrityError:
|
| 64 |
+
st.error(SIGNUP_ERROR_EXISTING_USER)
|
| 65 |
|
| 66 |
+
# Function for login section
|
| 67 |
+
def login_section():
|
| 68 |
+
st.title("Login")
|
| 69 |
+
st.markdown(f"<p style='{heading_style}'>Login</p>", unsafe_allow_html=True)
|
| 70 |
st.markdown("<p style='text-align: center;'>Please enter your login details:</p>", unsafe_allow_html=True)
|
|
|
|
|
|
|
|
|
|
| 71 |
|
| 72 |
+
username = st.text_input("Username", key="login_username", style=input_style, help="Enter your username")
|
| 73 |
+
password = st.text_input("Password", type="password", key="login_password", style=input_style, help="Enter your password")
|
| 74 |
+
|
| 75 |
+
if st.button("Login", style="margin-top: 10px;"):
|
| 76 |
if not username or not password:
|
| 77 |
st.error("Username and password are required for login.")
|
| 78 |
return
|
|
|
|
| 83 |
cursor.execute("SELECT * FROM users WHERE username = ?", (username,))
|
| 84 |
user = cursor.fetchone()
|
| 85 |
|
| 86 |
+
if user and user[2] == password:
|
| 87 |
+
st.success(LOGIN_SUCCESS_MSG)
|
| 88 |
st.write(f"You are logged in as: {user[1]}")
|
| 89 |
+
st.image("profile_image_placeholder.jpg", caption="Your Profile Image", width=100)
|
| 90 |
+
|
| 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 while trying to log in: {e}")
|
| 98 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 99 |
def main():
|
| 100 |
+
# Create the database table if it doesn't exist
|
| 101 |
create_table()
|
| 102 |
|
| 103 |
+
# Define the navigation tabs
|
| 104 |
tabs = ["Signup", "Login", "Generate Caption"]
|
| 105 |
+
|
| 106 |
+
# Select the active tab based on user input
|
| 107 |
selected_tab = st.sidebar.selectbox("Navigation", tabs)
|
| 108 |
|
| 109 |
+
# Route to the appropriate section based on the selected tab
|
| 110 |
if selected_tab == "Signup":
|
| 111 |
+
signup_section()
|
| 112 |
elif selected_tab == "Login":
|
| 113 |
+
login_section()
|
| 114 |
elif selected_tab == "Generate Caption":
|
| 115 |
+
# Check if a user is logged in before accessing the caption generation feature
|
| 116 |
if hasattr(st.session_state, "username"):
|
| 117 |
+
st.title("Generate Caption")
|
| 118 |
+
st.markdown("Upload an image to generate a caption:")
|
| 119 |
+
|
| 120 |
+
with st.sidebar:
|
| 121 |
+
st.title("Options")
|
| 122 |
+
selected_languages = st.multiselect("Select languages for translation:", ['en', 'ta', 'hi', 'zh-cn', 'es', 'fr', 'de', 'it', 'ja'])
|
| 123 |
+
img_url = st.text_input("Enter Image URL:")
|
| 124 |
+
img_upload = st.file_uploader("Upload Image:", type=['jpg', 'png', 'jpeg'])
|
| 125 |
+
|
| 126 |
+
col1, col2 = st.columns([2, 3])
|
| 127 |
+
|
| 128 |
+
if img_url or img_upload:
|
| 129 |
+
if img_url:
|
| 130 |
+
img = Image.open(requests.get(img_url, stream=True).raw)
|
| 131 |
+
else:
|
| 132 |
+
img = Image.open(img_upload)
|
| 133 |
+
|
| 134 |
+
img = img.convert('RGB')
|
| 135 |
+
col1.image(img, caption="Input Image", use_column_width=True)
|
| 136 |
+
|
| 137 |
+
caption_model = get_caption_model()
|
| 138 |
+
generated_caption = generate_caption(img, caption_model)
|
| 139 |
+
|
| 140 |
+
if generated_caption:
|
| 141 |
+
col2.markdown('<div style="margin-top: 15px; padding: 10px; background-color: #e6f7ff; border-radius: 5px;">' + generated_caption + '</div>', unsafe_allow_html=True)
|
| 142 |
+
else:
|
| 143 |
+
col2.markdown('<div style="margin-top: 15px; padding: 10px; background-color: #e6f7ff; border-radius: 5px;">Caption generation failed.</div>', unsafe_allow_html=True)
|
| 144 |
+
|
| 145 |
+
if generated_caption:
|
| 146 |
+
st.markdown("<p style='font-size: 24px; font-weight: bold; margin-bottom: 20px;'>Generated Caption:</p>", unsafe_allow_html=True)
|
| 147 |
+
st.write(generated_caption)
|
| 148 |
+
|
| 149 |
+
if "en" in selected_languages:
|
| 150 |
+
st.markdown("<p style='font-size: 24px; font-weight: bold; margin-bottom: 20px;'>Edit Caption:</p>", unsafe_allow_html=True)
|
| 151 |
+
edited_caption = st.text_area("Edit the caption", value=generated_caption)
|
| 152 |
+
|
| 153 |
+
if edited_caption:
|
| 154 |
+
st.markdown("<p style='font-size: 24px; font-weight: bold; margin-bottom: 20px;'>Edited Caption:</p>", unsafe_allow_html=True)
|
| 155 |
+
st.write(edited_caption)
|
| 156 |
+
|
| 157 |
+
for lang in selected_languages:
|
| 158 |
+
if lang != "en":
|
| 159 |
+
translated_caption = translator.translate(edited_caption, src="en", dest=lang)
|
| 160 |
+
st.markdown(f"<p style='font-size: 24px; font-weight: bold; margin-bottom: 20px;'>{lang.upper()} Translation:</p>", unsafe_allow_html=True)
|
| 161 |
+
st.write(translated_caption.text)
|
| 162 |
+
|
| 163 |
+
username = st.session_state.username
|
| 164 |
+
update_caption(username, edited_caption) # Update the caption in the database
|
| 165 |
+
|
| 166 |
+
st.success("Caption updated and saved successfully!")
|
| 167 |
+
else:
|
| 168 |
+
st.info("Caption editing is only available for English language captions.")
|
| 169 |
else:
|
| 170 |
st.write("Please login to access this feature.")
|
| 171 |
|
| 172 |
if __name__ == "__main__":
|
| 173 |
+
main()
|