rombodawg commited on
Commit
2b23995
·
verified ·
1 Parent(s): 4bc33ce

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +43 -36
app.py CHANGED
@@ -51,6 +51,11 @@ tr:hover {
51
  max-height: 600px;
52
  overflow-y: auto;
53
  }
 
 
 
 
 
54
  """
55
 
56
  # List of schizo words to check for
@@ -160,7 +165,7 @@ def fetch_model_readme(model_id):
160
  print(f"Error fetching README for {model_id}: {e}")
161
  return None
162
 
163
- def generate_leaderboard_data(num_models=100, model_type="all"):
164
  """Generate leaderboard data by analyzing model cards"""
165
  api = HfApi(token=HF_TOKEN)
166
 
@@ -169,21 +174,18 @@ def generate_leaderboard_data(num_models=100, model_type="all"):
169
  # Filter to text-generation models using API parameters
170
  models = list_models(
171
  task="text-generation",
172
- limit=num_models * 5 # Get more models than needed to account for ones without READMEs
173
  )
174
  else:
175
  # Get all models
176
  models = list_models(
177
- limit=num_models * 5 # Get more models than needed to account for ones without READMEs
178
  )
179
 
180
  leaderboard_data = []
181
- count = 0
182
 
183
  for model in models:
184
- if count >= num_models:
185
- break
186
-
187
  model_id = model.id
188
  readme_content = fetch_model_readme(model_id)
189
 
@@ -206,10 +208,11 @@ def generate_leaderboard_data(num_models=100, model_type="all"):
206
  "markdown_count": ratings["markdown_count"]
207
  })
208
 
209
- count += 1
210
 
211
  # Status update
212
- print(f"Processed {count}/{num_models} models")
 
213
 
214
  # Sort by combined rating in descending order
215
  leaderboard_data.sort(key=lambda x: x["combined_rating"], reverse=True)
@@ -305,18 +308,30 @@ def create_leaderboard_html(leaderboard_data):
305
 
306
  return html
307
 
308
- @spaces.GPU()
309
- def update_leaderboard(num_models, model_type):
310
- """Update leaderboard with new data"""
311
  # Show loading message
312
- yield "Loading models and analyzing Schizo ratings... This may take a few minutes."
313
 
314
  try:
315
- leaderboard_data = generate_leaderboard_data(num_models, model_type)
316
  leaderboard_html = create_leaderboard_html(leaderboard_data)
317
  yield leaderboard_html
318
  except Exception as e:
319
- yield f"<p>Error generating leaderboard: {str(e)}</p>"
 
 
 
 
 
 
 
 
 
 
 
 
 
320
 
321
  with gr.Blocks(css=CSS, theme="soft") as demo:
322
  gr.HTML(TITLE)
@@ -324,36 +339,28 @@ with gr.Blocks(css=CSS, theme="soft") as demo:
324
 
325
  with gr.Row():
326
  with gr.Column():
327
- num_models_slider = gr.Slider(
328
- minimum=10,
329
- maximum=200,
330
- step=10,
331
- value=50,
332
- label="Number of Models to Analyze",
333
- )
334
-
335
  model_type_dropdown = gr.Dropdown(
336
- choices=["all", "llm"],
337
  value="llm",
338
  label="Model Type Filter",
339
  )
340
 
341
- update_button = gr.Button("Update Leaderboard")
342
 
343
- leaderboard_html = gr.HTML()
344
 
345
- update_button.click(
346
- fn=update_leaderboard,
347
- inputs=[num_models_slider, model_type_dropdown],
 
 
 
 
 
 
 
348
  outputs=[leaderboard_html],
349
  )
350
-
351
- # Add a default leaderboard with a few example entries for initial display
352
- default_leaderboard = [
353
- {"model_id": "example/model-1", "combined_rating": 150.0, "word_rating": 100.0, "wordiness_rating": 25.0, "visual_rating": 25.0},
354
- {"model_id": "example/model-2", "combined_rating": 100.0, "word_rating": 80.0, "wordiness_rating": 10.0, "visual_rating": 10.0},
355
- {"model_id": "example/model-3", "combined_rating": 75.0, "word_rating": 50.0, "wordiness_rating": 12.5, "visual_rating": 12.5}
356
- ]
357
 
358
  if __name__ == "__main__":
359
  demo.launch()
 
51
  max-height: 600px;
52
  overflow-y: auto;
53
  }
54
+ .loading {
55
+ text-align: center;
56
+ font-size: 18px;
57
+ padding: 20px;
58
+ }
59
  """
