awacke1 commited on
Commit
44f6f5f
1 Parent(s): 29e3e35

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +105 -0
app.py ADDED
@@ -0,0 +1,105 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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, ModelCard
6
+
7
+ def search_hub(query: str, search_type: str) -> pd.DataFrame:
8
+ api = HfApi()
9
+ data = []
10
+
11
+ if search_type == "Models":
12
+ results = api.list_models(search=query)
13
+ data = [{"id": model.modelId, "author": model.author, "downloads": model.downloads,
14
+ "link": f"https://huggingface.co/{model.modelId}"} for model in results]
15
+ elif search_type == "Datasets":
16
+ results = api.list_datasets(search=query)
17
+ data = [{"id": dataset.id, "author": dataset.author, "downloads": dataset.downloads,
18
+ "link": f"https://huggingface.co/datasets/{dataset.id}"} for dataset in results]
19
+ elif search_type == "Spaces":
20
+ results = api.list_spaces(search=query)
21
+ data = [{"id": space.id, "author": space.author,
22
+ "link": f"https://huggingface.co/spaces/{space.id}"} for space in results]
23
+
24
+ return pd.DataFrame(data)
25
+
26
+ def open_url(row):
27
+ if row is not None and not row.empty:
28
+ url = row.iloc[0]['link']
29
+ return f'<a href="{url}" target="_blank">{url}</a>'
30
+ else:
31
+ return ""
32
+
33
+ def load_metadata(row, search_type):
34
+ if row is not None and not row.empty:
35
+ item_id = row.iloc[0]['id']
36
+
37
+ if search_type == "Models":
38
+ try:
39
+ card = ModelCard.load(item_id)
40
+ return card
41
+ except Exception as e:
42
+ return f"Error loading model card: {str(e)}"
43
+ elif search_type == "Datasets":
44
+ api = HfApi()
45
+ metadata = api.dataset_info(item_id)
46
+ return str(metadata)
47
+ elif search_type == "Spaces":
48
+ api = HfApi()
49
+ metadata = api.space_info(item_id)
50
+ return str(metadata)
51
+ else:
52
+ return ""
53
+ else:
54
+ return ""
55
+
56
+ def SwarmyTime(data: List[Dict]) -> Dict:
57
+ """
58
+ Aggregates all content from the given data.
59
+
60
+ :param data: List of dictionaries containing the search results
61
+ :return: Dictionary with aggregated content
62
+ """
63
+ aggregated = {
64
+ "total_items": len(data),
65
+ "unique_authors": set(),
66
+ "total_downloads": 0,
67
+ "item_types": {"Models": 0, "Datasets": 0, "Spaces": 0}
68
+ }
69
+
70
+ for item in data:
71
+ aggregated["unique_authors"].add(item.get("author", "Unknown"))
72
+ aggregated["total_downloads"] += item.get("downloads", 0)
73
+
74
+ if "modelId" in item:
75
+ aggregated["item_types"]["Models"] += 1
76
+ elif "dataset" in item.get("id", ""):
77
+ aggregated["item_types"]["Datasets"] += 1
78
+ else:
79
+ aggregated["item_types"]["Spaces"] += 1
80
+
81
+ aggregated["unique_authors"] = len(aggregated["unique_authors"])
82
+
83
+ return aggregated
84
+
85
+ with gr.Blocks() as demo:
86
+ gr.Markdown("## Search the Hugging Face Hub")
87
+ with gr.Row():
88
+ search_query = gr.Textbox(label="Search Query")
89
+ search_type = gr.Radio(["Models", "Datasets", "Spaces"], label="Search Type", value="Models")
90
+ search_button = gr.Button("Search")
91
+ results_df = gr.DataFrame(label="Search Results", wrap=True, interactive=True)
92
+ url_output = gr.HTML(label="URL")
93
+ metadata_output = gr.Textbox(label="Metadata", lines=10)
94
+ aggregated_output = gr.JSON(label="Aggregated Content")
95
+
96
+ def search_and_aggregate(query, search_type):
97
+ df = search_hub(query, search_type)
98
+ aggregated = SwarmyTime(df.to_dict('records'))
99
+ return df, aggregated
100
+
101
+ search_button.click(search_and_aggregate, inputs=[search_query, search_type], outputs=[results_df, aggregated_output])
102
+ results_df.select(open_url, outputs=[url_output])
103
+ results_df.select(load_metadata, inputs=[results_df, search_type], outputs=[metadata_output])
104
+
105
+ demo.launch(debug=True)