Spaces:
Running
Running
Commit
·
c69bbef
1
Parent(s):
67ffe93
Refactor summary generation to use Gradio client for TextRank, Luhn, LSA, and LexRank summarizers; improve error handling and logging
Browse files- main.py +1 -5
- math_summarizer.py +36 -44
- nlp_summarizer.py +1 -0
main.py
CHANGED
@@ -17,20 +17,16 @@ def create_client(api_key):
|
|
17 |
|
18 |
def generate_summary(client, corpus):
|
19 |
response = {}
|
20 |
-
print("Generating Math Summary")
|
21 |
math_summary = generate_math_summary(corpus)
|
22 |
if not math_summary:
|
23 |
print("Error generating Math Summary")
|
24 |
response['summary_status'] = "error"
|
25 |
response['summary'] = None
|
26 |
-
response['mindmap_status'] = "
|
27 |
response['mindmap'] = None
|
28 |
return response
|
29 |
else:
|
30 |
-
print("Math Summary Generated Successfully")
|
31 |
-
print("Generating NLP Summary and Mindmap")
|
32 |
response = generate_nlp_summary_and_mindmap(client, corpus)
|
33 |
-
print("NLP Summary and Mindmap Generated Successfully")
|
34 |
return response
|
35 |
|
36 |
def main(corpus):
|
|
|
17 |
|
18 |
def generate_summary(client, corpus):
|
19 |
response = {}
|
|
|
20 |
math_summary = generate_math_summary(corpus)
|
21 |
if not math_summary:
|
22 |
print("Error generating Math Summary")
|
23 |
response['summary_status'] = "error"
|
24 |
response['summary'] = None
|
25 |
+
response['mindmap_status'] = "error"
|
26 |
response['mindmap'] = None
|
27 |
return response
|
28 |
else:
|
|
|
|
|
29 |
response = generate_nlp_summary_and_mindmap(client, corpus)
|
|
|
30 |
return response
|
31 |
|
32 |
def main(corpus):
|
math_summarizer.py
CHANGED
@@ -1,54 +1,40 @@
|
|
1 |
-
from
|
2 |
-
from sumy.nlp.tokenizers import Tokenizer
|
3 |
-
from sumy.summarizers.text_rank import TextRankSummarizer
|
4 |
-
from sumy.summarizers.luhn import LuhnSummarizer
|
5 |
-
from sumy.summarizers.lex_rank import LexRankSummarizer
|
6 |
-
from sumy.summarizers.lsa import LsaSummarizer
|
7 |
-
from sumy.nlp.stemmers import Stemmer
|
8 |
-
from sumy.utils import get_stop_words
|
9 |
-
import nltk
|
10 |
import threading
|
11 |
|
12 |
-
LANGUAGE = "english"
|
13 |
-
SENTENCES_COUNT = 15
|
14 |
-
|
15 |
def generate_textrank_summary(research_paper_text):
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
sentences = summarizer(parser.document, SENTENCES_COUNT)
|
23 |
-
summary = ""
|
24 |
-
for sentence in sentences:
|
25 |
-
summary += str(sentence) + ""
|
26 |
return summary
|
27 |
|
28 |
def generate_luhn_summary(research_paper_text):
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
sentences = summarizer(parser.document, SENTENCES_COUNT)
|
36 |
-
summary = ""
|
37 |
-
for sentence in sentences:
|
38 |
-
summary += str(sentence) + ""
|
39 |
return summary
|
40 |
|
41 |
def generate_lsa_summary(research_paper_text):
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
|
|
|
|
|
|
|
|
|
|
52 |
return summary
|
53 |
|
54 |
def sanitize_text(input_string):
|
@@ -64,9 +50,10 @@ def sanitize_text(input_string):
|
|
64 |
raise
|
65 |
|
66 |
def generate_math_summary(research_paper_text):
|
|
|
67 |
sanitized_text = sanitize_text(research_paper_text)
|
68 |
try:
|
69 |
-
textrank_summary = luhn_summary = lsa_summary = None
|
70 |
def run_textrank():
|
71 |
nonlocal textrank_summary
|
72 |
textrank_summary = generate_textrank_summary(sanitized_text)
|
@@ -76,15 +63,20 @@ def generate_math_summary(research_paper_text):
|
|
76 |
def run_lsa():
|
77 |
nonlocal lsa_summary
|
78 |
lsa_summary = generate_lsa_summary(sanitized_text)
|
|
|
|
|
|
|
79 |
threads = []
|
80 |
threads.append(threading.Thread(target=run_textrank))
|
81 |
threads.append(threading.Thread(target=run_luhn))
|
82 |
threads.append(threading.Thread(target=run_lsa))
|
|
|
83 |
for thread in threads:
|
84 |
thread.start()
|
85 |
for thread in threads:
|
86 |
thread.join()
|
87 |
-
math_summary = textrank_summary.replace("\n", "") +
|
|
|
88 |
return math_summary
|
89 |
except Exception as e:
|
90 |
print(e)
|
|
|
1 |
+
from gradio_client import Client
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
import threading
|
3 |
|
|
|
|
|
|
|
4 |
def generate_textrank_summary(research_paper_text):
|
5 |
+
print("Generating TextRank summary")
|
6 |
+
client = Client("raannakasturi/TextRankSummarizer")
|
7 |
+
summary = client.predict(
|
8 |
+
text_corpus=research_paper_text,
|
9 |
+
api_name="/textrank_summarizer"
|
10 |
+
)
|
|
|
|
|
|
|
|
|
11 |
return summary
|
12 |
|
13 |
def generate_luhn_summary(research_paper_text):
|
14 |
+
print("Generating Luhn summary")
|
15 |
+
client = Client("raannakasturi/LuhnSummarizer")
|
16 |
+
summary = client.predict(
|
17 |
+
text_corpus=research_paper_text,
|
18 |
+
api_name="/luhn_summarizer"
|
19 |
+
)
|
|
|
|
|
|
|
|
|
20 |
return summary
|
21 |
|
22 |
def generate_lsa_summary(research_paper_text):
|
23 |
+
print("Generating LSA summary")
|
24 |
+
client = Client("raannakasturi/LSASummarizer")
|
25 |
+
summary = client.predict(
|
26 |
+
text_corpus=research_paper_text,
|
27 |
+
api_name="/lsa_summarizer"
|
28 |
+
)
|
29 |
+
return summary
|
30 |
+
|
31 |
+
def generate_lexrank_summary(research_paper_text):
|
32 |
+
print("Generating LexRank summary")
|
33 |
+
client = Client("raannakasturi/LexRankSummarizer")
|
34 |
+
summary = client.predict(
|
35 |
+
text_corpus=research_paper_text,
|
36 |
+
api_name="/lexrank_summarizer"
|
37 |
+
)
|
38 |
return summary
|
39 |
|
40 |
def sanitize_text(input_string):
|
|
|
50 |
raise
|
51 |
|
52 |
def generate_math_summary(research_paper_text):
|
53 |
+
print("Generating math summary")
|
54 |
sanitized_text = sanitize_text(research_paper_text)
|
55 |
try:
|
56 |
+
textrank_summary = luhn_summary = lsa_summary = lexrank_summary = None
|
57 |
def run_textrank():
|
58 |
nonlocal textrank_summary
|
59 |
textrank_summary = generate_textrank_summary(sanitized_text)
|
|
|
63 |
def run_lsa():
|
64 |
nonlocal lsa_summary
|
65 |
lsa_summary = generate_lsa_summary(sanitized_text)
|
66 |
+
def run_lexrank():
|
67 |
+
nonlocal lexrank_summary
|
68 |
+
lexrank_summary = generate_lexrank_summary(sanitized_text)
|
69 |
threads = []
|
70 |
threads.append(threading.Thread(target=run_textrank))
|
71 |
threads.append(threading.Thread(target=run_luhn))
|
72 |
threads.append(threading.Thread(target=run_lsa))
|
73 |
+
threads.append(threading.Thread(target=run_lexrank))
|
74 |
for thread in threads:
|
75 |
thread.start()
|
76 |
for thread in threads:
|
77 |
thread.join()
|
78 |
+
math_summary = textrank_summary.replace("\n", "") + luhn_summary.replace("\n", "") + lsa_summary.replace("\n", "") + lexrank_summary.replace("\n", "")
|
79 |
+
print("Math summary generated")
|
80 |
return math_summary
|
81 |
except Exception as e:
|
82 |
print(e)
|
nlp_summarizer.py
CHANGED
@@ -53,4 +53,5 @@ def generate_nlp_summary_and_mindmap(client, temp_summary):
|
|
53 |
thread.start()
|
54 |
for thread in threads:
|
55 |
thread.join()
|
|
|
56 |
return response
|
|
|
53 |
thread.start()
|
54 |
for thread in threads:
|
55 |
thread.join()
|
56 |
+
print("NLP Summary and Mindmap generated")
|
57 |
return response
|