asrilmurdian commited on
Commit
93addbf
·
verified ·
1 Parent(s): 2d21c86

Update app.py

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