akhaliq's picture
akhaliq HF staff
Update app.py
4681e49 verified
raw
history blame
6.24 kB
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.current_page = 1
self.papers = []
self.total_pages = 1
def fetch_papers(self):
try:
response = requests.get(f"{API_URL}?limit=50")
response.raise_for_status()
data = response.json()
# Sort papers by upvotes (descending order)
self.papers = sorted(data, key=lambda x: x.get('paper', {}).get('upvotes', 0), reverse=True)
self.total_pages = (len(self.papers) + self.papers_per_page - 1) // self.papers_per_page
self.current_page = 1 # Reset to first page after fetching
return True
except requests.RequestException as e:
print(f"Error fetching papers: {e}")
return False
def format_paper(self, paper):
title = paper.get('title', 'No title')
url = f"https://huggingface.co/papers/{paper['paper'].get('id', '')}"
authors = ', '.join([author.get('name', '') for author in paper['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 = (datetime.now(timezone.utc) - published_time).days
return f"""
<div class="paper-item">
<h3><a href="{url}" target="_blank">{title}</a></h3>
<p class="paper-meta">
<span class="upvotes">{upvotes} upvotes</span> |
<span class="authors">by {authors}</span> |
<span class="time-ago">{time_ago} days ago</span> |
<span class="comments">{comments} comments</span>
</p>
</div>
"""
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>"
return "".join([self.format_paper(paper) for paper in current_papers])
def search_papers(self, query):
if not query:
self.current_page = 1
return self.render_papers()
filtered_papers = [paper for paper in self.papers if query.lower() in paper.get('title', '').lower()]
return "".join([self.format_paper(paper) for paper in filtered_papers[:self.papers_per_page]])
def next_page(self):
if self.current_page < self.total_pages:
self.current_page += 1
return self.render_papers(), f"Page {self.current_page} of {self.total_pages}"
def prev_page(self):
if self.current_page > 1:
self.current_page -= 1
return self.render_papers(), f"Page {self.current_page} of {self.total_pages}"
css = """
body {
font-family: 'Roboto', sans-serif;
background-color: #f0f2f5;
margin: 0;
padding: 0;
}
.container {
max-width: 800px;
margin: 2rem auto;
background-color: white;
border-radius: 8px;
box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
padding: 2rem;
}
.title {
text-align: center;
color: #1a202c;
font-size: 2rem;
margin-bottom: 1.5rem;
}
.search-row {
display: flex;
gap: 1rem;
margin-bottom: 1.5rem;
}
.paper-list {
max-height: 500px;
overflow-y: auto;
border: 1px solid #e2e8f0;
border-radius: 8px;
padding: 1rem;
}
.paper-item {
border-bottom: 1px solid #e2e8f0;
padding: 1rem 0;
}
.paper-item:last-child {
border-bottom: none;
}
.paper-item h3 {
margin: 0 0 0.5rem 0;
}
.paper-item a {
color: #2b6cb0;
text-decoration: none;
font-weight: 600;
}
.paper-item a:hover {
text-decoration: underline;
}
.paper-meta {
font-size: 0.875rem;
color: #4a5568;
}
.footer {
display: flex;
justify-content: space-between;
align-items: center;
margin-top: 1rem;
}
button {
background-color: #4299e1;
color: white;
border: none;
padding: 0.5rem 1rem;
border-radius: 4px;
cursor: pointer;
transition: background-color 0.3s;
}
button:hover {
background-color: #3182ce;
}
.no-papers {
text-align: center;
color: #718096;
padding: 1rem;
}
"""
paper_manager = PaperManager()
def initialize_app():
if paper_manager.fetch_papers():
return paper_manager.render_papers(), f"Page {paper_manager.current_page} of {paper_manager.total_pages}"
else:
return "<div class='no-papers'>Failed to fetch papers. Please try again later.</div>", "Error"
def refresh_papers():
if paper_manager.fetch_papers():
return paper_manager.render_papers(), f"Page {paper_manager.current_page} of {paper_manager.total_pages}"
else:
return "<div class='no-papers'>Failed to refresh papers. Please try again later.</div>", "Error"
demo = gr.Blocks(css=css)
with demo:
with gr.Column(elem_classes=["container"]):
gr.Markdown("# Daily Papers - HackerNews Style", elem_classes=["title"])
with gr.Row(elem_classes=["search-row"]):
search_input = gr.Textbox(label="Search papers", placeholder="Enter search term...")
refresh_button = gr.Button("Refresh", variant="primary")
paper_list = gr.HTML(elem_classes=["paper-list"])
with gr.Row(elem_classes=["footer"]):
prev_button = gr.Button("Previous Page")
page_info = gr.Markdown()
next_button = gr.Button("Next Page")
demo.load(initialize_app, outputs=[paper_list, page_info])
search_input.change(paper_manager.search_papers, inputs=[search_input], outputs=[paper_list])
refresh_button.click(refresh_papers, outputs=[paper_list, page_info])
prev_button.click(paper_manager.prev_page, outputs=[paper_list, page_info])
next_button.click(paper_manager.next_page, outputs=[paper_list, page_info])
demo.launch()