Spaces:
Running
Running

Refactor summary generation to use Gradio client for TextRank, Luhn, LSA, and LexRank summarizers; improve error handling and logging
c69bbef
from gradio_client import Client | |
import threading | |
def generate_textrank_summary(research_paper_text): | |
print("Generating TextRank summary") | |
client = Client("raannakasturi/TextRankSummarizer") | |
summary = client.predict( | |
text_corpus=research_paper_text, | |
api_name="/textrank_summarizer" | |
) | |
return summary | |
def generate_luhn_summary(research_paper_text): | |
print("Generating Luhn summary") | |
client = Client("raannakasturi/LuhnSummarizer") | |
summary = client.predict( | |
text_corpus=research_paper_text, | |
api_name="/luhn_summarizer" | |
) | |
return summary | |
def generate_lsa_summary(research_paper_text): | |
print("Generating LSA summary") | |
client = Client("raannakasturi/LSASummarizer") | |
summary = client.predict( | |
text_corpus=research_paper_text, | |
api_name="/lsa_summarizer" | |
) | |
return summary | |
def generate_lexrank_summary(research_paper_text): | |
print("Generating LexRank summary") | |
client = Client("raannakasturi/LexRankSummarizer") | |
summary = client.predict( | |
text_corpus=research_paper_text, | |
api_name="/lexrank_summarizer" | |
) | |
return summary | |
def sanitize_text(input_string): | |
try: | |
encoded_bytes = input_string.encode('utf-8') | |
decoded_string = encoded_bytes.decode('utf-8') | |
return decoded_string | |
except UnicodeEncodeError as e: | |
print(f"Encoding error: {e}") | |
raise | |
except UnicodeDecodeError as e: | |
print(f"Decoding error: {e}") | |
raise | |
def generate_math_summary(research_paper_text): | |
print("Generating math summary") | |
sanitized_text = sanitize_text(research_paper_text) | |
try: | |
textrank_summary = luhn_summary = lsa_summary = lexrank_summary = None | |
def run_textrank(): | |
nonlocal textrank_summary | |
textrank_summary = generate_textrank_summary(sanitized_text) | |
def run_luhn(): | |
nonlocal luhn_summary | |
luhn_summary = generate_luhn_summary(sanitized_text) | |
def run_lsa(): | |
nonlocal lsa_summary | |
lsa_summary = generate_lsa_summary(sanitized_text) | |
def run_lexrank(): | |
nonlocal lexrank_summary | |
lexrank_summary = generate_lexrank_summary(sanitized_text) | |
threads = [] | |
threads.append(threading.Thread(target=run_textrank)) | |
threads.append(threading.Thread(target=run_luhn)) | |
threads.append(threading.Thread(target=run_lsa)) | |
threads.append(threading.Thread(target=run_lexrank)) | |
for thread in threads: | |
thread.start() | |
for thread in threads: | |
thread.join() | |
math_summary = textrank_summary.replace("\n", "") + luhn_summary.replace("\n", "") + lsa_summary.replace("\n", "") + lexrank_summary.replace("\n", "") | |
print("Math summary generated") | |
return math_summary | |
except Exception as e: | |
print(e) | |
return False |