.oO SearXNG Developer Documentation Oo.
Loading...
Searching...
No Matches
huggingface.py
Go to the documentation of this file.
1
# SPDX-License-Identifier: AGPL-3.0-or-later
2
"""`Hugging Face`_ search engine for SearXNG.
3
4
.. _Hugging Face: https://huggingface.co
5
6
Configuration
7
=============
8
9
The engine has the following additional settings:
10
11
- :py:obj:`huggingface_endpoint`
12
13
Configurations for endpoints:
14
15
.. code:: yaml
16
17
- name: huggingface
18
engine: huggingface
19
shortcut: hf
20
21
- name: huggingface datasets
22
huggingface_endpoint: datasets
23
engine: huggingface
24
shortcut: hfd
25
26
- name: huggingface spaces
27
huggingface_endpoint: spaces
28
engine: huggingface
29
shortcut: hfs
30
31
Implementations
32
===============
33
34
"""
35
36
from
urllib.parse
import
urlencode
37
from
datetime
import
datetime
38
39
from
searx.exceptions
import
SearxEngineAPIException
40
from
searx.utils
import
html_to_text
41
from
searx.result_types
import
EngineResults, MainResult
42
43
about = {
44
"website"
:
"https://huggingface.co/"
,
45
"wikidata_id"
:
"Q108943604"
,
46
"official_api_documentation"
:
"https://huggingface.co/docs/hub/en/api"
,
47
"use_official_api"
:
True
,
48
"require_api_key"
:
False
,
49
"results"
:
"JSON"
,
50
}
51
52
categories = [
'it'
,
'repos'
]
53
54
base_url =
"https://huggingface.co"
55
56
huggingface_endpoint =
'models'
57
"""Hugging Face supports datasets, models, spaces as search endpoint.
58
59
- ``datasets``: search for datasets
60
- ``models``: search for models
61
- ``spaces``: search for spaces
62
"""
63
64
65
def
init
(_):
66
if
huggingface_endpoint
not
in
(
'datasets'
,
'models'
,
'spaces'
):
67
raise
SearxEngineAPIException
(f
"Unsupported Hugging Face endpoint: {huggingface_endpoint}"
)
68
69
70
def
request
(query, params):
71
query_params = {
72
"direction"
: -1,
73
"search"
: query,
74
}
75
76
params[
"url"
] = f
"{base_url}/api/{huggingface_endpoint}?{urlencode(query_params)}"
77
78
return
params
79
80
81
def
response
(resp) -> EngineResults:
82
results =
EngineResults
()
83
84
data = resp.json()
85
86
for
entry
in
data:
87
if
huggingface_endpoint !=
'models'
:
88
url = f
"{base_url}/{huggingface_endpoint}/{entry['id']}"
89
else
:
90
url = f
"{base_url}/{entry['id']}"
91
92
published_date =
None
93
try
:
94
published_date = datetime.strptime(entry[
"createdAt"
],
"%Y-%m-%dT%H:%M:%S.%fZ"
)
95
except
(ValueError, TypeError):
96
pass
97
98
contents = []
99
if
entry.get(
"likes"
):
100
contents.append(f
"Likes: {entry['likes']}"
)
101
if
entry.get(
"downloads"
):
102
contents.append(f
"Downloads: {entry['downloads']:,}"
)
103
if
entry.get(
"tags"
):
104
contents.append(f
"Tags: {', '.join(entry['tags'])}"
)
105
if
entry.get(
"description"
):
106
contents.append(f
"Description: {entry['description']}"
)
107
108
item =
MainResult
(
109
title=entry[
"id"
],
110
content=html_to_text(
" | "
.join(contents)),
111
url=url,
112
publishedDate=published_date,
113
)
114
results.add(item)
115
116
return
results
searx.exceptions.SearxEngineAPIException
Definition
exceptions.py:54
searx.result_types.EngineResults
Definition
__init__.py:51
searx.result_types._base.MainResult
Definition
_base.py:344
searx.engines.huggingface.init
init(_)
Definition
huggingface.py:65
searx.engines.huggingface.response
EngineResults response(resp)
Definition
huggingface.py:81
searx.engines.huggingface.request
request(query, params)
Definition
huggingface.py:70
searx.exceptions
Definition
exceptions.py:1
searx.result_types
Definition
__init__.py:1
searx.utils
Definition
utils.py:1
searxng
searx
engines
huggingface.py
Generated on Thu Apr 24 2025 22:38:20 for .oO SearXNG Developer Documentation Oo. by
1.13.2