Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,5 +1,3 @@
|
|
| 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
|
|
@@ -156,33 +154,54 @@ with gr.Blocks(title="🤗 ModelVerse Explorer", fill_width=True, css=custom_css
|
|
| 156 |
return gr.update(visible=choice == "Tag Filter"), gr.update(visible=choice == "Pipeline Filter")
|
| 157 |
filter_choice_radio.change(fn=_toggle_filters_visibility, inputs=filter_choice_radio, outputs=[tag_filter_dropdown, pipeline_filter_dropdown])
|
| 158 |
|
| 159 |
-
|
|
|
|
| 160 |
progress(0, desc=f"Loading dataset '{HF_DATASET_ID}'...")
|
|
|
|
| 161 |
try:
|
| 162 |
current_df, load_success_flag, status_msg_from_load = load_models_data()
|
| 163 |
if load_success_flag:
|
| 164 |
-
progress(0.
|
| 165 |
date_display = "Pre-processed (date unavailable)"
|
| 166 |
if 'data_download_timestamp' in current_df.columns and pd.notna(current_df['data_download_timestamp'].iloc[0]):
|
| 167 |
ts = pd.to_datetime(current_df['data_download_timestamp'].iloc[0], utc=True)
|
| 168 |
date_display = ts.strftime('%B %d, %Y, %H:%M:%S %Z')
|
| 169 |
param_count = (current_df['params'] > 0).sum() if 'params' in current_df.columns else 0
|
| 170 |
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"
|
| 171 |
-
status_msg_ui = "Data loaded. Ready to generate plot."
|
| 172 |
else:
|
| 173 |
data_info_text = f"### Data Load Failed\n- {status_msg_from_load}"
|
| 174 |
-
status_msg_ui = status_msg_from_load
|
| 175 |
except Exception as e:
|
| 176 |
-
|
| 177 |
-
data_info_text = f"### Critical Error\n- {
|
| 178 |
load_success_flag = False
|
| 179 |
-
|
| 180 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 181 |
|
| 182 |
def ui_generate_plot_controller(metric_choice, filter_type, tag_choice, pipeline_choice,
|
| 183 |
param_range_indices, k_orgs, skip_orgs_input, df_current_models, progress=gr.Progress()):
|
| 184 |
if df_current_models is None or df_current_models.empty:
|
| 185 |
-
return create_treemap(pd.DataFrame(), metric_choice, "Error: Model Data Not Loaded"), "Model data is not loaded."
|
| 186 |
|
| 187 |
progress(0.1, desc="Preparing data...")
|
| 188 |
tag_to_use = tag_choice if filter_type == "Tag Filter" else None
|
|
@@ -208,10 +227,11 @@ with gr.Blocks(title="🤗 ModelVerse Explorer", fill_width=True, css=custom_css
|
|
| 208 |
plot_stats_md = f"## Plot Statistics\n- **Models shown**: {total_items_in_plot:,}\n- **Total {metric_choice}**: {int(total_value_in_plot):,}"
|
| 209 |
return plotly_fig, plot_stats_md
|
| 210 |
|
|
|
|
| 211 |
demo.load(
|
| 212 |
-
fn=
|
| 213 |
inputs=[],
|
| 214 |
-
outputs=[models_data_state, loading_complete_state, data_info_md, status_message_md]
|
| 215 |
)
|
| 216 |
|
| 217 |
generate_plot_button.click(
|
|
@@ -223,6 +243,4 @@ with gr.Blocks(title="🤗 ModelVerse Explorer", fill_width=True, css=custom_css
|
|
| 223 |
|
| 224 |
if __name__ == "__main__":
|
| 225 |
print(f"Application starting...")
|
| 226 |
-
demo.queue().launch()
|
| 227 |
-
|
| 228 |
-
# --- END OF FINAL, POLISHED FILE app.py ---
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import pandas as pd
|
| 3 |
import plotly.express as px
|
|
|
|
| 154 |
return gr.update(visible=choice == "Tag Filter"), gr.update(visible=choice == "Pipeline Filter")
|
| 155 |
filter_choice_radio.change(fn=_toggle_filters_visibility, inputs=filter_choice_radio, outputs=[tag_filter_dropdown, pipeline_filter_dropdown])
|
| 156 |
|
| 157 |
+
## CHANGE: Renamed and modified ui_load_data_controller to also generate the initial plot
|
| 158 |
+
def load_and_generate_initial_plot(progress=gr.Progress()):
|
| 159 |
progress(0, desc=f"Loading dataset '{HF_DATASET_ID}'...")
|
| 160 |
+
# --- Part 1: Data Loading ---
|
| 161 |
try:
|
| 162 |
current_df, load_success_flag, status_msg_from_load = load_models_data()
|
| 163 |
if load_success_flag:
|
| 164 |
+
progress(0.5, desc="Processing data...")
|
| 165 |
date_display = "Pre-processed (date unavailable)"
|
| 166 |
if 'data_download_timestamp' in current_df.columns and pd.notna(current_df['data_download_timestamp'].iloc[0]):
|
| 167 |
ts = pd.to_datetime(current_df['data_download_timestamp'].iloc[0], utc=True)
|
| 168 |
date_display = ts.strftime('%B %d, %Y, %H:%M:%S %Z')
|
| 169 |
param_count = (current_df['params'] > 0).sum() if 'params' in current_df.columns else 0
|
| 170 |
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"
|
|
|
|
| 171 |
else:
|
| 172 |
data_info_text = f"### Data Load Failed\n- {status_msg_from_load}"
|
|
|
|
| 173 |
except Exception as e:
|
| 174 |
+
status_msg_from_load = f"An unexpected error occurred: {str(e)}"
|
| 175 |
+
data_info_text = f"### Critical Error\n- {status_msg_from_load}"
|
| 176 |
load_success_flag = False
|
| 177 |
+
current_df = pd.DataFrame()
|
| 178 |
+
print(f"Critical error in load_and_generate_initial_plot: {e}")
|
| 179 |
+
|
| 180 |
+
# --- Part 2: Generate Initial Plot ---
|
| 181 |
+
# We call the existing plot generation function with the default values from the UI
|
| 182 |
+
progress(0.6, desc="Generating initial plot...")
|
| 183 |
+
# Get default values directly from the UI component definitions
|
| 184 |
+
default_metric = "downloads"
|
| 185 |
+
default_filter_type = "None"
|
| 186 |
+
default_tag = None
|
| 187 |
+
default_pipeline = None
|
| 188 |
+
default_param_indices = PARAM_CHOICES_DEFAULT_INDICES
|
| 189 |
+
default_k = 25
|
| 190 |
+
default_skip_orgs = "TheBloke,MaziyarPanahi,unsloth,modularai,Gensyn,bartowski"
|
| 191 |
+
|
| 192 |
+
# Reuse the existing controller function for plotting
|
| 193 |
+
initial_plot, initial_status = ui_generate_plot_controller(
|
| 194 |
+
default_metric, default_filter_type, default_tag, default_pipeline,
|
| 195 |
+
default_param_indices, default_k, default_skip_orgs, current_df, progress
|
| 196 |
+
)
|
| 197 |
+
|
| 198 |
+
# Return all the necessary updates for the UI
|
| 199 |
+
return current_df, load_success_flag, data_info_text, initial_status, initial_plot
|
| 200 |
|
| 201 |
def ui_generate_plot_controller(metric_choice, filter_type, tag_choice, pipeline_choice,
|
| 202 |
param_range_indices, k_orgs, skip_orgs_input, df_current_models, progress=gr.Progress()):
|
| 203 |
if df_current_models is None or df_current_models.empty:
|
| 204 |
+
return create_treemap(pd.DataFrame(), metric_choice, "Error: Model Data Not Loaded"), "Model data is not loaded. Cannot generate plot."
|
| 205 |
|
| 206 |
progress(0.1, desc="Preparing data...")
|
| 207 |
tag_to_use = tag_choice if filter_type == "Tag Filter" else None
|
|
|
|
| 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 |
+
## CHANGE: Updated demo.load to call the new function and to add plot_output to the outputs list
|
| 231 |
demo.load(
|
| 232 |
+
fn=load_and_generate_initial_plot,
|
| 233 |
inputs=[],
|
| 234 |
+
outputs=[models_data_state, loading_complete_state, data_info_md, status_message_md, plot_output]
|
| 235 |
)
|
| 236 |
|
| 237 |
generate_plot_button.click(
|
|
|
|
| 243 |
|
| 244 |
if __name__ == "__main__":
|
| 245 |
print(f"Application starting...")
|
| 246 |
+
demo.queue().launch()
|
|
|
|
|
|