evijit HF Staff commited on
Commit
fa2c2d2
·
verified ·
1 Parent(s): 4517d15

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -34
app.py CHANGED
@@ -1,4 +1,4 @@
1
- # --- START OF FULLY CORRECTED FILE app.py ---
2
 
3
  import gradio as gr
4
  import pandas as pd
@@ -8,7 +8,6 @@ from datasets import load_dataset # Import the datasets library
8
 
9
  # --- Constants ---
10
  PARAM_CHOICES = ['< 1B', '1B', '5B', '12B', '32B', '64B', '128B', '256B', '> 500B']
11
- # --- NEW: Define the default value using indices ---
12
  PARAM_CHOICES_DEFAULT_INDICES = [0, len(PARAM_CHOICES) - 1]
13
 
14
  HF_DATASET_ID = "evijit/orgstats_daily_data"
@@ -63,7 +62,6 @@ def make_treemap_data(df, count_by, top_k=25, tag_filter=None, pipeline_filter=N
63
  else: print(f"Warning: 'pipeline_tag' column not found for filtering.")
64
  if param_range:
65
  min_params, max_params = get_param_range_values(param_range)
66
- # --- MODIFIED: Check against labels, not indices ---
67
  is_default_range = (param_range[0] == PARAM_CHOICES[0] and param_range[1] == PARAM_CHOICES[-1])
68
  if not is_default_range and 'params' in filtered_df.columns:
69
  if min_params is not None: filtered_df = filtered_df[filtered_df['params'] >= min_params]
@@ -106,25 +104,17 @@ with gr.Blocks(title="ModelVerse Explorer", fill_width=True) as demo:
106
  tag_filter_dropdown = gr.Dropdown(label="Select Tag", choices=TAG_FILTER_CHOICES, value=None, visible=False)
107
  pipeline_filter_dropdown = gr.Dropdown(label="Select Pipeline Tag", choices=PIPELINE_TAGS, value=None, visible=False)
108
 
109
- # --- MODIFIED: Replaced RangeSlider with Slider and a dynamic Markdown label ---
110
  with gr.Group():
111
  with gr.Row():
112
- gr.Markdown("<div style='padding-top: 10px; font-weight: 500;'>Parameters</div>")
113
  reset_params_button = gr.Button("🔄 Reset", visible=False, size="sm", min_width=80)
114
 
115
- # This markdown will show the selected text labels
116
- param_label_display = gr.Markdown(f"Range: **{PARAM_CHOICES[0]}** to **{PARAM_CHOICES[-1]}**")
117
-
118
  param_slider = gr.Slider(
119
- minimum=0,
120
- maximum=len(PARAM_CHOICES) - 1,
121
- step=1,
122
  value=PARAM_CHOICES_DEFAULT_INDICES,
123
- label=None, # Label is handled by components above
124
- show_label=False
125
  )
126
- # --- END OF MODIFICATION ---
127
-
128
  top_k_slider = gr.Slider(label="Number of Top Organizations", minimum=5, maximum=50, value=25, step=5)
129
  skip_orgs_textbox = gr.Textbox(label="Organizations to Skip (comma-separated)", value="TheBloke,MaziyarPanahi,unsloth,modularai,Gensyn,bartowski")
130
  generate_plot_button = gr.Button(value="Generate Plot", variant="primary", interactive=False)
@@ -134,37 +124,40 @@ with gr.Blocks(title="ModelVerse Explorer", fill_width=True) as demo:
134
  status_message_md = gr.Markdown("Initializing...")
135
  data_info_md = gr.Markdown("")
136
 
137
- # --- NEW AND MODIFIED: Event handlers for the new Slider ---
138
  def _update_slider_ui_elements(current_range_indices):
139
- """Updates the label and reset button visibility based on the slider's state."""
 
 
 
 
140
  min_idx, max_idx = int(current_range_indices[0]), int(current_range_indices[1])
141
- min_label = PARAM_CHOICES[min_idx]
142
- max_label = PARAM_CHOICES[max_idx]
143
 
144
- is_default = (min_idx == PARAM_CHOICES_DEFAULT_INDICES[0] and max_idx == PARAM_CHOICES_DEFAULT_INDICES[1])
 
145
 
146
- label_md = f"Range: **{min_label}** to **{max_label}**"
147
  button_visibility = gr.update(visible=not is_default)
148
 
149
  return label_md, button_visibility
150
 
151
  def _reset_param_slider_and_ui():
152
  """Resets the slider to default and updates the UI elements accordingly."""
153
- default_indices = PARAM_CHOICES_DEFAULT_INDICES
154
- default_label = f"Range: **{PARAM_CHOICES[default_indices[0]]}** to **{PARAM_CHOICES[default_indices[1]]}**"
155
-
156
- return gr.update(value=default_indices), default_label, gr.update(visible=False)
157
 
158
- param_slider.release(fn=_update_slider_ui_elements, inputs=param_slider, outputs=[param_label_display, reset_params_button])
 
159
  reset_params_button.click(fn=_reset_param_slider_and_ui, outputs=[param_slider, param_label_display, reset_params_button])
160
- # --- END OF NEW EVENT HANDLERS ---
161
-
162
  def _update_button_interactivity(is_loaded_flag): return gr.update(interactive=is_loaded_flag)
163
  loading_complete_state.change(fn=_update_button_interactivity, inputs=loading_complete_state, outputs=generate_plot_button)
164
 
165
  def _toggle_filters_visibility(choice): return gr.update(visible=choice == "Tag Filter"), gr.update(visible=choice == "Pipeline Filter")
166
  filter_choice_radio.change(fn=_toggle_filters_visibility, inputs=filter_choice_radio, outputs=[tag_filter_dropdown, pipeline_filter_dropdown])
167
 
 
168
  def ui_load_data_controller(progress=gr.Progress()):
169
  progress(0, desc=f"Loading dataset '{HF_DATASET_ID}' from Hugging Face Hub...")
170
  status_msg_ui, data_info_text, load_success_flag = "Loading data...", "", False
@@ -172,8 +165,16 @@ with gr.Blocks(title="ModelVerse Explorer", fill_width=True) as demo:
172
  current_df, load_success_flag, status_msg_from_load = load_models_data()
173
  if load_success_flag:
174
  progress(0.9, desc="Processing loaded data...")
175
- ts = pd.to_datetime(current_df['data_download_timestamp'].iloc[0]).tz_localize('UTC') if 'data_download_timestamp' in current_df.columns and not current_df.empty and pd.notna(current_df['data_download_timestamp'].iloc[0]) else None
176
- date_display = ts.strftime('%B %d, %Y, %H:%M:%S %Z') if ts else "Pre-processed (date unavailable)"
 
 
 
 
 
 
 
 
177
  param_count = (current_df['params'] > 0).sum() if 'params' in current_df.columns else 0
178
  data_info_text = f"### Data Information\n- Source: `{HF_DATASET_ID}`\n- Status: {status_msg_from_load}\n- Total models loaded: {len(current_df):,}\n- Models with parameter counts: {param_count:,}\n- Data as of: {date_display}\n"
179
  status_msg_ui = "Data loaded successfully. Ready to generate plot."
@@ -186,7 +187,6 @@ with gr.Blocks(title="ModelVerse Explorer", fill_width=True) as demo:
186
  print(f"Critical error in ui_load_data_controller: {e}")
187
  return current_df, load_success_flag, data_info_text, status_msg_ui
188
 
189
- # --- MODIFIED: Controller now takes slider indices and converts them to labels ---
190
  def ui_generate_plot_controller(metric_choice, filter_type, tag_choice, pipeline_choice,
191
  param_range_indices, k_orgs, skip_orgs_input, df_current_models, progress=gr.Progress()):
192
  if df_current_models is None or df_current_models.empty:
@@ -196,7 +196,6 @@ with gr.Blocks(title="ModelVerse Explorer", fill_width=True) as demo:
196
  pipeline_to_use = pipeline_choice if filter_type == "Pipeline Filter" else None
197
  orgs_to_skip = [org.strip() for org in skip_orgs_input.split(',') if org.strip()] if skip_orgs_input else []
198
 
199
- # Convert indices from slider back to text labels for filtering logic
200
  min_label = PARAM_CHOICES[int(param_range_indices[0])]
201
  max_label = PARAM_CHOICES[int(param_range_indices[1])]
202
  param_labels_for_filtering = [min_label, max_label]
@@ -221,7 +220,7 @@ with gr.Blocks(title="ModelVerse Explorer", fill_width=True) as demo:
221
  generate_plot_button.click(
222
  fn=ui_generate_plot_controller,
223
  inputs=[count_by_dropdown, filter_choice_radio, tag_filter_dropdown, pipeline_filter_dropdown,
224
- param_slider, top_k_slider, skip_orgs_textbox, models_data_state], # MODIFIED: Use param_slider
225
  outputs=[plot_output, status_message_md]
226
  )
227
 
@@ -229,4 +228,4 @@ if __name__ == "__main__":
229
  print(f"Application starting. Data will be loaded from Hugging Face dataset: {HF_DATASET_ID}")
230
  demo.queue().launch()
231
 
232
- # --- END OF FULLY CORRECTED FILE app.py ---
 
1
+ # --- START OF FULLY CORRECTED AND IMPROVED FILE app.py ---
2
 
3
  import gradio as gr
4
  import pandas as pd
 
8
 
9
  # --- Constants ---
10
  PARAM_CHOICES = ['< 1B', '1B', '5B', '12B', '32B', '64B', '128B', '256B', '> 500B']
 
11
  PARAM_CHOICES_DEFAULT_INDICES = [0, len(PARAM_CHOICES) - 1]
12
 
13
  HF_DATASET_ID = "evijit/orgstats_daily_data"
 
62
  else: print(f"Warning: 'pipeline_tag' column not found for filtering.")
63
  if param_range:
64
  min_params, max_params = get_param_range_values(param_range)
 
65
  is_default_range = (param_range[0] == PARAM_CHOICES[0] and param_range[1] == PARAM_CHOICES[-1])
66
  if not is_default_range and 'params' in filtered_df.columns:
67
  if min_params is not None: filtered_df = filtered_df[filtered_df['params'] >= min_params]
 
104
  tag_filter_dropdown = gr.Dropdown(label="Select Tag", choices=TAG_FILTER_CHOICES, value=None, visible=False)
105
  pipeline_filter_dropdown = gr.Dropdown(label="Select Pipeline Tag", choices=PIPELINE_TAGS, value=None, visible=False)
106
 
 
107
  with gr.Group():
108
  with gr.Row():
109
+ param_label_display = gr.Markdown("<div style='font-weight: 500;'>Parameters</div>")
110
  reset_params_button = gr.Button("🔄 Reset", visible=False, size="sm", min_width=80)
111
 
 
 
 
112
  param_slider = gr.Slider(
113
+ minimum=0, maximum=len(PARAM_CHOICES) - 1, step=1,
 
 
114
  value=PARAM_CHOICES_DEFAULT_INDICES,
115
+ label="Parameter Range", show_label=False # Use a hidden label for accessibility
 
116
  )
117
+
 
118
  top_k_slider = gr.Slider(label="Number of Top Organizations", minimum=5, maximum=50, value=25, step=5)
119
  skip_orgs_textbox = gr.Textbox(label="Organizations to Skip (comma-separated)", value="TheBloke,MaziyarPanahi,unsloth,modularai,Gensyn,bartowski")
120
  generate_plot_button = gr.Button(value="Generate Plot", variant="primary", interactive=False)
 
124
  status_message_md = gr.Markdown("Initializing...")
125
  data_info_md = gr.Markdown("")
126
 
127
+ # --- MODIFIED: More robust event handlers for the slider ---
128
  def _update_slider_ui_elements(current_range_indices):
129
+ """Updates the labels above the slider and the reset button visibility."""
130
+ if not isinstance(current_range_indices, list) or len(current_range_indices) != 2:
131
+ # This is a defensive check to prevent crashes if the input is malformed.
132
+ return gr.update(), gr.update()
133
+
134
  min_idx, max_idx = int(current_range_indices[0]), int(current_range_indices[1])
135
+ min_label, max_label = PARAM_CHOICES[min_idx], PARAM_CHOICES[max_idx]
 
136
 
137
+ # Using HTML for bold is more reliable in gr.Markdown
138
+ label_md = f"<div style='font-weight: 500;'>Parameters <span style='float: right; font-weight: normal; color: #555;'>{min_label} to {max_label}</span></div>"
139
 
140
+ is_default = (min_idx == 0 and max_idx == len(PARAM_CHOICES) - 1)
141
  button_visibility = gr.update(visible=not is_default)
142
 
143
  return label_md, button_visibility
144
 
145
  def _reset_param_slider_and_ui():
146
  """Resets the slider to default and updates the UI elements accordingly."""
147
+ default_label = "<div style='font-weight: 500;'>Parameters</div>"
148
+ return gr.update(value=PARAM_CHOICES_DEFAULT_INDICES), default_label, gr.update(visible=False)
 
 
149
 
150
+ # Use .change() for better reliability
151
+ param_slider.change(fn=_update_slider_ui_elements, inputs=param_slider, outputs=[param_label_display, reset_params_button])
152
  reset_params_button.click(fn=_reset_param_slider_and_ui, outputs=[param_slider, param_label_display, reset_params_button])
153
+
 
154
  def _update_button_interactivity(is_loaded_flag): return gr.update(interactive=is_loaded_flag)
155
  loading_complete_state.change(fn=_update_button_interactivity, inputs=loading_complete_state, outputs=generate_plot_button)
156
 
157
  def _toggle_filters_visibility(choice): return gr.update(visible=choice == "Tag Filter"), gr.update(visible=choice == "Pipeline Filter")
158
  filter_choice_radio.change(fn=_toggle_filters_visibility, inputs=filter_choice_radio, outputs=[tag_filter_dropdown, pipeline_filter_dropdown])
159
 
160
+ # --- MODIFIED: Fixed the timezone handling logic ---
161
  def ui_load_data_controller(progress=gr.Progress()):
162
  progress(0, desc=f"Loading dataset '{HF_DATASET_ID}' from Hugging Face Hub...")
163
  status_msg_ui, data_info_text, load_success_flag = "Loading data...", "", False
 
165
  current_df, load_success_flag, status_msg_from_load = load_models_data()
166
  if load_success_flag:
167
  progress(0.9, desc="Processing loaded data...")
168
+ date_display = "Pre-processed (date unavailable)"
169
+ if 'data_download_timestamp' in current_df.columns and not current_df.empty and pd.notna(current_df['data_download_timestamp'].iloc[0]):
170
+ timestamp = pd.to_datetime(current_df['data_download_timestamp'].iloc[0])
171
+ # Check if tz-aware. If so, convert. If not, localize.
172
+ if timestamp.tzinfo is None:
173
+ ts = timestamp.tz_localize('UTC')
174
+ else:
175
+ ts = timestamp.tz_convert('UTC')
176
+ date_display = ts.strftime('%B %d, %Y, %H:%M:%S %Z')
177
+
178
  param_count = (current_df['params'] > 0).sum() if 'params' in current_df.columns else 0
179
  data_info_text = f"### Data Information\n- Source: `{HF_DATASET_ID}`\n- Status: {status_msg_from_load}\n- Total models loaded: {len(current_df):,}\n- Models with parameter counts: {param_count:,}\n- Data as of: {date_display}\n"
180
  status_msg_ui = "Data loaded successfully. Ready to generate plot."
 
187
  print(f"Critical error in ui_load_data_controller: {e}")
188
  return current_df, load_success_flag, data_info_text, status_msg_ui
189
 
 
190
  def ui_generate_plot_controller(metric_choice, filter_type, tag_choice, pipeline_choice,
191
  param_range_indices, k_orgs, skip_orgs_input, df_current_models, progress=gr.Progress()):
192
  if df_current_models is None or df_current_models.empty:
 
196
  pipeline_to_use = pipeline_choice if filter_type == "Pipeline Filter" else None
197
  orgs_to_skip = [org.strip() for org in skip_orgs_input.split(',') if org.strip()] if skip_orgs_input else []
198
 
 
199
  min_label = PARAM_CHOICES[int(param_range_indices[0])]
200
  max_label = PARAM_CHOICES[int(param_range_indices[1])]
201
  param_labels_for_filtering = [min_label, max_label]
 
220
  generate_plot_button.click(
221
  fn=ui_generate_plot_controller,
222
  inputs=[count_by_dropdown, filter_choice_radio, tag_filter_dropdown, pipeline_filter_dropdown,
223
+ param_slider, top_k_slider, skip_orgs_textbox, models_data_state],
224
  outputs=[plot_output, status_message_md]
225
  )
226
 
 
228
  print(f"Application starting. Data will be loaded from Hugging Face dataset: {HF_DATASET_ID}")
229
  demo.queue().launch()
230
 
231
+ # --- END OF FULLY CORRECTED AND IMPROVED FILE app.py ---