-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
189 lines (157 loc) · 6.21 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
from flask import Flask, render_template, redirect,request
import json
import requests
import openai
from docx import Document
from sumy.parsers.plaintext import PlaintextParser
from sumy.nlp.tokenizers import Tokenizer
import re
import nltk
import heapq
import os
app = Flask(__name__)
@app.route("/")
def home():
return render_template("ip.html")
@app.route("/home")
def red_home():
return redirect("/")
@app.route("/submit", methods=['POST'])
def submit():
if request.method=='POST':
d = {}
if request.form["para"]:
d["para"] = request.form["para"]
else:
f = request.files['formFile']
ext = (f.filename).split(".")[-1]
f.save(f.filename)
# print(ext)
if ext=="docx":
doc = Document(f.filename)
paras = doc.paragraphs
content = ""
for i in range(8,len(paras),3):
p = paras[i+1].text.replace("’","'").replace("‘","'").replace('“','"').replace('”','"').replace("…",".")
content += paras[i].text.split(" - ")[0] + ": " + p + "\n"
d["para"] = content
elif ext=="txt":
tmp = open(f.filename, "r")
d["para"] = tmp.read()
else:
d["para"] = ""
d["abstractive"] = {}
d["extractive"] = {}
# print(d)
# m1 = plaraphy_model(d)
# d["abstractive"]["Plaraphy"] = m1
# print(m1)
m2 = openai_model(d)
d["abstractive"]["OpenAI"] = m2
# print(m2)
m3 = lexrank_model(d["para"])
d["extractive"]["LexRank"] = m3
# print(m3)
m4 = latent_summary_analysis_model(d["para"])
d["extractive"]["LSA"] = m4
# print(m4)
m5 = klsum_model(d["para"])
d["extractive"]["KL Sum"] = m5
# print(m5)
m6 = luhn_model(d["para"])
d["extractive"]["Luhn"] = m6
# print(m6)
m7 = nlp_model(d["para"])
d["abstractive"]["NLP"] = m7
# print(d)
# print(d.keys(), d["extractive"].keys(), d["abstractive"].keys())
return render_template("op.html", data=d)
def plaraphy_model(data):
url = 'https://app.plaraphy.com/api/summarizer'
payload = 'text='+data["para"]+'&output_percent=10'
headers = {
'accept': 'application/json',
'content-type': 'application/x-www-form-urlencoded',
'authorization': 'Bearer 24379|3FHJrWgFaALt1iKp3L7MujvMDfHohVNfcOyiQBcT',
'cache-control': 'no-cache',
}
response = requests.request('POST', url, data=payload, headers=headers)
# return response.text
print(response.text)
js = json.loads(response.text)
return js["summary"].split(". ")
def openai_model(data):
openai.api_key = "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"
response = openai.Completion.create(
model="text-davinci-003",
prompt=f"Summarize the following meeting in 10 bullet points: {data}",
temperature=0.7,
max_tokens=256,
top_p=1,
frequency_penalty=0,
presence_penalty=0
)
# print(response, type(response))
# print(response.choices[0].text, type(response.choices[0].text))
return list(map(lambda x: x[3:], response.choices[0].text.split('\n')[2:]))
def lexrank_model(data):
from sumy.summarizers.lex_rank import LexRankSummarizer
lexrank_summarizer = LexRankSummarizer()
sumy_parser = PlaintextParser.from_string(data, Tokenizer('english'))
lexrank_summary = lexrank_summarizer(sumy_parser.document,sentences_count=10)
# return " ".join(list(map(str, lexrank_summary)))
# print(lexrank_summary)
return lexrank_summary
def latent_summary_analysis_model(data):
from sumy.summarizers.lsa import LsaSummarizer
sumy_parser = PlaintextParser.from_string(data, Tokenizer('english'))
lsa_summarizer = LsaSummarizer()
lsa_summary = lsa_summarizer(sumy_parser.document, 10)
# return " ".join(list(map(str, lsa_summary)))
return lsa_summary
def luhn_model(data):
from sumy.summarizers.luhn import LuhnSummarizer
sumy_parser = PlaintextParser.from_string(data, Tokenizer('english'))
luhn_summarizer = LuhnSummarizer()
luhn_summary = luhn_summarizer(sumy_parser.document, sentences_count=10)
# return " ".join(list(map(str,luhn_summary)))
return luhn_summary
def klsum_model(data):
from sumy.summarizers.kl import KLSummarizer
sumy_parser = PlaintextParser.from_string(data, Tokenizer('english'))
kl_summarizer = KLSummarizer()
kl_summary = kl_summarizer(sumy_parser.document, sentences_count=10)
# return " ".join(list(map(str,kl_summary)))
return kl_summary
def nlp_model(data):
article_text = re.sub(r'\[[0-9]*\]', ' ', data)
article_text = re.sub(r'\s+', ' ', article_text)
formatted_article_text = re.sub('[^a-zA-Z]', ' ', article_text )
formatted_article_text = re.sub(r'\s+', ' ', formatted_article_text)
sentence_list = nltk.sent_tokenize(article_text)
nltk.download("stopwords")
stopwords = nltk.corpus.stopwords.words('english')
word_frequencies = {}
for word in nltk.word_tokenize(formatted_article_text):
if word not in stopwords:
if word not in word_frequencies.keys():
word_frequencies[word] = 1
else:
word_frequencies[word] += 1
maximum_frequncy = max(word_frequencies.values())
for word in word_frequencies.keys():
word_frequencies[word] = (word_frequencies[word]/maximum_frequncy)
sentence_scores = {}
for sent in sentence_list:
for word in nltk.word_tokenize(sent.lower()):
if word in word_frequencies.keys():
if len(sent.split(' ')) < 30:
if sent not in sentence_scores.keys():
sentence_scores[sent] = word_frequencies[word]
else:
sentence_scores[sent] += word_frequencies[word]
summary_sentences = heapq.nlargest(10, sentence_scores, key=sentence_scores.get)
# summary = ' '.join(summary_sentences)
return summary_sentences
if __name__ == "__main__":
app.run(debug=True)