jkorstad commited on
Commit
c0b662b
·
verified ·
1 Parent(s): cba111a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +60 -59
app.py CHANGED
@@ -111,7 +111,8 @@ df['url'] = 'https://huggingface.co/spaces/' + df['id']
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:
@@ -120,27 +121,32 @@ def search_spaces(search_text="", category="All Categories", offset=0, page_size
120
  if search_text:
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()
128
 
129
- # Format the results as HTML with clickable links and stats
130
  html_content = f"""
131
  <div style='margin-bottom: 20px; padding: 10px; background-color: var(--color-background-primary);
132
  border: 1px solid var(--color-border-primary); border-radius: 5px;'>
133
  <h3 style='color: var(--color-text-primary);'>Statistics:</h3>
134
- <p style='color: var(--color-text-primary);'>Showing {min(offset + page_size, total_spaces)} of {total_spaces} Spaces</p>
135
  <p style='color: var(--color-text-primary);'>Total Likes: {total_likes:,}</p>
136
  </div>
137
- <div style='max-height: 800px; overflow-y: auto;' id='spaces-container'>
 
 
 
 
138
  <div style='display: grid; grid-template-columns: repeat(3, 1fr); gap: 15px; padding: 10px;'>
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);
@@ -149,8 +155,7 @@ def search_spaces(search_text="", category="All Categories", offset=0, page_size
149
  box-shadow: 0 2px 4px rgba(0,0,0,0.1);
150
  display: flex;
151
  flex-direction: column;
152
- height: 100%;
153
- position: relative;'>
154
  <h3 style='margin-top: 0; margin-bottom: 10px;'>
155
  <a href='{row['url']}' target='_blank'
156
  style='color: #2196F3;
@@ -159,8 +164,7 @@ def search_spaces(search_text="", category="All Categories", offset=0, page_size
159
  </h3>
160
  <div style='height: 2px;
161
  background: var(--color-border-primary);
162
- margin: 10px 0;
163
- width: 100%;'></div>
164
  <p style='color: var(--color-text-primary); margin: 8px 0;'>
165
  <span style='background-color: var(--color-accent-soft);
166
  padding: 2px 8px;
@@ -180,18 +184,14 @@ def search_spaces(search_text="", category="All Categories", offset=0, page_size
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(
190
- primary_hue="blue",
191
- secondary_hue="blue",
192
- )
193
- ) as app:
194
- current_offset = gr.State(value=0)
195
 
196
  gr.Markdown("""
197
  # 🤗 Hugging Face Spaces Explorer
@@ -202,65 +202,66 @@ def create_app():
202
  """)
203
 
204
  with gr.Row():
205
- with gr.Column(scale=1):
206
- category_dropdown = gr.Dropdown(
207
- choices=["All Categories"] + sorted(df['category'].unique()),
208
- label="Select Category",
209
- value="All Categories"
210
- )
211
- search_input = gr.Textbox(
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.update(visible=has_more), # Changed from gr.Button.update
225
- remaining_text: gr.update( # Changed from 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
262
 
263
-
264
  # Launch the app
265
  app = create_app()
266
  app.launch(share=True)
 
111
 
112
  # ------------------------------------------------------
113
 
114
+ def search_spaces(search_text="", category="All Categories", offset=0, limit=30):
115
+ # Filter spaces
116
  if category == "All Categories":
117
  spaces_df = df
118
  else:
 
121
  if search_text:
122
  spaces_df = spaces_df[spaces_df['title'].str.lower().str.contains(search_text.lower())]
123
 
124
+ # Sort by likes and get total count
125
+ spaces_df = spaces_df.sort_values('likes', ascending=False)
126
  total_spaces = len(spaces_df)
 
127
 
128
+ # Get the current page of spaces
129
+ spaces = spaces_df.iloc[offset:offset + limit][['title', 'likes', 'url', 'category']]
130
  total_likes = spaces_df['likes'].sum()
131
 
132
+ # Generate HTML content
133
  html_content = f"""
134
  <div style='margin-bottom: 20px; padding: 10px; background-color: var(--color-background-primary);
135
  border: 1px solid var(--color-border-primary); border-radius: 5px;'>
136
  <h3 style='color: var(--color-text-primary);'>Statistics:</h3>
137
+ <p style='color: var(--color-text-primary);'>Showing {offset + 1}-{min(offset + limit, total_spaces)} of {total_spaces} Spaces</p>
138
  <p style='color: var(--color-text-primary);'>Total Likes: {total_likes:,}</p>
139
  </div>
140
+ """
141
+
142
+ # Add grid container
143
+ html_content += """
144
+ <div style='max-height: 800px; overflow-y: auto;'>
145
  <div style='display: grid; grid-template-columns: repeat(3, 1fr); gap: 15px; padding: 10px;'>
146
  """
147
 
148
+ # Add space cards
149
+ for _, row in spaces.iterrows():
 
150
  html_content += f"""
151
  <div style='padding: 15px;
152
  border: 2px solid var(--color-border-primary);
 
155
  box-shadow: 0 2px 4px rgba(0,0,0,0.1);
156
  display: flex;
157
  flex-direction: column;
158
+ height: 100%;'>
 
159
  <h3 style='margin-top: 0; margin-bottom: 10px;'>
160
  <a href='{row['url']}' target='_blank'
161
  style='color: #2196F3;
 
164
  </h3>
165
  <div style='height: 2px;
166
  background: var(--color-border-primary);
167
+ margin: 10px 0;'></div>
 
168
  <p style='color: var(--color-text-primary); margin: 8px 0;'>
169
  <span style='background-color: var(--color-accent-soft);
170
  padding: 2px 8px;
 
184
 
185
  html_content += "</div></div>"
186
 
187
+ has_more = offset + limit < total_spaces
188
+ remaining = total_spaces - (offset + limit) if has_more else 0
189
+
190
+ return html_content, has_more, remaining
191
 
192
  def create_app():
193
+ with gr.Blocks(title="Hugging Face Spaces Explorer", theme=gr.themes.Soft()) as app:
194
+ offset = gr.State(value=0)
 
 
 
 
 
 
195
 
196
  gr.Markdown("""
197
  # 🤗 Hugging Face Spaces Explorer
 
202
  """)
203
 
204
  with gr.Row():
205
+ category_dropdown = gr.Dropdown(
206
+ choices=["All Categories"] + sorted(df['category'].unique()),
207
+ label="Select Category",
208
+ value="All Categories"
209
+ )
210
+ search_input = gr.Textbox(
211
+ label="Search Spaces",
212
+ placeholder="Enter search terms..."
213
+ )
 
214
 
215
  spaces_display = gr.HTML()
216
+ load_more_button = gr.Button("Load More", visible=False)
217
+ remaining_count = gr.Markdown(visible=False)
218
 
219
+ def load_more(search_text, category, current_offset):
220
+ new_offset = current_offset + 30
221
+ content, has_more, remaining = search_spaces(search_text, category, new_offset)
222
  return {
223
  spaces_display: content,
224
+ load_more_button: gr.update(visible=has_more),
225
+ remaining_count: gr.update(visible=has_more, value=f"*{remaining} more spaces available*"),
226
+ offset: new_offset
 
 
227
  }
228
 
229
+ def reset_and_search(search_text, category):
230
+ content, has_more, remaining = search_spaces(search_text, category, 0)
231
+ return {
232
+ spaces_display: content,
233
+ load_more_button: gr.update(visible=has_more),
234
+ remaining_count: gr.update(visible=has_more, value=f"*{remaining} more spaces available*"),
235
+ offset: 0
236
+ }
237
 
238
  # Initial load
239
  app.load(
240
+ fn=lambda: reset_and_search("", "All Categories"),
241
+ outputs=[spaces_display, load_more_button, remaining_count, offset]
242
  )
243
 
244
+ # Event handlers
245
  category_dropdown.change(
246
+ fn=reset_and_search,
247
+ inputs=[search_input, category_dropdown],
248
+ outputs=[spaces_display, load_more_button, remaining_count, offset]
249
  )
250
 
251
  search_input.change(
252
+ fn=reset_and_search,
253
+ inputs=[search_input, category_dropdown],
254
+ outputs=[spaces_display, load_more_button, remaining_count, offset]
255
  )
256
 
257
+ load_more_button.click(
 
258
  fn=load_more,
259
+ inputs=[search_input, category_dropdown, offset],
260
+ outputs=[spaces_display, load_more_button, remaining_count, offset]
261
  )
262
 
263
  return app
264
 
 
265
  # Launch the app
266
  app = create_app()
267
  app.launch(share=True)