Commit
•
d0eeb69
1
Parent(s):
565f2f2
chore: Refactor comment posting functionality and improve performance
Browse files
app.py
CHANGED
@@ -96,9 +96,6 @@ def format_comment(result: str):
|
|
96 |
return result
|
97 |
|
98 |
|
99 |
-
from typing import Tuple
|
100 |
-
|
101 |
-
|
102 |
def post_comment(
|
103 |
paper_url: str, comment: str, comment_id: str | None = None, token: str = HF_TOKEN
|
104 |
) -> Tuple[bool, str]:
|
@@ -123,9 +120,12 @@ def post_comment(
|
|
123 |
paper_id = paper_url.split("/")[-1]
|
124 |
if comment_id:
|
125 |
url = f"https://huggingface.co/api/papers/{paper_id}/comment/{comment_id}/reply"
|
|
|
|
|
126 |
else:
|
127 |
url = f"https://huggingface.co/api/papers/{paper_id}/comment"
|
128 |
-
|
|
|
129 |
headers = {
|
130 |
"Authorization": f"Bearer {token}",
|
131 |
"Content-Type": "application/json",
|
@@ -240,36 +240,47 @@ def log_comments(paper_url: str, comment: str):
|
|
240 |
|
241 |
|
242 |
def return_recommendations(
|
243 |
-
url: str, comment_id: str | None
|
244 |
) -> str:
|
245 |
arxiv_id = parse_arxiv_id_from_paper_url(url)
|
246 |
recommendations = get_recommendations_from_semantic_scholar(f"ArXiv:{arxiv_id}")
|
247 |
filtered_recommendations = filter_recommendations(recommendations)
|
|
|
248 |
if post_to_paper:
|
249 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
250 |
if existing_comments:
|
251 |
-
gr.Info(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
252 |
else:
|
253 |
-
|
254 |
-
|
|
|
255 |
)
|
256 |
-
if
|
257 |
-
|
258 |
-
|
259 |
-
|
260 |
-
|
261 |
-
|
262 |
-
|
263 |
-
|
264 |
-
comment_status, posted_comment_id = post_comment(
|
265 |
-
url, comment, token=HF_TOKEN
|
266 |
-
)
|
267 |
-
if comment_status:
|
268 |
-
log_comments(url, comment)
|
269 |
-
gr.Info(f"Posted comment {posted_comment_id}")
|
270 |
-
else:
|
271 |
-
gr.Info("Failed to post comment")
|
272 |
-
return format_recommendation_into_markdown(arxiv_id, filtered_recommendations)
|
273 |
|
274 |
|
275 |
title = "Semantic Scholar Paper Recommender"
|
|
|
96 |
return result
|
97 |
|
98 |
|
|
|
|
|
|
|
99 |
def post_comment(
|
100 |
paper_url: str, comment: str, comment_id: str | None = None, token: str = HF_TOKEN
|
101 |
) -> Tuple[bool, str]:
|
|
|
120 |
paper_id = paper_url.split("/")[-1]
|
121 |
if comment_id:
|
122 |
url = f"https://huggingface.co/api/papers/{paper_id}/comment/{comment_id}/reply"
|
123 |
+
gr.Info(f"Replying to comment {comment_id}")
|
124 |
+
print(f"Replying to comment {comment_id}")
|
125 |
else:
|
126 |
url = f"https://huggingface.co/api/papers/{paper_id}/comment"
|
127 |
+
print(f"Posting comment for {paper_url}")
|
128 |
+
gr.Info(f"Posting comment for {paper_url}")
|
129 |
headers = {
|
130 |
"Authorization": f"Bearer {token}",
|
131 |
"Content-Type": "application/json",
|
|
|
240 |
|
241 |
|
242 |
def return_recommendations(
|
243 |
+
url: str, comment_id: str | None, post_to_paper: bool = True
|
244 |
) -> str:
|
245 |
arxiv_id = parse_arxiv_id_from_paper_url(url)
|
246 |
recommendations = get_recommendations_from_semantic_scholar(f"ArXiv:{arxiv_id}")
|
247 |
filtered_recommendations = filter_recommendations(recommendations)
|
248 |
+
|
249 |
if post_to_paper:
|
250 |
+
formatted_recommendation = format_recommendation_into_markdown(
|
251 |
+
arxiv_id, filtered_recommendations
|
252 |
+
)
|
253 |
+
comment = format_comment(formatted_recommendation)
|
254 |
+
|
255 |
+
# Check if a librarian-bot comment already exists.
|
256 |
+
existing_comments, existing_comment_id = check_if_lib_bot_comment_exists(url)
|
257 |
if existing_comments:
|
258 |
+
gr.Info(
|
259 |
+
f"Librarian-bot already commented on this paper. Comment ID: {existing_comment_id}. No further action will be taken."
|
260 |
+
)
|
261 |
+
return formatted_recommendation # Return formatted recommendation without posting.
|
262 |
+
|
263 |
+
# If no existing librarian-bot comment, check if a specific comment_id is provided for replying.
|
264 |
+
if comment_id:
|
265 |
+
comment_status, posted_comment_id = post_comment(
|
266 |
+
url, comment, comment_id, token=HF_TOKEN
|
267 |
+
)
|
268 |
+
if comment_status:
|
269 |
+
log_comments(url, comment)
|
270 |
+
gr.Info(f"Posted reply to comment {posted_comment_id}")
|
271 |
else:
|
272 |
+
# If no comment_id is provided, post a new comment.
|
273 |
+
comment_status, posted_comment_id = post_comment(
|
274 |
+
url, comment, token=HF_TOKEN
|
275 |
)
|
276 |
+
if comment_status:
|
277 |
+
log_comments(url, comment)
|
278 |
+
gr.Info(f"Posted new comment {posted_comment_id}")
|
279 |
+
|
280 |
+
if not comment_status:
|
281 |
+
gr.Info("Failed to post comment")
|
282 |
+
|
283 |
+
return formatted_recommendation
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
284 |
|
285 |
|
286 |
title = "Semantic Scholar Paper Recommender"
|