naotakigawa commited on
Commit
af73c80
·
1 Parent(s): 3685670

Upload 3 files

Browse files
Files changed (3) hide show
  1. app.py +100 -38
  2. common.py +27 -4
  3. requirements.txt +3 -1
app.py CHANGED
@@ -1,4 +1,3 @@
1
-
2
  import streamlit as st
3
  import os
4
  import pickle
@@ -19,9 +18,47 @@ from llama_index.graph_stores import SimpleGraphStore
19
  from llama_index.storage.docstore import SimpleDocumentStore
20
  from llama_index.storage.index_store import SimpleIndexStore
21
  import tiktoken
22
- import streamlit_authenticator as stauth
23
- import yaml
24
- from logging import getLogger, StreamHandler, Formatter
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25
 
26
  index_name = "./data/storage"
27
  pkl_name = "./data/stored_documents.pkl"
@@ -51,14 +88,13 @@ def initialize_index():
51
  , chunk_overlap=DEFAULT_CHUNK_OVERLAP
52
  , tokenizer=tiktoken.encoding_for_model("gpt-3.5-turbo").encode)
53
  node_parser = SimpleNodeParser(text_splitter=text_splitter)
54
- service_context = ServiceContext.from_defaults(node_parser=node_parser)
55
  d = 1536
56
  k=2
57
  faiss_index = faiss.IndexFlatL2(d)
58
  # デバッグ用
59
  llama_debug_handler = LlamaDebugHandler()
60
  callback_manager = CallbackManager([llama_debug_handler])
61
- service_context = ServiceContext.from_defaults(callback_manager=callback_manager)
62
  lock = Lock()
63
  with lock:
64
  if os.path.exists(index_name):
@@ -69,9 +105,8 @@ def initialize_index():
69
  index_store=SimpleIndexStore.from_persist_dir(persist_dir=index_name),
70
  )
71
  st.session_state.index = load_index_from_storage(storage_context=storage_context,service_context=service_context)
72
- # index = load_index_from_storage(StorageContext.from_defaults(persist_dir=index_name), service_context=service_context)
73
  response_synthesizer = get_response_synthesizer(response_mode='refine')
