Devika Nair M commited on
Commit
ff3bfda
·
unverified ·
1 Parent(s): caaf58e

Update Briefly.py

Browse files
Files changed (1) hide show
  1. Briefly.py +17 -14
Briefly.py CHANGED
@@ -1,20 +1,24 @@
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('summarization')
13
- return model
14
 
15
- data = gnewsclient.NewsClient(max_results=0)
16
 
 
 
 
 
 
 
17
 
 
18
  # obtain urls and it's content
19
  def getNews(topic,location):
20
  count=0
@@ -46,11 +50,11 @@ def getNews(topic,location):
46
 
47
 
48
  # Summarizes the content- minimum word limit 30 and maximum 60
49
- def getNewsSummary(contents,summarizer):
50
  summaries=[]
51
  for content in contents:
52
- minimum=len(content.split())
53
- summaries.append(summarizer(content,max_length=60,min_length=min(30,minimum),do_sample=False,truncation=True)[0]['summary_text'])
54
  return summaries
55
 
56
 
@@ -98,8 +102,7 @@ def DisplaySummary(titles,authors,summaries,keywords,urls):
98
  st.text("")
99
 
100
 
101
- def main():
102
- summarizer=load_model()
103
  st.title('Briefly')
104
  with st.expander('Read trending news in less than 60 words...', expanded=True):
105
  with st.form(key='form1'):
@@ -110,10 +113,10 @@ def main():
110
  if submit_button:
111
  with st.spinner('Fetching news...'):
112
  contents,titles,authors,urls=getNews(topic,location)
113
- summaries=getNewsSummary(contents,summarizer)
114
  keywords=generateKeyword(contents)
115
  DisplaySummary(titles,authors,summaries,keywords,urls)
116
 
117
 
118
  if __name__ == '__main__':
119
- 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
  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 using Accelerated Inference API
10
+ API_URL = "https://api-inference.huggingface.co/models/sshleifer/distilbart-cnn-12-6"
11
+ headers = {"Authorization": "Bearer api_GWvAcWrXIkIVYvBeyDyzJBOfGXhekPhFID"}
 
 
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
 
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
+ summary = query({"inputs":content})[0]["summary_text"]
57
+ summaries.append(summary)
58
  return summaries
59
 
60
 
 
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'):
 
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()