Spaces:
Runtime error
Runtime error
| # Import libraries | |
| import os | |
| import streamlit as st | |
| from PIL import Image | |
| from streamlit_extras.switch_page_button import switch_page | |
| from baam_functions import * | |
| from datetime import datetime | |
| from bokeh.models.widgets import Button | |
| from bokeh.models import CustomJS | |
| from streamlit_bokeh_events import streamlit_bokeh_events | |
| # Set direction as current folder | |
| sourceFileDir = os.path.dirname(os.path.abspath(__file__)) | |
| os.chdir(sourceFileDir) | |
| logo = Image.open('img/logo.png') | |
| st.set_page_config(page_title = "BAAM", page_icon = logo) | |
| # Initialise variables to pass between pages | |
| st.session_state['user_dict'] = {} | |
| st.session_state['verification'] = False | |
| st.session_state['location'] = "" | |
| st.session_state['raw_location'] = "" | |
| st.session_state['username'] = "" | |
| # st.session_state['typing_speed'] = 0 | |
| def main(): | |
| """Banking Advanced Authentication System""" | |
| # # Note that API key's running out of budget | |
| # contact_url = "https://www.linkedin.com/in/linhvuu" | |
| # st.write("I am running out of energy. Please contact [my assistant](%s) to wake me up." % contact_url) | |
| menu = ["Home", "SignUp", "Login"] | |
| choice = st.sidebar.selectbox("Menu", menu) | |
| if choice == "Home": | |
| st.subheader("Banking Advanced Authentication Module") | |
| st.image("img/home.png") | |
| st.write("Please select one action from the Menu on the side bar. Thank you.") | |
| st.image("img/menu.png") | |
| elif choice == "SignUp": | |
| st.subheader("Create New Account") | |
| new_user = st.text_input("Username") | |
| new_password = st.text_input("Password",type='password') | |
| if st.button("Signup"): | |
| create_user_table() | |
| add_user_data(new_user,make_hashes(new_password)) | |
| st.success("You have successfully created a valid Account") | |
| st.info("Go to Login Menu to login") | |
| elif choice == "Login": | |
| # Header of the page | |
| col1, col2, col3 = st.columns(3) | |
| with col1: | |
| st.write(' ') | |
| # Logo | |
| with col2: | |
| st.image("img/Standard_Chartered.png", width=175) | |
| with col3: | |
| st.write(' ') | |
| # Login form | |
| st.subheader("Login") | |
| username = st.text_input("Username") | |
| password = st.text_input("Password",type='password') | |
| is_tester = st.checkbox('Login to test') | |
| login_button = Button(label="Login") | |
| # Collect location when login button is clicked | |
| login_button.js_on_event("button_click", CustomJS(code=""" | |
| navigator.geolocation.getCurrentPosition( | |
| (loc) => { | |
| document.dispatchEvent(new CustomEvent("GET_LOCATION", {detail: {lat: loc.coords.latitude, lon: loc.coords.longitude}})) | |
| } | |
| ) | |
| """)) | |
| # Get location | |
| location = streamlit_bokeh_events( | |
| login_button, | |
| events="GET_LOCATION", | |
| key="get_location", | |
| refresh_on_update=False, | |
| override_height=75, | |
| debounce_time=0) | |
| # If can get location | |
| if location: | |
| # Hash password & verify password | |
| hashed_pswd = make_hashes(password) | |
| result = login_user(username,check_hashes(password, hashed_pswd)) | |
| # When the username and password are correct | |
| if result: | |
| # time_start = st.session_state['time_start'] | |
| # Stop counting time after the user finished entering username, password and clicked login button | |
| login_time = datetime.now() | |
| # # *** Calculate the login duration in seconds (the time it takes user to enter username, password and click login button) | |
| # login_duration = login_time - time_start | |
| # login_duration_seconds = login_duration.total_seconds() | |
| # # Calculate typing speed (characters per minute) | |
| # typing_speed = (len(username) + len(password)) * 60 / login_duration_seconds | |
| # Launch app for end user | |
| if not(is_tester): | |
| # Collect user's information when logging in | |
| #user_dict, st.session_state['location'] = collect_data(username, location, login_time, typing_speed) | |
| user_dict, st.session_state['location'] = collect_data(username, location, login_time) | |
| # Store user information to pass to other pages | |
| st.session_state['user_dict'] = user_dict | |
| verification = verify_user(user_dict) | |
| # THAO LE CODE: | |
| # weight edit in logic_tl | |
| weight = {'device_uuid': 40, 'mac_address': 40, 'device_name': 30.0, 'device_model': 20.0, \ | |
| 'device_vendor': 4.0, 'ip_v4': 30, 'isp_name': 15.0, 'ip_country': 3.0, 'suburd': 30, 'district': 22.5, \ | |
| 'city': 15.0, 'country': 3.0} | |
| # ThaoLe note: | |
| # st.write(f"print the user_dict {user_dict}") | |
| # st.write(f'type of user_dict {type(user_dict)}') | |
| # end not | |
| st.session_state['verification'] = verification | |
| # Add to the login database after successful verification | |
| if verification: | |
| add_login_data(user_dict) | |
| # Open Transfer Money Page | |
| switch_page("Transfer Money 💸") | |
| # Launch Testing Tool | |
| else: | |
| st.session_state['raw_location'] = location | |
| st.session_state['username'] = username | |
| # st.session_state['typing_speed'] = typing_speed | |
| switch_page("SelectUseCase") | |
| else: | |
| st.warning("Incorrect Username/Password") | |
| # Start counting time | |
| st.session_state['time_start'] = datetime.now() | |
| # #To export database to csv file (can be commented out if not needed) | |
| # export_csv() | |
| if __name__ == '__main__': | |
| main() | |