74
- st.session_state.query_engine = st.session_state.index.as_query_engine(response_synthesizer=response_synthesizer)
75
  st.session_state.chat_engine = CondenseQuestionChatEngine.from_defaults(
76
  query_engine=st.session_state.query_engine,
77
  condense_question_prompt=custom_prompt,
@@ -85,7 +120,7 @@ def initialize_index():
85
  st.session_state.index = VectorStoreIndex.from_documents(documents, storage_context=storage_context,service_context=service_context)
86
  st.session_state.index.storage_context.persist(persist_dir=index_name)
87
  response_synthesizer = get_response_synthesizer(response_mode='refine')
88
- st.session_state.query_engine = st.session_state.index.as_query_engine(response_synthesizer=response_synthesizer)
89
  st.session_state.chat_engine = CondenseQuestionChatEngine.from_defaults(
90
  query_engine=st.session_state.query_engine,
91
  condense_question_prompt=custom_prompt,
@@ -98,32 +133,59 @@ def initialize_index():
98
  else:
99
  st.session_state.stored_docs=list()
100
 
101
- with open('config.yaml') as file:
102
- config = yaml.load(file, Loader=yaml.SafeLoader)
103
-
104
- authenticator = stauth.Authenticate(
105
- config['credentials'],
106
- config['cookie']['name'],
107
- config['cookie']['key'],
108
- config['cookie']['expiry_days'],
109
- config['preauthorized'],
110
- )
111
-
112
- name, authentication_status, username = authenticator.login('Login', 'main')
113
-
114
-
115
- if 'authentication_status' not in st.session_state:
116
- st.session_state['authentication_status'] = None
117
-
118
- if st.session_state["authentication_status"]:
119
- authenticator.logout('Logout', 'main')
120
- st.write(f'ログインに成功しました')
121
- initialize_index()
122
- # ここにログイン��の処理を書く。
123
- elif st.session_state["authentication_status"] is False:
124
- st.error('ユーザ名またはパスワードが間違っています')
125
- elif st.session_state["authentication_status"] is None:
126
- st.warning('ユーザ名やパスワードを入力してください')
127
-
128
-
129
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import streamlit as st
2
  import os
3
  import pickle
 
18
  from llama_index.storage.docstore import SimpleDocumentStore
19
  from llama_index.storage.index_store import SimpleIndexStore
20
  import tiktoken
21
+ import requests
22
+
23
+ from requests_oauthlib import OAuth2Session
24
+ from time import time
25
+ from dotenv import load_dotenv
26
+ from streamlit import net_util
27
+
28
+ load_dotenv()
29
+
30
+ # 接続元制御
31
+ ALLOW_IP_ADDRESS = os.environ["ALLOW_IP_ADDRESS"]
32
+
33
+ # Azure AD app registration details
34
+ CLIENT_ID = os.environ["CLIENT_ID"]
35
+ CLIENT_SECRET = os.environ["CLIENT_SECRET"]
36
+ TENANT_ID = os.environ["TENANT_ID"]
37
+
38
+ # Azure API
39
+ AUTHORITY = f"https://login.microsoftonline.com/{TENANT_ID}"
40
+ REDIRECT_PATH = os.environ["REDIRECT_PATH"]
41
+ TOKEN_URL = f"{AUTHORITY}/oauth2/v2.0/token"
42
+ AUTHORIZATION_URL = f"{AUTHORITY}/oauth2/v2.0/authorize"
43
+ SCOPES = ["openid", "profile", "User.Read"]
44
+
45
+ # 認証用URL取得
46
+ def authorization_request():
47
+ oauth = OAuth2Session(CLIENT_ID, redirect_uri=REDIRECT_PATH, scope=SCOPES)
48
+ authorization_url, state = oauth.authorization_url(AUTHORIZATION_URL)
49
+ return authorization_url, state
50
+
51
+ # 認証トークン取得
52
+ def token_request(authorization_response, state):
53
+ oauth = OAuth2Session(CLIENT_ID, state=state)
54
+ token = oauth.fetch_token(
55
+ TOKEN_URL,
56
+ code=authorization_response[0],
57
+ authorization_response=authorization_response,
58
+ client_secret=CLIENT_SECRET,
59
+
60
+ )
61
+ return token
62
 
63
  index_name = "./data/storage"
64
  pkl_name = "./data/stored_documents.pkl"
 
88
  , chunk_overlap=DEFAULT_CHUNK_OVERLAP
89
  , tokenizer=tiktoken.encoding_for_model("gpt-3.5-turbo").encode)
90
  node_parser = SimpleNodeParser(text_splitter=text_splitter)
 
91
  d = 1536
92
  k=2
93
  faiss_index = faiss.IndexFlatL2(d)
94
  # デバッグ用
95
  llama_debug_handler = LlamaDebugHandler()
96
  callback_manager = CallbackManager([llama_debug_handler])
97
+ service_context = ServiceContext.from_defaults(node_parser=node_parser,callback_manager=callback_manager)
98
  lock = Lock()
99
  with lock:
100
  if os.path.exists(index_name):
 
105
  index_store=SimpleIndexStore.from_persist_dir(persist_dir=index_name),
106
  )
107
  st.session_state.index = load_index_from_storage(storage_context=storage_context,service_context=service_context)
 
108
  response_synthesizer = get_response_synthesizer(response_mode='refine')
