Update math_summarizer.py
Browse files- math_summarizer.py +56 -82
math_summarizer.py
CHANGED
|
@@ -1,83 +1,57 @@
|
|
| 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
|
| 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 |
-
def run_textrank():
|
| 58 |
-
nonlocal textrank_summary
|
| 59 |
-
textrank_summary = generate_textrank_summary(sanitized_text)
|
| 60 |
-
def run_luhn():
|
| 61 |
-
nonlocal luhn_summary
|
| 62 |
-
luhn_summary = generate_luhn_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)
|
| 83 |
return False
|
|
|
|
| 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 sanitize_text(input_string):
|
| 23 |
+
try:
|
| 24 |
+
encoded_bytes = input_string.encode('utf-8')
|
| 25 |
+
decoded_string = encoded_bytes.decode('utf-8')
|
| 26 |
+
return decoded_string
|
| 27 |
+
except UnicodeEncodeError as e:
|
| 28 |
+
print(f"Encoding error: {e}")
|
| 29 |
+
raise
|
| 30 |
+
except UnicodeDecodeError as e:
|
| 31 |
+
print(f"Decoding error: {e}")
|
| 32 |
+
raise
|
| 33 |
+
|
| 34 |
+
def generate_math_summary(research_paper_text):
|
| 35 |
+
print("Generating math summary")
|
| 36 |
+
sanitized_text = sanitize_text(research_paper_text)
|
| 37 |
+
try:
|
| 38 |
+
textrank_summary = luhn_summary = lsa_summary = lexrank_summary = None
|
| 39 |
+
def run_textrank():
|
| 40 |
+
nonlocal textrank_summary
|
| 41 |
+
textrank_summary = generate_textrank_summary(sanitized_text)
|
| 42 |
+
def run_luhn():
|
| 43 |
+
nonlocal luhn_summary
|
| 44 |
+
luhn_summary = generate_luhn_summary(sanitized_text)
|
| 45 |
+
threads = []
|
| 46 |
+
threads.append(threading.Thread(target=run_textrank))
|
| 47 |
+
threads.append(threading.Thread(target=run_luhn))
|
| 48 |
+
for thread in threads:
|
| 49 |
+
thread.start()
|
| 50 |
+
for thread in threads:
|
| 51 |
+
thread.join()
|
| 52 |
+
math_summary = textrank_summary.replace("\n", "") + luhn_summary.replace("\n", "")
|
| 53 |
+
print("Math summary generated")
|
| 54 |
+
return math_summary
|
| 55 |
+
except Exception as e:
|
| 56 |
+
print(e)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
return False
|