Spaces:
Runtime error
Runtime error
updates
Browse files
app.py
CHANGED
|
@@ -1,6 +1,6 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import requests
|
| 3 |
-
from datetime import datetime, timezone
|
| 4 |
|
| 5 |
API_URL = "https://huggingface.co/api/daily_papers"
|
| 6 |
|
|
@@ -10,6 +10,7 @@ class PaperManager:
|
|
| 10 |
self.current_page = 1
|
| 11 |
self.papers = []
|
| 12 |
self.total_pages = 1
|
|
|
|
| 13 |
|
| 14 |
def calculate_score(self, paper):
|
| 15 |
"""
|
|
@@ -31,21 +32,28 @@ class PaperManager:
|
|
| 31 |
score = upvotes / ((time_diff_hours + 2) ** 1.5)
|
| 32 |
return score
|
| 33 |
|
| 34 |
-
def fetch_papers(self):
|
|
|
|
|
|
|
|
|
|
| 35 |
try:
|
| 36 |
response = requests.get(f"{API_URL}?limit=100")
|
| 37 |
response.raise_for_status()
|
| 38 |
data = response.json()
|
| 39 |
|
|
|
|
|
|
|
|
|
|
| 40 |
# Sort papers by calculated score descending
|
| 41 |
self.papers = sorted(
|
| 42 |
-
|
| 43 |
key=lambda x: self.calculate_score(x),
|
| 44 |
reverse=True
|
| 45 |
)
|
| 46 |
|
| 47 |
self.total_pages = max((len(self.papers) + self.papers_per_page - 1) // self.papers_per_page, 1)
|
| 48 |
self.current_page = 1
|
|
|
|
| 49 |
return True
|
| 50 |
except requests.RequestException as e:
|
| 51 |
print(f"Error fetching papers: {e}")
|
|
@@ -54,6 +62,36 @@ class PaperManager:
|
|
| 54 |
print(f"Unexpected error: {e}")
|
| 55 |
return False
|
| 56 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
def format_paper(self, paper, rank):
|
| 58 |
title = paper.get('title', 'No title')
|
| 59 |
paper_id = paper.get('paper', {}).get('id', '')
|
|
@@ -110,6 +148,15 @@ class PaperManager:
|
|
| 110 |
self.current_page -= 1
|
| 111 |
return self.render_papers()
|
| 112 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 113 |
paper_manager = PaperManager()
|
| 114 |
|
| 115 |
def initialize_app():
|
|
@@ -119,7 +166,7 @@ def initialize_app():
|
|
| 119 |
return "<div class='no-papers'>Failed to fetch papers. Please try again later.</div>"
|
| 120 |
|
| 121 |
def refresh_papers():
|
| 122 |
-
if paper_manager.fetch_papers():
|
| 123 |
return paper_manager.render_papers()
|
| 124 |
else:
|
| 125 |
return "<div class='no-papers'>Failed to refresh papers. Please try again later.</div>"
|
|
@@ -284,7 +331,8 @@ with demo:
|
|
| 284 |
|
| 285 |
Once your paper is submitted, it will automatically appear in this demo.
|
| 286 |
""")
|
| 287 |
-
|
|
|
|
| 288 |
with gr.Row():
|
| 289 |
gr.HTML("""
|
| 290 |
<table border="0" cellpadding="0" cellspacing="0" class="header-table">
|
|
@@ -300,14 +348,26 @@ with demo:
|
|
| 300 |
</tr>
|
| 301 |
</table>
|
| 302 |
""")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 303 |
# Paper list
|
| 304 |
paper_list = gr.HTML()
|
|
|
|
| 305 |
# Navigation Buttons
|
| 306 |
with gr.Row():
|
| 307 |
prev_button = gr.Button("Prev")
|
| 308 |
next_button = gr.Button("Next")
|
| 309 |
|
| 310 |
-
|
| 311 |
# Load papers on app start
|
| 312 |
demo.load(initialize_app, outputs=[paper_list])
|
| 313 |
|
|
@@ -317,6 +377,13 @@ with demo:
|
|
| 317 |
refresh_button = gr.Button("Refresh", visible=False, elem_id="refresh-hidden")
|
| 318 |
refresh_button.click(refresh_papers, outputs=[paper_list])
|
| 319 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 320 |
# Bind the visible Refresh button to the hidden one using JavaScript
|
| 321 |
gr.HTML("""
|
| 322 |
<script>
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import requests
|
| 3 |
+
from datetime import datetime, timezone, timedelta
|
| 4 |
|
| 5 |
API_URL = "https://huggingface.co/api/daily_papers"
|
| 6 |
|
|
|
|
| 10 |
self.current_page = 1
|
| 11 |
self.papers = []
|
| 12 |
self.total_pages = 1
|
| 13 |
+
self.time_filter = 'All Time' # Default filter
|
| 14 |
|
| 15 |
def calculate_score(self, paper):
|
| 16 |
"""
|
|
|
|
| 32 |
score = upvotes / ((time_diff_hours + 2) ** 1.5)
|
| 33 |
return score
|
| 34 |
|
| 35 |
+
def fetch_papers(self, time_filter='All Time'):
|
| 36 |
+
"""
|
| 37 |
+
Fetch papers from the API and apply time filtering.
|
| 38 |
+
"""
|
| 39 |
try:
|
| 40 |
response = requests.get(f"{API_URL}?limit=100")
|
| 41 |
response.raise_for_status()
|
| 42 |
data = response.json()
|
| 43 |
|
| 44 |
+
# Apply time filter
|
| 45 |
+
filtered_data = self.apply_time_filter(data, time_filter)
|
| 46 |
+
|
| 47 |
# Sort papers by calculated score descending
|
| 48 |
self.papers = sorted(
|
| 49 |
+
filtered_data,
|
| 50 |
key=lambda x: self.calculate_score(x),
|
| 51 |
reverse=True
|
| 52 |
)
|
| 53 |
|
| 54 |
self.total_pages = max((len(self.papers) + self.papers_per_page - 1) // self.papers_per_page, 1)
|
| 55 |
self.current_page = 1
|
| 56 |
+
self.time_filter = time_filter
|
| 57 |
return True
|
| 58 |
except requests.RequestException as e:
|
| 59 |
print(f"Error fetching papers: {e}")
|
|
|
|
| 62 |
print(f"Unexpected error: {e}")
|
| 63 |
return False
|
| 64 |
|
| 65 |
+
def apply_time_filter(self, data, time_filter):
|
| 66 |
+
"""
|
| 67 |
+
Filter papers based on the selected timeframe.
|
| 68 |
+
"""
|
| 69 |
+
if time_filter == 'All Time':
|
| 70 |
+
return data
|
| 71 |
+
|
| 72 |
+
now = datetime.now(timezone.utc)
|
| 73 |
+
if time_filter == 'Last Week':
|
| 74 |
+
threshold = now - timedelta(weeks=1)
|
| 75 |
+
elif time_filter == 'Last Month':
|
| 76 |
+
threshold = now - timedelta(days=30)
|
| 77 |
+
elif time_filter == 'Last Year':
|
| 78 |
+
threshold = now - timedelta(days=365)
|
| 79 |
+
else:
|
| 80 |
+
# If an unknown filter is provided, default to all time
|
| 81 |
+
return data
|
| 82 |
+
|
| 83 |
+
filtered = []
|
| 84 |
+
for paper in data:
|
| 85 |
+
published_at_str = paper.get('publishedAt', '')
|
| 86 |
+
try:
|
| 87 |
+
published_time = datetime.fromisoformat(published_at_str.replace('Z', '+00:00'))
|
| 88 |
+
if published_time >= threshold:
|
| 89 |
+
filtered.append(paper)
|
| 90 |
+
except ValueError:
|
| 91 |
+
# Skip papers with invalid date formats
|
| 92 |
+
continue
|
| 93 |
+
return filtered
|
| 94 |
+
|
| 95 |
def format_paper(self, paper, rank):
|
| 96 |
title = paper.get('title', 'No title')
|
| 97 |
paper_id = paper.get('paper', {}).get('id', '')
|
|
|
|
| 148 |
self.current_page -= 1
|
| 149 |
return self.render_papers()
|
| 150 |
|
| 151 |
+
def set_time_filter(self, time_filter):
|
| 152 |
+
"""
|
| 153 |
+
Set the time filter and fetch papers accordingly.
|
| 154 |
+
"""
|
| 155 |
+
if self.fetch_papers(time_filter):
|
| 156 |
+
return self.render_papers()
|
| 157 |
+
else:
|
| 158 |
+
return "<div class='no-papers'>Failed to fetch papers. Please try again later.</div>"
|
| 159 |
+
|
| 160 |
paper_manager = PaperManager()
|
| 161 |
|
| 162 |
def initialize_app():
|
|
|
|
| 166 |
return "<div class='no-papers'>Failed to fetch papers. Please try again later.</div>"
|
| 167 |
|
| 168 |
def refresh_papers():
|
| 169 |
+
if paper_manager.fetch_papers(paper_manager.time_filter):
|
| 170 |
return paper_manager.render_papers()
|
| 171 |
else:
|
| 172 |
return "<div class='no-papers'>Failed to refresh papers. Please try again later.</div>"
|
|
|
|
| 331 |
|
| 332 |
Once your paper is submitted, it will automatically appear in this demo.
|
| 333 |
""")
|
| 334 |
+
|
| 335 |
+
# Header with Refresh Button and Time Filter
|
| 336 |
with gr.Row():
|
| 337 |
gr.HTML("""
|
| 338 |
<table border="0" cellpadding="0" cellspacing="0" class="header-table">
|
|
|
|
| 348 |
</tr>
|
| 349 |
</table>
|
| 350 |
""")
|
| 351 |
+
|
| 352 |
+
# Time Filter Dropdown
|
| 353 |
+
with gr.Row(elem_classes=["time-filter-row"], elem_id="time-filter-row"):
|
| 354 |
+
gr.HTML("<label for='time-filter'>Filter by Timeframe: </label>")
|
| 355 |
+
time_filter_dropdown = gr.Dropdown(
|
| 356 |
+
choices=["All Time", "Last Week", "Last Month", "Last Year"],
|
| 357 |
+
value="All Time",
|
| 358 |
+
label="Timeframe",
|
| 359 |
+
interactive=True,
|
| 360 |
+
elem_id="time-filter-dropdown"
|
| 361 |
+
)
|
| 362 |
+
|
| 363 |
# Paper list
|
| 364 |
paper_list = gr.HTML()
|
| 365 |
+
|
| 366 |
# Navigation Buttons
|
| 367 |
with gr.Row():
|
| 368 |
prev_button = gr.Button("Prev")
|
| 369 |
next_button = gr.Button("Next")
|
| 370 |
|
|
|
|
| 371 |
# Load papers on app start
|
| 372 |
demo.load(initialize_app, outputs=[paper_list])
|
| 373 |
|
|
|
|
| 377 |
refresh_button = gr.Button("Refresh", visible=False, elem_id="refresh-hidden")
|
| 378 |
refresh_button.click(refresh_papers, outputs=[paper_list])
|
| 379 |
|
| 380 |
+
# Time Filter change
|
| 381 |
+
time_filter_dropdown.change(
|
| 382 |
+
paper_manager.set_time_filter,
|
| 383 |
+
inputs=[time_filter_dropdown],
|
| 384 |
+
outputs=[paper_list]
|
| 385 |
+
)
|
| 386 |
+
|
| 387 |
# Bind the visible Refresh button to the hidden one using JavaScript
|
| 388 |
gr.HTML("""
|
| 389 |
<script>
|