add an expandable sources section
Browse files
app.py
CHANGED
|
@@ -82,18 +82,32 @@ def get_answer(question):
|
|
| 82 |
chain = load_chain()
|
| 83 |
result = chain({"query": question})
|
| 84 |
|
|
|
|
|
|
|
| 85 |
# format sources
|
| 86 |
-
unique_sources = set()
|
| 87 |
|
|
|
|
|
|
|
| 88 |
for item in result['source_documents']:
|
| 89 |
unique_sources.add(item.metadata['page'])
|
| 90 |
|
| 91 |
-
|
| 92 |
-
|
| 93 |
for item in unique_sources:
|
| 94 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 95 |
|
| 96 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 97 |
|
| 98 |
|
| 99 |
# From here down is all the StreamLit UI.
|
|
@@ -103,12 +117,13 @@ st.markdown("#### Have a conversaion with 1984 by George Orwell π")
|
|
| 103 |
|
| 104 |
with st.sidebar:
|
| 105 |
api_key = st.text_input(label = "Paste your OpenAI API key here to get started", type = "password")
|
| 106 |
-
|
| 107 |
-
# if api_key:
|
| 108 |
os.environ["OPENAI_API_KEY"] = api_key
|
| 109 |
-
|
| 110 |
st.info("This isn't saved π")
|
| 111 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 112 |
# streamlit-chat not working
|
| 113 |
# i get this error: https://discuss.streamlit.io/t/your-app-is-having-trouble-loading-the-xxx-component/25046
|
| 114 |
# if "generated" not in st.session_state:
|
|
@@ -135,15 +150,23 @@ ask = col2.button("Ask")
|
|
| 135 |
if ask:
|
| 136 |
|
| 137 |
if api_key is "":
|
| 138 |
-
output = "Whoops looks like you forgot your API key buddy"
|
|
|
|
|
|
|
| 139 |
else:
|
| 140 |
with st.spinner("Um... excuse me but... this can take about a minute for your first question because some stuff have to be downloaded π₯Ίππ»ππ»"):
|
| 141 |
try:
|
| 142 |
-
|
| 143 |
except:
|
| 144 |
-
output = "What's going on? That's not the right API key"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 145 |
|
| 146 |
-
|
|
|
|
|
|
|
| 147 |
|
| 148 |
# streamlit-chat
|
| 149 |
# st.session_state.past.append(user_input)
|
|
|
|
| 82 |
chain = load_chain()
|
| 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']:
|
| 92 |
unique_sources.add(item.metadata['page'])
|
| 93 |
|
| 94 |
+
unique_pages = ""
|
|
|
|
| 95 |
for item in unique_sources:
|
| 96 |
+
unique_pages += str(item) + ", "
|
| 97 |
+
|
| 98 |
+
pages = unique_pages # will look like 1, 2, 3,
|
| 99 |
+
|
| 100 |
+
# source text
|
| 101 |
+
full_source = ""
|
| 102 |
+
for item in result['source_documents']:
|
| 103 |
+
full_source += f"- **Page: {item.metadata['page']}**" + "\n" + item.page_content + "\n\n"
|
| 104 |
|
| 105 |
+
# will look like:
|
| 106 |
+
# - Page: {number}
|
| 107 |
+
# {extracted text from book}
|
| 108 |
+
extract = full_source
|
| 109 |
+
|
| 110 |
+
return answer, pages, extract
|
| 111 |
|
| 112 |
|
| 113 |
# From here down is all the StreamLit UI.
|
|
|
|
| 117 |
|
| 118 |
with st.sidebar:
|
| 119 |
api_key = st.text_input(label = "Paste your OpenAI API key here to get started", type = "password")
|
|
|
|
|
|
|
| 120 |
os.environ["OPENAI_API_KEY"] = api_key
|
|
|
|
| 121 |
st.info("This isn't saved π")
|
| 122 |
|
| 123 |
+
st.markdown("---")
|
| 124 |
+
|
| 125 |
+
st.write("Based on [Talk2Book](https://github.com/batmanscode/Talk2Book)")
|
| 126 |
+
|
| 127 |
# streamlit-chat not working
|
| 128 |
# i get this error: https://discuss.streamlit.io/t/your-app-is-having-trouble-loading-the-xxx-component/25046
|
| 129 |
# if "generated" not in st.session_state:
|
|
|
|
| 150 |
if ask:
|
| 151 |
|
| 152 |
if api_key is "":
|
| 153 |
+
# output = "Whoops looks like you forgot your API key buddy"
|
| 154 |
+
st.write("**1984:** Whoops looks like you forgot your API key buddy")
|
| 155 |
+
break
|
| 156 |
else:
|
| 157 |
with st.spinner("Um... excuse me but... this can take about a minute for your first question because some stuff have to be downloaded π₯Ίππ»ππ»"):
|
| 158 |
try:
|
| 159 |
+
answer, pages, extract = get_answer(question=user_input)
|
| 160 |
except:
|
| 161 |
+
# output = "What's going on? That's not the right API key"
|
| 162 |
+
st.write("**1984:** What\'s going on? That's not the right API key")
|
| 163 |
+
break
|
| 164 |
+
|
| 165 |
+
st.write(f"**1984:** {answer}")
|
| 166 |
|
| 167 |
+
# sources
|
| 168 |
+
with st.expander(label = f"From pages: {pages}", expanded = False):
|
| 169 |
+
st.markdown(extract)
|
| 170 |
|
| 171 |
# streamlit-chat
|
| 172 |
# st.session_state.past.append(user_input)
|