evijit HF Staff commited on
Commit
c0b7e37
·
verified ·
1 Parent(s): b13e026

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +47 -85
app.py CHANGED
@@ -4,67 +4,20 @@ import gradio as gr
4
  import pandas as pd
5
  import plotly.express as px
6
  import time
7
- import json
8
  from datasets import load_dataset
 
 
9
 
10
  # --- Constants ---
11
  PARAM_CHOICES = ['< 1B', '1B', '5B', '12B', '32B', '64B', '128B', '256B', '> 500B']
12
- # This hidden textbox will store the slider's state as a JSON string
13
- PARAM_STATE_DEFAULT_JSON = json.dumps([0, len(PARAM_CHOICES) - 1])
14
 
15
  TOP_K_CHOICES = list(range(5, 51, 5))
16
  HF_DATASET_ID = "evijit/orgstats_daily_data"
17
  TAG_FILTER_CHOICES = [ "Audio & Speech", "Time series", "Robotics", "Music", "Video", "Images", "Text", "Biomedical", "Sciences" ]
18
  PIPELINE_TAGS = [ 'text-generation', 'text-to-image', 'text-classification', 'text2text-generation', 'audio-to-audio', 'feature-extraction', 'image-classification', 'translation', 'reinforcement-learning', 'fill-mask', 'text-to-speech', 'automatic-speech-recognition', 'image-text-to-text', 'token-classification', 'sentence-similarity', 'question-answering', 'image-feature-extraction', 'summarization', 'zero-shot-image-classification', 'object-detection', 'image-segmentation', 'image-to-image', 'image-to-text', 'audio-classification', 'visual-question-answering', 'text-to-video', 'zero-shot-classification', 'depth-estimation', 'text-ranking', 'image-to-video', 'multiple-choice', 'unconditional-image-generation', 'video-classification', 'text-to-audio', 'time-series-forecasting', 'any-to-any', 'video-text-to-text', 'table-question-answering' ]
19
 
20
- # --- Custom HTML, CSS, and JavaScript for the Slider ---
21
- custom_slider_js = """
22
- function createCustomSlider() {{
23
- const paramChoices = {js_param_choices};
24
- const slider = document.getElementById('noui-slider-container');
25
- if (slider.noUiSlider) {{ return; }} // Don't re-create if it already exists
26
-
27
- noUiSlider.create(slider, {{
28
- start: [0, paramChoices.length - 1],
29
- connect: true,
30
- step: 1,
31
- range: {{ 'min': 0, 'max': paramChoices.length - 1 }},
32
- pips: {{
33
- mode: 'values',
34
- values: Array.from(Array(paramChoices.length).keys()),
35
- density: 100 / (paramChoices.length - 1),
36
- format: {{ to: function(value) {{ return paramChoices[value]; }} }}
37
- }}
38
- }});
39
-
40
- const paramRangeStateInput = document.querySelector('#param-range-state-js textarea');
41
- const resetBtn = document.getElementById('reset-params-btn');
42
- const initialRange = [0, paramChoices.length - 1];
43
-
44
- function update(values) {{
45
- const intValues = values.map(v => parseInt(v, 10));
46
- const isDefault = intValues[0] === initialRange[0] && intValues[1] === initialRange[1];
47
-
48
- // Show/hide reset button
49
- resetBtn.style.display = isDefault ? 'none' : 'inline-block';
50
-
51
- // Update hidden state for Python
52
- const newValue = JSON.stringify(intValues);
53
- if (paramRangeStateInput.value !== newValue) {{
54
- paramRangeStateInput.value = newValue;
55
- const event = new Event('input', {{ bubbles: true }});
56
- paramRangeStateInput.dispatchEvent(event);
57
- }}
58
- }}
59
-
60
- slider.noUiSlider.on('update', update);
61
-
62
- // The reset button in the HTML calls this JS function directly
63
- window.resetSlider = function() {{
64
- slider.noUiSlider.set(initialRange);
65
- }}
66
- }}
67
- """
68
 
69
  def load_models_data():
70
  overall_start_time = time.time()
@@ -80,7 +33,9 @@ def load_models_data():
80
  print(msg)
81
  return df, True, msg
82
  except Exception as e:
83
- return pd.DataFrame(), False, f"Failed to load dataset. Error: {e}"
 
 
84
 
85
  def get_param_range_values(param_range_labels):
86
  min_label, max_label = param_range_labels
@@ -123,12 +78,7 @@ def create_treemap(treemap_data, count_by, title=None):
123
  fig.update_traces(textinfo="label+value+percent root", hovertemplate="<b>%{label}</b><br>%{value:,} " + count_by + "<br>%{percentRoot:.2%} of total<extra></extra>")
