Spaces:
Build error
Build error
MANIKANDAN A commited on
Commit ·
7f1c645
1
Parent(s): cf11161
Update app.py
Browse files17 unmodified lines181920212223242518 unmodified lines44454647484950515253545512 unmodified lines6869707172737475767778792 unmodified lines828384858687886 unmodified lines95969798991001011021031041051061071081091107 unmodified lines11811912012112212312412512612712812911 unmodified lines14114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118217 unmodified linescaption_model = get_model()# ConstantsSIGNUP_SUCCESS_MSG = "Signup successful! You can now login."SIGNUP_ERROR_EXISTING_USER = "Username already exists. Please choose a different username."18 unmodified linesrole TEXT NOT NULL)''')# Function for signup sectiondef signup_section():st.markdown(f"<p style='{heading_style}'>Signup</p>", unsafe_allow_html=True)new_username = st.text_input("New Username", key="new_username", help="Choose a unique username")new_password = st.text_input("New Password", type="password", key="new_password", help="Password should be at least 8 characters long")new_email = st.text_input("Email", key="new_email", help="Enter a valid email address")12 unmodified lines(new_username, new_password, new_email, role))st.success(SIGNUP_SUCCESS_MSG)st.balloons()except sqlite3.IntegrityError:st.error(SIGNUP_ERROR_EXISTING_USER)# Function for login sectiondef login_section():st.markdown(f"<p style='{heading_style}'>Login</p>", unsafe_allow_html=True)username = st.text_input("Username", key="login_username", help="Enter your username")password = st.text_input("Password", type="password", key="login_password",help="Enter your password")2 unmodified linesif not username or not password:st.error("Username and password are required for login.")returntry:with sqlite3.connect("login.db") as conn:cursor = conn.cursor()6 unmodified linesst.session_state.username = usernamest.session_state.selected_tab = "Generate Caption"st.balloons()else:st.error(LOGIN_ERROR_INVALID_CREDENTIALS)except sqlite3.OperationalError as e:st.error(f"An error occurred while trying to log in: {e}")def translate_caption(caption, target_language):translated = translator.translate(caption, dest=target_language)return translated.textdef predict(cap_col):captions = []pred_caption = generate_caption('tmp.jpg', caption_model)7 unmodified linesif pred_caption not in captions:translated_caption = translate_caption(pred_caption, target_language)captions.append(translated_caption)cap_col.markdown('<div class="caption-container">', unsafe_allow_html=True)for c in captions: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)cap_col.markdown('</div>', unsafe_allow_html=True)def main():# Create the database table if it doesn't exist11 unmodified lineselif selected_tab == "Login":login_section()elif selected_tab == "Generate Caption":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)col1, col2 = st.columns(2)# Image URL inputimg_url = st.text_input(label='Enter Image URL')# Image upload inputimg_upload = st.file_uploader(label='Upload Image', type=['jpg', 'png', 'jpeg'])# Language selection dropdowntarget_language = st.selectbox('Select Target Language', ['en', 'ta', 'hi', 'es', 'fr', 'zh-cn'], index=0)# Process image and generate captionsif img_url:img = Image.open(requests.get(img_url, stream=True).raw)img = img.convert('RGB')col1.image(img, caption="Input Image", use_column_width=True)img.save('tmp.jpg')predict(col2)st.markdown('<center style="opacity: 70%">OR</center>', unsafe_allow_html=True)elif img_upload:img = img_upload.read()img = Image.open(io.BytesIO(img))img = img.convert('RGB')col1.image(img, caption="Input Image", use_column_width=True)img.save('tmp.jpg')predict(col2)# Remove temporary image fileif img_url or img_upload:os.remove('tmp.jpg')else:st.write("Please login to access this feature.")if __name__ == "__main__":main()17 unmodified lines18192021222318 unmodified lines4243444546474849505112 unmodified lines646566676869707172737475762 unmodified lines798081828384856 unmodified lines92939495969798991001011021031041051061071087 unmodified lines11611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016111 unmodified lines17317417517617717817918018118218317 unmodified linescaption_model = get_model()# ConstantsSIGNUP_SUCCESS_MSG = "Signup successful! You can now login."SIGNUP_ERROR_EXISTING_USER = "Username already exists. Please choose a different username."18 unmodified linesrole TEXT NOT NULL)''')# Function for signup sectiondef signup_section():st.markdown(f"<p style='{heading_style}'>Signup</p>", unsafe_allow_html=True)new_username = st.text_input("New Username", key="new_username", help="Choose a unique username")new_password = st.text_input("New Password", type="password", key="new_password", help="Password should be at least 8 characters long")new_email = st.text_input("Email", key="new_email", help="Enter a valid email address")12 unmodified lines(new_username, new_password, new_email, role))st.success(SIGNUP_SUCCESS_MSG)st.balloons()# After successful signup, direct the user to the login pagelogin_section()except sqlite3.IntegrityError:st.error(SIGNUP_ERROR_EXISTING_USER)# Function for login sectiondef login_section():st.markdown(f"<p style='{heading_style}'>Login</p>", unsafe_allow_html=True)username = st.text_input("Username", key="login_username", help="Enter your username")password = st.text_input("Password", type="password", key="login_password",help="Enter your password")2 unmodified linesif not username or not password:st.error("Username and password are required for login.")returntry:with sqlite3.connect("login.db") as conn:cursor = conn.cursor()6 unmodified linesst.session_state.username = usernamest.session_state.selected_tab = "Generate Caption"st.balloons()# After successful login, direct the user to the image caption prediction functiongenerate_caption_section()else:st.error(LOGIN_ERROR_INVALID_CREDENTIALS)except sqlite3.OperationalError as e:st.error(f"An error occurred while trying to log in: {e}")def translate_caption(caption, target_language):translated = translator.translate(caption, dest=target_language)return translated.textdef predict(cap_col):captions = []pred_caption = generate_caption('tmp.jpg', caption_model)7 unmodified linesif pred_caption not in captions:translated_caption = translate_caption(pred_caption, target_language)captions.append(translated_caption)cap_col.markdown('<div class="caption-container">', unsafe_allow_html=True)for c in captions: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)cap_col.markdown('</div>', unsafe_allow_html=True)def generate_caption_section():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)col1, col2 = st.columns(2)# Image URL inputimg_url = st.text_input(label='Enter Image URL')# Image upload inputimg_upload = st.file_uploader(label='Upload Image', type=['jpg', 'png', 'jpeg'])# Language selection dropdowntarget_language = st.selectbox('Select Target Language', ['en', 'ta', 'hi', 'es', 'fr', 'zh-cn'], index=0)# Process image and generate captionsif img_url:img = Image.open(requests.get(img_url, stream=True).raw)img = img.convert('RGB')col1.image(img, caption="Input Image", use_column_width=True)img.save('tmp.jpg')predict(col2)st.markdown('<center style="opacity: 70%">OR</center>', unsafe_allow_html=True)elif img_upload:img = img_upload.read()img = Image.open(io.BytesIO(img))img = img.convert('RGB')col1.image(img, caption="Input Image", use_column_width=True)img.save('tmp.jpg')predict(col2)# Remove temporary image fileif img_url or img_upload:os.remove('tmp.jpg')def main():# Create the database table if it doesn't exist11 unmodified lineselif selected_tab == "Login":login_section()elif selected_tab == "Generate Caption":# Check if the user is logged in before allowing access to the image caption generationif 'username' in st.session_state:generate_caption_section()else:st.write("Please login to access this feature.")if __name__ == "__main__":main()
app.py+50-49