Devika Nair M
commited on
Update Briefly.py
Browse files- Briefly.py +23 -18
Briefly.py
CHANGED
@@ -1,24 +1,28 @@
|
|
1 |
import streamlit as st #Web App
|
2 |
from gnewsclient import gnewsclient # for fetching google news
|
3 |
from newspaper import Article # to obtain text from news articles
|
|
|
4 |
import spacy # to obtain keyword
|
5 |
from annotated_text import annotated_text # to display keywords
|
6 |
-
import requests
|
7 |
|
8 |
|
9 |
-
# Load sshleifer/distilbart-cnn-12-6 model
|
10 |
-
|
|
|
|
|
|
|
11 |
|
|
|
12 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
|
14 |
-
def query(payload):
|
15 |
-
response = requests.post(API_URL, headers=headers, json=payload)
|
16 |
-
return response.json()
|
17 |
-
|
18 |
-
|
19 |
-
data = gnewsclient.NewsClient(max_results=0)
|
20 |
|
21 |
-
st.cache(allow_output_mutation=True)
|
22 |
# obtain urls and it's content
|
23 |
def getNews(topic,location):
|
24 |
count=0
|
@@ -48,13 +52,13 @@ def getNews(topic,location):
|
|
48 |
continue
|
49 |
return contents,titles,authors,urls
|
50 |
|
51 |
-
|
52 |
# Summarizes the content- minimum word limit 30 and maximum 60
|
53 |
-
def getNewsSummary(contents):
|
54 |
summaries=[]
|
55 |
for content in contents:
|
56 |
-
|
57 |
-
summaries.append(
|
58 |
return summaries
|
59 |
|
60 |
|
@@ -102,21 +106,22 @@ def DisplaySummary(titles,authors,summaries,keywords,urls):
|
|
102 |
st.text("")
|
103 |
|
104 |
|
105 |
-
def main():
|
|
|
106 |
st.title('Briefly')
|
107 |
with st.expander('Read trending news in less than 60 words...', expanded=True):
|
108 |
with st.form(key='form1'):
|
109 |
topic=st.selectbox('Category:',data.topics[2:]+["World"])
|
110 |
location=st.selectbox('Location:',data.locations)
|
111 |
submit_button=st.form_submit_button()
|
112 |
-
|
113 |
if submit_button:
|
114 |
with st.spinner('Fetching news...'):
|
115 |
contents,titles,authors,urls=getNews(topic,location)
|
116 |
-
summaries=getNewsSummary(contents)
|
117 |
keywords=generateKeyword(contents)
|
118 |
DisplaySummary(titles,authors,summaries,keywords,urls)
|
119 |
|
120 |
|
121 |
if __name__ == '__main__':
|
122 |
-
main()
|
|
|
1 |
import streamlit as st #Web App
|
2 |
from gnewsclient import gnewsclient # for fetching google news
|
3 |
from newspaper import Article # to obtain text from news articles
|
4 |
+
from transformers import pipeline # to summarize text
|
5 |
import spacy # to obtain keyword
|
6 |
from annotated_text import annotated_text # to display keywords
|
|
|
7 |
|
8 |
|
9 |
+
# Load sshleifer/distilbart-cnn-12-6 model
|
10 |
+
@st.cache(allow_output_mutation=True)
|
11 |
+
def load_model():
|
12 |
+
model = pipeline.from_pretrained("sshleifer/distilbart-cnn-12-6")
|
13 |
+
return model
|
14 |
|
15 |
+
data = gnewsclient.NewsClient(max_results=0)
|
16 |
|
17 |
+
#faster method - inference api - 30k characters/mo
|
18 |
+
#API_URL = "https://api-inference.huggingface.co/models/sshleifer/distilbart-cnn-12-6"
|
19 |
+
#API_KEY=os.getenv("API_KEY")
|
20 |
+
#headers = {"Authorization": f"Bearer {API_KEY}"}
|
21 |
+
#def query(payload):
|
22 |
+
# response = requests.post(API_URL, headers=headers, json=payload)
|
23 |
+
# return response.json()
|
24 |
|
|
|
|
|
|
|
|
|
|
|
|
|
25 |
|
|
|
26 |
# obtain urls and it's content
|
27 |
def getNews(topic,location):
|
28 |
count=0
|
|
|
52 |
continue
|
53 |
return contents,titles,authors,urls
|
54 |
|
55 |
+
|
56 |
# Summarizes the content- minimum word limit 30 and maximum 60
|
57 |
+
def getNewsSummary(contents,summarizer):
|
58 |
summaries=[]
|
59 |
for content in contents:
|
60 |
+
minimum=len(content.split())
|
61 |
+
summaries.append(summarizer(content,max_length=60,min_length=min(30,minimum),do_sample=False,truncation=True)[0]['summary_text'])
|
62 |
return summaries
|
63 |
|
64 |
|
|
|
106 |
st.text("")
|
107 |
|
108 |
|
109 |
+
def main():
|
110 |
+
summarizer=load_model()
|
111 |
st.title('Briefly')
|
112 |
with st.expander('Read trending news in less than 60 words...', expanded=True):
|
113 |
with st.form(key='form1'):
|
114 |
topic=st.selectbox('Category:',data.topics[2:]+["World"])
|
115 |
location=st.selectbox('Location:',data.locations)
|
116 |
submit_button=st.form_submit_button()
|
117 |
+
|
118 |
if submit_button:
|
119 |
with st.spinner('Fetching news...'):
|
120 |
contents,titles,authors,urls=getNews(topic,location)
|
121 |
+
summaries=getNewsSummary(contents,summarizer)
|
122 |
keywords=generateKeyword(contents)
|
123 |
DisplaySummary(titles,authors,summaries,keywords,urls)
|
124 |
|
125 |
|
126 |
if __name__ == '__main__':
|
127 |
+
main()
|