MANIKANDAN A
commited on
Commit
·
ed2b9aa
1
Parent(s):
35db173
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,20 +1,23 @@
|
|
| 1 |
-
import sqlite3
|
| 2 |
import io
|
| 3 |
import os
|
| 4 |
import streamlit as st
|
|
|
|
| 5 |
from PIL import Image
|
| 6 |
-
import tensorflow as tf
|
| 7 |
-
import numpy as np
|
| 8 |
from model import get_caption_model, generate_caption
|
| 9 |
from googletrans import Translator
|
| 10 |
-
import
|
| 11 |
|
| 12 |
# Initialize Streamlit app
|
| 13 |
st.set_page_config(page_title="Image Caption Generator", layout="wide")
|
| 14 |
|
| 15 |
-
# Initialize Translator
|
| 16 |
translator = Translator()
|
| 17 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
# Constants
|
| 19 |
SIGNUP_SUCCESS_MSG = "Signup successful! You can now login."
|
| 20 |
SIGNUP_ERROR_EXISTING_USER = "Username already exists. Please choose a different username."
|
|
@@ -39,7 +42,7 @@ def create_table():
|
|
| 39 |
role TEXT NOT NULL
|
| 40 |
)
|
| 41 |
''')
|
| 42 |
-
|
| 43 |
# Function for signup section
|
| 44 |
def signup_section():
|
| 45 |
|
|
@@ -93,24 +96,28 @@ def login_section():
|
|
| 93 |
else:
|
| 94 |
st.error(LOGIN_ERROR_INVALID_CREDENTIALS)
|
| 95 |
except sqlite3.OperationalError as e:
|
| 96 |
-
st.error(f"An error occurred while trying to log in: {e}")
|
|
|
|
|
|
|
| 97 |
|
| 98 |
def predict(cap_col):
|
| 99 |
captions = []
|
| 100 |
-
pred_caption = generate_caption('tmp.jpg',
|
| 101 |
|
| 102 |
cap_col.markdown('#### Predicted Captions:')
|
| 103 |
captions.append(pred_caption)
|
| 104 |
|
| 105 |
for _ in range(4):
|
| 106 |
-
pred_caption = generate_caption('tmp.jpg',
|
| 107 |
if pred_caption not in captions:
|
| 108 |
captions.append(pred_caption)
|
| 109 |
|
| 110 |
cap_col.markdown('<div class="caption-container">', unsafe_allow_html=True)
|
| 111 |
for c in captions:
|
| 112 |
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)
|
| 113 |
-
cap_col.markdown('</div>', unsafe_allow_html=True)
|
|
|
|
|
|
|
| 114 |
|
| 115 |
def main():
|
| 116 |
# Create the database table if it doesn't exist
|
|
@@ -139,8 +146,7 @@ def main():
|
|
| 139 |
img_url = st.text_input("Enter Image URL:")
|
| 140 |
img_upload = st.file_uploader("Upload Image:", type=['jpg', 'png', 'jpeg'])
|
| 141 |
|
| 142 |
-
col1, col2 = st.columns(
|
| 143 |
-
|
| 144 |
if img_url or img_upload:
|
| 145 |
if img_url:
|
| 146 |
img = Image.open(requests.get(img_url, stream=True).raw)
|
|
@@ -184,5 +190,11 @@ def main():
|
|
| 184 |
else:
|
| 185 |
st.write("Please login to access this feature.")
|
| 186 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 187 |
if __name__ == "__main__":
|
| 188 |
main()
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import io
|
| 2 |
import os
|
| 3 |
import streamlit as st
|
| 4 |
+
import requests
|
| 5 |
from PIL import Image
|
|
|
|
|
|
|
| 6 |
from model import get_caption_model, generate_caption
|
| 7 |
from googletrans import Translator
|
| 8 |
+
import sqlite3
|
| 9 |
|
| 10 |
# Initialize Streamlit app
|
| 11 |
st.set_page_config(page_title="Image Caption Generator", layout="wide")
|
| 12 |
|
|
|
|
| 13 |
translator = Translator()
|
| 14 |
|
| 15 |
+
@st.cache_resource
|
| 16 |
+
def get_model():
|
| 17 |
+
return get_caption_model()
|
| 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 |
|
|
|
|
| 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)
|
| 106 |
|
| 107 |
cap_col.markdown('#### Predicted Captions:')
|
| 108 |
captions.append(pred_caption)
|
| 109 |
|
| 110 |
for _ in range(4):
|
| 111 |
+
pred_caption = generate_caption('tmp.jpg', caption_model, add_noise=True)
|
| 112 |
if pred_caption not in captions:
|
| 113 |
captions.append(pred_caption)
|
| 114 |
|
| 115 |
cap_col.markdown('<div class="caption-container">', unsafe_allow_html=True)
|
| 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
|
|
|
|
| 146 |
img_url = st.text_input("Enter Image URL:")
|
| 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)
|
|
|
|
| 190 |
else:
|
| 191 |
st.write("Please login to access this feature.")
|
| 192 |
|
| 193 |
+
# Remove temporary image file
|
| 194 |
+
if img_url or img_upload:
|
| 195 |
+
os.remove('tmp.jpg')
|
| 196 |
+
|
| 197 |
if __name__ == "__main__":
|
| 198 |
main()
|
| 199 |
+
|
| 200 |
+
|