Update app.py
Browse files
app.py
CHANGED
@@ -2,7 +2,7 @@ from typing import List, Dict
|
|
2 |
import httpx
|
3 |
import gradio as gr
|
4 |
import pandas as pd
|
5 |
-
from huggingface_hub import HfApi
|
6 |
|
7 |
def search_hub(query: str, search_type: str) -> pd.DataFrame:
|
8 |
api = HfApi()
|
@@ -21,22 +21,48 @@ def search_hub(query: str, search_type: str) -> pd.DataFrame:
|
|
21 |
|
22 |
return pd.DataFrame(data)
|
23 |
|
24 |
-
def open_url(
|
25 |
-
if
|
|
|
26 |
return f'<a href="{url}" target="_blank">{url}</a>'
|
27 |
else:
|
28 |
return ""
|
29 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
30 |
with gr.Blocks() as demo:
|
31 |
gr.Markdown("## Search the Hugging Face Hub")
|
32 |
with gr.Row():
|
33 |
search_query = gr.Textbox(label="Search Query")
|
34 |
search_type = gr.Radio(["Models", "Datasets", "Spaces"], label="Search Type", value="Models")
|
35 |
search_button = gr.Button("Search")
|
36 |
-
results_df = gr.DataFrame(label="Search Results", wrap=True, interactive=
|
37 |
url_output = gr.HTML(label="URL")
|
|
|
38 |
|
39 |
search_button.click(search_hub, inputs=[search_query, search_type], outputs=[results_df])
|
40 |
-
results_df.select(
|
|
|
41 |
|
42 |
demo.launch(debug=True)
|
|
|
2 |
import httpx
|
3 |
import gradio as gr
|
4 |
import pandas as pd
|
5 |
+
from huggingface_hub import HfApi, ModelCard
|
6 |
|
7 |
def search_hub(query: str, search_type: str) -> pd.DataFrame:
|
8 |
api = HfApi()
|
|
|
21 |
|
22 |
return pd.DataFrame(data)
|
23 |
|
24 |
+
def open_url(row):
|
25 |
+
if row is not None and not row.empty:
|
26 |
+
url = f"https://huggingface.co/{row.iloc[0]['id']}"
|
27 |
return f'<a href="{url}" target="_blank">{url}</a>'
|
28 |
else:
|
29 |
return ""
|
30 |
|
31 |
+
def load_metadata(row, search_type):
|
32 |
+
if row is not None and not row.empty:
|
33 |
+
item_id = row.iloc[0]['id']
|
34 |
+
|
35 |
+
if search_type == "Models":
|
36 |
+
try:
|
37 |
+
card = ModelCard.load(item_id)
|
38 |
+
return card
|
39 |
+
except Exception as e:
|
40 |
+
return f"Error loading model card: {str(e)}"
|
41 |
+
elif search_type == "Datasets":
|
42 |
+
api = HfApi()
|
43 |
+
metadata = api.dataset_info(item_id)
|
44 |
+
return str(metadata)
|
45 |
+
elif search_type == "Spaces":
|
46 |
+
api = HfApi()
|
47 |
+
metadata = api.space_info(item_id)
|
48 |
+
return str(metadata)
|
49 |
+
else:
|
50 |
+
return ""
|
51 |
+
else:
|
52 |
+
return ""
|
53 |
+
|
54 |
with gr.Blocks() as demo:
|
55 |
gr.Markdown("## Search the Hugging Face Hub")
|
56 |
with gr.Row():
|
57 |
search_query = gr.Textbox(label="Search Query")
|
58 |
search_type = gr.Radio(["Models", "Datasets", "Spaces"], label="Search Type", value="Models")
|
59 |
search_button = gr.Button("Search")
|
60 |
+
results_df = gr.DataFrame(label="Search Results", wrap=True, interactive=True)
|
61 |
url_output = gr.HTML(label="URL")
|
62 |
+
metadata_output = gr.Textbox(label="Metadata", lines=10)
|
63 |
|
64 |
search_button.click(search_hub, inputs=[search_query, search_type], outputs=[results_df])
|
65 |
+
results_df.select(open_url, outputs=[url_output])
|
66 |
+
results_df.select(load_metadata, inputs=[results_df, search_type], outputs=[metadata_output])
|
67 |
|
68 |
demo.launch(debug=True)
|