-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathapp.py
488 lines (411 loc) · 12.9 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
import gradio as gr
import requests
from datetime import datetime, timezone
API_URL = "https://huggingface.co/api/daily_papers"
class PaperManager:
def __init__(self, papers_per_page=30):
self.papers_per_page = papers_per_page
self.current_page = 1
self.papers = []
self.total_pages = 1
self.sort_method = "hot" # Default sort method
self.raw_papers = [] # To store fetched data
def calculate_score(self, paper):
"""
Calculate the score of a paper based on upvotes and age.
This mimics the "hotness" algorithm used by platforms like Hacker News.
"""
upvotes = paper.get('paper', {}).get('upvotes', 0)
published_at_str = paper.get('publishedAt', datetime.now(timezone.utc).isoformat())
try:
published_time = datetime.fromisoformat(published_at_str.replace('Z', '+00:00'))
except ValueError:
# If parsing fails, use current time to minimize the impact on sorting
published_time = datetime.now(timezone.utc)
time_diff = datetime.now(timezone.utc) - published_time
time_diff_hours = time_diff.total_seconds() / 3600 # Convert time difference to hours
# Avoid division by zero and apply the hotness formula
score = upvotes / ((time_diff_hours + 2) ** 1.5)
return score
def calculate_rising_score(self, paper):
"""
Calculate the rising score of a paper.
This emphasizes recent upvotes and the rate of upvote accumulation.
"""
upvotes = paper.get('paper', {}).get('upvotes', 0)
published_at_str = paper.get('publishedAt', datetime.now(timezone.utc).isoformat())
try:
published_time = datetime.fromisoformat(published_at_str.replace('Z', '+00:00'))
except ValueError:
published_time = datetime.now(timezone.utc)
time_diff = datetime.now(timezone.utc) - published_time
time_diff_hours = time_diff.total_seconds() / 3600 # Convert time difference to hours
# Rising score favors papers that are gaining upvotes quickly
# Adjusted to have a linear decay over time
score = upvotes / (time_diff_hours + 1)
return score
def fetch_papers(self):
try:
response = requests.get(f"{API_URL}?limit=100")
response.raise_for_status()
data = response.json()
if not data:
print("No data received from API.")
return False
# Debug: Print keys of the first paper
print("Keys in the first paper:", data[0].keys())
self.raw_papers = data # Store raw data
self.sort_papers()
self.total_pages = max((len(self.papers) + self.papers_per_page - 1) // self.papers_per_page, 1)
self.current_page = 1
return True
except requests.RequestException as e:
print(f"Error fetching papers: {e}")
return False
except Exception as e:
print(f"Unexpected error: {e}")
return False
def sort_papers(self):
if self.sort_method == "hot":
self.papers = sorted(
self.raw_papers,
key=lambda x: self.calculate_score(x),
reverse=True
)
elif self.sort_method == "new":
self.papers = sorted(
self.raw_papers,
key=lambda x: x.get('publishedAt', ''),
reverse=True
)
elif self.sort_method == "rising":
self.papers = sorted(
self.raw_papers,
key=lambda x: self.calculate_rising_score(x),
reverse=True
)
else:
self.papers = sorted(
self.raw_papers,
key=lambda x: self.calculate_score(x),
reverse=True
)
def set_sort_method(self, method):
if method not in ["hot", "new", "rising"]:
method = "hot"
print(f"Setting sort method to: {method}")
self.sort_method = method
self.sort_papers()
self.current_page = 1
return True # Assume success
def format_paper(self, paper, rank):
title = paper.get('title', 'No title')
paper_id = paper.get('paper', {}).get('id', '')
url = f"https://huggingface.co/papers/{paper_id}"
authors = ', '.join([author.get('name', '') for author in paper.get('paper', {}).get('authors', [])]) or 'Unknown'
upvotes = paper.get('paper', {}).get('upvotes', 0)
comments = paper.get('numComments', 0)
published_time_str = paper.get('publishedAt', datetime.now(timezone.utc).isoformat())
try:
published_time = datetime.fromisoformat(published_time_str.replace('Z', '+00:00'))
except ValueError:
published_time = datetime.now(timezone.utc)
time_diff = datetime.now(timezone.utc) - published_time
time_ago_days = time_diff.days
time_ago = f"{time_ago_days} days ago" if time_ago_days > 0 else "today"
return f"""
<tr class="athing">
<td align="right" valign="top" class="title"><span class="rank">{rank}.</span></td>
<td valign="top" class="title">
<a href="{url}" class="storylink" target="_blank">{title}</a>
</td>
</tr>
<tr>
<td colspan="2" class="subtext">
<span class="score">{upvotes} points</span> Authors: {authors} {time_ago} | {comments} comments
</td>
</tr>
<tr class="spacer"><td colspan="2"></td></tr>
"""
def render_papers(self):
start = (self.current_page - 1) * self.papers_per_page
end = start + self.papers_per_page
current_papers = self.papers[start:end]
if not current_papers:
return "<div class='no-papers'>No papers available for this page.</div>"
papers_html = "".join([self.format_paper(paper, idx + start + 1) for idx, paper in enumerate(current_papers)])
return f"""
<table border="0" cellpadding="0" cellspacing="0" class="itemlist">
{papers_html}
</table>
"""
def next_page(self):
if self.current_page < self.total_pages:
self.current_page += 1
return self.render_papers()
def prev_page(self):
if self.current_page > 1:
self.current_page -= 1
return self.render_papers()
paper_manager = PaperManager()
def initialize_app():
if paper_manager.fetch_papers():
return paper_manager.render_papers()
else:
return "<div class='no-papers'>Failed to fetch papers. Please try again later.</div>"
def change_sort_method(method):
method_lower = method.lower()
print(f"Changing sort method to: {method_lower}")
if paper_manager.set_sort_method(method_lower):
print("Sort method set successfully.")
return paper_manager.render_papers()
else:
print("Failed to set sort method.")
return "<div class='no-papers'>Failed to sort papers. Please try again later.</div>"
css = """
/* Hacker News-like CSS */
body {
background-color: white;
font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
font-size: 11px;
color: #000;
margin: 0;
padding: 0;
}
a {
color: #0000ff;
text-decoration: none;
}
a:visited {
color: #551a8b;
}
.container {
width: 85%;
margin: auto;
padding-top: 10px;
}
.itemlist {
width: 100%;
border-collapse: collapse;
}
.header-table {
width: 100%;
background-color: #ff6600;
padding: 2px 10px;
}
.header-table a {
color: black;
font-weight: bold;
font-size: 14pt;
text-decoration: none;
}
.athing {
background-color: #f6f6ef;
}
.rank {
font-size: 10px;
color: #828282;
padding-right: 5px;
}
.storylink {
font-size: 13px;
font-weight: bold;
}
.subtext {
font-size: 10px;
color: #828282;
padding-left: 40px;
}
.subtext a {
color: #828282;
text-decoration: none;
}
.subtext a:hover {
text-decoration: underline;
}
.spacer {
height: 5px;
}
.no-papers {
text-align: center;
color: #828282;
padding: 1rem;
font-size: 14pt;
}
.pagination {
padding: 10px 0;
text-align: center;
}
.pagination button {
background-color: #f0f0f0; // Light gray background
border: 1px solid #d0d0d0; // Light border
color: #0066cc; // Blue text color
padding: 6px 12px;
margin: 0 3px;
cursor: pointer;
font-size: 14px;
font-weight: bold;
border-radius: 3px;
transition: background-color 0.2s ease, color 0.2s ease;
}
.pagination button:hover {
background-color: #e0e0e0; // Slightly darker on hover
color: #004499; // Darker blue on hover
}
.pagination button:disabled {
background-color: #f8f8f8;
color: #999999; // Gray text for disabled state
cursor: not-allowed;
}
@media (max-width: 640px) {
.header-table a {
font-size: 12pt;
}
.storylink {
font-size: 11px;
}
.subtext {
font-size: 9px;
}
.rank {
font-size: 9px;
}
.pagination button {
padding: 8px 16px;
font-size: 16px;
}
}
/* Dark mode */
@media (prefers-color-scheme: dark) {
body {
background-color: #121212;
color: #e0e0e0;
}
a {
color: #add8e6;
}
a:visited {
color: #9370db;
}
.header-table {
background-color: #333;
}
.header-table a {
color: #e0e0e0;
}
.athing {
background-color: #1e1e1e;
}
.rank {
color: #b0b0b0;
}
.subtext {
color: #b0b0b0;
}
.subtext a {
color: #b0b0b0;
}
.no-papers {
color: #b0b0b0;
}
.pagination button {
background-color: #2a2a2a;
border-color: #3a3a3a;
color: #4499ff; // Lighter blue for dark mode
}
.pagination button:hover {
background-color: #3a3a3a;
color: #66b3ff; // Even lighter blue on hover for dark mode
}
.pagination button:disabled {
background-color: #222222;
color: #666666;
}
}
/* Add these rules to explicitly remove all borders */
table, tr, td {
border: none;
border-collapse: collapse;
border-spacing: 0;
}
/* Remove border and add padding for the radio buttons */
.sort-radio {
border: none !important;
padding: 10px 10px 10px 10px !important; // Added left and right padding
}
/* Style the radio buttons to match the Hacker News theme */
.sort-radio .gr-form {
border: none !important;
background: transparent !important;
}
.sort-radio .gr-form.gr-box {
border-radius: 0 !important;
box-shadow: none !important;
}
.sort-radio .gr-radio-row {
padding: 0 !important;
}
/* Style the radio button labels */
.sort-radio label span {
font-size: 14px;
color: #828282;
margin-left: 5px;
margin-right: 15px; // Add some space between options
}
/* Style the selected radio button */
.sort-radio input[type="radio"]:checked + label span {
color: #ff6600;
font-weight: bold;
}
"""
demo = gr.Blocks(css=css)
with demo:
with gr.Column(elem_classes=["container"]):
# Accordion for Submission Instructions
with gr.Accordion("How to Submit a Paper", open=False):
gr.Markdown("""
**Submit the paper to Daily Papers:**
[https://huggingface.co/papers/submit](https://huggingface.co/papers/submit)
Once your paper is submitted, it will automatically appear in this demo.
""")
# Header without Refresh Button
with gr.Row():
gr.HTML("""
<table border="0" cellpadding="0" cellspacing="0" class="header-table">
<tr>
<td>
<span class="pagetop">
<b class="hnname"><a href="#">Daily Papers</a></b>
</span>
</td>
<!-- Removed the Refresh button cell -->
</tr>
</table>
""")
# Sorting Options
with gr.Column():
sort_radio = gr.Radio(
choices=["Hot", "New", "Rising"],
value="Hot",
label="", # Remove the original label
interactive=True,
elem_classes=["sort-radio"]
)
# Paper list
paper_list = gr.HTML()
# Navigation Buttons
with gr.Row(elem_classes=["pagination"]):
prev_button = gr.Button("Prev")
next_button = gr.Button("Next")
# Load papers on app start
demo.load(initialize_app, outputs=[paper_list])
# Button clicks for pagination
prev_button.click(paper_manager.prev_page, outputs=[paper_list])
next_button.click(paper_manager.next_page, outputs=[paper_list])
# Sort option change
sort_radio.change(
fn=change_sort_method,
inputs=[sort_radio],
outputs=[paper_list]
)
demo.launch()