Spaces:
Sleeping
Sleeping
Commit
·
178b25e
1
Parent(s):
3cf466e
Upload 4 files
Browse files- app.py +32 -36
- common.py +15 -0
- config.yaml +13 -0
- requirements.txt +3 -1
app.py
CHANGED
@@ -19,10 +19,12 @@ 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 |
from logging import getLogger, StreamHandler, Formatter
|
23 |
|
24 |
index_name = "./data/storage"
|
25 |
-
pkl_name = "
|
26 |
|
27 |
custom_prompt = Prompt("""\
|
28 |
以下はこれまでの会話履歴と、ドキュメントを検索して回答する必要がある、ユーザーからの会話文です。
|
@@ -37,41 +39,12 @@ custom_prompt = Prompt("""\
|
|
37 |
Search query:
|
38 |
""")
|
39 |
|
40 |
-
# # list of (human_message, ai_message) tuples
|
41 |
-
custom_chat_history = [
|
42 |
-
(
|
43 |
-
'こんにちは、アシスタント。これから質問に答えて下さい。',
|
44 |
-
'こんにちは。了解しました。'
|
45 |
-
)
|
46 |
-
]
|
47 |
chat_history = []
|
48 |
|
49 |
logging.basicConfig(level=logging.INFO)
|
50 |
logger = logging.getLogger("__name__")
|
51 |
logger.debug("調査用ログ")
|
52 |
|
53 |
-
|
54 |
-
st.title("💬 Chatbot")
|
55 |
-
if "messages" not in st.session_state:
|
56 |
-
st.session_state["messages"] = [{"role": "assistant", "content": "お困りごとはございますか?"}]
|
57 |
-
|
58 |
-
for msg in st.session_state.messages:
|
59 |
-
st.chat_message(msg["role"]).write(msg["content"])
|
60 |
-
|
61 |
-
if prompt := st.chat_input():
|
62 |
-
st.session_state.messages.append({"role": "user", "content": prompt})
|
63 |
-
st.chat_message("user").write(prompt)
|
64 |
-
response = st.session_state.chat_engine.chat(prompt)
|
65 |
-
# response = openai.ChatCompletion.create(model="gpt-3.5-turbo", messages=st.session_state.messages)
|
66 |
-
msg = str(response)
|
67 |
-
st.session_state.messages.append({"role": "assistant", "content": msg})
|
68 |
-
st.chat_message("assistant").write(msg)
|
69 |
-
|
70 |
-
if st.button("リセット",use_container_width=True):
|
71 |
-
st.session_state.chat_engine.reset()
|
72 |
-
st.session_state.messages = [{"role": "assistant", "content": "お困りごとはございますか?"}]
|
73 |
-
logger.info("reset")
|
74 |
-
|
75 |
def initialize_index():
|
76 |
logger.info("initialize_index start")
|
77 |
text_splitter = TokenTextSplitter(separator="。", chunk_size=1500
|
@@ -125,9 +98,32 @@ def initialize_index():
|
|
125 |
else:
|
126 |
st.session_state.stored_docs=list()
|
127 |
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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"
|
28 |
|
29 |
custom_prompt = Prompt("""\
|
30 |
以下はこれまでの会話履歴と、ドキュメントを検索して回答する必要がある、ユーザーからの会話文です。
|
|
|
39 |
Search query:
|
40 |
""")
|
41 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
42 |
chat_history = []
|
43 |
|
44 |
logging.basicConfig(level=logging.INFO)
|
45 |
logger = logging.getLogger("__name__")
|
46 |
logger.debug("調査用ログ")
|
47 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
48 |
def initialize_index():
|
49 |
logger.info("initialize_index start")
|
50 |
text_splitter = TokenTextSplitter(separator="。", chunk_size=1500
|
|
|
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 |
+
|
common.py
ADDED
@@ -0,0 +1,15 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# common.py
|
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 |
+
value = stx.CookieManager().get(cookie='some_cookie_name')
|
13 |
+
if value is None:
|
14 |
+
st.warning("**ログインしてください**")
|
15 |
+
st.stop()
|
config.yaml
ADDED
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
cookie:
|
2 |
+
expiry_days: 10
|
3 |
+
key: some_signature_keys
|
4 |
+
name: some_cookie_name
|
5 |
+
credentials:
|
6 |
+
usernames:
|
7 |
+
stylez:
|
8 |
+
email: [email protected]
|
9 |
+
name: test user
|
10 |
+
password: $2b$12$LUwDsoPgN7T3v1xK42rGR.yCiDauGBH4fYeia7CN84F2v8tY3IEs2
|
11 |
+
preauthorized:
|
12 |
+
emails:
|
13 |
requirements.txt
CHANGED
@@ -6,4 +6,6 @@ anthropic
|
|
6 |
llama-index==0.7.4
|
7 |
pypdf==3.9.0
|
8 |
faiss-cpu==1.7.4
|
9 |
-
html2text
|
|
|
|
|
|
6 |
llama-index==0.7.4
|
7 |
pypdf==3.9.0
|
8 |
faiss-cpu==1.7.4
|
9 |
+
html2text
|
10 |
+
streamlit-authenticator
|
11 |
+
extra_streamlit_components
|