ReXploreBackend / main.py
raannakasturi's picture
Refactor text handling in fix_text function and simplify title escaping in paper_data function
c072563
raw
history blame
4.88 kB
import json
import os
import time
import dotenv
import html
from summarize_paper import summarize_paper
from fetch_data import fetch_paper_data_with_category
from post_blog import post_blog
from send_mail import send_email
dotenv.load_dotenv()
access_key = os.getenv("ACCESS_KEY")
def fix_text(text):
text = html.escape(text.encode('utf-8').decode('utf-8').replace("â¦", "..., "))
fixed_text = ""
for word in text.split():
try:
fixed_text += word.encode('latin1').decode('utf-8')+" "
except:
fixed_text += word+" "
return fixed_text.encode('utf-8').decode()
def paper_data(paper_data, wait_time=5):
data = {"status": "success"}
data['data'] = {}
paper_data = json.loads(paper_data)
for category, papers in paper_data.items():
print(f"Processing category: {category}")
data['data'][category] = {}
for paper_id, details in papers.items():
doi = details.get("doi")
pdf_url = details.get("pdf_url")
title = details.get("title")
title = html.escape(title)
citation = details.get("citation")
if not all([paper_id, doi, pdf_url, title, citation]):
print(f"Skipping paper with ID: {paper_id} (missing details)")
continue
summary = None
mindmap = None
max_retries = 3
retry_count = 0
while (not summary or not mindmap) and retry_count < max_retries:
try:
summary, mindmap = summarize_paper(pdf_url, paper_id, access_key)
if summary and mindmap:
break
except Exception as e:
print(f"Error summarizing paper {paper_id}: {e}")
retry_count += 1
if retry_count < max_retries:
print(f"Retrying paper {paper_id} in 3 minutes")
time.sleep(3*60)
if not summary or not mindmap:
print(f"Failed to summarize paper {paper_id} after {max_retries} attempts")
continue
try:
title = fix_text(title)
citation = fix_text(citation)
title = html.escape(str(title).strip())
citation = html.escape(str(citation).strip())
summary = html.escape(str(summary).strip())
mindmap = html.escape(str(mindmap).strip())
status = post_blog(doi, title, category, summary, mindmap, citation, access_key, wait_time)
except Exception as e:
print(f"Error posting blog '{title}': {e}")
continue
data['data'][category][paper_id] = {
"id": paper_id,
"doi": doi,
"title": title,
"category": category,
"posted": status,
"citation": citation,
"summary": summary,
"mindmap": mindmap,
}
data = json.dumps(data, indent=4, ensure_ascii=False)
return data
def post_blogpost(uaccess_key, wait_time=5):
if uaccess_key != access_key:
return False
data = fetch_paper_data_with_category(uaccess_key)
pdata = paper_data(data, wait_time)
try:
send_email(pdata)
print("\n-------------------------------------------------------\nMail Sent\n-------------------------------------------------------\n")
except Exception as e:
print(f"\n-------------------------------------------------------\nError sending mail: {e}\n-------------------------------------------------------\n")
finally:
print("\n-------------------------------------------------------\nProcess Completed\n-------------------------------------------------------\n")
return pdata
def test(uaccess_key):
if uaccess_key != access_key:
return False
data = {
"Economics": {
"2501.00578":{
"paper_id":"2501.00578",
"doi":"https://doi.org/10.1002/alz.14328",
"title":"Bound-State Beta Decay of $\\mathbf{\\mathrm{^{205}{Tl}^{81+}}}$ Ions and the LOREX Project",
"category":"Economics",
"pdf_url":"https://arxiv.org/pdf/2501.00578",
"citation":"Miller, A. D. (2025). The limits of tolerance (Version 1). arXiv. https://doi.org/10.48550/ARXIV.2501.00578",
},
},
}
status = paper_data(json.dumps(data, ensure_ascii=False, indent=4))
return status
if __name__ == '__main__':
data = test(access_key)
print(data)