-
Notifications
You must be signed in to change notification settings - Fork 5
/
app.py
142 lines (128 loc) · 5.46 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
"""Streamlit app to summarize text, e.g. blog articles"""
# Import from standard library
import logging
# Import from 3rd party libraries
import streamlit as st
import streamlit.components.v1 as components
# Import modules
import scrape as scr
import oai
# Configure logger
logging.basicConfig(format="\n%(asctime)s\n%(message)s", level=logging.INFO, force=True)
# Define functions
def summarize(text: str):
"""Summarize text."""
summary_prompt = "\n\Write an engaging summary for the above article in less than 120 characters:\n\n"
openai = oai.Openai()
flagged = openai.moderate(text)
if flagged:
st.session_state.error = "Input flagged as inappropriate."
return
st.session_state.error = ""
st.session_state.summary = (
openai.complete(prompt=text + summary_prompt)
.strip()
.replace("\n", " ")
.replace('"', "")
)
# Render streamlit page
st.set_page_config(page_title="Summarizer", page_icon="🤖")
if "summary" not in st.session_state:
st.session_state.summary = ""
if "error" not in st.session_state:
st.session_state.error = ""
st.title("Summarize web content")
st.markdown(
"""This mini-app scrapes the paragraphs from a web page,
e.g. a blog post, and summarizes them into a Tweet-sized
statement using OpenAI's [GPTs](https://beta.openai.com/docs/models/overview). You can find the code on [GitHub](https://github.com/kinosal/summarizer) and the author on [Twitter](https://twitter.com/kinosal)."""
)
selectbox = st.selectbox("Raw text or URL source", ("URL", "Raw text"))
if selectbox == "Raw text":
raw_text = st.text_area(label="Text", height=300, max_chars=6000)
if raw_text:
summarize(raw_text)
if st.session_state.summary:
st.text_area(
label="Raw text summary",
value=st.session_state.summary,
height=100,
)
logging.info(f"Text: {raw_text}\nSummary: {st.session_state.summary}")
st.button(
label="Regenerate summary",
type="secondary",
on_click=summarize,
args=[raw_text],
)
elif selectbox == "URL":
url = st.text_input(label="URL")
if url:
scraper = scr.Scraper()
response = scraper.request_url(url)
if "invalid" in str(response).lower():
st.error(str(response))
elif response.status_code != 200:
st.error(f"Response status {response.status_code}")
else:
url_text = (
scraper.extract_content(response)[:6000].strip().replace("\n", " ")
)
summarize(url_text)
if st.session_state.summary:
st.text_area(
label="URL summary", value=st.session_state.summary, height=100
)
logging.info(f"URL: {url}\nSummary: {st.session_state.summary}")
# Force responsive layout for columns also on mobile
st.write(
"""<style>
[data-testid="column"] {
width: calc(50% - 1rem);
flex: 1 1 calc(50% - 1rem);
min-width: calc(50% - 1rem);
}
</style>""",
unsafe_allow_html=True,
)
col1, col2 = st.columns(2)
with col1:
components.html(
f"""
<a href="https://twitter.com/share?ref_src=twsrc%5Etfw" class="twitter-share-button" data-size="large" data-text="{st.session_state.summary}\n- Summary generated via web-summarizer.streamlit.app of" data-url="{url}" data-show-count="false">Tweet</a><script async src="https://platform.twitter.com/widgets.js" charset="utf-8"></script>
""",
height=45,
)
with col2:
st.button(
label="Regenerate summary",
type="secondary",
on_click=summarize,
args=[url_text],
)
if st.session_state.error:
st.error(st.session_state.error)
if st.session_state.summary:
st.markdown("""---""")
col1, col2 = st.columns(2)
with col1:
st.markdown(
"**Other Streamlit apps by [@kinosal](https://twitter.com/kinosal)**"
)
st.markdown("[Tweet Generator](https://tweets.streamlit.app)")
st.markdown("[Twitter Wrapped](https://twitter-likes.streamlit.app)")
st.markdown("[Code Translator](https://english-to-code.streamlit.app)")
st.markdown("[PDF Analyzer](https://pdf-keywords.streamlit.app)")
with col2:
st.write("If you like this app, please consider to")
components.html(
"""
<form action="https://www.paypal.com/donate" method="post" target="_top">
<input type="hidden" name="hosted_button_id" value="8JJTGY95URQCQ" />
<input type="image" src="https://pics.paypal.com/00/s/MDY0MzZhODAtNGI0MC00ZmU5LWI3ODYtZTY5YTcxOTNlMjRm/file.PNG" height="35" border="0" name="submit" title="Donate with PayPal" alt="Donate with PayPal button" />
<img alt="" border="0" src="https://www.paypal.com/en_US/i/scr/pixel.gif" width="1" height="1" />
</form>
""",
height=45,
)
st.write("so I can keep it alive. Thank you!")