Spaces:
Runtime error
Runtime error
Upload app.py
Browse files
app.py
ADDED
@@ -0,0 +1,58 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import wikipedia
|
3 |
+
import spacy
|
4 |
+
from spacy.lang.en.stop_words import STOP_WORDS
|
5 |
+
from string import punctuation
|
6 |
+
import nltk
|
7 |
+
nltk.download('wordnet', quiet=True)
|
8 |
+
from nltk.stem import WordNetLemmatizer
|
9 |
+
from heapq import nlargest
|
10 |
+
import warnings
|
11 |
+
|
12 |
+
|
13 |
+
warnings.filterwarnings("ignore")
|
14 |
+
|
15 |
+
def get_wiki_summary(rawtext = input('Requested Topic from Wikipedia : ')):
|
16 |
+
text = wikipedia.summary(rawtext)
|
17 |
+
print('\033[1m' + "Original Text Fetched from Wikipedia" + '\033[0m')
|
18 |
+
|
19 |
+
print(text)
|
20 |
+
|
21 |
+
stopwords = list(STOP_WORDS)
|
22 |
+
nlp = spacy.load("en_core_web_sm")
|
23 |
+
|
24 |
+
lemmatizer = WordNetLemmatizer()
|
25 |
+
tokens = [lemmatizer.lemmatize(str(token).lower()) for token in nlp(text) if str(token) not in punctuation and str(token).lower() not in stopwords and len(token) >1]
|
26 |
+
word_counts = {}
|
27 |
+
|
28 |
+
for token in tokens:
|
29 |
+
if token in word_counts.keys():
|
30 |
+
word_counts[token] += 1
|
31 |
+
else:
|
32 |
+
word_counts[token] = 1
|
33 |
+
|
34 |
+
|
35 |
+
|
36 |
+
sentence_scores = {}
|
37 |
+
|
38 |
+
for sentence in nlp(text).sents:
|
39 |
+
sentence_scores[sentence] = 0
|
40 |
+
for wrd in sentence:
|
41 |
+
if lemmatizer.lemmatize(str(wrd).lower()) in word_counts.keys():
|
42 |
+
sentence_scores[sentence] += word_counts[lemmatizer.lemmatize(str(wrd).lower())]
|
43 |
+
|
44 |
+
summary_length = int(len(sentence_scores)*0.20)
|
45 |
+
summary = str()
|
46 |
+
|
47 |
+
for sentence in nlp(text).sents:
|
48 |
+
for i in range(0,summary_length):
|
49 |
+
if str(sentence).find(str(nlargest(summary_length, sentence_scores, key = sentence_scores.get)[i])) == 0:
|
50 |
+
summary += str(sentence)
|
51 |
+
summary += ' '
|
52 |
+
|
53 |
+
|
54 |
+
print('\033[1m' + "Summarized Text" + '\033[0m')
|
55 |
+
|
56 |
+
return print(summary)
|
57 |
+
|
58 |
+
gr.Interface(fn=get_wiki_summary, inputs=gr.inputs.Textbox(lines=7, label="Input Text"), outputs="text").launch(inline=False)
|