Update app.py
Browse files
app.py
CHANGED
@@ -1,90 +1,76 @@
|
|
1 |
import gradio as gr
|
2 |
import requests
|
3 |
-
import json
|
4 |
from datetime import datetime, timezone
|
5 |
|
6 |
-
# Fetch data from the API
|
7 |
API_URL = "https://huggingface.co/api/daily_papers"
|
8 |
|
9 |
-
|
10 |
-
|
11 |
-
papers_per_page =
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
|
|
|
|
18 |
data = response.json()
|
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 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
total_pages = (len(papers) + papers_per_page - 1) // papers_per_page
|
75 |
-
return render_papers()
|
76 |
-
|
77 |
-
def next_page():
|
78 |
-
global current_page
|
79 |
-
if current_page < total_pages:
|
80 |
-
current_page += 1
|
81 |
-
return render_papers(), f"Page {current_page} of {total_pages}"
|
82 |
-
|
83 |
-
def prev_page():
|
84 |
-
global current_page
|
85 |
-
if current_page > 1:
|
86 |
-
current_page -= 1
|
87 |
-
return render_papers(), f"Page {current_page} of {total_pages}"
|
88 |
|
89 |
css = """
|
90 |
body {
|
@@ -107,6 +93,13 @@ body {
|
|
107 |
}
|
108 |
"""
|
109 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
110 |
demo = gr.Blocks(css=css)
|
111 |
|
112 |
with demo:
|
@@ -114,16 +107,16 @@ with demo:
|
|
114 |
with gr.Row(elem_classes=["search-row"]):
|
115 |
search_input = gr.Textbox(label="Search papers", placeholder="Enter search term...")
|
116 |
refresh_button = gr.Button("Refresh")
|
117 |
-
paper_list = gr.HTML(render_papers(), elem_classes=["paper-list"])
|
118 |
|
119 |
with gr.Row():
|
120 |
prev_button = gr.Button("Previous Page")
|
121 |
next_button = gr.Button("Next Page")
|
122 |
-
page_info = gr.Markdown(f"Page {current_page} of {total_pages}")
|
123 |
|
124 |
-
search_input.change(search_papers, inputs=[search_input], outputs=[paper_list])
|
125 |
-
refresh_button.click(refresh_papers, outputs=[paper_list])
|
126 |
-
prev_button.click(prev_page, outputs=[paper_list, page_info])
|
127 |
-
next_button.click(next_page, outputs=[paper_list, page_info])
|
128 |
|
129 |
-
demo.launch()
|
|
|
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 |
|
7 |
+
class PaperManager:
|
8 |
+
def __init__(self, papers_per_page=10):
|
9 |
+
self.papers_per_page = papers_per_page
|
10 |
+
self.current_page = 1
|
11 |
+
self.papers = []
|
12 |
+
self.total_pages = 0
|
13 |
+
|
14 |
+
def fetch_papers(self, page=1):
|
15 |
+
try:
|
16 |
+
response = requests.get(f"{API_URL}?page={page}&limit={self.papers_per_page}")
|
17 |
+
response.raise_for_status()
|
18 |
data = response.json()
|
19 |
+
self.papers = sorted(data, key=lambda x: x.get('paper', {}).get('upvotes', 0), reverse=True)
|
20 |
+
self.total_pages = (len(self.papers) + self.papers_per_page - 1) // self.papers_per_page
|
21 |
+
return True
|
22 |
+
except requests.RequestException as e:
|
23 |
+
print(f"Error fetching papers: {e}")
|
24 |
+
return False
|
25 |
+
|
26 |
+
def format_paper(self, paper):
|
27 |
+
title = paper.get('title', 'No title')
|
28 |
+
url = f"https://huggingface.co/papers/{paper['paper'].get('id', '')}"
|
29 |
+
authors = ', '.join([author.get('name', '') for author in paper['paper'].get('authors', [])])
|
30 |
+
upvotes = paper.get('paper', {}).get('upvotes', 0)
|
31 |
+
comments = paper.get('numComments', 0)
|
32 |
+
published_time = datetime.fromisoformat(paper.get('publishedAt', datetime.now(timezone.utc).isoformat()).replace('Z', '+00:00'))
|
33 |
+
time_ago = (datetime.now(timezone.utc) - published_time).days
|
34 |
+
|
35 |
+
return f"""<div style='border-bottom: 1px solid #eee; padding: 10px 0;'>
|
36 |
+
<a href='{url}' target='_blank' style='color: #000; text-decoration: none; font-weight: bold;'>{title}</a>
|
37 |
+
<div style='font-size: 0.8em; color: #666; margin-top: 5px;'>
|
38 |
+
{upvotes} upvotes | by {authors} | {time_ago} days ago | {comments} comments
|
39 |
+
</div>
|
40 |
+
</div>"""
|
41 |
+
|
42 |
+
def render_papers(self):
|
43 |
+
if not self.papers:
|
44 |
+
if not self.fetch_papers(self.current_page):
|
45 |
+
return "<div>Failed to fetch papers. Please try again later.</div>"
|
46 |
+
|
47 |
+
start_index = (self.current_page - 1) * self.papers_per_page
|
48 |
+
end_index = start_index + self.papers_per_page
|
49 |
+
current_papers = self.papers[start_index:end_index]
|
50 |
+
|
51 |
+
if not current_papers:
|
52 |
+
return "<div>No more papers available.</div>"
|
53 |
+
|
54 |
+
return "".join([self.format_paper(paper) for paper in current_papers])
|
55 |
+
|
56 |
+
def search_papers(self, query):
|
57 |
+
if not query:
|
58 |
+
return self.render_papers()
|
59 |
+
|
60 |
+
filtered_papers = [paper for paper in self.papers if query.lower() in paper.get('title', '').lower()]
|
61 |
+
return "".join([self.format_paper(paper) for paper in filtered_papers[:self.papers_per_page]])
|
62 |
+
|
63 |
+
def next_page(self):
|
64 |
+
if self.current_page < self.total_pages:
|
65 |
+
self.current_page += 1
|
66 |
+
self.fetch_papers(self.current_page)
|
67 |
+
return self.render_papers(), f"Page {self.current_page} of {self.total_pages}"
|
68 |
+
|
69 |
+
def prev_page(self):
|
70 |
+
if self.current_page > 1:
|
71 |
+
self.current_page -= 1
|
72 |
+
self.fetch_papers(self.current_page)
|
73 |
+
return self.render_papers(), f"Page {self.current_page} of {self.total_pages}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
74 |
|
75 |
css = """
|
76 |
body {
|
|
|
93 |
}
|
94 |
"""
|
95 |
|
96 |
+
paper_manager = PaperManager()
|
97 |
+
|
98 |
+
def refresh_papers():
|
99 |
+
paper_manager.current_page = 1
|
100 |
+
paper_manager.fetch_papers()
|
101 |
+
return paper_manager.render_papers(), f"Page {paper_manager.current_page} of {paper_manager.total_pages}"
|
102 |
+
|
103 |
demo = gr.Blocks(css=css)
|
104 |
|
105 |
with demo:
|
|
|
107 |
with gr.Row(elem_classes=["search-row"]):
|
108 |
search_input = gr.Textbox(label="Search papers", placeholder="Enter search term...")
|
109 |
refresh_button = gr.Button("Refresh")
|
110 |
+
paper_list = gr.HTML(paper_manager.render_papers(), elem_classes=["paper-list"])
|
111 |
|
112 |
with gr.Row():
|
113 |
prev_button = gr.Button("Previous Page")
|
114 |
next_button = gr.Button("Next Page")
|
115 |
+
page_info = gr.Markdown(f"Page {paper_manager.current_page} of {paper_manager.total_pages}")
|
116 |
|
117 |
+
search_input.change(paper_manager.search_papers, inputs=[search_input], outputs=[paper_list])
|
118 |
+
refresh_button.click(refresh_papers, outputs=[paper_list, page_info])
|
119 |
+
prev_button.click(paper_manager.prev_page, outputs=[paper_list, page_info])
|
120 |
+
next_button.click(paper_manager.next_page, outputs=[paper_list, page_info])
|
121 |
|
122 |
+
demo.launch()
|