Spaces:
Runtime error
Runtime error
| import gradio as gr | |
| import requests | |
| from datetime import datetime, timezone, timedelta | |
| 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 | |
| self.time_filter = 'All Time' # Default filter | |
| def calculate_score(self, paper): | |
| """ | |
| Calculate the score of a paper based on upvotes and age. | |
| This mimics the "hotness" algorithm used by platforms like Hacker News. | |
| """ | |
| upvotes = paper.get('paper', {}).get('upvotes', 0) | |
| published_at_str = paper.get('publishedAt', datetime.now(timezone.utc).isoformat()) | |
| try: | |
| published_time = datetime.fromisoformat(published_at_str.replace('Z', '+00:00')) | |
| except ValueError: | |
| # If parsing fails, use current time to minimize the impact on sorting | |
| published_time = datetime.now(timezone.utc) | |
| time_diff = datetime.now(timezone.utc) - published_time | |
| time_diff_hours = time_diff.total_seconds() / 3600 # Convert time difference to hours | |
| # Avoid division by zero and apply the hotness formula | |
| score = upvotes / ((time_diff_hours + 2) ** 1.5) | |
| return score | |
| def fetch_papers(self, time_filter='All Time'): | |
| """ | |
| Fetch papers from the API and apply time filtering. | |
| """ | |
| try: | |
| response = requests.get(f"{API_URL}?limit=100") | |
| response.raise_for_status() | |
| data = response.json() | |
| # Apply time filter | |
| filtered_data = self.apply_time_filter(data, time_filter) | |
| # Sort papers by calculated score descending | |
| self.papers = sorted( | |
| filtered_data, | |
| key=lambda x: self.calculate_score(x), | |
| reverse=True | |
| ) | |
| self.total_pages = max((len(self.papers) + self.papers_per_page - 1) // self.papers_per_page, 1) | |
| self.current_page = 1 | |
| self.time_filter = time_filter | |
| 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 apply_time_filter(self, data, time_filter): | |
| """ | |
| Filter papers based on the selected timeframe. | |
| """ | |
| if time_filter == 'All Time': | |
| return data | |
| now = datetime.now(timezone.utc) | |
| if time_filter == 'Last Week': | |
| threshold = now - timedelta(weeks=1) | |
| elif time_filter == 'Last Month': | |
| threshold = now - timedelta(days=30) | |
| elif time_filter == 'Last Year': | |
| threshold = now - timedelta(days=365) | |
| else: | |
| # If an unknown filter is provided, default to all time | |
| return data | |
| filtered = [] | |
| for paper in data: | |
| published_at_str = paper.get('publishedAt', '') | |
| try: | |
| published_time = datetime.fromisoformat(published_at_str.replace('Z', '+00:00')) | |
| if published_time >= threshold: | |
| filtered.append(paper) | |
| except ValueError: | |
| # Skip papers with invalid date formats | |
| continue | |
| return filtered | |
| 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', [])]) or 'Unknown' | |
| 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_diff = datetime.now(timezone.utc) - published_time | |
| time_ago_days = time_diff.days | |
| time_ago = f"{time_ago_days} days ago" if time_ago_days > 0 else "today" | |
| return f""" | |
| <tr class="athing"> | |
| <td align="right" valign="top" class="title"><span class="rank">{rank}.</span></td> | |
| <td valign="top" class="title"> | |
| <a href="{url}" class="storylink" target="_blank">{title}</a> | |
| </td> | |
| </tr> | |
| <tr> | |
| <td colspan="1"></td> | |
| <td class="subtext"> | |
| <span class="score">{upvotes} upvotes</span><br> | |
| authors: {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() | |
| def prev_page(self): | |
| if self.current_page > 1: | |
| self.current_page -= 1 | |
| return self.render_papers() | |
| def set_time_filter(self, time_filter): | |
| """ | |
| Set the time filter and fetch papers accordingly. | |
| """ | |
| if self.fetch_papers(time_filter): | |
| return self.render_papers() | |
| else: | |
| return "<div class='no-papers'>Failed to fetch papers. Please try again later.</div>" | |
| 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>" | |
| def refresh_papers(): | |
| if paper_manager.fetch_papers(paper_manager.time_filter): | |
| return paper_manager.render_papers() | |
| else: | |
| return "<div class='no-papers'>Failed to refresh 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%; | |
| } | |
| .header-table { | |
| width: 100%; | |
| background-color: #ff6600; | |
| padding: 2px 10px; | |
| } | |
| .header-table a { | |
| color: black; | |
| font-weight: bold; | |
| font-size: 14pt; | |
| text-decoration: none; | |
| } | |
| .itemlist .athing { | |
| background-color: #f6f6ef; | |
| } | |
| .rank { | |
| font-size: 14pt; | |
| color: #828282; | |
| padding-right: 5px; | |
| } | |
| .storylink { | |
| font-size: 10pt; | |
| } | |
| .subtext { | |
| font-size: 8pt; | |
| color: #828282; | |
| padding-left: 40px; | |
| } | |
| .subtext a { | |
| color: #828282; | |
| text-decoration: none; | |
| } | |
| #refresh-button { | |
| background: none; | |
| border: none; | |
| color: black; | |
| font-weight: bold; | |
| font-size: 14pt; | |
| cursor: pointer; | |
| } | |
| .no-papers { | |
| text-align: center; | |
| color: #828282; | |
| padding: 1rem; | |
| font-size: 14pt; | |
| } | |
| @media (max-width: 640px) { | |
| .header-table a { | |
| font-size: 12pt; | |
| } | |
| .storylink { | |
| font-size: 9pt; | |
| } | |
| .subtext { | |
| font-size: 7pt; | |
| } | |
| } | |
| /* Dark mode */ | |
| @media (prefers-color-scheme: dark) { | |
| body { | |
| background-color: #121212; | |
| color: #e0e0e0; | |
| } | |
| a { | |
| color: #add8e6; | |
| } | |
| a:visited { | |
| color: #9370db; | |
| } | |
| .header-table { | |
| background-color: #ff6600; | |
| } | |
| .header-table a { | |
| color: black; | |
| } | |
| .itemlist .athing { | |
| background-color: #1e1e1e; | |
| } | |
| .rank { | |
| color: #b0b0b0; | |
| } | |
| .subtext { | |
| color: #b0b0b0; | |
| } | |
| .subtext a { | |
| color: #b0b0b0; | |
| } | |
| #refresh-button { | |
| color: #e0e0e0; | |
| } | |
| .no-papers { | |
| color: #b0b0b0; | |
| } | |
| } | |
| """ | |
| demo = gr.Blocks(css=css) | |
| with demo: | |
| with gr.Column(elem_classes=["container"]): | |
| # Accordion for Submission Instructions | |
| with gr.Accordion("How to Submit a Paper", open=False): | |
| gr.Markdown(""" | |
| ### Steps to Submit Your Paper | |
| **Step 1:** Search for your paper and index on Hugging Face: | |
| [https://huggingface.co/papers?search=true](https://huggingface.co/papers?search=true) | |
| **Step 2:** Submit the paper to Daily Papers: | |
| [https://huggingface.co/papers](https://huggingface.co/papers) | |
| Once your paper is submitted, it will automatically appear in this demo. | |
| """) | |
| # Header with Refresh Button and Time Filter | |
| with gr.Row(): | |
| gr.HTML(""" | |
| <table border="0" cellpadding="0" cellspacing="0" class="header-table"> | |
| <tr> | |
| <td> | |
| <span class="pagetop"> | |
| <b class="hnname"><a href="#">Daily Papers</a></b> | |
| </span> | |
| </td> | |
| <td align="right"> | |
| <button id="refresh-button">Refresh</button> | |
| </td> | |
| </tr> | |
| </table> | |
| """) | |
| # Time Filter Dropdown | |
| with gr.Row(elem_classes=["time-filter-row"], elem_id="time-filter-row"): | |
| gr.HTML("<label for='time-filter'>Filter by Timeframe: </label>") | |
| time_filter_dropdown = gr.Dropdown( | |
| choices=["All Time", "Last Week", "Last Month", "Last Year"], | |
| value="All Time", | |
| label="Timeframe", | |
| interactive=True, | |
| elem_id="time-filter-dropdown" | |
| ) | |
| # Paper list | |
| paper_list = gr.HTML() | |
| # Navigation Buttons | |
| with gr.Row(): | |
| prev_button = gr.Button("Prev") | |
| next_button = gr.Button("Next") | |
| # Load papers on app start | |
| demo.load(initialize_app, outputs=[paper_list]) | |
| # Button clicks | |
| prev_button.click(paper_manager.prev_page, outputs=[paper_list]) | |
| next_button.click(paper_manager.next_page, outputs=[paper_list]) | |
| refresh_button = gr.Button("Refresh", visible=False, elem_id="refresh-hidden") | |
| refresh_button.click(refresh_papers, outputs=[paper_list]) | |
| # Time Filter change | |
| time_filter_dropdown.change( | |
| paper_manager.set_time_filter, | |
| inputs=[time_filter_dropdown], | |
| outputs=[paper_list] | |
| ) | |
| # Bind the visible Refresh button to the hidden one using JavaScript | |
| gr.HTML(""" | |
| <script> | |
| document.getElementById('refresh-button').addEventListener('click', function() { | |
| document.getElementById('refresh-hidden').click(); | |
| }); | |
| </script> | |
| """) | |
| demo.launch() |