|
72 | 72 | from ._eval_results import EvalResultEntry, parse_eval_result_entries |
73 | 73 | from ._inference_endpoints import InferenceEndpoint, InferenceEndpointScalingMetric, InferenceEndpointType |
74 | 74 | from ._jobs_api import JobHardware, JobInfo, JobSpec, ScheduledJobInfo, _create_job_spec |
75 | | -from ._space_api import SpaceHardware, SpaceRuntime, SpaceStorage, SpaceVariable, Volume |
| 75 | +from ._space_api import SpaceHardware, SpaceRuntime, SpaceSearchResult, SpaceStorage, SpaceVariable, Volume |
76 | 76 | from ._upload_large_folder import upload_large_folder_internal |
77 | 77 | from .community import ( |
78 | 78 | Discussion, |
@@ -2905,6 +2905,65 @@ def list_spaces( |
2905 | 2905 | item["siblings"] = None |
2906 | 2906 | yield SpaceInfo(**item) |
2907 | 2907 |
|
| 2908 | + @validate_hf_hub_args |
| 2909 | + def search_spaces( |
| 2910 | + self, |
| 2911 | + query: str, |
| 2912 | + *, |
| 2913 | + filter: str | Iterable[str] | None = None, |
| 2914 | + sdk: str | list[str] | None = None, |
| 2915 | + include_non_running: bool = False, |
| 2916 | + token: bool | str | None = None, |
| 2917 | + ) -> Iterable[SpaceSearchResult]: |
| 2918 | + """Search Spaces on the Hub using semantic search. |
| 2919 | + |
| 2920 | + This endpoint uses semantic search (embedding-based) for multi-word queries |
| 2921 | + and full-text search for single-word queries. |
| 2922 | + |
| 2923 | + Args: |
| 2924 | + query (`str`): |
| 2925 | + The search query string. |
| 2926 | + filter (`str` or `Iterable[str]`, *optional*): |
| 2927 | + A string tag or list of tags to filter by. |
| 2928 | + sdk (`str` or `list[str]`, *optional*): |
| 2929 | + Filter by SDK (e.g. `"gradio"`, `"docker"`, `"static"`). |
| 2930 | + include_non_running (`bool`, *optional*): |
| 2931 | + Whether to include non-running Spaces in results. Defaults to `False`. |
| 2932 | + token (`bool` or `str`, *optional*): |
| 2933 | + A valid user access token (string). Defaults to the locally saved |
| 2934 | + token, which is the recommended method for authentication (see |
| 2935 | + https://huggingface.co/docs/huggingface_hub/quick-start#authentication). |
| 2936 | + To disable authentication, pass `False`. |
| 2937 | + |
| 2938 | + Returns: |
| 2939 | + `Iterable[SpaceSearchResult]`: an iterable of [`SpaceSearchResult`] objects. |
| 2940 | + |
| 2941 | + Example: |
| 2942 | + ```python |
| 2943 | + >>> from huggingface_hub import HfApi |
| 2944 | + >>> api = HfApi() |
| 2945 | + >>> results = list(api.search_spaces("generate image")) |
| 2946 | + >>> results[0].id |
| 2947 | + 'mrfakename/Z-Image-Turbo' |
| 2948 | + >>> results[0].ai_category |
| 2949 | + 'Image Generation' |
| 2950 | + ``` |
| 2951 | + """ |
| 2952 | + path = f"{self.endpoint}/api/spaces/semantic-search" |
| 2953 | + headers = self._build_hf_headers(token=token) |
| 2954 | + params: dict[str, Any] = {"q": query} |
| 2955 | + if filter is not None: |
| 2956 | + params["filter"] = filter |
| 2957 | + if sdk is not None: |
| 2958 | + params["sdk"] = sdk |
| 2959 | + if include_non_running: |
| 2960 | + params["includeNonRunning"] = True |
| 2961 | + |
| 2962 | + r = get_session().get(path, headers=headers, params=params) |
| 2963 | + hf_raise_for_status(r) |
| 2964 | + for item in r.json(): |
| 2965 | + yield SpaceSearchResult(item) |
| 2966 | + |
2908 | 2967 | @validate_hf_hub_args |
2909 | 2968 | def unlike( |
2910 | 2969 | self, |
@@ -13581,6 +13640,7 @@ def get_local_safetensors_metadata(path: str | Path) -> SafetensorsRepoMetadata: |
13581 | 13640 | get_dataset_leaderboard = api.get_dataset_leaderboard |
13582 | 13641 |
|
13583 | 13642 | list_spaces = api.list_spaces |
| 13643 | +search_spaces = api.search_spaces |
13584 | 13644 | space_info = api.space_info |
13585 | 13645 |
|
13586 | 13646 | kernel_info = api.kernel_info |
|
0 commit comments