Spaces:
Running
Running
better execption handling
Browse files1234567895 unmodified lines1516171819202114 unmodified lines363738394041424318 unmodified lines62636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201 unmodified line1221231241251261271281291301311321331 unmodified line1351361371381391401411421431441451461471481492 unmodified lines15215315415515615715815916016116216316416516614 unmodified lines18118218318418518618718818919019112 unmodified lines2042052062072082092106 unmodified lines2172182192202212222232242252262272282292302312322332342352362372381 unmodified line2402412422432442452462472482492508 unmodified lines25926026126226326426526626726826927027127227327427516 unmodified lines29229329429529629729829930010 unmodified lines31131231331431531631731831932032132214 unmodified lines3373383393403413423433443453463473483493503513523537 unmodified lines3613623633643653663673683693703713723733743753763773783793803813823833843853863873 unmodified lines3913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454461 unmodified line4484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794801 unmodified line482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533import jsonimport gradio as grimport pandas as pdimport plotly.express as pximport osimport numpy as np # Make sure NumPy is importedimport duckdbfrom tqdm.auto import tqdm # Standard tqdm for console, gr.Progress will track itimport time5 unmodified lines"X-Large (20-50GB)": (20, 50), "XX-Large (>50GB)": (50, float('inf'))}PROCESSED_PARQUET_FILE_PATH = "models_processed.parquet"HF_PARQUET_URL = 'https://huggingface.co/datasets/cfahlgren1/hub-stats/resolve/main/models.parquet'TAG_FILTER_CHOICES = ["Audio & Speech", "Time series", "Robotics", "Music", "Video", "Images",14 unmodified lines'table-question-answering',]# --- Utility Functions ---def extract_model_size(safetensors_data): # Renamed for consistency if used, preprocessor uses extract_model_file_size_gbtry:if pd.isna(safetensors_data): return 0.0data_to_parse = safetensors_data18 unmodified linesmodel_id_str = str(model_id)return model_id_str.split("/")[0] if "/" in model_id_str else "unaffiliated"# --- THIS IS THE CORRECTED process_tags_for_series from preprocess.py ---def process_tags_for_series(series_of_tags_values, tqdm_cls=None): # Added tqdm_cls for Gradio progressprocessed_tags_accumulator = []# Determine the iterable (use tqdm if tqdm_cls is provided, else direct iteration)iterable = series_of_tags_valuesif tqdm_cls and tqdm_cls != tqdm : # Check if it's Gradio's progress trackeriterable = tqdm_cls(series_of_tags_values, desc="Standardizing Tags (App)", unit="row")elif tqdm_cls == tqdm: # For direct console tqdm if passediterable = tqdm(series_of_tags_values, desc="Standardizing Tags (App)", unit="row", leave=False)for i, tags_value_from_series in enumerate(iterable):temp_processed_list_for_row = []current_value_for_error_msg = str(tags_value_from_series)[:200]try:if isinstance(tags_value_from_series, list):current_tags_in_list = []for tag_item in tags_value_from_series:try:if pd.isna(tag_item): continuestr_tag = str(tag_item)stripped_tag = str_tag.strip()if stripped_tag:current_tags_in_list.append(stripped_tag)except Exception as e_inner_list_proc:print(f"APP ERROR processing item '{tag_item}' (type: {type(tag_item)}) within a list for row {i}. Error: {e_inner_list_proc}. Original: {current_value_for_error_msg}")temp_processed_list_for_row = current_tags_in_listelif isinstance(tags_value_from_series, np.ndarray):current_tags_in_list = []for tag_item in tags_value_from_series.tolist():try:if pd.isna(tag_item): continuestr_tag = str(tag_item)stripped_tag = str_tag.strip()if stripped_tag:current_tags_in_list.append(stripped_tag)except Exception as e_inner_array_proc:print(f"APP ERROR processing item '{tag_item}' (type: {type(tag_item)}) within a NumPy array for row {i}. Error: {e_inner_array_proc}. Original: {current_value_for_error_msg}")temp_processed_list_for_row = current_tags_in_listelif tags_value_from_series is None or pd.isna(tags_value_from_series):temp_processed_list_for_row = []elif isinstance(tags_value_from_series, str):processed_str_tags = []if (tags_value_from_series.startswith('[') and tags_value_from_series.endswith(']')) or \(tags_value_from_series.startswith('(') and tags_value_from_series.endswith(')')):try:evaluated_tags = ast.literal_eval(tags_value_from_series)if isinstance(evaluated_tags, (list, tuple)):current_eval_list = []for tag_item in evaluated_tags:if pd.isna(tag_item): continue1 unmodified lineif str_tag: current_eval_list.append(str_tag)processed_str_tags = current_eval_listexcept (ValueError, SyntaxError):passif not processed_str_tags:try:json_tags = json.loads(tags_value_from_series)if isinstance(json_tags, list):current_json_list = []for tag_item in json_tags:if pd.isna(tag_item): continue1 unmodified lineif str_tag: current_json_list.append(str_tag)processed_str_tags = current_json_listexcept json.JSONDecodeError:processed_str_tags = [tag.strip() for tag in tags_value_from_series.split(',') if tag.strip()]except Exception as e_json_other:print(f"APP ERROR during JSON processing for string '{current_value_for_error_msg}' for row {i}. Error: {e_json_other}")processed_str_tags = [tag.strip() for tag in tags_value_from_series.split(',') if tag.strip()]temp_processed_list_for_row = processed_str_tagselse:if pd.isna(tags_value_from_series):temp_processed_list_for_row = []else:str_val = str(tags_value_from_series).strip()2 unmodified linesprocessed_tags_accumulator.append(temp_processed_list_for_row)except Exception as e_outer_tag_proc:print(f"APP CRITICAL UNHANDLED ERROR processing row {i}: value '{current_value_for_error_msg}' (type: {type(tags_value_from_series)}). Error: {e_outer_tag_proc}. Appending [].")processed_tags_accumulator.append([])return processed_tags_accumulator# --- END OF CORRECTED process_tags_for_series ---def load_models_data(force_refresh=False, tqdm_cls=None): # tqdm_cls for Gradio progressif tqdm_cls is None: tqdm_cls = tqdm # Default to standard tqdm if Noneoverall_start_time = time.time()print(f"Gradio load_models_data called with force_refresh={force_refresh}")14 unmodified linesif missing_cols:raise ValueError(f"Pre-processed Parquet is missing columns: {missing_cols}. Please run preprocessor or refresh data in app.")if 'has_robot' in df.columns:robot_count_parquet = df['has_robot'].sum()print(f"DIAGNOSTIC (App - Parquet Load): 'has_robot' column found. Number of True values: {robot_count_parquet}")else:print("DIAGNOSTIC (App - Parquet Load): 'has_robot' column NOT FOUND.")msg = f"Successfully loaded pre-processed data in {elapsed:.2f}s. Shape: {df.shape}"print(msg)12 unmodified linesprint("force_refresh=True (Gradio). Fetching fresh data...")fetch_start = time.time()try:query = f"SELECT * FROM read_parquet('{HF_PARQUET_URL}')"df_raw = duckdb.sql(query).df()if df_raw is None or df_raw.empty: raise ValueError("Fetched data is empty or None.")raw_data_source_msg = f"Fetched by Gradio in {time.time() - fetch_start:.2f}s. Rows: {len(df_raw)}"6 unmodified linesreturn pd.DataFrame(), False, err_msgprint(f"Initiating processing for data newly fetched by Gradio. {raw_data_source_msg}")df = pd.DataFrame() # This will be our processed DataFrameproc_start = time.time()core_cols = {'id': str, 'downloads': float, 'downloadsAllTime': float, 'likes': float,'pipeline_tag': str, 'tags': object, 'safetensors': object}for col, dtype in core_cols.items():if col in df_raw.columns:df[col] = df_raw[col] # Assign raw data firstif dtype == float: df[col] = pd.to_numeric(df[col], errors='coerce').fillna(0.0)elif dtype == str: df[col] = df[col].astype(str).fillna('')# For 'tags' and 'safetensors' (object type), no specific conversion here, done laterelse: # If column is missing in raw dataif col in ['downloads', 'downloadsAllTime', 'likes']: df[col] = 0.0elif col == 'pipeline_tag': df[col] = ''elif col == 'tags': df[col] = pd.Series([[] for _ in range(len(df_raw))]) # Default to empty listselif col == 'safetensors': df[col] = None # Default to Noneelif col == 'id': return pd.DataFrame(), False, "Critical: 'id' column missing."output_filesize_col_name = 'params'1 unmodified linedf[output_filesize_col_name] = pd.to_numeric(df_raw[output_filesize_col_name], errors='coerce').fillna(0.0)elif 'safetensors' in df.columns:safetensors_iter = df['safetensors']if tqdm_cls and tqdm_cls != tqdm:safetensors_iter = tqdm_cls(df['safetensors'], desc="Extracting model sizes (GB)", unit="row")elif tqdm_cls == tqdm:safetensors_iter = tqdm(df['safetensors'], desc="Extracting model sizes (GB)", unit="row", leave=False)df[output_filesize_col_name] = [extract_model_size(s) for s in safetensors_iter]df[output_filesize_col_name] = pd.to_numeric(df[output_filesize_col_name], errors='coerce').fillna(0.0)else:8 unmodified lineselif 5 <= numeric_size_gb < 20: return "Large (5-20GB)"elif 20 <= numeric_size_gb < 50: return "X-Large (20-50GB)"elif numeric_size_gb >= 50: return "XX-Large (>50GB)"else: return "Small (<1GB)" # Defaultdf['size_category'] = df[output_filesize_col_name].apply(get_size_category_gradio)df['tags'] = process_tags_for_series(df['tags'], tqdm_cls=tqdm_cls)df['temp_tags_joined'] = df['tags'].apply(lambda tl: '~~~'.join(str(t).lower().strip() for t in tl if pd.notna(t) and str(t).strip()) if isinstance(tl, list) else '')tag_map = {'has_audio': ['audio'], 'has_speech': ['speech'], 'has_music': ['music'],'has_robot': ['robot', 'robotics'],'has_bio': ['bio'], 'has_med': ['medic', 'medical'],'has_series': ['series', 'time-series', 'timeseries'],'has_video': ['video'], 'has_image': ['image', 'vision'],16 unmodified linesnot (output_filesize_col_name in df_raw.columns and pd.api.types.is_numeric_dtype(df_raw[output_filesize_col_name])):df = df.drop(columns=['safetensors'], errors='ignore')if force_refresh and 'has_robot' in df.columns:robot_count_app_proc = df['has_robot'].sum()print(f"DIAGNOSTIC (App - Force Refresh Processing): 'has_robot' column processed. Number of True values: {robot_count_app_proc}")print(f"Data processing by Gradio completed in {time.time() - proc_start:.2f}s.")10 unmodified lines"Biomedical": "is_biomed", "Time series": "has_series", "Sciences": "has_science","Video": "has_video", "Images": "has_image", "Text": "has_text"}if 'has_robot' in filtered_df.columns:initial_robot_count = filtered_df['has_robot'].sum()if tag_filter and tag_filter in col_map:target_col = col_map[tag_filter]if target_col in filtered_df.columns:filtered_df = filtered_df[filtered_df[target_col]]else:print(f"Warning: Tag filter column '{col_map[tag_filter]}' not found in DataFrame.")if pipeline_filter:14 unmodified linesif filtered_df.empty: return pd.DataFrame()if count_by not in filtered_df.columns or not pd.api.types.is_numeric_dtype(filtered_df[count_by]):filtered_df[count_by] = pd.to_numeric(filtered_df.get(count_by), errors="coerce").fillna(0.0)org_totals = filtered_df.groupby("organization")[count_by].sum().nlargest(top_k, keep='first')top_orgs_list = org_totals.index.tolist()treemap_data = filtered_df[filtered_df["organization"].isin(top_orgs_list)][["id", "organization", count_by]].copy()treemap_data["root"] = "models"treemap_data[count_by] = pd.to_numeric(treemap_data[count_by], errors="coerce").fillna(0.0)return treemap_datadef create_treemap(treemap_data, count_by, title=None):if treemap_data.empty:fig = px.treemap(names=["No data matches filters"], parents=[""], values=[1])fig.update_layout(title="No data matches the selected filters", margin=dict(t=50, l=25, r=25, b=25))return figfig = px.treemap(7 unmodified lineswith gr.Blocks(title="HuggingFace Model Explorer") as demo:models_data_state = gr.State(pd.DataFrame())loading_complete_state = gr.State(False)with gr.Row():gr.Markdown("# HuggingFace Models TreeMap Visualization")with gr.Row():with gr.Column(scale=1):count_by_dropdown = gr.Dropdown(label="Metric", choices=[("Downloads (last 30 days)", "downloads"), ("Downloads (All Time)", "downloadsAllTime"), ("Likes", "likes")], value="downloads")filter_choice_radio = gr.Radio(label="Filter Type", choices=["None", "Tag Filter", "Pipeline Filter"], value="None")tag_filter_dropdown = gr.Dropdown(label="Select Tag", choices=TAG_FILTER_CHOICES, value=None, visible=False)pipeline_filter_dropdown = gr.Dropdown(label="Select Pipeline Tag", choices=PIPELINE_TAGS, value=None, visible=False)size_filter_dropdown = gr.Dropdown(label="Model Size Filter", choices=["None"] + list(MODEL_SIZE_RANGES.keys()), value="None")top_k_slider = gr.Slider(label="Number of Top Organizations", minimum=5, maximum=50, value=25, step=5)skip_orgs_textbox = gr.Textbox(label="Organizations to Skip (comma-separated)", value="TheBloke,MaziyarPanahi,unsloth,modularai,Gensyn,bartowski")generate_plot_button = gr.Button(value="Generate Plot", variant="primary", interactive=False)refresh_data_button = gr.Button(value="Refresh Data from Hugging Face", variant="secondary")with gr.Column(scale=3):plot_output = gr.Plot()status_message_md = gr.Markdown("Initializing...")data_info_md = gr.Markdown("")def _update_button_interactivity(is_loaded_flag):return gr.update(interactive=is_loaded_flag)3 unmodified linesreturn gr.update(visible=choice == "Tag Filter"), gr.update(visible=choice == "Pipeline Filter")filter_choice_radio.change(fn=_toggle_filters_visibility, inputs=filter_choice_radio, outputs=[tag_filter_dropdown, pipeline_filter_dropdown])def ui_load_data_controller(force_refresh_ui_trigger=False, progress=gr.Progress(track_tqdm=True)):print(f"ui_load_data_controller called with force_refresh_ui_trigger={force_refresh_ui_trigger}")status_msg_ui = "Loading data..."data_info_text = ""current_df = pd.DataFrame()load_success_flag = False# data_as_of_date_display = "N/A" # Will be set inside the logictry:current_df, load_success_flag, status_msg_from_load = load_models_data(force_refresh=force_refresh_ui_trigger, tqdm_cls=progress.tqdm if progress else tqdm)if load_success_flag:# Default value for data_as_of_date_displaydata_as_of_date_display = "Pre-processed (date unavailable or invalid)"if force_refresh_ui_trigger: # Data was just fetched by Gradiodata_as_of_date_display = pd.Timestamp.now(tz='UTC').strftime('%B %d, %Y, %H:%M:%S %Z')# If loaded from pre-processed parquet, check for its timestamp columnelif 'data_download_timestamp' in current_df.columns and not current_df.empty:try:# Step 1: Safely get the value from the DataFrame's first row for the timestamp columnraw_val_from_df = current_df['data_download_timestamp'].iloc[0]# Step 2: Process if raw_val_from_df is a list/arrayscalar_timestamp_val = Noneif isinstance(raw_val_from_df, (list, tuple, np.ndarray)):if len(raw_val_from_df) > 0:scalar_timestamp_val = raw_val_from_df[0]else:scalar_timestamp_val = raw_val_from_df# Step 3: Check for NA and convert the scalar value to datetimeif pd.notna(scalar_timestamp_val):dt_obj = pd.to_datetime(scalar_timestamp_val)if pd.notna(dt_obj):if dt_obj.tzinfo is None:dt_obj = dt_obj.tz_localize('UTC')data_as_of_date_display = dt_obj.strftime('%B %d, %Y, %H:%M:%S %Z')except IndexError:print(f"DEBUG: IndexError encountered while processing 'data_download_timestamp'. DF empty: {current_df.empty}")if 'data_download_timestamp' in current_df.columns and not current_df.empty:print(f"DEBUG: Head of 'data_download_timestamp': {str(current_df['data_download_timestamp'].head(1))}") # Ensure string conversion for printexcept Exception as e_ts_proc:print(f"Error processing 'data_download_timestamp' from parquet: {e_ts_proc}")# Build data info stringsize_dist_lines = []if 'size_category' in current_df.columns:for cat in MODEL_SIZE_RANGES.keys():1 unmodified linesize_dist_lines.append(f" - {cat}: {count:,} models")else: size_dist_lines.append(" - Size category information not available.")size_dist = "\n".join(size_dist_lines)data_info_text = (f"### Data Information\n"f"- Overall Status: {status_msg_from_load}\n"f"- Total models loaded: {len(current_df):,}\n"f"- Data as of: {data_as_of_date_display}\n"f"- Size categories:\n{size_dist}")if not current_df.empty and 'has_robot' in current_df.columns:robot_true_count = current_df['has_robot'].sum()data_info_text += f"\n- **Models flagged 'has_robot'**: {robot_true_count}"if 0 < robot_true_count <= 10:sample_robot_ids = current_df[current_df['has_robot']]['id'].head(5).tolist()data_info_text += f"\n - Sample 'has_robot' model IDs: `{', '.join(sample_robot_ids)}`"elif not current_df.empty:data_info_text += "\n- **Models flagged 'has_robot'**: 'has_robot' column not found."status_msg_ui = "Data loaded successfully. Ready to generate plot."else: # load_success_flag is Falsedata_info_text = f"### Data Load Failed\n- {status_msg_from_load}"status_msg_ui = status_msg_from_loadexcept Exception as e:status_msg_ui = f"An unexpected error occurred in ui_load_data_controller: {str(e)}"data_info_text = f"### Critical Error\n- {status_msg_ui}"print(f"Critical error in ui_load_data_controller: {e}") # This is the original error printload_success_flag = Falsereturn current_df, load_success_flag, data_info_text, status_msg_uidef ui_generate_plot_controller(metric_choice, filter_type, tag_choice, pipeline_choice,1 unmodified lineif df_current_models is None or df_current_models.empty:empty_fig = create_treemap(pd.DataFrame(), metric_choice, "Error: Model Data Not Loaded")error_msg = "Model data is not loaded or is empty. Please load or refresh data first."gr.Warning(error_msg)return empty_fig, error_msgtag_to_use = tag_choice if filter_type == "Tag Filter" else Nonepipeline_to_use = pipeline_choice if filter_type == "Pipeline Filter" else Nonesize_to_use = size_choice if size_choice != "None" else Noneorgs_to_skip = [org.strip() for org in skip_orgs_input.split(',') if org.strip()] if skip_orgs_input else []treemap_df = make_treemap_data(df_current_models, metric_choice, k_orgs, tag_to_use, pipeline_to_use, size_to_use, orgs_to_skip)title_labels = {"downloads": "Downloads (last 30 days)", "downloadsAllTime": "Downloads (All Time)", "likes": "Likes"}chart_title = f"HuggingFace Models - {title_labels.get(metric_choice, metric_choice)} by Organization"plotly_fig = create_treemap(treemap_df, metric_choice, chart_title)if treemap_df.empty:plot_stats_md = "No data matches the selected filters. Try adjusting your filters."else:total_items_in_plot = len(treemap_df['id'].unique())total_value_in_plot = treemap_df[metric_choice].sum()plot_stats_md = (f"## Plot Statistics\n- **Models shown**: {total_items_in_plot:,}\n- **Total {metric_choice}**: {int(total_value_in_plot):,}")return plotly_fig, plot_stats_mddemo.load(fn=lambda progress=gr.Progress(track_tqdm=True): ui_load_data_controller(force_refresh_ui_trigger=False, progress=progress),inputs=[],outputs=[models_data_state, loading_complete_state, data_info_md, status_message_md])refresh_data_button.click(fn=lambda progress=gr.Progress(track_tqdm=True): ui_load_data_controller(force_refresh_ui_trigger=True, progress=progress),inputs=[],outputs=[models_data_state, loading_complete_state, data_info_md, status_message_md])generate_plot_button.click(fn=ui_generate_plot_controller,inputs=[count_by_dropdown, filter_choice_radio, tag_filter_dropdown, pipeline_filter_dropdown,size_filter_dropdown, top_k_slider, skip_orgs_textbox, models_data_state],outputs=[plot_output, status_message_md])if __name__ == "__main__":if not os.path.exists(PROCESSED_PARQUET_FILE_PATH):print(f"WARNING: Pre-processed data file '{PROCESSED_PARQUET_FILE_PATH}' not found.")print("It is highly recommended to run the preprocessing script (preprocess.py) first.")else:print(f"Found pre-processed data file: '{PROCESSED_PARQUET_FILE_PATH}'.")demo.launch()12345678910115 unmodified lines1718192021222314 unmodified lines3839404142434418 unmodified lines636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221 unmodified line1241251261271281291301311321331341351361371 unmodified line1391401411421431441451461471481491501511521531541551561572 unmodified lines16016116216316416516616716816917017117214 unmodified lines18718818919019119219319419519619719819920020112 unmodified lines2142152162172182192206 unmodified lines2272282292302312322332342352362372382392402412422432442452462471 unmodified line2492502512522532542552568 unmodified lines26526626726826927027127227327427527627727827928016 unmodified lines29729829930030130230330430530630730830910 unmodified lines32032132232332432532632732832933033133233333433533633733833934034134234334414 unmodified lines3593603613623633643653663673683693703713723733747 unmodified lines3823833843853863873883893903913923933943953963973983994004014024034044053 unmodified lines4094104114124134144154164174184194204214224234244254264274284294304314324334344354361 unmodified line4384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694701 unmodified line472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526# --- START OF FILE app.py ---import jsonimport gradio as grimport pandas as pdimport plotly.express as pximport osimport numpy as npimport duckdbfrom tqdm.auto import tqdm # Standard tqdm for console, gr.Progress will track itimport time5 unmodified lines"X-Large (20-50GB)": (20, 50), "XX-Large (>50GB)": (50, float('inf'))}PROCESSED_PARQUET_FILE_PATH = "models_processed.parquet"HF_PARQUET_URL = 'https://huggingface.co/datasets/cfahlgren1/hub-stats/resolve/main/models.parquet' # Added for completeness within app.py contextTAG_FILTER_CHOICES = ["Audio & Speech", "Time series", "Robotics", "Music", "Video", "Images",14 unmodified lines'table-question-answering',]def extract_model_size(safetensors_data):try:if pd.isna(safetensors_data): return 0.0data_to_parse = safetensors_data18 unmodified linesmodel_id_str = str(model_id)return model_id_str.split("/")[0] if "/" in model_id_str else "unaffiliated"def process_tags_for_series(series_of_tags_values):processed_tags_accumulator = []for i, tags_value_from_series in enumerate(tqdm(series_of_tags_values, desc="Standardizing Tags", leave=False, unit="row")):temp_processed_list_for_row = []current_value_for_error_msg = str(tags_value_from_series)[:200] # Truncate for long error messagestry:# Order of checks is important!# 1. Handle explicit Python lists firstif isinstance(tags_value_from_series, list):current_tags_in_list = []for idx_tag, tag_item in enumerate(tags_value_from_series):try:# Ensure item is not NaN before string conversion if it might be a float NaN in a listif pd.isna(tag_item): continuestr_tag = str(tag_item)stripped_tag = str_tag.strip()if stripped_tag:current_tags_in_list.append(stripped_tag)except Exception as e_inner_list_proc:print(f"ERROR processing item '{tag_item}' (type: {type(tag_item)}) within a list for row {i}. Error: {e_inner_list_proc}. Original list: {current_value_for_error_msg}")temp_processed_list_for_row = current_tags_in_list# 2. Handle NumPy arrayselif isinstance(tags_value_from_series, np.ndarray):# Convert to list, then process elements, handling potential NaNs within the arraycurrent_tags_in_list = []for idx_tag, tag_item in enumerate(tags_value_from_series.tolist()): # .tolist() is crucialtry:if pd.isna(tag_item): continue # Check for NaN after converting to Python typestr_tag = str(tag_item)stripped_tag = str_tag.strip()if stripped_tag:current_tags_in_list.append(stripped_tag)except Exception as e_inner_array_proc:print(f"ERROR processing item '{tag_item}' (type: {type(tag_item)}) within a NumPy array for row {i}. Error: {e_inner_array_proc}. Original array: {current_value_for_error_msg}")temp_processed_list_for_row = current_tags_in_list# 3. Handle simple None or pd.NA after lists and arrays (which might contain pd.NA elements handled above)elif tags_value_from_series is None or pd.isna(tags_value_from_series): # Now pd.isna is safe for scalarstemp_processed_list_for_row = []# 4. Handle strings (could be JSON-like, list-like, or comma-separated)elif isinstance(tags_value_from_series, str):processed_str_tags = []# Attempt ast.literal_eval for strings that look like lists/tuplesif (tags_value_from_series.startswith('[') and tags_value_from_series.endswith(']')) or \(tags_value_from_series.startswith('(') and tags_value_from_series.endswith(')')):try:evaluated_tags = ast.literal_eval(tags_value_from_series)if isinstance(evaluated_tags, (list, tuple)): # Check if eval result is a list/tuple# Recursively process this evaluated list/tuple, as its elements could be complex# For simplicity here, assume elements are simple strings after evalcurrent_eval_list = []for tag_item in evaluated_tags:if pd.isna(tag_item): continue1 unmodified lineif str_tag: current_eval_list.append(str_tag)processed_str_tags = current_eval_listexcept (ValueError, SyntaxError):pass # If ast.literal_eval fails, let it fall to JSON or comma split# If ast.literal_eval didn't populate, try JSONif not processed_str_tags:try:json_tags = json.loads(tags_value_from_series)if isinstance(json_tags, list):# Similar to above, assume elements are simple strings after JSON parsingcurrent_json_list = []for tag_item in json_tags:if pd.isna(tag_item): continue1 unmodified lineif str_tag: current_json_list.append(str_tag)processed_str_tags = current_json_listexcept json.JSONDecodeError:# If not a valid JSON list, fall back to comma splitting as the final string strategyprocessed_str_tags = [tag.strip() for tag in tags_value_from_series.split(',') if tag.strip()]except Exception as e_json_other:print(f"ERROR during JSON processing for string '{current_value_for_error_msg}' for row {i}. Error: {e_json_other}")processed_str_tags = [tag.strip() for tag in tags_value_from_series.split(',') if tag.strip()] # Fallbacktemp_processed_list_for_row = processed_str_tags# 5. Fallback for other scalar types (e.g., int, float that are not NaN)else:# This path is for non-list, non-ndarray, non-None/NaN, non-string types.# Or for NaNs that slipped through if they are not None or pd.NA (e.g. float('nan'))if pd.isna(tags_value_from_series): # Catch any remaining NaNs like float('nan')temp_processed_list_for_row = []else:str_val = str(tags_value_from_series).strip()2 unmodified linesprocessed_tags_accumulator.append(temp_processed_list_for_row)except Exception as e_outer_tag_proc:print(f"CRITICAL UNHANDLED ERROR processing row {i}: value '{current_value_for_error_msg}' (type: {type(tags_value_from_series)}). Error: {e_outer_tag_proc}. Appending [].")processed_tags_accumulator.append([])return processed_tags_accumulatordef load_models_data(force_refresh=False, tqdm_cls=None):if tqdm_cls is None: tqdm_cls = tqdmoverall_start_time = time.time()print(f"Gradio load_models_data called with force_refresh={force_refresh}")14 unmodified linesif missing_cols:raise ValueError(f"Pre-processed Parquet is missing columns: {missing_cols}. Please run preprocessor or refresh data in app.")# --- Diagnostic for 'has_robot' after loading parquet ---if 'has_robot' in df.columns:robot_count_parquet = df['has_robot'].sum()print(f"DIAGNOSTIC (App - Parquet Load): 'has_robot' column found. Number of True values: {robot_count_parquet}")if 0 < robot_count_parquet < 10:print(f"Sample 'has_robot' models (from parquet): {df[df['has_robot']]['id'].head().tolist()}")else:print("DIAGNOSTIC (App - Parquet Load): 'has_robot' column NOT FOUND.")# --- End Diagnostic ---msg = f"Successfully loaded pre-processed data in {elapsed:.2f}s. Shape: {df.shape}"print(msg)12 unmodified linesprint("force_refresh=True (Gradio). Fetching fresh data...")fetch_start = time.time()try:query = f"SELECT * FROM read_parquet('{HF_PARQUET_URL}')" # Ensure HF_PARQUET_URL is defineddf_raw = duckdb.sql(query).df()if df_raw is None or df_raw.empty: raise ValueError("Fetched data is empty or None.")raw_data_source_msg = f"Fetched by Gradio in {time.time() - fetch_start:.2f}s. Rows: {len(df_raw)}"6 unmodified linesreturn pd.DataFrame(), False, err_msgprint(f"Initiating processing for data newly fetched by Gradio. {raw_data_source_msg}")df = pd.DataFrame()proc_start = time.time()core_cols = {'id': str, 'downloads': float, 'downloadsAllTime': float, 'likes': float,'pipeline_tag': str, 'tags': object, 'safetensors': object}for col, dtype in core_cols.items():if col in df_raw.columns:df[col] = df_raw[col]if dtype == float: df[col] = pd.to_numeric(df[col], errors='coerce').fillna(0.0)elif dtype == str: df[col] = df[col].astype(str).fillna('')else:if col in ['downloads', 'downloadsAllTime', 'likes']: df[col] = 0.0elif col == 'pipeline_tag': df[col] = ''elif col == 'tags': df[col] = pd.Series([[] for _ in range(len(df_raw))])elif col == 'safetensors': df[col] = Noneelif col == 'id': return pd.DataFrame(), False, "Critical: 'id' column missing."output_filesize_col_name = 'params'1 unmodified linedf[output_filesize_col_name] = pd.to_numeric(df_raw[output_filesize_col_name], errors='coerce').fillna(0.0)elif 'safetensors' in df.columns:safetensors_iter = df['safetensors']if tqdm_cls != tqdm :safetensors_iter = tqdm_cls(df['safetensors'], desc="Extracting model sizes (GB)")df[output_filesize_col_name] = [extract_model_size(s) for s in safetensors_iter]df[output_filesize_col_name] = pd.to_numeric(df[output_filesize_col_name], errors='coerce').fillna(0.0)else:8 unmodified lineselif 5 <= numeric_size_gb < 20: return "Large (5-20GB)"elif 20 <= numeric_size_gb < 50: return "X-Large (20-50GB)"elif numeric_size_gb >= 50: return "XX-Large (>50GB)"else: return "Small (<1GB)"df['size_category'] = df[output_filesize_col_name].apply(get_size_category_gradio)df['tags'] = process_tags_for_series(df['tags'])df['temp_tags_joined'] = df['tags'].apply(lambda tl: '~~~'.join(str(t).lower() for t in tl if pd.notna(t) and str(t).strip()) if isinstance(tl, list) else '')tag_map = {'has_audio': ['audio'], 'has_speech': ['speech'], 'has_music': ['music'],'has_robot': ['robot', 'robotics'],'has_bio': ['bio'], 'has_med': ['medic', 'medical'],'has_series': ['series', 'time-series', 'timeseries'],'has_video': ['video'], 'has_image': ['image', 'vision'],16 unmodified linesnot (output_filesize_col_name in df_raw.columns and pd.api.types.is_numeric_dtype(df_raw[output_filesize_col_name])):df = df.drop(columns=['safetensors'], errors='ignore')# --- Diagnostic for 'has_robot' after app-side processing (force_refresh path) ---if force_refresh and 'has_robot' in df.columns:robot_count_app_proc = df['has_robot'].sum()print(f"DIAGNOSTIC (App - Force Refresh Processing): 'has_robot' column processed. Number of True values: {robot_count_app_proc}")if 0 < robot_count_app_proc < 10:print(f"Sample 'has_robot' models (App processed): {df[df['has_robot']]['id'].head().tolist()}")# --- End Diagnostic ---print(f"Data processing by Gradio completed in {time.time() - proc_start:.2f}s.")10 unmodified lines"Biomedical": "is_biomed", "Time series": "has_series", "Sciences": "has_science","Video": "has_video", "Images": "has_image", "Text": "has_text"}# --- Diagnostic within make_treemap_data ---if 'has_robot' in filtered_df.columns:initial_robot_count = filtered_df['has_robot'].sum()print(f"DIAGNOSTIC (make_treemap_data entry): Input df has {initial_robot_count} 'has_robot' models.")else:print("DIAGNOSTIC (make_treemap_data entry): 'has_robot' column NOT in input df.")# --- End Diagnostic ---if tag_filter and tag_filter in col_map:target_col = col_map[tag_filter]if target_col in filtered_df.columns:# --- Diagnostic for specific 'Robotics' filter application ---if tag_filter == "Robotics":count_before_robot_filter = filtered_df[target_col].sum()print(f"DIAGNOSTIC (make_treemap_data): Applying 'Robotics' filter. Models with '{target_col}'=True before this filter step: {count_before_robot_filter}")# --- End Diagnostic ---filtered_df = filtered_df[filtered_df[target_col]]if tag_filter == "Robotics":print(f"DIAGNOSTIC (make_treemap_data): After 'Robotics' filter ({target_col}), df rows: {len(filtered_df)}")else:print(f"Warning: Tag filter column '{col_map[tag_filter]}' not found in DataFrame.")if pipeline_filter:14 unmodified linesif filtered_df.empty: return pd.DataFrame()if count_by not in filtered_df.columns or not pd.api.types.is_numeric_dtype(filtered_df[count_by]):filtered_df[count_by] = pd.to_numeric(filtered_df.get(count_by), errors="coerce").fillna(0.0)org_totals = filtered_df.groupby("organization")[count_by].sum().nlargest(top_k, keep='first')top_orgs_list = org_totals.index.tolist()treemap_data = filtered_df[filtered_df["organization"].isin(top_orgs_list)][["id", "organization", count_by]].copy()treemap_data["root"] = "models"treemap_data[count_by] = pd.to_numeric(treemap_data[count_by], errors="coerce").fillna(0.0)return treemap_datadef create_treemap(treemap_data, count_by, title=None):if treemap_data.empty:fig = px.treemap(names=["No data matches filters"], parents=[""], values=[1])fig.update_layout(title="No data matches the selected filters", margin=dict(t=50, l=25, r=25, b=25))return figfig = px.treemap(7 unmodified lineswith gr.Blocks(title="HuggingFace Model Explorer") as demo:models_data_state = gr.State(pd.DataFrame())loading_complete_state = gr.State(False)with gr.Row(): gr.Markdown("# HuggingFace Models TreeMap Visualization")with gr.Row():with gr.Column(scale=1):count_by_dropdown = gr.Dropdown(label="Metric", choices=[("Downloads (last 30 days)", "downloads"), ("Downloads (All Time)", "downloadsAllTime"), ("Likes", "likes")], value="downloads")filter_choice_radio = gr.Radio(label="Filter Type", choices=["None", "Tag Filter", "Pipeline Filter"], value="None")tag_filter_dropdown = gr.Dropdown(label="Select Tag", choices=TAG_FILTER_CHOICES, value=None, visible=False)pipeline_filter_dropdown = gr.Dropdown(label="Select Pipeline Tag", choices=PIPELINE_TAGS, value=None, visible=False)size_filter_dropdown = gr.Dropdown(label="Model Size Filter", choices=["None"] + list(MODEL_SIZE_RANGES.keys()), value="None")top_k_slider = gr.Slider(label="Number of Top Organizations", minimum=5, maximum=50, value=25, step=5)skip_orgs_textbox = gr.Textbox(label="Organizations to Skip (comma-separated)", value="TheBloke,MaziyarPanahi,unsloth,modularai,Gensyn,bartowski")generate_plot_button = gr.Button(value="Generate Plot", variant="primary", interactive=False)refresh_data_button = gr.Button(value="Refresh Data from Hugging Face", variant="secondary")with gr.Column(scale=3):plot_output = gr.Plot()status_message_md = gr.Markdown("Initializing...")data_info_md = gr.Markdown("")def _update_button_interactivity(is_loaded_flag):return gr.update(interactive=is_loaded_flag)3 unmodified linesreturn gr.update(visible=choice == "Tag Filter"), gr.update(visible=choice == "Pipeline Filter")filter_choice_radio.change(fn=_toggle_filters_visibility, inputs=filter_choice_radio, outputs=[tag_filter_dropdown, pipeline_filter_dropdown])def ui_load_data_controller(force_refresh_ui_trigger=False, progress=gr.Progress(track_tqdm=True)):print(f"ui_load_data_controller called with force_refresh_ui_trigger={force_refresh_ui_trigger}")status_msg_ui = "Loading data..."data_info_text = ""current_df = pd.DataFrame()load_success_flag = Falsedata_as_of_date_display = "N/A"try:current_df, load_success_flag, status_msg_from_load = load_models_data(force_refresh=force_refresh_ui_trigger, tqdm_cls=progress.tqdm)if load_success_flag:if force_refresh_ui_trigger:data_as_of_date_display = pd.Timestamp.now(tz='UTC').strftime('%B %d, %Y, %H:%M:%S %Z')elif 'data_download_timestamp' in current_df.columns and not current_df.empty and pd.notna(current_df['data_download_timestamp'].iloc[0]):timestamp_from_parquet = pd.to_datetime(current_df['data_download_timestamp'].iloc[0])if timestamp_from_parquet.tzinfo is None:timestamp_from_parquet = timestamp_from_parquet.tz_localize('UTC')data_as_of_date_display = timestamp_from_parquet.strftime('%B %d, %Y, %H:%M:%S %Z')else:data_as_of_date_display = "Pre-processed (date unavailable)"size_dist_lines = []if 'size_category' in current_df.columns:for cat in MODEL_SIZE_RANGES.keys():1 unmodified linesize_dist_lines.append(f" - {cat}: {count:,} models")else: size_dist_lines.append(" - Size category information not available.")size_dist = "\n".join(size_dist_lines)data_info_text = (f"### Data Information\n"f"- Overall Status: {status_msg_from_load}\n"f"- Total models loaded: {len(current_df):,}\n"f"- Data as of: {data_as_of_date_display}\n"f"- Size categories:\n{size_dist}")# --- MODIFICATION: Add 'has_robot' count to UI data_info_text ---if not current_df.empty and 'has_robot' in current_df.columns:robot_true_count = current_df['has_robot'].sum()data_info_text += f"\n- **Models flagged 'has_robot'**: {robot_true_count}"if 0 < robot_true_count <= 10: # If a few are found, list some IDssample_robot_ids = current_df[current_df['has_robot']]['id'].head(5).tolist()data_info_text += f"\n - Sample 'has_robot' model IDs: `{', '.join(sample_robot_ids)}`"elif not current_df.empty:data_info_text += "\n- **Models flagged 'has_robot'**: 'has_robot' column not found in loaded data."# --- END MODIFICATION ---status_msg_ui = "Data loaded successfully. Ready to generate plot."else:data_info_text = f"### Data Load Failed\n- {status_msg_from_load}"status_msg_ui = status_msg_from_loadexcept Exception as e:status_msg_ui = f"An unexpected error occurred in ui_load_data_controller: {str(e)}"data_info_text = f"### Critical Error\n- {status_msg_ui}"print(f"Critical error in ui_load_data_controller: {e}")load_success_flag = Falsereturn current_df, load_success_flag, data_info_text, status_msg_uidef ui_generate_plot_controller(metric_choice, filter_type, tag_choice, pipeline_choice,1 unmodified lineif df_current_models is None or df_current_models.empty:empty_fig = create_treemap(pd.DataFrame(), metric_choice, "Error: Model Data Not Loaded")error_msg = "Model data is not loaded or is empty. Please load or refresh data first."gr.Warning(error_msg)return empty_fig, error_msgtag_to_use = tag_choice if filter_type == "Tag Filter" else Nonepipeline_to_use = pipeline_choice if filter_type == "Pipeline Filter" else Nonesize_to_use = size_choice if size_choice != "None" else Noneorgs_to_skip = [org.strip() for org in skip_orgs_input.split(',') if org.strip()] if skip_orgs_input else []# --- Diagnostic before calling make_treemap_data ---if 'has_robot' in df_current_models.columns:robot_count_before_treemap = df_current_models['has_robot'].sum()print(f"DIAGNOSTIC (ui_generate_plot_controller): df_current_models entering make_treemap_data has {robot_count_before_treemap} 'has_robot' models.")# --- End Diagnostic ---treemap_df = make_treemap_data(df_current_models, metric_choice, k_orgs, tag_to_use, pipeline_to_use, size_to_use, orgs_to_skip)title_labels = {"downloads": "Downloads (last 30 days)", "downloadsAllTime": "Downloads (All Time)", "likes": "Likes"}chart_title = f"HuggingFace Models - {title_labels.get(metric_choice, metric_choice)} by Organization"plotly_fig = create_treemap(treemap_df, metric_choice, chart_title)if treemap_df.empty:plot_stats_md = "No data matches the selected filters. Try adjusting your filters."else:total_items_in_plot = len(treemap_df['id'].unique())total_value_in_plot = treemap_df[metric_choice].sum()plot_stats_md = (f"## Plot Statistics\n- **Models shown**: {total_items_in_plot:,}\n- **Total {metric_choice}**: {int(total_value_in_plot):,}")return plotly_fig, plot_stats_mddemo.load(fn=lambda progress=gr.Progress(track_tqdm=True): ui_load_data_controller(force_refresh_ui_trigger=False, progress=progress),inputs=[],outputs=[models_data_state, loading_complete_state, data_info_md, status_message_md])refresh_data_button.click(fn=lambda progress=gr.Progress(track_tqdm=True): ui_load_data_controller(force_refresh_ui_trigger=True, progress=progress),inputs=[],outputs=[models_data_state, loading_complete_state, data_info_md, status_message_md])generate_plot_button.click(fn=ui_generate_plot_controller,inputs=[count_by_dropdown, filter_choice_radio, tag_filter_dropdown, pipeline_filter_dropdown,size_filter_dropdown, top_k_slider, skip_orgs_textbox, models_data_state],outputs=[plot_output, status_message_md])if __name__ == "__main__":if not os.path.exists(PROCESSED_PARQUET_FILE_PATH):print(f"WARNING: Pre-processed data file '{PROCESSED_PARQUET_FILE_PATH}' not found.")print("It is highly recommended to run the preprocessing script (e.g., preprocess.py) first.") # Corrected script nameelse:print(f"Found pre-processed data file: '{PROCESSED_PARQUET_FILE_PATH}'.")demo.launch()# --- END OF FILE app.py ---
app.py+118-125