akhaliq HF staff commited on
Commit
bd08853
·
verified ·
1 Parent(s): 92a4d78

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -11
app.py CHANGED
@@ -21,18 +21,21 @@ class PaperManager:
21
  return papers, total_pages
22
  except requests.RequestException as e:
23
  print(f"Error fetching papers: {e}")
24
- return None, 1
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(
33
- paper.get('publishedAt', datetime.now(timezone.utc).isoformat()).replace('Z', '+00:00')
34
- )
35
- time_ago = (datetime.now(timezone.utc) - published_time).days
 
 
36
 
37
  return f"""<div style='border-bottom: 1px solid #eee; padding: 10px 0;'>
38
  <a href='{url}' target='_blank' style='color: #000; text-decoration: none; font-weight: bold;'>{title}</a>
@@ -52,6 +55,8 @@ class PaperManager:
52
  return self.render_papers(papers)
53
 
54
  filtered_papers = [paper for paper in papers if query.lower() in paper.get('title', '').lower()]
 
 
55
  return self.render_papers(filtered_papers)
56
 
57
  css = """
@@ -106,6 +111,7 @@ def initialize():
106
  return html, 1, total_pages, papers # paper_list, current_page, total_pages, papers
107
 
108
  def refresh_papers(current_page, query):
 
109
  papers, total_pages = paper_manager.fetch_papers(page=1)
110
  if query:
111
  html = paper_manager.search_papers(papers, query)
@@ -132,6 +138,12 @@ def change_page(direction, current_page, total_pages, papers, query):
132
  html = paper_manager.render_papers(papers)
133
  return html, new_page, total_pages, papers
134
 
 
 
 
 
 
 
135
  demo = gr.Blocks(css=css)
136
 
137
  with demo:
@@ -165,13 +177,13 @@ with demo:
165
  outputs=[paper_list, current_page_state, total_pages_state, papers_state])
166
 
167
  # Previous Page
168
- prev_button.click(change_page,
169
- inputs=["prev", current_page_state, total_pages_state, papers_state, search_input],
170
  outputs=[paper_list, current_page_state, total_pages_state, papers_state])
171
 
172
  # Next Page
173
- next_button.click(change_page,
174
- inputs=["next", current_page_state, total_pages_state, papers_state, search_input],
175
  outputs=[paper_list, current_page_state, total_pages_state, papers_state])
176
 
177
  demo.launch()
 
21
  return papers, total_pages
22
  except requests.RequestException as e:
23
  print(f"Error fetching papers: {e}")
24
+ return [], 1 # Return empty list and default to 1 page
25
 
26
  def format_paper(self, paper):
27
  title = paper.get('title', 'No title')
28
+ paper_id = paper.get('paper', {}).get('id', '')
29
+ url = f"https://huggingface.co/papers/{paper_id}" if paper_id else "#"
30
+ authors = ', '.join([author.get('name', 'Unknown') for author in paper.get('paper', {}).get('authors', [])])
31
  upvotes = paper.get('paper', {}).get('upvotes', 0)
32
  comments = paper.get('numComments', 0)
33
+ published_at = paper.get('publishedAt', datetime.now(timezone.utc).isoformat())
34
+ try:
35
+ published_time = datetime.fromisoformat(published_at.replace('Z', '+00:00'))
36
+ time_ago = (datetime.now(timezone.utc) - published_time).days
37
+ except ValueError:
38
+ time_ago = "Unknown"
39
 
40
  return f"""<div style='border-bottom: 1px solid #eee; padding: 10px 0;'>
41
  <a href='{url}' target='_blank' style='color: #000; text-decoration: none; font-weight: bold;'>{title}</a>
 
55
  return self.render_papers(papers)
56
 
57
  filtered_papers = [paper for paper in papers if query.lower() in paper.get('title', '').lower()]
58
+ if not filtered_papers:
59
+ return "<div>No papers match your search query.</div>"
60
  return self.render_papers(filtered_papers)
61
 
62
  css = """
 
111
  return html, 1, total_pages, papers # paper_list, current_page, total_pages, papers
112
 
113
  def refresh_papers(current_page, query):
114
+ # Refresh always starts from page 1
115
  papers, total_pages = paper_manager.fetch_papers(page=1)
116
  if query:
117
  html = paper_manager.search_papers(papers, query)
 
138
  html = paper_manager.render_papers(papers)
139
  return html, new_page, total_pages, papers
140
 
141
+ def go_prev(current_page, total_pages, papers, query):
142
+ return change_page("prev", current_page, total_pages, papers, query)
143
+
144
+ def go_next(current_page, total_pages, papers, query):
145
+ return change_page("next", current_page, total_pages, papers, query)
146
+
147
  demo = gr.Blocks(css=css)
148
 
149
  with demo:
 
177
  outputs=[paper_list, current_page_state, total_pages_state, papers_state])
178
 
179
  # Previous Page
180
+ prev_button.click(go_prev,
181
+ inputs=[current_page_state, total_pages_state, papers_state, search_input],
182
  outputs=[paper_list, current_page_state, total_pages_state, papers_state])
183
 
184
  # Next Page
185
+ next_button.click(go_next,
186
+ inputs=[current_page_state, total_pages_state, papers_state, search_input],
187
  outputs=[paper_list, current_page_state, total_pages_state, papers_state])
188
 
189
  demo.launch()