124
  return fig
125
 
126
- custom_head = """
127
- <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/noUiSlider/15.7.1/nouislider.min.css">
128
- <script src="https://cdnjs.cloudflare.com/ajax/libs/noUiSlider/15.7.1/nouislider.min.js"></script>
129
- """
130
-
131
- with gr.Blocks(title="🤗 ModelVerse Explorer", fill_width=True, head=custom_head) as demo:
132
  models_data_state = gr.State(pd.DataFrame())
133
  loading_complete_state = gr.State(False)
134
 
@@ -143,25 +93,20 @@ with gr.Blocks(title="🤗 ModelVerse Explorer", fill_width=True, head=custom_he
143
  pipeline_filter_dropdown = gr.Dropdown(label="Select Pipeline Tag", choices=PIPELINE_TAGS, value=None, visible=False)
144
 
145
  with gr.Group():
146
- gr.HTML("""
147
- <div style="display: flex; justify-content: space-between; align-items: center; margin-bottom: -1rem;">
148
- <label class="gradio-label">Parameters</label>
149
- <button id="reset-params-btn" onclick="window.resetSlider()" class="gr-button gr-button-sm gr-button-secondary" style="display: none;">🔄 Reset</button>
150
- </div>
151
- """)
152
- gr.HTML("""
153
- <div id="noui-slider-container" style="margin: 2rem 1rem;"></div>
154
- <style>
155
- .noUi-value { font-size: 12px; color: #777; }
156
- .noUi-pips-horizontal { padding: 10px 0; height: 50px; }
157
- .noUi-connect { background: #000; }
158
- .noUi-handle { border-radius: 50%; width: 22px; height: 22px; right: -11px; top: -8px; box-shadow: none; border: 2px solid #000; background: #FFF; cursor: pointer; }
159
- .noUi-handle:focus { outline: none; }
160
- .noUi-handle::after, .noUi-handle::before { display: none; }
161
- </style>
162
- """)
163
- param_range_state_js = gr.Textbox(value=PARAM_STATE_DEFAULT_JSON, visible=False, elem_id="param-range-state-js")
164
-
165
  top_k_dropdown = gr.Dropdown(label="Number of Top Organizations", choices=TOP_K_CHOICES, value=25)
166
  skip_orgs_textbox = gr.Textbox(label="Organizations to Skip (comma-separated)", value="TheBloke,MaziyarPanahi,unsloth,modularai,Gensyn,bartowski")
167
  generate_plot_button = gr.Button(value="Generate Plot", variant="primary", interactive=False)
@@ -170,6 +115,23 @@ with gr.Blocks(title="🤗 ModelVerse Explorer", fill_width=True, head=custom_he
170
  plot_output = gr.Plot()
171
  status_message_md = gr.Markdown("Initializing...")
172
  data_info_md = gr.Markdown("")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
173
 
174
  def _update_button_interactivity(is_loaded_flag): return gr.update(interactive=is_loaded_flag)
175
  loading_complete_state.change(fn=_update_button_interactivity, inputs=loading_complete_state, outputs=generate_plot_button)
@@ -194,11 +156,14 @@ with gr.Blocks(title="🤗 ModelVerse Explorer", fill_width=True, head=custom_he
194
  data_info_text = f"### Data Load Failed\n- {status_msg_from_load}"
195
  status_msg_ui = status_msg_from_load
196
  except Exception as e:
197
- status_msg_ui, data_info_text, load_success_flag = f"An unexpected error occurred: {str(e)}", f"### Critical Error\n- {status_msg_ui}", False
 
 
 
198
  return current_df, load_success_flag, data_info_text, status_msg_ui
199
 
200
  def ui_generate_plot_controller(metric_choice, filter_type, tag_choice, pipeline_choice,
201
- param_range_json, k_orgs, skip_orgs_input, df_current_models, progress=gr.Progress()):
202
  if df_current_models is None or df_current_models.empty:
203
  return create_treemap(pd.DataFrame(), metric_choice, "Error: Model Data Not Loaded"), "Model data is not loaded."
204
 
@@ -207,7 +172,6 @@ with gr.Blocks(title="🤗 ModelVerse Explorer", fill_width=True, head=custom_he
207
  pipeline_to_use = pipeline_choice if filter_type == "Pipeline Filter" else None
208
  orgs_to_skip = [org.strip() for org in skip_orgs_input.split(',') if org.strip()]
209
 
210
- param_range_indices = json.loads(param_range_json)
211
  min_label = PARAM_CHOICES[int(param_range_indices[0])]
212
  max_label = PARAM_CHOICES[int(param_range_indices[1])]
213
  param_labels_for_filtering = [min_label, max_label]
@@ -227,23 +191,21 @@ with gr.Blocks(title="🤗 ModelVerse Explorer", fill_width=True, head=custom_he
227
  plot_stats_md = f"## Plot Statistics\n- **Models shown**: {total_items_in_plot:,}\n- **Total {metric_choice}**: {int(total_value_in_plot):,}"
228
  return plotly_fig, plot_stats_md
229
 
230
- final_js = custom_slider_js.format(js_param_choices=json.dumps(PARAM_CHOICES))
231
  demo.load(
232
  fn=ui_load_data_controller,
233
  inputs=[],
234
- outputs=[models_data_state, loading_complete_state, data_info_md, status_message_md],
235
- js=f"() => {{ {final_js} }}"
236
  )
237
 
238
  generate_plot_button.click(
239
  fn=ui_generate_plot_controller,
240
  inputs=[count_by_dropdown, filter_choice_radio, tag_filter_dropdown, pipeline_filter_dropdown,
241
- param_range_state_js, top_k_dropdown, skip_orgs_textbox, models_data_state],
242
  outputs=[plot_output, status_message_md]
243
  )
244
 
245
  if __name__ == "__main__":
246
  print(f"Application starting...")
247
- demo.queue().launch(ssr_mode=False)
248
 
249
  # --- END OF FINAL, POLISHED FILE app.py ---
 
4
  import pandas as pd
5
  import plotly.express as px
6
  import time
 
7
  from datasets import load_dataset
8
+ # Using the stable, community-built RangeSlider component
9
+ from gradio_rangeslider import RangeSlider
10
 
11
  # --- Constants ---
12
  PARAM_CHOICES = ['< 1B', '1B', '5B', '12B', '32B', '64B', '128B', '256B', '> 500B']
13
+ # The RangeSlider component uses a tuple for its default value
14
+ PARAM_CHOICES_DEFAULT_INDICES = (0, len(PARAM_CHOICES) - 1)
15
 
16
  TOP_K_CHOICES = list(range(5, 51, 5))
17
  HF_DATASET_ID = "evijit/orgstats_daily_data"
18
  TAG_FILTER_CHOICES = [ "Audio & Speech", "Time series", "Robotics", "Music", "Video", "Images", "Text", "Biomedical", "Sciences" ]
19
  PIPELINE_TAGS = [ 'text-generation', 'text-to-image', 'text-classification', 'text2text-generation', 'audio-to-audio', 'feature-extraction', 'image-classification', 'translation', 'reinforcement-learning', 'fill-mask', 'text-to-speech', 'automatic-speech-recognition', 'image-text-to-text', 'token-classification', 'sentence-similarity', 'question-answering', 'image-feature-extraction', 'summarization', 'zero-shot-image-classification', 'object-detection', 'image-segmentation', 'image-to-image', 'image-to-text', 'audio-classification', 'visual-question-answering', 'text-to-video', 'zero-shot-classification', 'depth-estimation', 'text-ranking', 'image-to-video', 'multiple-choice', 'unconditional-image-generation', 'video-classification', 'text-to-audio', 'time-series-forecasting', 'any-to-any', 'video-text-to-text', 'table-question-answering' ]
20
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21
 
22
  def load_models_data():
23
  overall_start_time = time.time()
 
33
  print(msg)
34
  return df, True, msg
35
  except Exception as e:
36
+ err_msg = f"Failed to load dataset. Error: {e}"
37
+ print(err_msg)
38
+ return pd.DataFrame(), False, err_msg
39
 
40
  def get_param_range_values(param_range_labels):
41
  min_label, max_label = param_range_labels
 
78
  fig.update_traces(textinfo="label+value+percent root", hovertemplate="<b>%{label}</b><br>%{value:,} " + count_by + "<br>%{percentRoot:.2%} of total<extra></extra>")
79
  return fig
80
 
81
+ with gr.Blocks(title="🤗 ModelVerse Explorer", fill_width=True) as demo:
 
 
 
 
 
82
  models_data_state = gr.State(pd.DataFrame())
83
  loading_complete_state = gr.State(False)
84
 
 
93
  pipeline_filter_dropdown = gr.Dropdown(label="Select Pipeline Tag", choices=PIPELINE_TAGS, value=None, visible=False)
94
 
95
  with gr.Group():
96
+ with gr.Row():
97
+ gr.Markdown("<div style='font-weight: 500; padding-top: 10px;'>Parameters</div>")
98
+ reset_params_button = gr.Button("🔄 Reset", visible=False, size="sm", min_width=80)
99
+
100
+ param_range_slider = RangeSlider(
101
+ minimum=0,
102
+ maximum=len(PARAM_CHOICES) - 1,
103
+ value=PARAM_CHOICES_DEFAULT_INDICES,
104
+ step=1,
105
+ label=None,
106
+ show_label=False,
107
+ )
108
+ param_range_display = gr.Markdown(f"Range: `{PARAM_CHOICES[0]}` to `{PARAM_CHOICES[-1]}`")
109
+
 
 
 
 
 
110
  top_k_dropdown = gr.Dropdown(label="Number of Top Organizations", choices=TOP_K_CHOICES, value=25)
111
  skip_orgs_textbox = gr.Textbox(label="Organizations to Skip (comma-separated)", value="TheBloke,MaziyarPanahi,unsloth,modularai,Gensyn,bartowski")
112
  generate_plot_button = gr.Button(value="Generate Plot", variant="primary", interactive=False)
 
115
  plot_output = gr.Plot()
116
  status_message_md = gr.Markdown("Initializing...")
117
  data_info_md = gr.Markdown("")
118
+
119
+ def update_param_ui(value: tuple):
120
+ min_idx, max_idx = int(value[0]), int(value[1])
121
+ is_default = (min_idx == 0 and max_idx == len(PARAM_CHOICES) - 1)
122
+
123
+ display_text = f"Range: `{PARAM_CHOICES[min_idx]}` to `{PARAM_CHOICES[max_idx]}`"
124
+ button_visibility = gr.update(visible=not is_default)
125
+
126
+ return display_text, button_visibility
127
+
128
+ param_range_slider.change(update_param_ui, param_range_slider, [param_range_display, reset_params_button])
129
+
130
+ def reset_params():
131
+ default_text = f"Range: `{PARAM_CHOICES[0]}` to `{PARAM_CHOICES[-1]}`"
132
+ return PARAM_CHOICES_DEFAULT_INDICES, default_text, gr.update(visible=False)
133
+
134
+ reset_params_button.click(reset_params, outputs=[param_range_slider, param_range_display, reset_params_button])
135
 
136
  def _update_button_interactivity(is_loaded_flag): return gr.update(interactive=is_loaded_flag)
137
  loading_complete_state.change(fn=_update_button_interactivity, inputs=loading_complete_state, outputs=generate_plot_button)
 
156
  data_info_text = f"### Data Load Failed\n- {status_msg_from_load}"
157
  status_msg_ui = status_msg_from_load
158
  except Exception as e:
159
+ status_msg_ui = f"An unexpected error occurred: {str(e)}"
160
+ data_info_text = f"### Critical Error\n- {status_msg_ui}"
161
+ load_success_flag = False
162
+ print(f"Critical error in ui_load_data_controller: {e}")
163
  return current_df, load_success_flag, data_info_text, status_msg_ui
164
 
165
  def ui_generate_plot_controller(metric_choice, filter_type, tag_choice, pipeline_choice,
166
+ param_range_indices, k_orgs, skip_orgs_input, df_current_models, progress=gr.Progress()):
167
  if df_current_models is None or df_current_models.empty:
168
  return create_treemap(pd.DataFrame(), metric_choice, "Error: Model Data Not Loaded"), "Model data is not loaded."
169
 
 
172
  pipeline_to_use = pipeline_choice if filter_type == "Pipeline Filter" else None
173
  orgs_to_skip = [org.strip() for org in skip_orgs_input.split(',') if org.strip()]
174
 
 
175
  min_label = PARAM_CHOICES[int(param_range_indices[0])]
176
  max_label = PARAM_CHOICES[int(param_range_indices[1])]
177
  param_labels_for_filtering = [min_label, max_label]
 
191
  plot_stats_md = f"## Plot Statistics\n- **Models shown**: {total_items_in_plot:,}\n- **Total {metric_choice}**: {int(total_value_in_plot):,}"
192
  return plotly_fig, plot_stats_md
193
 
 
194
  demo.load(
195
  fn=ui_load_data_controller,
196
  inputs=[],
197
+ outputs=[models_data_state, loading_complete_state, data_info_md, status_message_md]
 
198
  )
199
 
200
  generate_plot_button.click(
201
  fn=ui_generate_plot_controller,
202
  inputs=[count_by_dropdown, filter_choice_radio, tag_filter_dropdown, pipeline_filter_dropdown,
203
+ param_range_slider, top_k_dropdown, skip_orgs_textbox, models_data_state],
204
  outputs=[plot_output, status_message_md]
205
  )
206
 
207
  if __name__ == "__main__":
208
  print(f"Application starting...")
209
+ demo.queue().launch()
210
 
211
  # --- END OF FINAL, POLISHED FILE app.py ---