evijit HF Staff commited on
Commit
634bf47
·
verified ·
1 Parent(s): eec69ec

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +87 -32
app.py CHANGED
@@ -1,21 +1,70 @@
1
- # --- START OF FINAL POLISHED FILE app.py ---
2
 
3
  import gradio as gr
4
  import pandas as pd
5
  import plotly.express as px
6
  import time
 
7
  from datasets import load_dataset
8
- from gradio_rangeslider import RangeSlider
9
 
10
  # --- Constants ---
11
  PARAM_CHOICES = ['< 1B', '1B', '5B', '12B', '32B', '64B', '128B', '256B', '> 500B']
12
- PARAM_CHOICES_DEFAULT_INDICES = (0, len(PARAM_CHOICES) - 1)
 
13
 
14
  TOP_K_CHOICES = list(range(5, 51, 5))
15
  HF_DATASET_ID = "evijit/orgstats_daily_data"
16
  TAG_FILTER_CHOICES = [ "Audio & Speech", "Time series", "Robotics", "Music", "Video", "Images", "Text", "Biomedical", "Sciences" ]
17
  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' ]
18
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
 
20
  def load_models_data():
21
  overall_start_time = time.time()
@@ -31,9 +80,7 @@ def load_models_data():
31
  print(msg)
32
  return df, True, msg
33
  except Exception as e:
34
- err_msg = f"Failed to load dataset. Error: {e}"
35
- print(err_msg)
36
- return pd.DataFrame(), False, err_msg
37
 
38
  def get_param_range_values(param_range_labels):
39
  min_label, max_label = param_range_labels
@@ -76,11 +123,15 @@ def create_treemap(treemap_data, count_by, title=None):
76
  fig.update_traces(textinfo="label+value+percent root", hovertemplate="<b>%{label}</b><br>%{value:,} " + count_by + "<br>%{percentRoot:.2%} of total<extra></extra>")
77
  return fig
78
 
79
- with gr.Blocks(title="🤗 ModelVerse Explorer", fill_width=True) as demo:
 
 
 
 
 
80
  models_data_state = gr.State(pd.DataFrame())
81
  loading_complete_state = gr.State(False)
82
 
83
- # --- FIX 1: The application title is restored here ---
84
  with gr.Row():
85
  gr.Markdown("# 🤗 ModelVerse Explorer")
86
 
@@ -91,16 +142,26 @@ with gr.Blocks(title="🤗 ModelVerse Explorer", fill_width=True) as demo:
91
  tag_filter_dropdown = gr.Dropdown(label="Select Tag", choices=TAG_FILTER_CHOICES, value=None, visible=False)
92
  pipeline_filter_dropdown = gr.Dropdown(label="Select Pipeline Tag", choices=PIPELINE_TAGS, value=None, visible=False)
93
 
94
- param_range_slider = RangeSlider(
95
- minimum=0,
96
- maximum=len(PARAM_CHOICES) - 1,
97
- value=PARAM_CHOICES_DEFAULT_INDICES,
98
- step=1,
99
- label="Parameters"
100
- )
101
- # This markdown provides the meaningful labels for the slider's numeric range
102
- param_range_display = gr.Markdown(f"Range: `{PARAM_CHOICES[0]}` to `{PARAM_CHOICES[-1]}`")
103
-
 
 
 
 
 
 
 
 
 
 
104
  top_k_dropdown = gr.Dropdown(label="Number of Top Organizations", choices=TOP_K_CHOICES, value=25)
105
  skip_orgs_textbox = gr.Textbox(label="Organizations to Skip (comma-separated)", value="TheBloke,MaziyarPanahi,unsloth,modularai,Gensyn,bartowski")
106
  generate_plot_button = gr.Button(value="Generate Plot", variant="primary", interactive=False)
@@ -109,12 +170,6 @@ with gr.Blocks(title="🤗 ModelVerse Explorer", fill_width=True) as demo:
109
  plot_output = gr.Plot()
110
  status_message_md = gr.Markdown("Initializing...")
