File size: 2,493 Bytes
8c76e83
 
 
 
8c72805
8c76e83
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8c72805
 
 
8c76e83
 
 
 
8c72805
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8c76e83
 
 
 
 
 
8c72805
8c76e83
8c72805
8c76e83
 
8c72805
 
8c76e83
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
from typing import List, Dict
import httpx
import gradio as gr
import pandas as pd
from huggingface_hub import HfApi, ModelCard

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(row):
    if row is not None and not row.empty:
        url = f"https://huggingface.co/{row.iloc[0]['id']}"
        return f'<a href="{url}" target="_blank">{url}</a>'
    else:
        return ""

def load_metadata(row, search_type):
    if row is not None and not row.empty:
        item_id = row.iloc[0]['id']
        
        if search_type == "Models":
            try:
                card = ModelCard.load(item_id)
                return card
            except Exception as e:
                return f"Error loading model card: {str(e)}"
        elif search_type == "Datasets":
            api = HfApi()
            metadata = api.dataset_info(item_id)
            return str(metadata)
        elif search_type == "Spaces":
            api = HfApi()
            metadata = api.space_info(item_id)
            return str(metadata)
        else:
            return ""
    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=True)
    url_output = gr.HTML(label="URL")
    metadata_output = gr.Textbox(label="Metadata", lines=10)

    search_button.click(search_hub, inputs=[search_query, search_type], outputs=[results_df])
    results_df.select(open_url, outputs=[url_output])
    results_df.select(load_metadata, inputs=[results_df, search_type], outputs=[metadata_output])

demo.launch(debug=True)