60
 
61
  # List of schizo words to check for
 
165
  print(f"Error fetching README for {model_id}: {e}")
166
  return None
167
 
168
+ def generate_leaderboard_data(model_type="llm", max_models=500):
169
  """Generate leaderboard data by analyzing model cards"""
170
  api = HfApi(token=HF_TOKEN)
171
 
 
174
  # Filter to text-generation models using API parameters
175
  models = list_models(
176
  task="text-generation",
177
+ limit=max_models # Set a reasonable limit to avoid overwhelming the API
178
  )
179
  else:
180
  # Get all models
181
  models = list_models(
182
+ limit=max_models # Set a reasonable limit to avoid overwhelming the API
183
  )
184
 
185
  leaderboard_data = []
186
+ processed_count = 0
187
 
188
  for model in models:
 
 
 
189
  model_id = model.id
190
  readme_content = fetch_model_readme(model_id)
191
 
 
208
  "markdown_count": ratings["markdown_count"]
209
  })
210
 
211
+ processed_count += 1
212
 
213
  # Status update
214
+ if processed_count % 10 == 0:
215
+ print(f"Processed {processed_count} models")
216
 
217
  # Sort by combined rating in descending order
218
  leaderboard_data.sort(key=lambda x: x["combined_rating"], reverse=True)
 
308
 
309
  return html
310
 
311
+ def load_leaderboard(model_type):
312
+ """Load the leaderboard with models"""
 
313
  # Show loading message
314
+ yield '<div class="loading">Loading models and analyzing Schizo ratings... This may take a few minutes.</div>'
315
 
316
  try:
317
+ leaderboard_data = generate_leaderboard_data(model_type)
318
  leaderboard_html = create_leaderboard_html(leaderboard_data)
319
  yield leaderboard_html
320
  except Exception as e:
321
+ yield f'<div class="loading">Error generating leaderboard: {str(e)}</div>'
322
+
323
+ # Background loading thread
324
+ def background_loader(model_type, progress=None):
325
+ try:
326
+ leaderboard_data = generate_leaderboard_data(model_type)
327
+ leaderboard_html = create_leaderboard_html(leaderboard_data)
328
+ return leaderboard_html
329
+ except Exception as e:
330
+ return f'<div class="loading">Error generating leaderboard: {str(e)}</div>'
331
+
332
+ @spaces.GPU()
333
+ def init_leaderboard():
334
+ return '<div class="loading">Initializing leaderboard... Please wait while we analyze Hugging Face models.</div>'
335
 
336
  with gr.Blocks(css=CSS, theme="soft") as demo:
337
  gr.HTML(TITLE)
 
339
 
340
  with gr.Row():
341
  with gr.Column():
 
 
 
 
 
 
 
 
342
  model_type_dropdown = gr.Dropdown(
343
+ choices=["llm", "all"],
344
  value="llm",
345
  label="Model Type Filter",
346
  )
347
 
348
+ refresh_button = gr.Button("Refresh Leaderboard")
349
 
350
+ leaderboard_html = gr.HTML(value=init_leaderboard)
351
 
352
+ # Load leaderboard on startup and when refresh button is clicked
353
+ demo.load(
354
+ fn=load_leaderboard,
355
+ inputs=[model_type_dropdown],
356
+ outputs=[leaderboard_html],
357
+ )
358
+
359
+ refresh_button.click(
360
+ fn=load_leaderboard,
361
+ inputs=[model_type_dropdown],
362
  outputs=[leaderboard_html],
363
  )
 
 
 
 
 
 
 
364
 
365
  if __name__ == "__main__":
366
  demo.launch()