Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,7 +1,7 @@
|
|
1 |
import os
|
2 |
import streamlit as st
|
3 |
|
4 |
-
from langchain.embeddings import HuggingFaceInstructEmbeddings
|
5 |
from langchain.vectorstores.faiss import FAISS
|
6 |
from langchain.chains import VectorDBQA
|
7 |
from huggingface_hub import snapshot_download
|
@@ -9,38 +9,52 @@ from langchain import OpenAI
|
|
9 |
from langchain import PromptTemplate
|
10 |
|
11 |
|
12 |
-
st.set_page_config(page_title="
|
13 |
|
14 |
|
15 |
#### sidebar section 1 ####
|
16 |
with st.sidebar:
|
17 |
-
book = st.radio("Choose
|
18 |
-
["
|
19 |
)
|
20 |
-
|
21 |
-
|
22 |
-
|
|
|
|
|
|
|
|
|
|
|
23 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
|
25 |
-
|
26 |
-
st.markdown(f"#### Have a conversation with {BOOK_NAME} by {AUTHOR_NAME} π")
|
27 |
|
|
|
|
|
28 |
|
29 |
|
|
|
30 |
|
31 |
##### functionss ####
|
32 |
@st.experimental_singleton(show_spinner=False)
|
33 |
-
def load_vectorstore():
|
34 |
# download from hugging face
|
35 |
-
cache_dir=
|
36 |
-
snapshot_download(repo_id="
|
37 |
repo_type="dataset",
|
38 |
revision="main",
|
39 |
-
allow_patterns=f"
|
40 |
cache_dir=cache_dir,
|
41 |
)
|
42 |
|
43 |
-
target_dir =
|
44 |
|
45 |
# Walk through the directory tree recursively
|
46 |
for root, dirs, files in os.walk(cache_dir):
|
@@ -49,11 +63,7 @@ def load_vectorstore():
|
|
49 |
# Get the full path of the target directory
|
50 |
target_path = os.path.join(root, target_dir)
|
51 |
|
52 |
-
|
53 |
-
embeddings = HuggingFaceInstructEmbeddings(
|
54 |
-
embed_instruction="Represent the book passage for retrieval: ",
|
55 |
-
query_instruction="Represent the question for retrieving supporting texts from the book passage: "
|
56 |
-
)
|
57 |
|
58 |
# load faiss
|
59 |
docsearch = FAISS.load_local(folder_path=target_path, embeddings=embeddings)
|
@@ -62,40 +72,42 @@ def load_vectorstore():
|
|
62 |
|
63 |
|
64 |
@st.experimental_memo(show_spinner=False)
|
65 |
-
def load_prompt(
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
{{
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
83 |
|
84 |
|
85 |
@st.experimental_singleton(show_spinner=False)
|
86 |
def load_chain():
|
87 |
-
llm =
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
vectorstore=load_vectorstore(),
|
94 |
-
k=8,
|
95 |
-
return_source_documents=True,
|
96 |
-
)
|
97 |
|
98 |
-
return
|
99 |
|
100 |
|
101 |
def get_answer(question):
|
@@ -128,23 +140,8 @@ def get_answer(question):
|
|
128 |
|
129 |
return answer, pages, extract
|
130 |
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
##### sidebar section 2 ####
|
135 |
-
|
136 |
-
api_key = st.text_input(label = "And paste your OpenAI API key here to get started",
|
137 |
-
type = "password",
|
138 |
-
help = "This isn't saved π"
|
139 |
-
)
|
140 |
-
os.environ["OPENAI_API_KEY"] = api_key
|
141 |
-
|
142 |
-
st.markdown("---")
|
143 |
-
|
144 |
-
st.info("Based on [Talk2Book](https://github.com/batmanscode/Talk2Book)")
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
|
149 |
##### main ####
|
150 |
user_input = st.text_input("Your question", "Who are you?", key="input")
|
@@ -160,18 +157,14 @@ ask = col2.button("Ask", type="primary")
|
|
160 |
|
161 |
if ask:
|
162 |
|
163 |
-
|
164 |
-
|
165 |
-
|
166 |
-
|
167 |
-
|
168 |
-
|
169 |
-
|
170 |
-
|
171 |
-
st.write(f"**{BOOK_NAME}:** What\'s going on? That's not the right API key")
|
172 |
-
st.stop()
|
173 |
-
|
174 |
-
st.write(f"**{BOOK_NAME}:** {answer}")
|
175 |
|
176 |
# sources
|
177 |
with st.expander(label = f"From pages: {pages}", expanded = False):
|
|
|
1 |
import os
|
2 |
import streamlit as st
|
3 |
|
4 |
+
from langchain.embeddings import HuggingFaceInstructEmbeddings, HuggingFaceEmbeddings
|
5 |
from langchain.vectorstores.faiss import FAISS
|
6 |
from langchain.chains import VectorDBQA
|
7 |
from huggingface_hub import snapshot_download
|
|
|
9 |
from langchain import PromptTemplate
|
10 |
|
11 |
|
12 |
+
st.set_page_config(page_title="CFA Level 1", page_icon="π")
|
13 |
|
14 |
|
15 |
#### sidebar section 1 ####
|
16 |
with st.sidebar:
|
17 |
+
book = st.radio("Choose an Embedding Model: ",
|
18 |
+
["Instruct", "Sbert"]
|
19 |
)
|
20 |
+
|
21 |
+
#load embedding models
|
22 |
+
@st.experimental_singleton(show_spinner=True)
|
23 |
+
def load_embedding_models(model):
|
24 |
+
|
25 |
+
if model == 'Sbert':
|
26 |
+
model_sbert = "sentence-transformers/all-mpnet-base-v2"
|
27 |
+
emb = HuggingFaceEmbeddings(model_name=model_sbert)
|
28 |
|
29 |
+
elif model == 'Instruct':
|
30 |
+
embed_instruction = "Represent the financial paragraph for document retrieval: "
|
31 |
+
query_instruction = "Represent the question for retrieving supporting documents: "
|
32 |
+
model_instr = "hkunlp/instructor-large"
|
33 |
+
emb = HuggingFaceInstructEmbeddings(model_name=model_instr,
|
34 |
+
embed_instruction=embed_instruction,
|
35 |
+
query_instruction=query_instruction)
|
36 |
|
37 |
+
return emb
|
|
|
38 |
|
39 |
+
st.title(f"Talk to CFA Level 1 Book")
|
40 |
+
st.markdown(f"#### Have a conversation with the CFA Curriculum by the CFA Institute π")
|
41 |
|
42 |
|
43 |
+
embeddings = load_embedding_models(book)
|
44 |
|
45 |
##### functionss ####
|
46 |
@st.experimental_singleton(show_spinner=False)
|
47 |
+
def load_vectorstore(embeddings):
|
48 |
# download from hugging face
|
49 |
+
cache_dir="cfa_level_1_cache"
|
50 |
+
snapshot_download(repo_id="nickmuchi/CFA_Level_1_Text_Embeddings",
|
51 |
repo_type="dataset",
|
52 |
revision="main",
|
53 |
+
allow_patterns=f"CFA_Level_1/*",
|
54 |
cache_dir=cache_dir,
|
55 |
)
|
56 |
|
57 |
+
target_dir = "book/CFA/*"
|
58 |
|
59 |
# Walk through the directory tree recursively
|
60 |
for root, dirs, files in os.walk(cache_dir):
|
|
|
63 |
# Get the full path of the target directory
|
64 |
target_path = os.path.join(root, target_dir)
|
65 |
|
66 |
+
|
|
|
|
|
|
|
|
|
67 |
|
68 |
# load faiss
|
69 |
docsearch = FAISS.load_local(folder_path=target_path, embeddings=embeddings)
|
|
|
72 |
|
73 |
|
74 |
@st.experimental_memo(show_spinner=False)
|
75 |
+
def load_prompt():
|
76 |
+
system_template="""You are an expert in finance, economics, investing, ethics, derivatives and markets.
|
77 |
+
Use the following pieces of context to answer the users question. If you don't know the answer,
|
78 |
+
just say that you don't know, don't try to make up an answer. Provide a source reference.
|
79 |
+
ALWAYS return a "sources" part in your answer.
|
80 |
+
The "sources" part should be a reference to the source of the documents from which you got your answer. List all sources used
|
81 |
+
|
82 |
+
The output should be a markdown code snippet formatted in the following schema:
|
83 |
+
```json
|
84 |
+
{{
|
85 |
+
answer: is foo
|
86 |
+
sources: xyz
|
87 |
+
}}
|
88 |
+
```
|
89 |
+
Begin!
|
90 |
+
----------------
|
91 |
+
{context}"""
|
92 |
+
messages = [
|
93 |
+
SystemMessagePromptTemplate.from_template(system_template),
|
94 |
+
HumanMessagePromptTemplate.from_template("{question}")
|
95 |
+
]
|
96 |
+
prompt = ChatPromptTemplate.from_messages(messages)
|
97 |
+
|
98 |
+
return prompt
|
99 |
|
100 |
|
101 |
@st.experimental_singleton(show_spinner=False)
|
102 |
def load_chain():
|
103 |
+
llm = ChatOpenAI(temperature=0)
|
104 |
+
|
105 |
+
qa = ChatVectorDBChain.from_llm(llm,
|
106 |
+
load_vectorstore(embeddings),
|
107 |
+
qa_prompt=load_prompt(),
|
108 |
+
return_source_documents=True)
|
|
|
|
|
|
|
|
|
109 |
|
110 |
+
return qa
|
111 |
|
112 |
|
113 |
def get_answer(question):
|
|
|
140 |
|
141 |
return answer, pages, extract
|
142 |
|
|
|
|
|
|
|
143 |
##### sidebar section 2 ####
|
144 |
+
api_key = os.environ["OPENAI_API_KEY"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
145 |
|
146 |
##### main ####
|
147 |
user_input = st.text_input("Your question", "Who are you?", key="input")
|
|
|
157 |
|
158 |
if ask:
|
159 |
|
160 |
+
with st.spinner("this can take about a minute for your first question because some models have to be downloaded π₯Ίππ»ππ»"):
|
161 |
+
try:
|
162 |
+
answer, pages, extract = get_answer(question=user_input)
|
163 |
+
except:
|
164 |
+
st.write(f"Error with Download")
|
165 |
+
st.stop()
|
166 |
+
|
167 |
+
st.write(f"{answer}")
|
|
|
|
|
|
|
|
|
168 |
|
169 |
# sources
|
170 |
with st.expander(label = f"From pages: {pages}", expanded = False):
|