MANIKANDAN A
commited on
Commit
·
e5b1de1
1
Parent(s):
1beed06
Update app.py
Browse files
app.py
CHANGED
|
@@ -13,7 +13,7 @@ st.set_page_config(page_title="Image Caption Generator", layout="wide")
|
|
| 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()
|
|
@@ -33,9 +33,9 @@ def signup():
|
|
| 33 |
st.markdown("<p style='font-size: 24px; font-weight: bold; margin-bottom: 20px; text-align: center;'>Signup</p>", unsafe_allow_html=True)
|
| 34 |
st.markdown("<p style='text-align: center;'>Please fill in the details to sign up:</p>", unsafe_allow_html=True)
|
| 35 |
|
| 36 |
-
new_username = st.text_input("New Username"
|
| 37 |
-
new_password = st.text_input("New Password", type="password"
|
| 38 |
-
new_email = st.text_input("Email"
|
| 39 |
|
| 40 |
if st.button("Signup", style='margin-top: 10px;'):
|
| 41 |
if not new_username or not new_password or not new_email:
|
|
@@ -51,7 +51,6 @@ def signup():
|
|
| 51 |
cursor.execute("INSERT INTO users (username, password, email, role) VALUES (?, ?, ?, ?)",
|
| 52 |
(new_username, hashed_password, new_email, role))
|
| 53 |
st.success("Signup successful! You can now login.")
|
| 54 |
-
st.balloons()
|
| 55 |
except sqlite3.IntegrityError:
|
| 56 |
st.error("Username already exists. Please choose a different username.")
|
| 57 |
|
|
@@ -61,8 +60,8 @@ def login():
|
|
| 61 |
st.markdown("<p style='font-size: 24px; font-weight: bold; margin-bottom: 20px; text-align: center;'>Login</p>", unsafe_allow_html=True)
|
| 62 |
st.markdown("<p style='text-align: center;'>Please enter your login details:</p>", unsafe_allow_html=True)
|
| 63 |
|
| 64 |
-
username = st.text_input("Username"
|
| 65 |
-
password = st.text_input("Password", type="password"
|
| 66 |
|
| 67 |
if st.button("Login", style='margin-top: 10px;'):
|
| 68 |
if not username or not password:
|
|
@@ -82,13 +81,79 @@ def login():
|
|
| 82 |
|
| 83 |
st.session_state.username = username
|
| 84 |
st.session_state.selected_tab = "Generate Caption"
|
| 85 |
-
st.balloons()
|
| 86 |
else:
|
| 87 |
st.error("Login failed. Invalid username or password.")
|
| 88 |
except sqlite3.OperationalError as e:
|
| 89 |
st.error(f"An error occurred while trying to log in: {e}")
|
| 90 |
|
| 91 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
|
| 93 |
if __name__ == "__main__":
|
| 94 |
main()
|
|
|
|
| 13 |
translator = Translator()
|
| 14 |
|
| 15 |
# Function to create the SQLite table if it doesn't exist
|
| 16 |
+
@st.cache_resource
|
| 17 |
def create_table():
|
| 18 |
with sqlite3.connect("login.db") as conn:
|
| 19 |
cursor = conn.cursor()
|
|
|
|
| 33 |
st.markdown("<p style='font-size: 24px; font-weight: bold; margin-bottom: 20px; text-align: center;'>Signup</p>", unsafe_allow_html=True)
|
| 34 |
st.markdown("<p style='text-align: center;'>Please fill in the details to sign up:</p>", unsafe_allow_html=True)
|
| 35 |
|
| 36 |
+
new_username = st.text_input("New Username")
|
| 37 |
+
new_password = st.text_input("New Password", type="password")
|
| 38 |
+
new_email = st.text_input("Email")
|
| 39 |
|
| 40 |
if st.button("Signup", style='margin-top: 10px;'):
|
| 41 |
if not new_username or not new_password or not new_email:
|
|
|
|
| 51 |
cursor.execute("INSERT INTO users (username, password, email, role) VALUES (?, ?, ?, ?)",
|
| 52 |
(new_username, hashed_password, new_email, role))
|
| 53 |
st.success("Signup successful! You can now login.")
|
|
|
|
| 54 |
except sqlite3.IntegrityError:
|
| 55 |
st.error("Username already exists. Please choose a different username.")
|
| 56 |
|
|
|
|
| 60 |
st.markdown("<p style='font-size: 24px; font-weight: bold; margin-bottom: 20px; text-align: center;'>Login</p>", unsafe_allow_html=True)
|
| 61 |
st.markdown("<p style='text-align: center;'>Please enter your login details:</p>", unsafe_allow_html=True)
|
| 62 |
|
| 63 |
+
username = st.text_input("Username")
|
| 64 |
+
password = st.text_input("Password", type="password")
|
| 65 |
|
| 66 |
if st.button("Login", style='margin-top: 10px;'):
|
| 67 |
if not username or not password:
|
|
|
|
| 81 |
|
| 82 |
st.session_state.username = username
|
| 83 |
st.session_state.selected_tab = "Generate Caption"
|
|
|
|
| 84 |
else:
|
| 85 |
st.error("Login failed. Invalid username or password.")
|
| 86 |
except sqlite3.OperationalError as e:
|
| 87 |
st.error(f"An error occurred while trying to log in: {e}")
|
| 88 |
|
| 89 |
+
# Function to generate image caption and edit captions
|
| 90 |
+
@st.cache_resource
|
| 91 |
+
def generate_image_caption(img, caption_model):
|
| 92 |
+
return generate_caption(img, caption_model)
|
| 93 |
+
|
| 94 |
+
def display_edit_caption(selected_languages, generated_caption):
|
| 95 |
+
st.markdown("<p style='font-size: 24px; font-weight: bold; margin-bottom: 20px;'>Edit Caption:</p>", unsafe_allow_html=True)
|
| 96 |
+
edited_caption = st.text_area("Edit the caption", value=generated_caption, key="edited_caption")
|
| 97 |
+
|
| 98 |
+
if edited_caption:
|
| 99 |
+
st.markdown("<p style='font-size: 24px; font-weight: bold; margin-bottom: 20px;'>Edited Caption:</p>", unsafe_allow_html=True)
|
| 100 |
+
st.write(edited_caption)
|
| 101 |
+
|
| 102 |
+
for lang in selected_languages:
|
| 103 |
+
if lang != "en":
|
| 104 |
+
translated_caption = translator.translate(edited_caption, src="en", dest=lang)
|
| 105 |
+
st.markdown(f"<p style='font-size: 24px; font-weight: bold; margin-bottom: 20px;'>{lang.upper()} Translation:</p>", unsafe_allow_html=True)
|
| 106 |
+
st.write(translated_caption.text)
|
| 107 |
+
|
| 108 |
+
username = st.session_state.username
|
| 109 |
+
st.balloons()
|
| 110 |
+
st.success("Caption editing complete!")
|
| 111 |
+
|
| 112 |
+
# Main function to control the application flow
|
| 113 |
+
def main():
|
| 114 |
+
create_table()
|
| 115 |
+
|
| 116 |
+
tabs = ["Signup", "Login", "Generate Caption"]
|
| 117 |
+
selected_tab = st.sidebar.selectbox("Navigation", tabs)
|
| 118 |
+
|
| 119 |
+
if selected_tab == "Signup":
|
| 120 |
+
signup()
|
| 121 |
+
elif selected_tab == "Login":
|
| 122 |
+
login()
|
| 123 |
+
elif selected_tab == "Generate Caption":
|
| 124 |
+
if hasattr(st.session_state, "username"):
|
| 125 |
+
st.sidebar.info("Welcome to the Image Caption Generator!")
|
| 126 |
+
st.sidebar.warning("Be sure to upload a clear and relevant image.")
|
| 127 |
+
generate_caption_button = st.sidebar.button("Generate Caption")
|
| 128 |
+
|
| 129 |
+
if generate_caption_button:
|
| 130 |
+
st.sidebar.info("Generating caption... Please wait.")
|
| 131 |
+
|
| 132 |
+
with st.spinner("Generating caption..."):
|
| 133 |
+
img_url = st.sidebar.text_input("Enter Image URL:")
|
| 134 |
+
img_upload = st.sidebar.file_uploader("Upload Image:", type=['jpg', 'png', 'jpeg'])
|
| 135 |
+
|
| 136 |
+
if img_url or img_upload:
|
| 137 |
+
if img_url:
|
| 138 |
+
img = Image.open(requests.get(img_url, stream=True).raw)
|
| 139 |
+
else:
|
| 140 |
+
img = Image.open(img_upload)
|
| 141 |
+
|
| 142 |
+
img = img.convert('RGB')
|
| 143 |
+
caption_model = get_caption_model()
|
| 144 |
+
generated_caption = generate_image_caption(img, caption_model)
|
| 145 |
+
|
| 146 |
+
if generated_caption:
|
| 147 |
+
st.sidebar.success("Caption generated successfully!")
|
| 148 |
+
selected_languages = st.sidebar.multiselect("Select languages for translation:", ['en', 'ta', 'hi', 'zh-cn', 'es', 'fr', 'de', 'it', 'ja'])
|
| 149 |
+
display_edit_caption(selected_languages, generated_caption)
|
| 150 |
+
else:
|
| 151 |
+
st.sidebar.error("Caption generation failed.")
|
| 152 |
+
else:
|
| 153 |
+
generate_image_caption()
|
| 154 |
+
|
| 155 |
+
else:
|
| 156 |
+
st.write("Please login to access this feature.")
|
| 157 |
|
| 158 |
if __name__ == "__main__":
|
| 159 |
main()
|