aieye/emotion_classifier_tutorial
0
1import streamlit as st2import os3import requests4import os5import json6 7# Fetch the service account key JSON file contents8 9 10def initialize_login():11 if "login" not in st.session_state:12 st.columns(3)[1].image("assets/logo.png")13 username = st.text_input("Username")14 password = st.text_input("Password", type="password")15 16 if st.button("Login"):17 authorized = authenticate(username, password)18 if authorized["status"]:19 st.session_state["login"] = authorized20 os.makedirs(21 os.path.join(".sessions", st.session_state["login"]["username"]),22 exist_ok=True,23 )24 st.success("Login Successful!")25 st.experimental_rerun()26 else:27 st.error("Invalid username or password")28 else:29 st.sidebar.success(f'Hello, {st.session_state["login"]["Name"]}!')30 st.sidebar.image("assets/logo.png", use_column_width=True)31 32 33def authenticate(username, password):34 FIREBASE_WEB_API_KEY = os.environ.get("FIREBASE_WEB_API_KEY")35 payload = json.dumps(36 {37 "email": f"{username}@aieye.com",38 "password": password,39 "returnSecureToken": False,40 }41 )42 43 rest_api_url = (44 f"https://identitytoolkit.googleapis.com/v1/accounts:signInWithPassword"45 )46 r = requests.post(rest_api_url, params={"key": FIREBASE_WEB_API_KEY}, data=payload)47 print(r.json())48 49 if r.status_code == 200:50 return {51 "status": True,52 "Name": " ".join(username.split("_")),53 "username": username,54 }55 else:56 return {"status": False}57 58 59def get_login():60 return st.session_state.get("login", {"status": False})61 