from typing import List, Dict import httpx import gradio as gr import pandas as pd from huggingface_hub import HfApi def search_hub(query: str, search_type: str) -> pd.DataFrame: api = HfApi() if search_type == "Models": results = api.list_models(search=query) data = [{"id": model.modelId, "author": model.author, "downloads": model.downloads} for model in results] elif search_type == "Datasets": results = api.list_datasets(search=query) data = [{"id": dataset.id, "author": dataset.author, "downloads": dataset.downloads} for dataset in results] elif search_type == "Spaces": results = api.list_spaces(search=query) data = [{"id": space.id, "author": space.author} for space in results] else: data = [] return pd.DataFrame(data) def open_url(url): if url: return f'{url}' else: return "" with gr.Blocks() as demo: gr.Markdown("## Search the Hugging Face Hub") with gr.Row(): search_query = gr.Textbox(label="Search Query") search_type = gr.Radio(["Models", "Datasets", "Spaces"], label="Search Type", value="Models") search_button = gr.Button("Search") results_df = gr.DataFrame(label="Search Results", wrap=True, interactive=False) url_output = gr.HTML(label="URL") search_button.click(search_hub, inputs=[search_query, search_type], outputs=[results_df]) results_df.select(lambda row: open_url(f"https://huggingface.co/{row.iloc[0]}"), outputs=[url_output]) demo.launch(debug=True)