Commit
·
91bf496
1
Parent(s):
0255055
Add comment posting functionality
Browse files
app.py
CHANGED
@@ -1,9 +1,33 @@
|
|
1 |
import gradio as gr
|
2 |
import requests
|
3 |
from cachetools import cached, TTLCache
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
4 |
|
5 |
CACHE_TIME = 60 * 60 * 6 # 6 hours
|
6 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
|
8 |
def parse_arxiv_id_from_paper_url(url):
|
9 |
return url.split("/")[-1]
|
@@ -59,10 +83,106 @@ def format_recommendation_into_markdown(arxiv_id, recommendations):
|
|
59 |
return comment
|
60 |
|
61 |
|
62 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
63 |
arxiv_id = parse_arxiv_id_from_paper_url(url)
|
64 |
recommendations = get_recommendations_from_semantic_scholar(f"ArXiv:{arxiv_id}")
|
65 |
filtered_recommendations = filter_recommendations(recommendations)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
66 |
return format_recommendation_into_markdown(arxiv_id, filtered_recommendations)
|
67 |
|
68 |
|
@@ -73,15 +193,16 @@ description = (
|
|
73 |
" yet if they are new or have not been indexed by Semantic Scholar."
|
74 |
)
|
75 |
examples = [
|
76 |
-
"https://huggingface.co/papers/2309.12307",
|
77 |
-
"https://huggingface.co/papers/2211.10086",
|
78 |
]
|
79 |
interface = gr.Interface(
|
80 |
return_recommendations,
|
81 |
-
gr.Textbox(lines=1),
|
82 |
gr.Markdown(),
|
83 |
examples=examples,
|
84 |
title=title,
|
85 |
description=description,
|
86 |
)
|
|
|
87 |
interface.launch()
|
|
|
1 |
import gradio as gr
|
2 |
import requests
|
3 |
from cachetools import cached, TTLCache
|
4 |
+
from bs4 import BeautifulSoup
|
5 |
+
from httpx import Client
|
6 |
+
import json
|
7 |
+
from pathlib import Path
|
8 |
+
from huggingface_hub import CommitScheduler
|
9 |
+
from dotenv import load_dotenv
|
10 |
+
import os
|
11 |
+
|
12 |
+
load_dotenv()
|
13 |
+
|
14 |
+
HF_TOKEN = os.getenv("HF_TOKEN")
|
15 |
|
16 |
CACHE_TIME = 60 * 60 * 6 # 6 hours
|
17 |
|
18 |
+
client = Client()
|
19 |
+
|
20 |
+
REPO_ID = "librarian-bots/paper-recommendations-v2"
|
21 |
+
|
22 |
+
scheduler = CommitScheduler(
|
23 |
+
repo_id=REPO_ID,
|
24 |
+
repo_type="dataset",
|
25 |
+
folder_path="comments",
|
26 |
+
path_in_repo="data",
|
27 |
+
every=5,
|
28 |
+
token=HF_TOKEN,
|
29 |
+
)
|
30 |
+
|
31 |
|
32 |
def parse_arxiv_id_from_paper_url(url):
|
33 |
return url.split("/")[-1]
|
|
|
83 |
return comment
|
84 |
|
85 |
|
86 |
+
def format_comment(result: str):
|
87 |
+
result = (
|
88 |
+
"This is an automated message from the [Librarian Bot](https://huggingface.co/librarian-bots). I found the following papers similar to this paper. \n\n"
|
89 |
+
+ result
|
90 |
+
)
|
91 |
+
result += "\n\n Please give a thumbs up to this comment if you found it helpful!"
|
92 |
+
result += "\n\n If you want recommendations for any Paper on Hugging Face checkout [this](https://huggingface.co/spaces/librarian-bots/recommend_similar_papers) Space"
|
93 |
+
return result
|
94 |
+
|
95 |
+
|
96 |
+
def post_comment(
|
97 |
+
paper_url: str, comment: str, token: str | None = None, base_url: str | None = None
|
98 |
+
) -> bool:
|
99 |
+
if not base_url:
|
100 |
+
base_url = "https://huggingface.co"
|
101 |
+
paper_id = paper_url.split("/")[-1]
|
102 |
+
url = f"{base_url}/api/papers/{paper_id}/comment"
|
103 |
+
comment_data = {"comment": comment}
|
104 |
+
headers = {"Authorization": f"Bearer {token}", "Content-Type": "application/json"}
|
105 |
+
response = requests.post(url, json=comment_data, headers=headers)
|
106 |
+
if response.status_code == 201:
|
107 |
+
print(f"Comment posted successfully for {paper_url}!")
|
108 |
+
return True
|
109 |
+
else:
|
110 |
+
print(f"Failed to post comment! (Status Code: {response.status_code})")
|
111 |
+
print(response.text)
|
112 |
+
return False
|
113 |
+
|
114 |
+
|
115 |
+
def is_comment_from_librarian_bot(html: str) -> bool:
|
116 |
+
"""
|
117 |
+
Checks if the given HTML contains a comment from the librarian-bot.
|
118 |
+
|
119 |
+
Args:
|
120 |
+
html (str): The HTML content to check.
|
121 |
+
|
122 |
+
Returns:
|
123 |
+
bool: True if a comment from the librarian-bot is found, False otherwise.
|
124 |
+
"""
|
125 |
+
soup = BeautifulSoup(html, "lxml")
|
126 |
+
librarian_bot_links = soup.find_all("a", string="librarian-bot")
|
127 |
+
return any(librarian_bot_links)
|
128 |
+
|
129 |
+
|
130 |
+
def check_if_lib_bot_comment_exists(paper_url: str) -> bool:
|
131 |
+
"""
|
132 |
+
Checks if a comment from the librarian bot exists for a given paper URL.
|
133 |
+
|
134 |
+
Args:
|
135 |
+
paper_url (str): The URL of the paper.
|
136 |
+
|
137 |
+
Returns:
|
138 |
+
bool: True if a comment from the librarian bot exists, False otherwise.
|
139 |
+
"""
|
140 |
+
try:
|
141 |
+
resp = client.get(paper_url)
|
142 |
+
return is_comment_from_librarian_bot(resp.text)
|
143 |
+
except Exception as e:
|
144 |
+
print(f"Error checking if comment exists for {paper_url}: {e}")
|
145 |
+
return True # default to not posting comment
|
146 |
+
|
147 |
+
|
148 |
+
def log_comments(paper_url: str, comment: str):
|
149 |
+
"""
|
150 |
+
Logs comments for a given paper URL.
|
151 |
+
|
152 |
+
Args:
|
153 |
+
paper_url (str): The URL of the paper.
|
154 |
+
comment (str): The comment to be logged.
|
155 |
+
|
156 |
+
Returns:
|
157 |
+
None
|
158 |
+
"""
|
159 |
+
paper_id = paper_url.split("/")[-1]
|
160 |
+
file_path = Path(f"comments/{paper_id}.json")
|
161 |
+
if not file_path.exists():
|
162 |
+
with scheduler.lock:
|
163 |
+
with open(file_path, "w") as f:
|
164 |
+
data = {"paper_url": paper_url, "comment": comment}
|
165 |
+
json.dump(data, f)
|
166 |
+
|
167 |
+
|
168 |
+
def return_recommendations(url: str, post_to_paper: bool = True) -> str:
|
169 |
arxiv_id = parse_arxiv_id_from_paper_url(url)
|
170 |
recommendations = get_recommendations_from_semantic_scholar(f"ArXiv:{arxiv_id}")
|
171 |
filtered_recommendations = filter_recommendations(recommendations)
|
172 |
+
if post_to_paper:
|
173 |
+
if comment_already_exists := check_if_lib_bot_comment_exists(url):
|
174 |
+
gr.Info(
|
175 |
+
f"Existing comment: {comment_already_exists}...skipping posting comment"
|
176 |
+
)
|
177 |
+
else:
|
178 |
+
comment = format_comment(
|
179 |
+
format_recommendation_into_markdown(arxiv_id, filtered_recommendations)
|
180 |
+
)
|
181 |
+
if comment_status := post_comment(url, comment, token=HF_TOKEN):
|
182 |
+
log_comments(url, comment)
|
183 |
+
gr.Info(f"Comment status: {comment_status}")
|
184 |
+
else:
|
185 |
+
gr.Info("Failed to post comment")
|
186 |
return format_recommendation_into_markdown(arxiv_id, filtered_recommendations)
|
187 |
|
188 |
|
|
|
193 |
" yet if they are new or have not been indexed by Semantic Scholar."
|
194 |
)
|
195 |
examples = [
|
196 |
+
["https://huggingface.co/papers/2309.12307", False],
|
197 |
+
["https://huggingface.co/papers/2211.10086", False],
|
198 |
]
|
199 |
interface = gr.Interface(
|
200 |
return_recommendations,
|
201 |
+
[gr.Textbox(lines=1), gr.Checkbox(label="Post to Paper", default=False)],
|
202 |
gr.Markdown(),
|
203 |
examples=examples,
|
204 |
title=title,
|
205 |
description=description,
|
206 |
)
|
207 |
+
interface.queue()
|
208 |
interface.launch()
|