File size: 2,395 Bytes
c6bd2a2
 
 
 
 
 
3497525
 
 
354cb34
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3497525
 
354cb34
 
 
3497525
bd08853
 
 
3497525
 
bd08853
 
 
 
 
 
3497525
 
354cb34
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
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