Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| from huggingface_hub import HfApi | |
| # Initialize Hugging Face API | |
| api = HfApi() | |
| def fetch_model_metadata(model_name): | |
| """ | |
| Fetch model metadata from Hugging Face model card and return as key-value pairs. | |
| Uses safe access methods to avoid missing attributes. | |
| """ | |
| def flatten_object(obj, prefix=""): | |
| """ | |
| Recursively flatten objects and return a dictionary of key-value pairs. | |
| Lists are joined into a comma-separated string. | |
| """ | |
| flat_data = {} | |
| # If the object is a dictionary, iterate through its items | |
| if isinstance(obj, dict): | |
| for key, value in obj.items(): | |
| # Use the prefix to avoid key name collision | |
| flat_data.update(flatten_object(value, prefix + key + ".")) | |
| # If the object is a list, join its elements into a string | |
| elif isinstance(obj, list): | |
| flat_data[prefix[:-1]] = ", ".join([str(v) for v in obj]) | |
| # For all other objects, just convert to a string | |
| else: | |
| flat_data[prefix[:-1]] = str(obj) | |
| return flat_data | |
| try: | |
| # Get the model card details | |
| model_info = api.model_info(model_name) | |
| # Initialize an empty dictionary to hold the attributes | |
| attributes = {} | |
| # Safe extraction of general metadata attributes | |
| attributes["Model Name"] = getattr(model_info, 'modelId', 'Unknown') | |
| attributes["Visibility"] = "Private" if getattr(model_info, 'private', False) else "Public" | |
| attributes["Last Modified"] = getattr(model_info, 'lastModified', 'Unknown') | |
| attributes["Downloads"] = getattr(model_info, 'downloads', 'Unknown') | |
| attributes["Size"] = getattr(model_info, 'size', 'Unknown') | |
| attributes["License"] = getattr(model_info, 'license', 'Unknown') | |
| # Safe extraction of the 'creator' attribute, if it exists | |
| attributes["Creator"] = getattr(model_info, 'creator', 'Unknown') | |
| # Safely handle 'cardData', which may not have 'items()' | |
| if getattr(model_info, 'cardData', None): | |
| card_data = getattr(model_info, 'cardData', {}) | |
| # Flatten the cardData object using the flatten_object function | |
| flat_card_data = flatten_object(card_data) | |
| # Add the flattened card data to the attributes dictionary | |
| attributes.update(flat_card_data) | |
| # Return the attributes dictionary | |
| return attributes, None # Return attributes and no error message | |
| except Exception as e: | |
| # In case of error, return an empty dictionary and the error message | |
| return {}, f"Error fetching model info: {e}" | |
| def generate_key_value_table(attributes): | |
| """ | |
| Generate an HTML table displaying key-value pairs. | |
| """ | |
| table_html = "<table border='1' style='border-collapse:collapse;width:100%;'>" | |
| table_html += "<tr><th>Attribute</th><th>Value</th></tr>" | |
| # Safely handle displaying values | |
| for key, value in attributes.items(): | |
| # Check if the value is a complex object | |
| if isinstance(value, dict) or isinstance(value, list): | |
| value = str(value) # Convert complex object to string | |
| table_html += f"<tr><td>{key}</td><td>{value}</td></tr>" | |
| table_html += "</table>" | |
| return table_html | |
| # Create Gradio Interface | |
| with gr.Blocks() as demo: | |
| gr.Markdown("### Fetch Model Attributes from Hugging Face Model Card") | |
| # Input field for model name | |
| model_input = gr.Textbox(label="Model Name (e.g., bert-base-uncased)", placeholder="Enter model name", lines=1) | |
| # Output HTML for displaying key-value pairs in a table | |
| output_html = gr.HTML() | |
| # Output for any error messages | |
| status_label = gr.Label() | |
| # Button to fetch model data | |
| fetch_button = gr.Button("Fetch Model Information") | |
| # Fetch model data on button click | |
| fetch_button.click( | |
| fetch_model_metadata, | |
| inputs=[model_input], | |
| outputs=[output_html, status_label] | |
| ) | |
| # Display key-value table dynamically | |
| def update_table(attributes): | |
| return generate_key_value_table(attributes) | |
| # Launch the app | |
| demo.launch() | |