im commited on
Commit ·
14feef8
1
Parent(s): 0ade4ed
read query parameters to be able to pass a deep link skipping the search main page
Browse files
app.py
CHANGED
|
@@ -1,5 +1,4 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
-
import openai
|
| 3 |
import logging
|
| 4 |
import sys
|
| 5 |
import os
|
|
@@ -61,14 +60,11 @@ def scrape_the_article(url):
|
|
| 61 |
|
| 62 |
|
| 63 |
def init_session() -> None:
|
| 64 |
-
st.session_state
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
st.session_state.end = False
|
| 68 |
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
st.session_state.open_api_key = open_api_key
|
| 72 |
|
| 73 |
|
| 74 |
@st.cache_data
|
|
@@ -179,47 +175,72 @@ def show_question_input():
|
|
| 179 |
st.button(q, on_click=on_question_button, args=[q])
|
| 180 |
|
| 181 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 182 |
def main() -> None:
|
| 183 |
try:
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
if 'content' not in st.session_state:
|
| 191 |
-
init_session()
|
| 192 |
st.header("Doodle")
|
| 193 |
st.image("./assets/doodle-img.jpg")
|
| 194 |
description = """\
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
st.caption(description)
|
| 201 |
st.divider()
|
| 202 |
|
| 203 |
-
content_url = st.text_input(label='Paste your link, e.g. https://expresso.today',
|
|
|
|
| 204 |
placeholder='Paste your link, e.g. https://expresso.today')
|
| 205 |
col1, _, _, _, col2 = st.columns(5)
|
| 206 |
col1.button("Doodle")
|
| 207 |
if col2.button("Random Page"):
|
| 208 |
-
content_url =
|
| 209 |
-
|
| 210 |
if len(content_url) > 0:
|
| 211 |
if is_valid_web_link(content_url):
|
| 212 |
-
|
| 213 |
-
st.session_state.web_url = content_url
|
| 214 |
-
st.session_state.web_page = scrape_the_article(content_url)
|
| 215 |
-
st.session_state.title = st.session_state.web_page['title']
|
| 216 |
-
st.session_state.content = st.session_state.web_page['content']
|
| 217 |
st.experimental_rerun()
|
| 218 |
else:
|
| 219 |
-
st.warning(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 220 |
elif 'content_summary' not in st.session_state:
|
| 221 |
content_summary = get_content_summary(content=st.session_state.content, model_name="gpt-3.5-turbo-16k",
|
| 222 |
-
api_key=
|
| 223 |
st.session_state.content_summary = content_summary['summary']
|
| 224 |
st.session_state.content_block_summary = [s['block_summary'] for s in content_summary['blocks']]
|
| 225 |
st.session_state.content_block_questions = [s['block_question'] for s in content_summary['blocks']]
|
|
@@ -231,8 +252,7 @@ def main() -> None:
|
|
| 231 |
st.subheader(question)
|
| 232 |
st.divider()
|
| 233 |
with st.spinner(f'answering the question...'):
|
| 234 |
-
answer = qa(query=question, documents_to_search=20, model_name='gpt-4',
|
| 235 |
-
api_key=st.session_state.open_api_key)
|
| 236 |
show_audio_message(answer)
|
| 237 |
st.session_state.question = None
|
| 238 |
show_question_input()
|
|
@@ -248,7 +268,6 @@ def main() -> None:
|
|
| 248 |
st.experimental_rerun()
|
| 249 |
|
| 250 |
|
| 251 |
-
|
| 252 |
if __name__ == "__main__":
|
| 253 |
main()
|
| 254 |
|
|
|
|
| 1 |
import streamlit as st
|
|
|
|
| 2 |
import logging
|
| 3 |
import sys
|
| 4 |
import os
|
|
|
|
| 60 |
|
| 61 |
|
| 62 |
def init_session() -> None:
|
| 63 |
+
if 'init' not in st.session_state:
|
| 64 |
+
st.session_state.init = True
|
| 65 |
+
st.session_state.question = None
|
|
|
|
| 66 |
|
| 67 |
+
st.session_state.messages = []
|
|
|
|
|
|
|
| 68 |
|
| 69 |
|
| 70 |
@st.cache_data
|
|
|
|
| 175 |
st.button(q, on_click=on_question_button, args=[q])
|
| 176 |
|
| 177 |
|
| 178 |
+
def get_query_params():
|
| 179 |
+
if 'web_url' not in st.session_state:
|
| 180 |
+
params = st.experimental_get_query_params()
|
| 181 |
+
logging.debug(f"query parameters: {params}")
|
| 182 |
+
if 'web_url' in params:
|
| 183 |
+
web_url = params['web_url'][0]
|
| 184 |
+
if len(web_url) > 0:
|
| 185 |
+
if is_valid_web_link(web_url):
|
| 186 |
+
st.session_state.web_url = web_url
|
| 187 |
+
|
| 188 |
+
|
| 189 |
+
def show_header():
|
| 190 |
+
if 'web_url' in st.session_state:
|
| 191 |
+
col1, col2 = st.columns(2)
|
| 192 |
+
col1.caption(f"discussing: {st.session_state.web_url}")
|
| 193 |
+
if 'title' in st.session_state:
|
| 194 |
+
col2.caption(f"{st.session_state.title}")
|
| 195 |
+
|
| 196 |
+
|
| 197 |
+
def get_random_page():
|
| 198 |
+
return 'https://mailchi.mp/expresso/lightpeak'
|
| 199 |
+
|
| 200 |
+
|
| 201 |
def main() -> None:
|
| 202 |
try:
|
| 203 |
+
get_query_params()
|
| 204 |
+
init_session()
|
| 205 |
+
show_header()
|
| 206 |
+
|
| 207 |
+
if 'web_url' not in st.session_state:
|
|
|
|
|
|
|
|
|
|
| 208 |
st.header("Doodle")
|
| 209 |
st.image("./assets/doodle-img.jpg")
|
| 210 |
description = """\
|
| 211 |
+
Meet 'Doodle,' your shortcut to understanding the web! Got a lengthy article you're eyeing?
|
| 212 |
+
Just paste the link, and in an instant, Doodle delivers a crisp summary and intriguing questions for you to
|
| 213 |
+
chew on. Want to go hands-free? Doodle's text-to-speech feature will read it to you! Why the name 'Doodle'?
|
| 214 |
+
Just as a simple doodle can encapsulate a whole idea, we distill webpages down to their essence!
|
| 215 |
+
"""
|
| 216 |
st.caption(description)
|
| 217 |
st.divider()
|
| 218 |
|
| 219 |
+
content_url = st.text_input(label='Paste your link, e.g. https://expresso.today',
|
| 220 |
+
label_visibility='collapsed',
|
| 221 |
placeholder='Paste your link, e.g. https://expresso.today')
|
| 222 |
col1, _, _, _, col2 = st.columns(5)
|
| 223 |
col1.button("Doodle")
|
| 224 |
if col2.button("Random Page"):
|
| 225 |
+
content_url = get_random_page()
|
|
|
|
| 226 |
if len(content_url) > 0:
|
| 227 |
if is_valid_web_link(content_url):
|
| 228 |
+
st.session_state.web_url = content_url
|
|
|
|
|
|
|
|
|
|
|
|
|
| 229 |
st.experimental_rerun()
|
| 230 |
else:
|
| 231 |
+
st.warning(
|
| 232 |
+
"Whoops! That link seems to be doing the vanishing act. Could you give it another shot? Magic words: 'Valid Link, Please!' 🪄")
|
| 233 |
+
|
| 234 |
+
elif 'content' not in st.session_state:
|
| 235 |
+
with st.spinner(f"reading the web page '{st.session_state.web_url}' ..."):
|
| 236 |
+
st.session_state.web_page = scrape_the_article(st.session_state.web_url)
|
| 237 |
+
st.session_state.title = st.session_state.web_page['title']
|
| 238 |
+
st.session_state.content = st.session_state.web_page['content']
|
| 239 |
+
st.experimental_rerun()
|
| 240 |
+
|
| 241 |
elif 'content_summary' not in st.session_state:
|
| 242 |
content_summary = get_content_summary(content=st.session_state.content, model_name="gpt-3.5-turbo-16k",
|
| 243 |
+
api_key=open_api_key)
|
| 244 |
st.session_state.content_summary = content_summary['summary']
|
| 245 |
st.session_state.content_block_summary = [s['block_summary'] for s in content_summary['blocks']]
|
| 246 |
st.session_state.content_block_questions = [s['block_question'] for s in content_summary['blocks']]
|
|
|
|
| 252 |
st.subheader(question)
|
| 253 |
st.divider()
|
| 254 |
with st.spinner(f'answering the question...'):
|
| 255 |
+
answer = qa(query=question, documents_to_search=20, model_name='gpt-4', api_key=open_api_key)
|
|
|
|
| 256 |
show_audio_message(answer)
|
| 257 |
st.session_state.question = None
|
| 258 |
show_question_input()
|
|
|
|
| 268 |
st.experimental_rerun()
|
| 269 |
|
| 270 |
|
|
|
|
| 271 |
if __name__ == "__main__":
|
| 272 |
main()
|
| 273 |
|