Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
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()
|
| 9 |
+
|
| 10 |
+
if search_type == "Models":
|
| 11 |
+
results = api.list_models(search=query)
|
| 12 |
+
data = [{"id": model.modelId, "author": model.author, "downloads": model.downloads} for model in results]
|
| 13 |
+
elif search_type == "Datasets":
|
| 14 |
+
results = api.list_datasets(search=query)
|
| 15 |
+
data = [{"id": dataset.id, "author": dataset.author, "downloads": dataset.downloads} for dataset in results]
|
| 16 |
+
elif search_type == "Spaces":
|
| 17 |
+
results = api.list_spaces(search=query)
|
| 18 |
+
data = [{"id": space.id, "author": space.author} for space in results]
|
| 19 |
+
else:
|
| 20 |
+
data = []
|
| 21 |
+
|
| 22 |
+
return pd.DataFrame(data)
|
| 23 |
+
|
| 24 |
+
def open_url(url):
|
| 25 |
+
if url:
|
| 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=False)
|
| 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(lambda row: open_url(f"https://huggingface.co/{row.iloc[0]}"), outputs=[url_output])
|
| 41 |
+
|
| 42 |
+
demo.launch(debug=True)
|