jkorstad commited on
Commit
084d3d6
·
verified ·
1 Parent(s): c54626d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -26
app.py CHANGED
@@ -111,7 +111,7 @@ df['url'] = 'https://huggingface.co/spaces/' + df['id']
111
 
112
  # ------------------------------------------------------
113
 
114
- def search_spaces(search_text="", category="All Categories", offset=0, page_size=100):
115
  if category == "All Categories":
116
  spaces_df = df
117
  else:
@@ -121,7 +121,7 @@ def search_spaces(search_text="", category="All Categories", offset=0, page_size
121
  spaces_df = spaces_df[spaces_df['title'].str.lower().str.contains(search_text.lower())]
122
 
123
  total_spaces = len(spaces_df)
124
- spaces = spaces_df.nlargest(offset + page_size, 'likes')[['title', 'likes', 'url', 'category']]
125
 
126
  # Get category stats
127
  total_likes = spaces_df['likes'].sum()
@@ -139,7 +139,8 @@ def search_spaces(search_text="", category="All Categories", offset=0, page_size
139
  """
140
 
141
  # Only show the spaces from offset to offset + page_size
142
- for _, row in list(spaces.iterrows())[offset:offset + page_size]:
 
143
  html_content += f"""
144
  <div style='padding: 15px;
145
  border: 2px solid var(--color-border-primary);
@@ -177,24 +178,12 @@ def search_spaces(search_text="", category="All Categories", offset=0, page_size
177
  </div>
178
  """
179
 
180
- html_content += "</div>"
181
 
182
- # Add Load More button if there are more spaces
183
- if offset + page_size < total_spaces:
184
- html_content += f"""
185
- <div style='text-align: center; padding: 20px;'>
186
- <p style='color: var(--color-text-primary);'>
187
- {total_spaces - (offset + page_size)} more spaces available
188
- </p>
189
- </div>
190
- """
191
-
192
- html_content += "</div>"
193
- return html_content
194
 
195
  def create_app():
196
- current_offset = gr.State(value=0)
197
-
198
  with gr.Blocks(
199
  title="Hugging Face Spaces Explorer",
200
  theme=gr.themes.Soft(
@@ -202,6 +191,8 @@ def create_app():
202
  secondary_hue="blue",
203
  )
204
  ) as app:
 
 
205
  gr.Markdown("""
206
  # 🤗 Hugging Face Spaces Explorer
207
  Explore and discover popular Hugging Face Spaces by category.
@@ -221,31 +212,50 @@ def create_app():
221
  label="Search Spaces",
222
  placeholder="Enter search terms..."
223
  )
224
-
225
- spaces_display = gr.HTML(value=search_spaces("", "All Categories"))
226
  load_more_btn = gr.Button("Load More", visible=False)
 
 
 
 
 
 
 
 
 
 
 
 
227
 
228
  def load_more(search_text, category, offset):
229
  new_offset = offset + 30
230
- return search_spaces(search_text, category, new_offset), new_offset
 
 
 
 
 
 
231
 
232
  # Update display when category or search changes
233
  category_dropdown.change(
234
- fn=lambda x, y, _: (search_spaces(x, y), 0),
235
  inputs=[search_input, category_dropdown, current_offset],
236
- outputs=[spaces_display, current_offset]
237
  )
 
238
  search_input.change(
239
- fn=lambda x, y, _: (search_spaces(x, y), 0),
240
  inputs=[search_input, category_dropdown, current_offset],
241
- outputs=[spaces_display, current_offset]
242
  )
243
 
244
  # Load More button handler
245
  load_more_btn.click(
246
  fn=load_more,
247
  inputs=[search_input, category_dropdown, current_offset],
248
- outputs=[spaces_display, current_offset]
249
  )
250
 
251
  return app
 
111
 
112
  # ------------------------------------------------------
113
 
114
+ def search_spaces(search_text="", category="All Categories", offset=0, page_size=30):
115
  if category == "All Categories":
116
  spaces_df = df
117
  else:
 
121
  spaces_df = spaces_df[spaces_df['title'].str.lower().str.contains(search_text.lower())]
122
 
123
  total_spaces = len(spaces_df)
124
+ spaces = spaces_df.nlargest(total_spaces, 'likes')[['title', 'likes', 'url', 'category']]
125
 
126
  # Get category stats
127
  total_likes = spaces_df['likes'].sum()
 
139
  """
140
 
141
  # Only show the spaces from offset to offset + page_size
142
+ visible_spaces = spaces.iloc[offset:offset + page_size]
143
+ for _, row in visible_spaces.iterrows():
144
  html_content += f"""
145
  <div style='padding: 15px;
146
  border: 2px solid var(--color-border-primary);
 
178
  </div>
179
  """
180
 
181
+ html_content += "</div></div>"
182
 
183
+ has_more = offset + page_size < total_spaces
184
+ return html_content, has_more, total_spaces - (offset + page_size)
 
 
 
 
 
 
 
 
 
 
185
 
186
  def create_app():
 
 
187
  with gr.Blocks(
188
  title="Hugging Face Spaces Explorer",
189
  theme=gr.themes.Soft(
 
191
  secondary_hue="blue",
192
  )
193
  ) as app:
194
+ current_offset = gr.State(value=0)
195
+
196
  gr.Markdown("""
197
  # 🤗 Hugging Face Spaces Explorer
198
  Explore and discover popular Hugging Face Spaces by category.
 
212
  label="Search Spaces",
213
  placeholder="Enter search terms..."
214
  )
215
+
216
+ spaces_display = gr.HTML()
217
  load_more_btn = gr.Button("Load More", visible=False)
218
+ remaining_text = gr.Markdown(visible=False)
219
+
220
+ def update_display(search_text, category, offset):
221
+ content, has_more, remaining = search_spaces(search_text, category, offset)
222
+ return {
223
+ spaces_display: content,
224
+ load_more_btn: gr.Button.update(visible=has_more),
225
+ remaining_text: gr.Markdown.update(
226
+ visible=has_more,
227
+ value=f"*{remaining} more spaces available*"
228
+ )
229
+ }
230
 
231
  def load_more(search_text, category, offset):
232
  new_offset = offset + 30
233
+ return update_display(search_text, category, new_offset) | {'current_offset': new_offset}
234
+
235
+ # Initial load
236
+ app.load(
237
+ fn=lambda: update_display("", "All Categories", 0),
238
+ outputs=[spaces_display, load_more_btn, remaining_text]
239
+ )
240
 
241
  # Update display when category or search changes
242
  category_dropdown.change(
243
+ fn=lambda x, y, _: update_display(x, y, 0) | {'current_offset': 0},
244
  inputs=[search_input, category_dropdown, current_offset],
245
+ outputs=[spaces_display, load_more_btn, remaining_text, current_offset]
246
  )
247
+
248
  search_input.change(
249
+ fn=lambda x, y, _: update_display(x, y, 0) | {'current_offset': 0},
250
  inputs=[search_input, category_dropdown, current_offset],
251
+ outputs=[spaces_display, load_more_btn, remaining_text, current_offset]
252
  )
253
 
254
  # Load More button handler
255
  load_more_btn.click(
256
  fn=load_more,
257
  inputs=[search_input, category_dropdown, current_offset],
258
+ outputs=[spaces_display, load_more_btn, remaining_text, current_offset]
259
  )
260
 
261
  return app