BatuhanYilmaz commited on
Commit
f27b898
1 Parent(s): 0515258

Merge branch 'main' of https://huggingface.co/spaces/BatuhanYilmaz/Whisper-Auto-Subtitled-Video-Generator

Browse files
components/.env DELETED
@@ -1,4 +0,0 @@
1
- COGNITO_DOMAIN = "xxx"
2
- CLIENT_ID = "xxx"
3
- CLIENT_SECRET = "xxx"
4
- APP_URI = "xxx"
 
 
 
 
 
components/__init__.py DELETED
File without changes
components/authenticate.py DELETED
@@ -1,158 +0,0 @@
1
- import os
2
- import streamlit as st
3
- from dotenv import load_dotenv
4
- import requests
5
- import base64
6
- import json
7
-
8
- load_dotenv()
9
- COGNITO_DOMAIN = os.environ.get("COGNITO_DOMAIN")
10
- CLIENT_ID = os.environ.get("CLIENT_ID")
11
- CLIENT_SECRET = os.environ.get("CLIENT_SECRET")
12
- APP_URI = os.environ.get("APP_URI")
13
-
14
-
15
- def init_state():
16
- if "auth_code" not in st.session_state:
17
- st.session_state["auth_code"] = ""
18
- if "authenticated" not in st.session_state:
19
- st.session_state["authenticated"] = False
20
- if "user_cognito_groups" not in st.session_state:
21
- st.session_state["user_cognito_groups"] = []
22
-
23
- # Get the authorization code after the user has logged in
24
- def get_auth_code():
25
- auth_query_params = st.experimental_get_query_params()
26
- try:
27
- auth_code = dict(auth_query_params)["code"][0]
28
- except (KeyError, TypeError):
29
- auth_code = ""
30
- return auth_code
31
-
32
-
33
- # Set the authorization code after the user has logged in
34
- def set_auth_code():
35
- init_state()
36
- auth_code = get_auth_code()
37
- st.session_state["auth_code"] = auth_code
38
-
39
-
40
- # Get the access token from the authorization code
41
- def get_user_tokens(auth_code):
42
- # Variables to make a post request
43
- token_url = f"{COGNITO_DOMAIN}/oauth2/token"
44
- client_secret_string = f"{CLIENT_ID}:{CLIENT_SECRET}"
45
- client_secret_encoded = str(
46
- base64.b64encode(client_secret_string.encode("utf-8")), "utf-8"
47
- )
48
- headers = {
49
- "Content-Type": "application/x-www-form-urlencoded",
50
- "Authorization": f"Basic {client_secret_encoded}",
51
- }
52
- body = {
53
- "grant_type": "authorization_code",
54
- "client_id": CLIENT_ID,
55
- "code": auth_code,
56
- "redirect_uri": APP_URI,
57
- }
58
-
59
- token_response = requests.post(token_url, headers=headers, data=body)
60
- try:
61
- access_token = token_response.json()["access_token"]
62
- id_token = token_response.json()["id_token"]
63
- except (KeyError, TypeError):
64
- access_token = ""
65
- id_token = ""
66
-
67
- return access_token, id_token
68
-
69
-
70
- # Use access token to retrieve user info
71
- def get_user_info(access_token):
72
- userinfo_url = f"{COGNITO_DOMAIN}/oauth2/userInfo"
73
- headers = {
74
- "Content-Type": "application/json;charset=UTF-8",
75
- "Authorization": f"Bearer {access_token}",
76
- }
77
-
78
- userinfo_response = requests.get(userinfo_url, headers=headers)
79
-
80
- return userinfo_response.json()
81
-
82
-
83
- # Decode access token to JWT to get user's cognito groups
84
- def pad_base64(data):
85
- missing_padding = len(data) % 4
86
- if missing_padding != 0:
87
- data += "=" * (4 - missing_padding)
88
- return data
89
-
90
-
91
- def get_user_cognito_groups(id_token):
92
- user_cognito_groups = []
93
- if id_token != "":
94
- header, payload, signature = id_token.split(".")
95
- printable_payload = base64.urlsafe_b64decode(pad_base64(payload))
96
- payload_dict = json.loads(printable_payload)
97
- try:
98
- user_cognito_groups = list(dict(payload_dict)["cognito:groups"])
99
- except (KeyError, TypeError):
100
- pass
101
- return user_cognito_groups
102
-
103
-
104
- # Set streamlit state variables
105
- def set_st_state_vars():
106
- init_state()
107
- auth_code = get_auth_code()
108
- access_token, id_token = get_user_tokens(auth_code)
109
- user_cognito_groups = get_user_cognito_groups(id_token)
110
-
111
- if access_token != "":
112
- st.session_state["auth_code"] = auth_code
113
- st.session_state["authenticated"] = True
114
- st.session_state["user_cognito_groups"] = user_cognito_groups
115
-
116
-
117
- # Login/ Logout HTML components
118
- login_link = f"{COGNITO_DOMAIN}/login?client_id={CLIENT_ID}&response_type=code&scope=email+openid&redirect_uri={APP_URI}"
119
- logout_link = f"{COGNITO_DOMAIN}/logout?client_id={CLIENT_ID}&logout_uri={APP_URI}"
120
-
121
- html_css_login = """
122
- <style>
123
- .button-login {
124
- background-color: skyblue;
125
- color: white !important;
126
- padding: 1em 1.5em;
127
- text-decoration: none;
128
- text-transform: uppercase;
129
- }
130
-
131
- .button-login:hover {
132
- background-color: #555;
133
- text-decoration: none;
134
- }
135
-
136
- .button-login:active {
137
- background-color: black;
138
- }
139
-
140
- </style>
141
- """
142
-
143
- html_button_login = (
144
- html_css_login
145
- + f"<a href='{login_link}' class='button-login' target='_self'>Log In</a>"
146
- )
147
- html_button_logout = (
148
- html_css_login
149
- + f"<a href='{logout_link}' class='button-login' target='_self'>Log Out</a>"
150
- )
151
-
152
-
153
- def button_login():
154
- return st.sidebar.markdown(f"{html_button_login}", unsafe_allow_html=True)
155
-
156
-
157
- def button_logout():
158
- return st.sidebar.markdown(f"{html_button_logout}", unsafe_allow_html=True)