Spaces:
Building
Building
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" | |
return f"""<div style='border-bottom: 1px solid #eee; padding: 10px 0;'> | |
<a href='{url}' target='_blank' style='co | |