File size: 10,069 Bytes
c6bd2a2 3497525 354cb34 3497525 354cb34 3497525 bd08853 3497525 bd08853 6d3b49f 3497525 6d3b49f |
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 |
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=10):
self.papers_per_page = papers_per_page
self.cache = []
self.last_fetch_date = None
self.total_pages = 1
def fetch_papers(self):
"""
Fetch papers from the API if not already fetched today.
"""
today = datetime.now(timezone.utc).date()
if self.last_fetch_date == today and self.cache:
print("Using cached papers.")
return True
else:
try:
# Attempt to fetch all papers at once. Adjust 'limit' as per API's capability.
response = requests.get(f"{API_URL}?page=1&limit=1000")
response.raise_for_status()
data = response.json()
self.cache = sorted(data, key=lambda x: x.get('paper', {}).get('upvotes', 0), reverse=True)
self.last_fetch_date = today
self.total_pages = (len(self.cache) + self.papers_per_page - 1) // self.papers_per_page
print(f"Fetched {len(self.cache)} papers. Total pages: {self.total_pages}")
return True
except requests.RequestException as e:
print(f"Error fetching papers: {e}")
return False
def format_paper(self, paper):
"""
Format a single paper's information into HTML.
"""
title = paper.get('title', 'No title')
paper_id = paper.get('paper', {}).get('id', '')
url = f"https://huggingface.co/papers/{paper_id}" if paper_id else "#"
authors = ', '.join([author.get('name', 'Unknown') for author in paper.get('paper', {}).get('authors', [])])
upvotes = paper.get('paper', {}).get('upvotes', 0)
comments = paper.get('numComments', 0)
published_at = paper.get('publishedAt', datetime.now(timezone.utc).isoformat())
try:
published_time = datetime.fromisoformat(published_at.replace('Z', '+00:00'))
time_ago = (datetime.now(timezone.utc) - published_time).days
except ValueError:
time_ago = "Unknown"
# Ensure the triple-quoted string is properly closed
return f"""<div style='border-bottom: 1px solid #eee; padding: 10px 0;'>
<a href='{url}' target='_blank' style='color: #000; text-decoration: none; font-weight: bold;'>{title}</a>
<div style='font-size: 0.8em; color: #666; margin-top: 5px;'>
{upvotes} upvotes | by {authors} | {time_ago} days ago | {comments} comments
</div>
</div>"""
def render_papers(self, papers, page=1):
"""
Render HTML for a list of papers based on the current page.
"""
if not papers:
return "<div>No papers available for this page.</div>"
start = (page - 1) * self.papers_per_page
end = start + self.papers_per_page
page_papers = papers[start:end]
return "".join([self.format_paper(paper) for paper in page_papers])
def search_papers(self, query):
"""
Filter papers based on the search query.
"""
if not query:
return self.cache
return [paper for paper in self.cache if query.lower() in paper.get('title', '').lower()]
css = """
html, body {
height: 100%;
margin: 0;
padding: 0;
display: flex;
justify-content: center;
align-items: center;
background-color: #f0f0f0;
}
.container {
font-family: Arial, sans-serif;
max-width: 800px;
width: 100%;
background-color: white;
padding: 20px;
border-radius: 10px;
box-shadow: 0 0 10px rgba(0,0,0,0.1);
}
.paper-list {
max-height: 400px;
overflow-y: auto;
border: 1px solid #eee;
border-radius: 5px;
padding: 10px;
margin-bottom: 10px;
}
.search-row {
display: flex;
gap: 10px;
margin-bottom: 20px;
}
.title {
text-align: center;
color: #333;
}
.footer {
display: flex;
justify-content: space-between;
align-items: center;
margin-top: 10px;
}
"""
# Instantiate the PaperManager
paper_manager = PaperManager()
def initialize():
"""
Initialize the app by fetching papers and setting initial states.
"""
success = paper_manager.fetch_papers()
if success:
papers = paper_manager.cache
page = 1
total_pages = paper_manager.total_pages
page_papers = paper_manager.render_papers(papers, page)
page_info_text = f"Page {page} of {total_pages}"
return page_papers, page, total_pages, papers, page_info_text
else:
error_html = "<div>Error fetching papers. Please try again later.</div>"
return error_html, 1, 1, [], "Page 1 of 1"
def refresh_papers(current_page, query):
"""
Refresh the papers. Fetch new data if a new day has started.
"""
success = paper_manager.fetch_papers()
if success:
# Reset to first page on refresh
new_page = 1
if query:
filtered = paper_manager.search_papers(query)
total_pages = (len(filtered) + paper_manager.papers_per_page - 1) // paper_manager.papers_per_page if filtered else 1
page_papers = paper_manager.render_papers(filtered, new_page)
page_info_text = f"Page {new_page} of {total_pages}"
return page_papers, new_page, total_pages, filtered, page_info_text
else:
papers = paper_manager.cache
total_pages = paper_manager.total_pages
page_papers = paper_manager.render_papers(papers, new_page)
page_info_text = f"Page {new_page} of {total_pages}"
return page_papers, new_page, total_pages, papers, page_info_text
else:
error_html = "<div>Error fetching papers. Please try again later.</div>"
return error_html, current_page, paper_manager.total_pages, [], f"Page {current_page} of {paper_manager.total_pages}"
def search_papers(query, papers):
"""
Search for papers based on the query and update the display.
"""
if query:
filtered = paper_manager.search_papers(query)
else:
filtered = papers
total_pages = (len(filtered) + paper_manager.papers_per_page - 1) // paper_manager.papers_per_page if filtered else 1
page = 1
page_papers = paper_manager.render_papers(filtered, page)
page_info_text = f"Page {page} of {total_pages}"
return page_papers, page, total_pages, filtered, page_info_text
def change_page(direction, current_page, total_pages, papers, query):
"""
Change the current page based on the direction ('next' or 'prev').
"""
if direction == "next" and current_page < total_pages:
new_page = current_page + 1
elif direction == "prev" and current_page > 1:
new_page = current_page - 1
else:
new_page = current_page # No change if limits are reached
if not papers:
page_papers = "<div>No papers available for this page.</div>"
else:
page_papers = paper_manager.render_papers(papers, new_page)
page_info_text = f"Page {new_page} of {total_pages}"
return page_papers, new_page, total_pages, papers, page_info_text
def go_prev(current_page, total_pages, papers, query):
"""
Handle the 'Previous Page' button click.
"""
return change_page("prev", current_page, total_pages, papers, query)
def go_next(current_page, total_pages, papers, query):
"""
Handle the 'Next Page' button click.
"""
return change_page("next", current_page, total_pages, papers, query)
# Define the Gradio Blocks interface
demo = gr.Blocks(css=css)
with demo:
with gr.Column(elem_classes=["container"]):
# Title
gr.Markdown("# Daily Papers - HackerNews Style", elem_classes=["title"])
# Search and Refresh Row
with gr.Row(elem_classes=["search-row"]):
search_input = gr.Textbox(label="Search papers", placeholder="Enter search term...")
refresh_button = gr.Button("Refresh")
# Paper List Display
paper_list = gr.HTML(elem_classes=["paper-list"])
# Pagination Controls
with gr.Row(elem_classes=["footer"]):
prev_button = gr.Button("Previous Page")
page_info = gr.Markdown("Page 1 of 1")
next_button = gr.Button("Next Page")
# Hidden States
current_page_state = gr.State(1)
total_pages_state = gr.State(1)
papers_state = gr.State([])
# Initialize the app on load
demo.load(
initialize,
outputs=[paper_list, current_page_state, total_pages_state, papers_state, page_info]
)
# Search Functionality
search_input.submit(
search_papers,
inputs=[search_input, papers_state],
outputs=[paper_list, current_page_state, total_pages_state, papers_state, page_info]
)
search_input.change(
search_papers,
inputs=[search_input, papers_state],
outputs=[paper_list, current_page_state, total_pages_state, papers_state, page_info]
)
# Refresh Functionality
refresh_button.click(
refresh_papers,
inputs=[current_page_state, search_input],
outputs=[paper_list, current_page_state, total_pages_state, papers_state, page_info]
)
# Pagination Buttons
prev_button.click(
go_prev,
inputs=[current_page_state, total_pages_state, papers_state, search_input],
outputs=[paper_list, current_page_state, total_pages_state, papers_state, page_info]
)
next_button.click(
go_next,
inputs=[current_page_state, total_pages_state, papers_state, search_input],
outputs=[paper_list, current_page_state, total_pages_state, papers_state, page_info]
)
# Launch the Gradio app
demo.launch()
|