111
  data_info_md = gr.Markdown("")
112
-
113
- def update_param_display(value: tuple):
114
- min_idx, max_idx = int(value[0]), int(value[1])
115
- return f"Range: `{PARAM_CHOICES[min_idx]}` to `{PARAM_CHOICES[max_idx]}`"
116
-
117
- param_range_slider.change(update_param_display, param_range_slider, param_range_display)
118
 
119
  def _update_button_interactivity(is_loaded_flag): return gr.update(interactive=is_loaded_flag)
120
  loading_complete_state.change(fn=_update_button_interactivity, inputs=loading_complete_state, outputs=generate_plot_button)
@@ -139,14 +194,11 @@ with gr.Blocks(title="🤗 ModelVerse Explorer", fill_width=True) as demo:
139
  data_info_text = f"### Data Load Failed\n- {status_msg_from_load}"
140
  status_msg_ui = status_msg_from_load
141
  except Exception as e:
142
- status_msg_ui = f"An unexpected error occurred: {str(e)}"
143
- data_info_text = f"### Critical Error\n- {status_msg_ui}"
144
- load_success_flag = False
145
- print(f"Critical error in ui_load_data_controller: {e}")
146
  return current_df, load_success_flag, data_info_text, status_msg_ui
147
 
148
  def ui_generate_plot_controller(metric_choice, filter_type, tag_choice, pipeline_choice,
149
- param_range_indices, k_orgs, skip_orgs_input, df_current_models, progress=gr.Progress()):
150
  if df_current_models is None or df_current_models.empty:
151
  return create_treemap(pd.DataFrame(), metric_choice, "Error: Model Data Not Loaded"), "Model data is not loaded."
152
 
@@ -155,6 +207,7 @@ with gr.Blocks(title="🤗 ModelVerse Explorer", fill_width=True) as demo:
155
  pipeline_to_use = pipeline_choice if filter_type == "Pipeline Filter" else None
156
  orgs_to_skip = [org.strip() for org in skip_orgs_input.split(',') if org.strip()]
157
 
 
158
  min_label = PARAM_CHOICES[int(param_range_indices[0])]
159
  max_label = PARAM_CHOICES[int(param_range_indices[1])]
160
  param_labels_for_filtering = [min_label, max_label]
@@ -174,16 +227,18 @@ with gr.Blocks(title="🤗 ModelVerse Explorer", fill_width=True) as demo:
174
  plot_stats_md = f"## Plot Statistics\n- **Models shown**: {total_items_in_plot:,}\n- **Total {metric_choice}**: {int(total_value_in_plot):,}"
175
  return plotly_fig, plot_stats_md
176
 
 
177
  demo.load(
178
  fn=ui_load_data_controller,
179
  inputs=[],
180
- outputs=[models_data_state, loading_complete_state, data_info_md, status_message_md]
 
181
  )
182
 
183
  generate_plot_button.click(
184
  fn=ui_generate_plot_controller,
185
  inputs=[count_by_dropdown, filter_choice_radio, tag_filter_dropdown, pipeline_filter_dropdown,
186
- param_range_slider, top_k_dropdown, skip_orgs_textbox, models_data_state],
187
  outputs=[plot_output, status_message_md]
188
  )
189
 
@@ -191,4 +246,4 @@ if __name__ == "__main__":
191
  print(f"Application starting...")
192
  demo.queue().launch()
193
 
194
- # --- END OF FINAL POLISHED FILE app.py ---
 
1
+ # --- START OF FINAL, POLISHED FILE app.py ---
2
 
3
  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
  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
  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
 
 
135
  with gr.Row():
136
  gr.Markdown("# 🤗 ModelVerse Explorer")
137
 
 
142
  tag_filter_dropdown = gr.Dropdown(label="Select Tag", choices=TAG_FILTER_CHOICES, value=None, visible=False)
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
  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
  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
  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
  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
 
 
246
  print(f"Application starting...")
247
  demo.queue().launch()
248
 
249
+ # --- END OF FINAL, POLISHED FILE app.py ---