Priyanka-Kumavat commited on
Commit
43f1aad
1 Parent(s): 9e00f06

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +95 -0
  2. requirements.txt +3 -0
app.py ADDED
@@ -0,0 +1,95 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from heapq import nlargest
2
+ import spacy
3
+ from spacy.lang.en.stop_words import STOP_WORDS
4
+ from string import punctuation
5
+ import gradio as gr
6
+
7
+
8
+
9
+ # Stopwords
10
+ stopwords = list(STOP_WORDS)
11
+ nlp = spacy.load('en_core_web_sm')
12
+ punctuation = punctuation + '\n'
13
+ import spacy
14
+ from spacy.lang.en.stop_words import STOP_WORDS
15
+ from string import punctuation
16
+
17
+ # Prediction
18
+ def prediction(text):
19
+ doc = nlp(text)
20
+ len1 = len(text)
21
+ tokens = [token.text for token in doc]
22
+ word_frequencies = {}
23
+ for word in doc:
24
+ if word.text.lower() not in stopwords:
25
+ if word.text.lower() not in punctuation:
26
+ if word.text not in word_frequencies.keys():
27
+ word_frequencies[word.text] = 1
28
+ else:
29
+ word_frequencies[word.text] += 1
30
+ max_frequency = max(word_frequencies.values())
31
+ for word in word_frequencies.keys():
32
+ word_frequencies[word] = word_frequencies[word]/max_frequency
33
+ sentence_tokens = [sent for sent in doc.sents]
34
+ sentence_scores = {}
35
+ for sent in sentence_tokens:
36
+ for word in sent:
37
+ if word.text.lower() in word_frequencies.keys():
38
+ if sent not in sentence_scores.keys():
39
+ sentence_scores[sent] = word_frequencies[word.text.lower()]
40
+ else:
41
+ sentence_scores[sent] += word_frequencies[word.text.lower()]
42
+ select_length = int(len(sentence_tokens)*0.3)
43
+ summary = nlargest(select_length, sentence_scores, key = sentence_scores.get)
44
+ org_len = len(text.split(' '))
45
+ summary = (str(summary[0]))
46
+ sum_len = len(summary.split(' '))
47
+ return summary,org_len,sum_len
48
+
49
+
50
+
51
+
52
+ EXAMPLES = [["""
53
+ Maria Sharapova has basically no friends as tennis players on the WTA Tour. The Russian player has no problems in openly speaking about it and in a recent interview she said: 'I don't really hide any feelings too much.
54
+ I think everyone knows this is my job here. When I'm on the courts or when I'm on the court playing, I'm a competitor and I want to beat every single person whether they're in the locker room or across the net.
55
+ So I'm not the one to strike up a conversation about the weather and know that in the next few minutes I have to go and try to win a tennis match.
56
+ I'm a pretty competitive girl. I say my hellos, but I'm not sending any players flowers as well. Uhm, I'm not really friendly or close to many players.
57
+ I have not a lot of friends away from the courts.' When she said she is not really close to a lot of players, is that something strategic that she is doing? Is it different on the men's tour than the women's tour? 'No, not at all.
58
+ I think just because you're in the same sport doesn't mean that you have to be friends with everyone just because you're categorized, you're a tennis player, so you're going to get along with tennis players.
59
+ I think every person has different interests. I have friends that have completely different jobs and interests, and I've met them in very different parts of my life.
60
+ I think everyone just thinks because we're tennis players we should be the greatest of friends. But ultimately tennis is just a very small part of what we do.
61
+ There are so many other things that we're interested in, that we do.'
62
+ """],["""The tower is 324 metres (1,063 ft) tall, about the same height as an 81-storey building,
63
+ and the tallest structure in Paris. Its base is square, measuring 125 metres (410 ft) on each side.
64
+ During its construction, the Eiffel Tower surpassed the Washington Monument to become the tallest man-made structure in the world,
65
+ a title it held for 41 years until the Chrysler Building in New York City was finished in 1930.
66
+ It was the first structure to reach a height of 300 metres.
67
+ Due to the addition of a broadcasting aerial at the top of the tower in 1957, it is now taller than the Chrysler Building by 5.2 metres (17 ft).
68
+ Excluding transmitters, the Eiffel Tower is the second tallest free-standing structure in France after the Millau Viaduct."""]]
69
+
70
+ DESCRIPTION = """We are bombarded with lakhs of characters of text and information and not so much time.
71
+ Text summarization reads the whole documents, based on frequency of words and sentences it understands the
72
+ important sentences and gives us the summary of text.
73
+ We have used a pre-trained model which is a small English pipeline trained on written web text like news, comments for this demo.
74
+ This can be used in organizations that deal with lots of text documents like a law firm where the documents will be summarized in
75
+ one to two paragraph as per our needs."""
76
+ outputs = [
77
+ gr.Textbox(lines =5,label = "Summarization of text"),
78
+ gr.Number(label="Word Count of given Text"),
79
+ gr.Number(label="Word Count of Summarized Text")
80
+ ]
81
+
82
+ demo_app = gr.Interface(
83
+ fn=prediction,
84
+ inputs=gr.Textbox(lines =10,label = " Enter the Text", max_lines = 20),
85
+ outputs= outputs,
86
+ title = "Text Summarization",
87
+ examples = EXAMPLES,
88
+ description = DESCRIPTION,
89
+ #cache_example = True,
90
+ #live = True,
91
+ theme = 'huggingface'
92
+ )
93
+ #if __name__ == "__main__":
94
+ demo_app.launch()
95
+ #demo_app.launch(debug=True, enable_queue = True)
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ en-core-web-sm @ https://github.com/explosion/spacy-models/releases/download/en_core_web_sm-3.2.0/en_core_web_sm-3.2.0-py3-none-any.whl
2
+ spacy
3
+ gradio