refactor
Browse files
app.py
CHANGED
@@ -1,8 +1,5 @@
|
|
1 |
-
# modified version of https://github.com/hwchase17/langchain-streamlit-template/blob/master/main.py
|
2 |
-
|
3 |
import os
|
4 |
import streamlit as st
|
5 |
-
# from streamlit_chat import message
|
6 |
|
7 |
from langchain.embeddings import HuggingFaceInstructEmbeddings
|
8 |
from langchain.vectorstores.faiss import FAISS
|
@@ -12,6 +9,17 @@ from langchain import OpenAI
|
|
12 |
from langchain import PromptTemplate
|
13 |
|
14 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
15 |
@st.experimental_singleton(show_spinner=False)
|
16 |
def load_vectorstore():
|
17 |
# download from hugging face
|
@@ -43,12 +51,9 @@ def load_vectorstore():
|
|
43 |
|
44 |
return docsearch
|
45 |
|
46 |
-
@st.experimental_singleton(show_spinner=False)
|
47 |
-
def load_chain():
|
48 |
-
|
49 |
-
BOOK_NAME = "1984"
|
50 |
-
AUTHOR_NAME = "George Orwell"
|
51 |
|
|
|
|
|
52 |
prompt_template = f"""You're an AI version of {AUTHOR_NAME}'s book '{BOOK_NAME}' and are supposed to answer quesions people have for the book. Thanks to advancements in AI people can now talk directly to books.
|
53 |
People have a lot of questions after reading {BOOK_NAME}, you are here to answer them as you think the author {AUTHOR_NAME} would, using context from the book.
|
54 |
Where appropriate, briefly elaborate on your answer.
|
@@ -65,16 +70,22 @@ def load_chain():
|
|
65 |
template=prompt_template, input_variables=["context", "question"]
|
66 |
)
|
67 |
|
|
|
|
|
|
|
|
|
|
|
68 |
llm = OpenAI(temperature=0.2)
|
69 |
|
70 |
chain = VectorDBQA.from_chain_type(
|
71 |
-
chain_type_kwargs = {"prompt":
|
72 |
llm=llm,
|
73 |
chain_type="stuff",
|
74 |
vectorstore=load_vectorstore(),
|
75 |
k=8,
|
76 |
return_source_documents=True,
|
77 |
)
|
|
|
78 |
return chain
|
79 |
|
80 |
|
@@ -83,9 +94,7 @@ def get_answer(question):
|
|
83 |
result = chain({"query": question})
|
84 |
|
85 |
answer = result["result"]
|
86 |
-
|
87 |
-
# format sources
|
88 |
-
|
89 |
# pages
|
90 |
unique_sources = set()
|
91 |
for item in result['source_documents']:
|
@@ -95,7 +104,8 @@ def get_answer(question):
|
|
95 |
for item in unique_sources:
|
96 |
unique_pages += str(item) + ", "
|
97 |
|
98 |
-
|
|
|
99 |
|
100 |
# source text
|
101 |
full_source = ""
|
@@ -110,11 +120,9 @@ def get_answer(question):
|
|
110 |
return answer, pages, extract
|
111 |
|
112 |
|
113 |
-
|
114 |
-
st.set_page_config(page_title="Talk2Book: 1984", page_icon="π")
|
115 |
-
st.title("Talk2Book: 1984")
|
116 |
-
st.markdown("#### Have a conversaion with 1984 by George Orwell π")
|
117 |
|
|
|
118 |
with st.sidebar:
|
119 |
api_key = st.text_input(label = "Paste your OpenAI API key here to get started",
|
120 |
type = "password",
|
@@ -126,19 +134,11 @@ with st.sidebar:
|
|
126 |
|
127 |
st.info("Based on [Talk2Book](https://github.com/batmanscode/Talk2Book)")
|
128 |
|
129 |
-
# streamlit-chat not working
|
130 |
-
# i get this error: https://discuss.streamlit.io/t/your-app-is-having-trouble-loading-the-xxx-component/25046
|
131 |
-
# if "generated" not in st.session_state:
|
132 |
-
# st.session_state["generated"] = []
|
133 |
-
|
134 |
-
# if "past" not in st.session_state:
|
135 |
-
# st.session_state["past"] = []
|
136 |
|
137 |
-
|
138 |
-
user_input = st.text_input("Your question", "Who are you?", key="input")
|
139 |
-
return user_input
|
140 |
|
141 |
-
|
|
|
142 |
|
143 |
col1, col2 = st.columns([10, 1])
|
144 |
|
@@ -152,30 +152,18 @@ ask = col2.button("Ask")
|
|
152 |
if ask:
|
153 |
|
154 |
if api_key is "":
|
155 |
-
|
156 |
-
st.write("**1984:** Whoops looks like you forgot your API key buddy")
|
157 |
st.stop()
|
158 |
else:
|
159 |
with st.spinner("Um... excuse me but... this can take about a minute for your first question because some stuff have to be downloaded π₯Ίππ»ππ»"):
|
160 |
try:
|
161 |
answer, pages, extract = get_answer(question=user_input)
|
162 |
except:
|
163 |
-
|
164 |
-
st.write("**1984:** What\'s going on? That's not the right API key")
|
165 |
st.stop()
|
166 |
|
167 |
-
st.write(f"**
|
168 |
|
169 |
# sources
|
170 |
with st.expander(label = f"From pages: {pages}", expanded = False):
|
171 |
-
st.markdown(extract)
|
172 |
-
|
173 |
-
# streamlit-chat
|
174 |
-
# st.session_state.past.append(user_input)
|
175 |
-
# st.session_state.generated.append(output)
|
176 |
-
|
177 |
-
# if st.session_state["generated"]:
|
178 |
-
|
179 |
-
# for i in range(len(st.session_state["generated"]) - 1, -1, -1):
|
180 |
-
# message(st.session_state["generated"][i], key=str(i))
|
181 |
-
# message(st.session_state["past"][i], is_user=True, key=str(i) + "_user")
|
|
|
|
|
|
|
1 |
import os
|
2 |
import streamlit as st
|
|
|
3 |
|
4 |
from langchain.embeddings import HuggingFaceInstructEmbeddings
|
5 |
from langchain.vectorstores.faiss import FAISS
|
|
|
9 |
from langchain import PromptTemplate
|
10 |
|
11 |
|
12 |
+
BOOK_NAME = "1984"
|
13 |
+
AUTHOR_NAME = "George Orwell"
|
14 |
+
|
15 |
+
st.set_page_config(page_title="Talk2Book: 1984", page_icon="π")
|
16 |
+
st.title(f"Talk2Book: {BOOK_NAME}")
|
17 |
+
st.markdown(f"#### Have a conversaion with {BOOK_NAME} by {AUTHOR_NAME} π")
|
18 |
+
|
19 |
+
|
20 |
+
|
21 |
+
|
22 |
+
##### functionss ####
|
23 |
@st.experimental_singleton(show_spinner=False)
|
24 |
def load_vectorstore():
|
25 |
# download from hugging face
|
|
|
51 |
|
52 |
return docsearch
|
53 |
|
|
|
|
|
|
|
|
|
|
|
54 |
|
55 |
+
@st.experimental_memo(show_spinner=False):
|
56 |
+
def load_prompt(book_name, author_name):
|
57 |
prompt_template = f"""You're an AI version of {AUTHOR_NAME}'s book '{BOOK_NAME}' and are supposed to answer quesions people have for the book. Thanks to advancements in AI people can now talk directly to books.
|
58 |
People have a lot of questions after reading {BOOK_NAME}, you are here to answer them as you think the author {AUTHOR_NAME} would, using context from the book.
|
59 |
Where appropriate, briefly elaborate on your answer.
|
|
|
70 |
template=prompt_template, input_variables=["context", "question"]
|
71 |
)
|
72 |
|
73 |
+
return PROMPT
|
74 |
+
|
75 |
+
|
76 |
+
@st.experimental_singleton(show_spinner=False)
|
77 |
+
def load_chain():
|
78 |
llm = OpenAI(temperature=0.2)
|
79 |
|
80 |
chain = VectorDBQA.from_chain_type(
|
81 |
+
chain_type_kwargs = {"prompt": load_prompt(book_name=BOOK_NAME, author_name=AUTHOR_NAME)},
|
82 |
llm=llm,
|
83 |
chain_type="stuff",
|
84 |
vectorstore=load_vectorstore(),
|
85 |
k=8,
|
86 |
return_source_documents=True,
|
87 |
)
|
88 |
+
|
89 |
return chain
|
90 |
|
91 |
|
|
|
94 |
result = chain({"query": question})
|
95 |
|
96 |
answer = result["result"]
|
97 |
+
|
|
|
|
|
98 |
# pages
|
99 |
unique_sources = set()
|
100 |
for item in result['source_documents']:
|
|
|
104 |
for item in unique_sources:
|
105 |
unique_pages += str(item) + ", "
|
106 |
|
107 |
+
# will look like 1, 2, 3,
|
108 |
+
pages = unique_pages[:-1] # removes the last comma
|
109 |
|
110 |
# source text
|
111 |
full_source = ""
|
|
|
120 |
return answer, pages, extract
|
121 |
|
122 |
|
123 |
+
|
|
|
|
|
|
|
124 |
|
125 |
+
##### sidebar ####
|
126 |
with st.sidebar:
|
127 |
api_key = st.text_input(label = "Paste your OpenAI API key here to get started",
|
128 |
type = "password",
|
|
|
134 |
|
135 |
st.info("Based on [Talk2Book](https://github.com/batmanscode/Talk2Book)")
|
136 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
137 |
|
138 |
+
|
|
|
|
|
139 |
|
140 |
+
##### main ####
|
141 |
+
user_input = st.text_input("Your question", "Who are you?", key="input")
|
142 |
|
143 |
col1, col2 = st.columns([10, 1])
|
144 |
|
|
|
152 |
if ask:
|
153 |
|
154 |
if api_key is "":
|
155 |
+
st.write(f"**{BOOK_NAME}:** Whoops looks like you forgot your API key buddy")
|
|
|
156 |
st.stop()
|
157 |
else:
|
158 |
with st.spinner("Um... excuse me but... this can take about a minute for your first question because some stuff have to be downloaded π₯Ίππ»ππ»"):
|
159 |
try:
|
160 |
answer, pages, extract = get_answer(question=user_input)
|
161 |
except:
|
162 |
+
st.write(f"**{BOOK_NAME}:** What\'s going on? That's not the right API key")
|
|
|
163 |
st.stop()
|
164 |
|
165 |
+
st.write(f"**{BOOK_NAME}:** {answer}")
|
166 |
|
167 |
# sources
|
168 |
with st.expander(label = f"From pages: {pages}", expanded = False):
|
169 |
+
st.markdown(extract)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|