Bayhaqy commited on
Commit
1db46cf
1 Parent(s): e50c3fb

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +105 -0
app.py ADDED
@@ -0,0 +1,105 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import json
3
+ import requests
4
+ import time
5
+ from newspaper import Article
6
+
7
+
8
+ # Page title layout
9
+ c1, c2 = st.columns([0.32, 2])
10
+
11
+ with c1:
12
+ st.image("images/newspaper.png", width=85)
13
+
14
+ with c2:
15
+ st.title("FastNews Article Summarizer")
16
+
17
+ st.markdown("**Generate summaries of articles and blog posts using abstractive summarization with Google's Pegasus language model.**")
18
+
19
+
20
+ # Sidebar content
21
+ st.sidebar.subheader("About the app")
22
+ st.sidebar.info("This app uses 🤗HuggingFace's [google/pegasus-cnn_dailymail](https://huggingface.co/google/pegasus-cnn_dailymail) model.\
23
+ \nYou can find the source code [here](https://github.com/ivnlee/streamlit-text-summarizer)")
24
+ st.sidebar.write("\n\n")
25
+ st.sidebar.markdown("**Get a free API key from HuggingFace:**")
26
+ st.sidebar.markdown("* Create a [free account](https://huggingface.co/join) or [login](https://huggingface.co/login)")
27
+ st.sidebar.markdown("* Go to **Settings** and then **Access Tokens**")
28
+ st.sidebar.markdown("* Create a new Token (select 'read' role)")
29
+ st.sidebar.markdown("* Paste your API key in the text box")
30
+ st.sidebar.divider()
31
+ st.sidebar.write("Please make sure your article is in English and is not behind a paywall.")
32
+ st.sidebar.write("\n\n")
33
+ st.sidebar.divider()
34
+ st.sidebar.caption("Created by [Ivan Lee](https://ivan-lee.medium.com/) using [Streamlit](https://streamlit.io/)🎈.")
35
+
36
+
37
+ # Inputs
38
+ st.subheader("Enter the URL of the article you want to summarize")
39
+ default_url = "https://"
40
+ url = st.text_input("URL:", default_url)
41
+
42
+ headers_ = {
43
+ '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'
44
+ }
45
+
46
+ fetch_button = st.button("Fetch article")
47
+
48
+ if fetch_button:
49
+ article_url = url
50
+ session = requests.Session()
51
+
52
+ try:
53
+ response_ = session.get(article_url, headers=headers_, timeout=10)
54
+
55
+ if response_.status_code == 200:
56
+
57
+ with st.spinner('Fetching your article...'):
58
+ time.sleep(3)
59
+ st.success('Your article is ready for summarization!')
60
+
61
+ else:
62
+ st.write("Error occurred while fetching article.")
63
+
64
+ except Exception as e:
65
+ st.write(f"Error occurred while fetching article: {e}")
66
+
67
+
68
+ # HuggingFace API KEY input
69
+ API_KEY = st.text_input("Enter your HuggingFace API key", type="password")
70
+
71
+ # HuggingFace API inference URL.
72
+ API_URL = "https://api-inference.huggingface.co/models/google/pegasus-cnn_dailymail"
73
+
74
+ headers = {"Authorization": f"Bearer {API_KEY}"}
75
+
76
+
77
+ submit_button = st.button("Submit")
78
+
79
+ # Download and parse the article
80
+ if submit_button:
81
+ article = Article(url)
82
+ article.download()
83
+ article.parse()
84
+
85
+ title = article.title
86
+ text = article.text
87
+
88
+ # HuggingFace API request function
89
+ def query(payload):
90
+ response = requests.post(API_URL, headers=headers, json=payload)
91
+ return response.json()
92
+
93
+ with st.spinner('Doing some AI magic, please wait...'):
94
+ time.sleep(1)
95
+
96
+ # Query the API
97
+ output = query({"inputs": text, })
98
+
99
+ # Display the results
100
+ summary = output[0]['summary_text'].replace('<n>', " ")
101
+
102
+ st.divider()
103
+ st.subheader("Summary")
104
+ st.write(f"Your article: **{title}**")
105
+ st.write(f"**{summary}**")