File size: 6,482 Bytes
cf55fa7 d4ba28a cf55fa7 d4ba28a cf55fa7 d4ba28a 63f7996 d4ba28a cf55fa7 4525f68 cf55fa7 d4ba28a cf55fa7 4525f68 cf55fa7 4525f68 d4ba28a 4525f68 d4ba28a cf55fa7 d4ba28a cf55fa7 d4ba28a cf55fa7 d4ba28a cf55fa7 d4ba28a cf55fa7 d4ba28a cf55fa7 d4ba28a cf55fa7 d4ba28a cf55fa7 d4ba28a cf55fa7 d4ba28a cf55fa7 d4ba28a cf55fa7 d4ba28a cf55fa7 d4ba28a cf55fa7 d4ba28a cf55fa7 d4ba28a cf55fa7 d4ba28a cf55fa7 8accb87 d4ba28a 8accb87 d4ba28a 8accb87 d4ba28a 8accb87 d4ba28a 8accb87 d4ba28a cf55fa7 8accb87 cf55fa7 d4ba28a cf55fa7 d4ba28a cf55fa7 8accb87 d4ba28a 8accb87 cf55fa7 d4ba28a cf55fa7 d4ba28a cddc612 cf55fa7 03bdc59 d4ba28a e800f6d 4525f68 |
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 |
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
def fetch_papers(self):
try:
response = requests.get(f"{API_URL}?limit=100")
response.raise_for_status()
data = response.json()
# Sort papers primarily by 'publishedAt' descending, then by 'upvotes' descending
self.papers = sorted(
data,
key=lambda x: (
datetime.fromisoformat(
x.get('publishedAt', datetime.now(timezone.utc).isoformat()).replace('Z', '+00:00')
),
x.get('paper', {}).get('upvotes', 0)
),
reverse=True # Ensures descending order
)
self.total_pages = (len(self.papers) + self.papers_per_page - 1) // self.papers_per_page
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 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', [])])
upvotes = paper.get('paper', {}).get('upvotes', 0)
comments = paper.get('numComments', 0)
published_time = datetime.fromisoformat(
paper.get('publishedAt', datetime.now(timezone.utc).isoformat()).replace('Z', '+00:00')
)
time_ago_days = (datetime.now(timezone.utc) - published_time).days
time_ago = f"{time_ago_days} days ago" if time_ago_days > 0 else "today"
# Return HTML similar to Hacker News
return f"""
<tr class="athing">
<td align="right" valign="top" class="title"><span class="rank">{rank}.</span></td>
<td valign="top" class="votelinks">
<center><a href="#"><div class="votearrow"></div></a></center>
</td>
<td class="title">
<a href="{url}" class="storylink" target="_blank">{title}</a>
</td>
</tr>
<tr>
<td colspan="2"></td>
<td class="subtext">
<span class="score">{upvotes} points</span> by {authors} | {time_ago} | <a href="#">{comments} comments</a>
</td>
</tr>
<tr style="height:5px"></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()
else:
return self.render_papers() + "<center class='more'><span>No more papers.</span></center>"
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>"
css = """
body {
background-color: white;
font-family: Verdana, Geneva, sans-serif;
margin: 0;
padding: 0;
}
a {
color: #0000ff;
text-decoration: none;
}
a:visited {
color: #551A8B;
}
.container {
width: 85%;
margin: auto;
}
table {
width: 100%;
}
.title {
background-color: #ff6600;
padding: 2px 10px;
}
.title a {
color: black;
font-weight: bold;
font-size: 14pt;
}
.itemlist .athing {
background-color: #f6f6ef;
}
.rank {
font-size: 14pt;
color: #828282;
padding-right: 5px;
}
.votelinks {
width: 10px;
}
.votearrow {
width: 0;
height: 0;
border-left: 5px solid transparent;
border-right: 5px solid transparent;
border-bottom: 10px solid #828282;
margin: auto;
}
.storylink {
font-size: 10pt;
}
.subtext {
font-size: 8pt;
color: #828282;
padding-left: 40px;
}
.subtext a {
color: #828282;
text-decoration: none;
}
.more {
padding: 10px;
}
.no-papers {
text-align: center;
color: #828282;
padding: 1rem;
font-size: 14pt;
}
@media (max-width: 640px) {
.title a {
font-size: 12pt;
}
.storylink {
font-size: 9pt;
}
.subtext {
font-size: 7pt;
}
}
"""
demo = gr.Blocks(css=css)
with demo:
with gr.Column(elem_classes=["container"]):
# Header
gr.HTML("""
<table border="0" cellpadding="0" cellspacing="0" width="100%" bgcolor="#ff6600">
<tr>
<td style="padding: 8px;">
<span class="pagetop">
<b class="hnname"><a href="#" style="color: black; text-decoration: none;">Daily Papers</a></b>
</span>
</td>
</tr>
</table>
""")
# Paper list
paper_list = gr.HTML()
# Footer
more_button = gr.HTML("""
<center class='more'>
<a href="#" id="more-button">More</a>
</center>
""")
demo.load(initialize_app, outputs=[paper_list])
# JavaScript to handle the "More" link click
demo.add_script("""
document.getElementById('more-button').addEventListener('click', function(e) {
e.preventDefault();
gradioApp().querySelector('#component-2 button').click();
});
""")
# Hidden button to trigger next page
hidden_next_button = gr.Button(visible=False)
hidden_next_button.click(paper_manager.next_page, outputs=[paper_list])
demo.launch()
|