Create Website-Summarize
Browse files- pages/Website-Summarize +150 -0
pages/Website-Summarize
ADDED
@@ -0,0 +1,150 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from newspaper import Article
|
2 |
+
import streamlit as st
|
3 |
+
import requests
|
4 |
+
import json
|
5 |
+
import time
|
6 |
+
import os
|
7 |
+
import nltk
|
8 |
+
nltk.download('punkt')
|
9 |
+
|
10 |
+
## ............................................... ##
|
11 |
+
# Set page configuration (Call this once and make changes as needed)
|
12 |
+
st.set_page_config(page_title='Website Article Summarize', layout='wide', page_icon='📃')
|
13 |
+
|
14 |
+
|
15 |
+
## ............................................... ##
|
16 |
+
with st.container():
|
17 |
+
# Page title layout
|
18 |
+
col1, col2 = st.columns([0.32, 2])
|
19 |
+
with col1:
|
20 |
+
st.image("images/newspaper.png", width=85)
|
21 |
+
|
22 |
+
with col2:
|
23 |
+
st.title("Website Article Summarize")
|
24 |
+
st.markdown("**Generate summaries of articles from websites using abstractive summarization with Language Model and Library NewsPaper.**")
|
25 |
+
st.caption("Created by Bayhaqy.")
|
26 |
+
|
27 |
+
## ............................................... ##
|
28 |
+
with st.container():
|
29 |
+
# Sidebar content
|
30 |
+
st.sidebar.subheader("About the app")
|
31 |
+
st.sidebar.info("This app uses optional 🤗HuggingFace's Model [facebook/bart-large-cnn](https://huggingface.co/facebook/bart-large-cnn) \
|
32 |
+
or [pegasus_indonesian_base-finetune](https://huggingface.co/pegasus_indonesian_base-finetune) model and Library NewsPaper.")
|
33 |
+
st.sidebar.write("\n\n")
|
34 |
+
st.sidebar.markdown("**Get a free API key from HuggingFace:**")
|
35 |
+
st.sidebar.markdown("* Create a [free account](https://huggingface.co/join) or [login](https://huggingface.co/login)")
|
36 |
+
st.sidebar.markdown("* Go to **Settings** and then **Access Tokens**")
|
37 |
+
st.sidebar.markdown("* Create a new Token (select 'read' role)")
|
38 |
+
st.sidebar.markdown("* Paste your API key in the text box")
|
39 |
+
st.sidebar.divider()
|
40 |
+
st.sidebar.write("Please make sure you choose the correct model and is not behind a paywall.")
|
41 |
+
st.sidebar.write("\n\n")
|
42 |
+
st.sidebar.divider()
|
43 |
+
|
44 |
+
with st.container():
|
45 |
+
# Inputs
|
46 |
+
st.subheader("Enter the URL of the website article you want to summarize")
|
47 |
+
default_url = "https://"
|
48 |
+
url = st.text_input("URL:", default_url)
|
49 |
+
|
50 |
+
headers_ = {
|
51 |
+
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.82 Safari/537.36'
|
52 |
+
}
|
53 |
+
|
54 |
+
## ............................................... ##
|
55 |
+
if st.button("Fetch article"):
|
56 |
+
article_url = url
|
57 |
+
session = requests.Session()
|
58 |
+
|
59 |
+
try:
|
60 |
+
response_ = session.get(article_url, headers=headers_, timeout=10)
|
61 |
+
|
62 |
+
if response_.status_code == 200:
|
63 |
+
|
64 |
+
with st.spinner('Fetching your article...'):
|
65 |
+
time.sleep(3)
|
66 |
+
st.success('Your article is ready for summarization!')
|
67 |
+
article = Article(url)
|
68 |
+
article.download()
|
69 |
+
article.parse()
|
70 |
+
|
71 |
+
title = article.title
|
72 |
+
text = article.text
|
73 |
+
|
74 |
+
st.divider()
|
75 |
+
st.subheader("Real Article")
|
76 |
+
with st.expander("See Details"):
|
77 |
+
st.markdown(f"Your article: **{title}**")
|
78 |
+
st.markdown(f"**{text}**")
|
79 |
+
st.divider()
|
80 |
+
|
81 |
+
else:
|
82 |
+
st.write("Error occurred while fetching article.")
|
83 |
+
|
84 |
+
except Exception as e:
|
85 |
+
st.write(f"Error occurred while fetching article: {e}")
|
86 |
+
|
87 |
+
with st.container():
|
88 |
+
# HuggingFace API KEY input
|
89 |
+
#API_KEY = st.text_input("Enter your HuggingFace API key", type="password")
|
90 |
+
HF_TOKEN = os.environ.get("HF_TOKEN", None)
|
91 |
+
|
92 |
+
headers = {"Authorization": f"Bearer {HF_TOKEN}"}
|
93 |
+
|
94 |
+
# Selectbox to choose between API URLs
|
95 |
+
selected_api_url = st.selectbox("Select Model", options=["bart-large-cnn", "pegasus_indonesian_base-finetune"])
|
96 |
+
|
97 |
+
# Determine the selected Model
|
98 |
+
if selected_api_url == "bart-large-cnn":
|
99 |
+
API_URL = "https://api-inference.huggingface.co/models/facebook/bart-large-cnn"
|
100 |
+
else:
|
101 |
+
API_URL = "https://api-inference.huggingface.co/models/thonyyy/pegasus_indonesian_base-finetune"
|
102 |
+
|
103 |
+
with st.container():
|
104 |
+
# Download and parse the article
|
105 |
+
if st.button("Submit to Summarize"):
|
106 |
+
article = Article(url)
|
107 |
+
article.download()
|
108 |
+
article.parse()
|
109 |
+
article.nlp()
|
110 |
+
|
111 |
+
title = article.title
|
112 |
+
text = article.text
|
113 |
+
html = article.html
|
114 |
+
summ = article.summary
|
115 |
+
|
116 |
+
# HuggingFace API request function summary
|
117 |
+
def query_sum(payload):
|
118 |
+
response = requests.post(API_URL, headers=headers, json=payload)
|
119 |
+
return response.json()
|
120 |
+
|
121 |
+
with st.spinner('Doing some AI magic, please wait...'):
|
122 |
+
time.sleep(1)
|
123 |
+
|
124 |
+
# Query the API Summary
|
125 |
+
output_sum = query_sum({"inputs": text, })
|
126 |
+
|
127 |
+
# Display the results
|
128 |
+
summary = output_sum[0]['summary_text'].replace('<n>', " ")
|
129 |
+
|
130 |
+
st.divider()
|
131 |
+
st.subheader("Summary AI")
|
132 |
+
with st.expander("See Details"):
|
133 |
+
st.markdown(f"Your article: **{title}**")
|
134 |
+
st.markdown(f"**{summary}**")
|
135 |
+
|
136 |
+
st.divider()
|
137 |
+
st.subheader("Summary Library NewsPaper")
|
138 |
+
with st.expander("See Details"):
|
139 |
+
st.markdown(f"Your article: **{title}**")
|
140 |
+
st.markdown(f"**{summ}**")
|
141 |
+
|
142 |
+
st.divider()
|
143 |
+
st.subheader("Real Article")
|
144 |
+
with st.expander("See Details"):
|
145 |
+
st.markdown(f"Your article: **{title}**")
|
146 |
+
st.markdown(f"**{text}**")
|
147 |
+
|
148 |
+
with st.container():
|
149 |
+
st.markdown("----")
|
150 |
+
st.markdown("© 2023 Website Article Summarize App")
|