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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"nbformat": 4,
|
| 3 |
+
"nbformat_minor": 0,
|
| 4 |
+
"metadata": {
|
| 5 |
+
"colab": {
|
| 6 |
+
"provenance": []
|
| 7 |
+
},
|
| 8 |
+
"kernelspec": {
|
| 9 |
+
"name": "python3",
|
| 10 |
+
"display_name": "Python 3"
|
| 11 |
+
},
|
| 12 |
+
"language_info": {
|
| 13 |
+
"name": "python"
|
| 14 |
+
}
|
| 15 |
+
},
|
| 16 |
+
"cells": [
|
| 17 |
+
{
|
| 18 |
+
"cell_type": "code",
|
| 19 |
+
"execution_count": 1,
|
| 20 |
+
"metadata": {
|
| 21 |
+
"id": "-DBXBd1Q6SFF"
|
| 22 |
+
},
|
| 23 |
+
"outputs": [],
|
| 24 |
+
"source": [
|
| 25 |
+
"import requests\n",
|
| 26 |
+
"from typing import List, Dict, Any, Iterator\n",
|
| 27 |
+
"\n",
|
| 28 |
+
"class DatasetSearchClient:\n",
|
| 29 |
+
" def __init__(self, base_url: str = \"https://librarian-bots-dataset-column-search-api.hf.space\"):\n",
|
| 30 |
+
" self.base_url = base_url\n",
|
| 31 |
+
"\n",
|
| 32 |
+
" def search(self,\n",
|
| 33 |
+
" columns: List[str],\n",
|
| 34 |
+
" match_all: bool = False,\n",
|
| 35 |
+
" page_size: int = 100) -> Iterator[Dict[str, Any]]:\n",
|
| 36 |
+
" \"\"\"\n",
|
| 37 |
+
" Search datasets using the provided API, automatically handling pagination.\n",
|
| 38 |
+
"\n",
|
| 39 |
+
" Args:\n",
|
| 40 |
+
" columns (List[str]): List of column names to search for.\n",
|
| 41 |
+
" match_all (bool, optional): If True, match all columns. If False, match any column. Defaults to False.\n",
|
| 42 |
+
" page_size (int, optional): Number of results per page. Defaults to 100.\n",
|
| 43 |
+
"\n",
|
| 44 |
+
" Yields:\n",
|
| 45 |
+
" Dict[str, Any]: Each dataset result from all pages.\n",
|
| 46 |
+
"\n",
|
| 47 |
+
" Raises:\n",
|
| 48 |
+
" requests.RequestException: If there's an error with the HTTP request.\n",
|
| 49 |
+
" ValueError: If the API returns an unexpected response format.\n",
|
| 50 |
+
" \"\"\"\n",
|
| 51 |
+
" page = 1\n",
|
| 52 |
+
" total_results = None\n",
|
| 53 |
+
"\n",
|
| 54 |
+
" while total_results is None or (page - 1) * page_size < total_results:\n",
|
| 55 |
+
" params = {\n",
|
| 56 |
+
" \"columns\": columns,\n",
|
| 57 |
+
" \"match_all\": str(match_all).lower(),\n",
|
| 58 |
+
" \"page\": page,\n",
|
| 59 |
+
" \"page_size\": page_size\n",
|
| 60 |
+
" }\n",
|
| 61 |
+
"\n",
|
| 62 |
+
" try:\n",
|
| 63 |
+
" response = requests.get(f\"{self.base_url}/search\", params=params)\n",
|
| 64 |
+
" response.raise_for_status()\n",
|
| 65 |
+
" data = response.json()\n",
|
| 66 |
+
"\n",
|
| 67 |
+
" if not {\"total\", \"page\", \"page_size\", \"results\"}.issubset(data.keys()):\n",
|
| 68 |
+
" raise ValueError(\"Unexpected response format from the API\")\n",
|
| 69 |
+
"\n",
|
| 70 |
+
" if total_results is None:\n",
|
| 71 |
+
" total_results = data['total']\n",
|
| 72 |
+
"\n",
|
| 73 |
+
" for dataset in data['results']:\n",
|
| 74 |
+
" yield dataset\n",
|
| 75 |
+
"\n",
|
| 76 |
+
" page += 1\n",
|
| 77 |
+
"\n",
|
| 78 |
+
" except requests.RequestException as e:\n",
|
| 79 |
+
" raise requests.RequestException(f\"Error connecting to the API: {str(e)}\")\n",
|
| 80 |
+
" except ValueError as e:\n",
|
| 81 |
+
" raise ValueError(f\"Error processing API response: {str(e)}\")\n",
|
| 82 |
+
"\n",
|
| 83 |
+
"# Create an instance of the client\n",
|
| 84 |
+
"client = DatasetSearchClient()"
|
| 85 |
+
]
|
| 86 |
+
},
|
| 87 |
+
{
|
| 88 |
+
"cell_type": "code",
|
| 89 |
+
"source": [
|
| 90 |
+
"results = list(client.search(['tools'],match_all=True))\n",
|
| 91 |
+
"len(results)"
|
| 92 |
+
],
|
| 93 |
+
"metadata": {
|
| 94 |
+
"colab": {
|
| 95 |
+
"base_uri": "https://localhost:8080/"
|
| 96 |
+
},
|
| 97 |
+
"id": "9yupgFYx6Sqx",
|
| 98 |
+
"outputId": "ac6d7c15-2267-4bbd-ceaa-1d98faee188b"
|
| 99 |
+
},
|
| 100 |
+
"execution_count": 5,
|
| 101 |
+
"outputs": [
|
| 102 |
+
{
|
| 103 |
+
"output_type": "execute_result",
|
| 104 |
+
"data": {
|
| 105 |
+
"text/plain": [
|
| 106 |
+
"38"
|
| 107 |
+
]
|
| 108 |
+
},
|
| 109 |
+
"metadata": {},
|
| 110 |
+
"execution_count": 5
|
| 111 |
+
}
|
| 112 |
+
]
|
| 113 |
+
},
|
| 114 |
+
{
|
| 115 |
+
"cell_type": "code",
|
| 116 |
+
"source": [
|
| 117 |
+
"results[0]"
|
| 118 |
+
],
|
| 119 |
+
"metadata": {
|
| 120 |
+
"colab": {
|
| 121 |
+
"base_uri": "https://localhost:8080/"
|
| 122 |
+
},
|
| 123 |
+
"id": "atL-PQq76VrV",
|
| 124 |
+
"outputId": "f357fe16-a1f9-4bb2-ca3d-767f3ac6508d"
|
| 125 |
+
},
|
| 126 |
+
"execution_count": 6,
|
| 127 |
+
"outputs": [
|
| 128 |
+
{
|
| 129 |
+
"output_type": "execute_result",
|
| 130 |
+
"data": {
|
| 131 |
+
"text/plain": [
|
| 132 |
+
"{'hub_id': 'llamafactory/glaive_toolcall_en',\n",
|
| 133 |
+
" 'likes': 1,\n",
|
| 134 |
+
" 'downloads': 1151,\n",
|
| 135 |
+
" 'tags': ['task_categories:text-generation',\n",
|
| 136 |
+
" 'task_categories:question-answering',\n",
|
| 137 |
+
" 'language:en',\n",
|
| 138 |
+
" 'license:apache-2.0',\n",
|
| 139 |
+
" 'size_categories:1K<n<10K',\n",
|
| 140 |
+
" 'json',\n",
|
| 141 |
+
" 'text',\n",
|
| 142 |
+
" 'datasets',\n",
|
| 143 |
+
" 'mlcroissant',\n",
|
| 144 |
+
" 'region:us',\n",
|
| 145 |
+
" 'llama-factory',\n",
|
| 146 |
+
" 'croissant'],\n",
|
| 147 |
+
" 'created_at': 1715955540,\n",
|
| 148 |
+
" 'last_modified': 1717785919,\n",
|
| 149 |
+
" 'license': ['apache-2.0'],\n",
|
| 150 |
+
" 'language': ['en'],\n",
|
| 151 |
+
" 'config_name': 'default',\n",
|
| 152 |
+
" 'column_names': ['conversations', 'tools'],\n",
|
| 153 |
+
" 'features': [{'name': 'conversations',\n",
|
| 154 |
+
" 'list': [{'name': 'from', 'dtype': 'string'},\n",
|
| 155 |
+
" {'name': 'value', 'dtype': 'string'}]},\n",
|
| 156 |
+
" {'name': 'tools', 'dtype': 'string'}],\n",
|
| 157 |
+
" 'match_count': 1}"
|
| 158 |
+
]
|
| 159 |
+
},
|
| 160 |
+
"metadata": {},
|
| 161 |
+
"execution_count": 6
|
| 162 |
+
}
|
| 163 |
+
]
|
| 164 |
+
},
|
| 165 |
+
{
|
| 166 |
+
"cell_type": "code",
|
| 167 |
+
"source": [
|
| 168 |
+
"from huggingface_hub import create_collection, add_collection_item"
|
| 169 |
+
],
|
| 170 |
+
"metadata": {
|
| 171 |
+
"id": "pXKtgF3r7GSK"
|
| 172 |
+
},
|
| 173 |
+
"execution_count": 9,
|
| 174 |
+
"outputs": []
|
| 175 |
+
},
|
| 176 |
+
{
|
| 177 |
+
"cell_type": "code",
|
| 178 |
+
"source": [
|
| 179 |
+
"collection = create_collection(\"Probably function calling datasets\", namespace=\"librarian-bots\",)"
|
| 180 |
+
],
|
| 181 |
+
"metadata": {
|
| 182 |
+
"id": "MzkGofqF7M0i"
|
| 183 |
+
},
|
| 184 |
+
"execution_count": 11,
|
| 185 |
+
"outputs": []
|
| 186 |
+
},
|
| 187 |
+
{
|
| 188 |
+
"cell_type": "code",
|
| 189 |
+
"source": [
|
| 190 |
+
"collection.slug"
|
| 191 |
+
],
|
| 192 |
+
"metadata": {
|
| 193 |
+
"colab": {
|
| 194 |
+
"base_uri": "https://localhost:8080/",
|
| 195 |
+
"height": 36
|
| 196 |
+
},
|
| 197 |
+
"id": "rAGoahvb7Ucp",
|
| 198 |
+
"outputId": "c5f7b158-85cb-49be-903f-7caaa98f7b74"
|
| 199 |
+
},
|
| 200 |
+
"execution_count": 12,
|
| 201 |
+
"outputs": [
|
| 202 |
+
{
|
| 203 |
+
"output_type": "execute_result",
|
| 204 |
+
"data": {
|
| 205 |
+
"text/plain": [
|
| 206 |
+
"'librarian-bots/probably-function-calling-datasets-6683d24da13a7bb7efee7464'"
|
| 207 |
+
],
|
| 208 |
+
"application/vnd.google.colaboratory.intrinsic+json": {
|
| 209 |
+
"type": "string"
|
| 210 |
+
}
|
| 211 |
+
},
|
| 212 |
+
"metadata": {},
|
| 213 |
+
"execution_count": 12
|
| 214 |
+
}
|
| 215 |
+
]
|
| 216 |
+
},
|
| 217 |
+
{
|
| 218 |
+
"cell_type": "code",
|
| 219 |
+
"source": [
|
| 220 |
+
"for item in results:\n",
|
| 221 |
+
" add_collection_item(collection.slug, item['hub_id'], item_type=\"dataset\")"
|
| 222 |
+
],
|
| 223 |
+
"metadata": {
|
| 224 |
+
"id": "LR6nJyCL7ZZK"
|
| 225 |
+
},
|
| 226 |
+
"execution_count": 13,
|
| 227 |
+
"outputs": []
|
| 228 |
+
}
|
| 229 |
+
]
|
| 230 |
+
}
|