Ashrafb commited on
Commit
bfec11e
·
1 Parent(s): c3a6daf

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +62 -0
app.py ADDED
@@ -0,0 +1,62 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import bs4 as bs
3
+ import urllib.request
4
+ import re
5
+ import nltk
6
+ import heapq
7
+
8
+ # NLTK downloads
9
+ nltk.download('punkt')
10
+ nltk.download('stopwords')
11
+
12
+ # Function to summarize text
13
+ def summarize_text(url):
14
+ scraped_data = urllib.request.urlopen(url)
15
+ article = scraped_data.read()
16
+ parsed_article = bs.BeautifulSoup(article,'lxml')
17
+ paragraphs = parsed_article.find_all('p')
18
+ article_text = ""
19
+ for p in paragraphs:
20
+ article_text += p.text
21
+ article_text = re.sub(r'\[[0-9]*\]', ' ', article_text)
22
+ article_text = re.sub(r'\s+', ' ', article_text)
23
+ formatted_article_text = re.sub('[^a-zA-Z]', ' ', article_text )
24
+ formatted_article_text = re.sub(r'\s+', ' ', formatted_article_text)
25
+ sentence_list = nltk.sent_tokenize(article_text)
26
+ stopwords = nltk.corpus.stopwords.words('english')
27
+ word_frequencies = {}
28
+ for word in nltk.word_tokenize(formatted_article_text):
29
+ if word not in stopwords:
30
+ if word not in word_frequencies.keys():
31
+ word_frequencies[word] = 1
32
+ else:
33
+ word_frequencies[word] += 1
34
+ maximum_frequncy = max(word_frequencies.values())
35
+ for word in word_frequencies.keys():
36
+ word_frequencies[word] = (word_frequencies[word]/maximum_frequncy)
37
+ sentence_scores = {}
38
+ for sent in sentence_list:
39
+ for word in nltk.word_tokenize(sent.lower()):
40
+ if word in word_frequencies.keys():
41
+ if len(sent.split(' ')) < 30:
42
+ if sent not in sentence_scores.keys():
43
+ sentence_scores[sent] = word_frequencies[word]
44
+ else:
45
+ sentence_scores[sent] += word_frequencies[word]
46
+ summary_sentences = heapq.nlargest(7, sentence_scores, key=sentence_scores.get)
47
+ summary = ' '.join(summary_sentences)
48
+ return summary
49
+
50
+ # Streamlit UI
51
+ st.title("Text Summarization App")
52
+
53
+ # User input for URL
54
+ url = st.text_input("Enter URL:")
55
+
56
+ if url:
57
+ try:
58
+ summary = summarize_text(url)
59
+ st.subheader("Summary")
60
+ st.write(summary)
61
+ except Exception as e:
62
+ st.error(f"An error occurred: {e}")