109
+ st.session_state.query_engine = st.session_state.index.as_query_engine(response_synthesizer=response_synthesizer,service_context=service_context)
110
  st.session_state.chat_engine = CondenseQuestionChatEngine.from_defaults(
111
  query_engine=st.session_state.query_engine,
112
  condense_question_prompt=custom_prompt,
 
120
  st.session_state.index = VectorStoreIndex.from_documents(documents, storage_context=storage_context,service_context=service_context)
121
  st.session_state.index.storage_context.persist(persist_dir=index_name)
122
  response_synthesizer = get_response_synthesizer(response_mode='refine')
123
+ st.session_state.query_engine = st.session_state.index.as_query_engine(response_synthesizer=response_synthesizer,service_context=service_context)
124
  st.session_state.chat_engine = CondenseQuestionChatEngine.from_defaults(
125
  query_engine=st.session_state.query_engine,
126
  condense_question_prompt=custom_prompt,
 
133
  else:
134
  st.session_state.stored_docs=list()
135
 
136
+ def logout():
137
+ st.session_state["token"] = None
138
+ st.session_state["token_expires"] = None
139
+ st.session_state["authorization_state"] = None
140
+
141
+ # メイン
142
+ def app():
143
+ # 初期化
144
+ st.session_state["token"] = None
145
+ st.session_state["token_expires"] = time()
146
+ st.session_state["authorization_state"] = None
147
+
148
+ # 接続元IP許可判定
149
+ if not net_util.get_external_ip() in ALLOW_IP_ADDRESS:
150
+ st.title("HTTP 403 Forbidden")
151
+ return
152
+
153
+ # 接続元OK
154
+ st.title("Azure AD Login with Streamlit")
155
+
156
+ # 認証後のリダイレクトのGETパラメータ値を取得
157
+ authorization_response = st.experimental_get_query_params().get("code")
158
+
159
+ # 認証OK、トークン無し
160
+ if authorization_response and st.session_state["token"] is None:
161
+ # トークン設定
162
+ token = token_request(authorization_response, st.session_state["authorization_state"])
163
+ st.session_state["token"] = token
164
+ st.session_state["token_expires"] = token["expires_at"]
165
+
166
+ # トークン無し or 期限切れ
167
+ if st.session_state["token"] is None or float(st.session_state["token_expires"]) <= time():
168
+ # 認証用リンク表示
169
+ authorization_url, st.session_state["authorization_state"] = authorization_request()
170
+ st.markdown(f'[Click here to log in]({authorization_url})', unsafe_allow_html=True)
171
+ else:
172
+ # 認証OK
173
+ st.markdown(f"Logged in successfully. Welcome, {st.session_state['token']['token_type']}!")
174
+ if st.button("logout",use_container_width=True):
175
+ logout()
176
+ st.experimental_set_query_params(test="test")
177
+ st.experimental_rerun()
178
+ st.text("サイドバーから利用するメニューをお選びください。")
179
+ initialize_index()
180
+
181
+ if __name__ == "__main__":
182
+ if "token" not in st.session_state or st.session_state["token"] is None or float(st.session_state["token_expires"]) <= time():
183
+ app()
184
+ else:
185
+ st.title("Azure AD Login with Streamlit")
186
+ if st.button("logout",use_container_width=True):
187
+ logout()
188
+ st.experimental_set_query_params(test="test")
189
+ st.experimental_rerun()
190
+ st.text("ログイン済みです。")
191
+ st.text("サイドバーから利用するメニューをお選びください。")
common.py CHANGED
@@ -2,15 +2,38 @@
2
  import extra_streamlit_components as stx
3
  import streamlit as st
4
  import logging
 
 
 
 
5
 
6
  logging.basicConfig(level=logging.INFO)
7
  logger = logging.getLogger("__name__")
8
  logger.debug("調査用ログ")
9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10
  #ログインの確認
11
  def check_login():
12
- if 'authentication_status' not in st.session_state:
13
- st.session_state['authentication_status'] = None
14
- if st.session_state["authentication_status"] is None or False:
15
- st.warning("**ログインしてください**")
16
  st.stop()
 
2
  import extra_streamlit_components as stx
3
  import streamlit as st
4
  import logging
5
+ import os
6
+
7
+ from time import time
8
+ from requests_oauthlib import OAuth2Session
9
 
10
  logging.basicConfig(level=logging.INFO)
11
  logger = logging.getLogger("__name__")
12
  logger.debug("調査用ログ")
13
 
14
+ # 接続元制御
15
+ ALLOW_IP_ADDRESS = os.environ["ALLOW_IP_ADDRESS"]
16
+
17
+ # Azure AD app registration details
18
+ CLIENT_ID = os.environ["CLIENT_ID"]
19
+ TENANT_ID = os.environ["TENANT_ID"]
20
+
21
+ # Azure API
22
+ AUTHORITY = f"https://login.microsoftonline.com/{TENANT_ID}"
23
+ REDIRECT_PATH = os.environ["REDIRECT_PATH"]
24
+ AUTHORIZATION_URL = f"{AUTHORITY}/oauth2/v2.0/authorize"
25
+ SCOPES = ["openid", "profile", "User.Read"]
26
+
27
+ # 認証用URL取得
28
+ def authorization_request():
29
+ oauth = OAuth2Session(CLIENT_ID, redirect_uri=REDIRECT_PATH, scope=SCOPES)
30
+ authorization_url, state = oauth.authorization_url(AUTHORIZATION_URL)
31
+ return authorization_url, state
32
+
33
  #ログインの確認
34
  def check_login():
35
+ if "token" not in st.session_state or st.session_state["token"] is None or float(st.session_state["token_expires"]) <= time():
36
+ # 認証用リンク表示
37
+ authorization_url, st.session_state["authorization_state"] = authorization_request()
38
+ st.markdown(f'[Click here to log in]({authorization_url})', unsafe_allow_html=True)
39
  st.stop()
requirements.txt CHANGED
@@ -8,4 +8,6 @@ pypdf==3.9.0
8
  faiss-cpu==1.7.4
9
  html2text
10
  streamlit-authenticator
11
- extra_streamlit_components
 
 
 
8
  faiss-cpu==1.7.4
9
  html2text
10
  streamlit-authenticator
11
+ extra_streamlit_components
12
+ requests_oauthlib
13
+ python-dotenv