ehristoforu
commited on
Commit
•
1f8505f
0
Parent(s):
Duplicate from TNR-5/lib
Browse files- .gitattributes +29 -0
- .gitignore +145 -0
- README.md +39 -0
- app.py +118 -0
- embeddings/multi-qa-MiniLM-L6-cos-v1-embeddings.pt +3 -0
- hf_data/models.jsonl +3 -0
- hf_data/passages.jsonl +3 -0
- requirements.txt +6 -0
- st_utils.py +126 -0
.gitattributes
ADDED
@@ -0,0 +1,29 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
*.pkl filter=lfs diff=lfs merge=lfs -text
|
2 |
+
*.jsonl filter=lfs diff=lfs merge=lfs -text
|
3 |
+
*.7z filter=lfs diff=lfs merge=lfs -text
|
4 |
+
*.arrow filter=lfs diff=lfs merge=lfs -text
|
5 |
+
*.bin filter=lfs diff=lfs merge=lfs -text
|
6 |
+
*.bin.* filter=lfs diff=lfs merge=lfs -text
|
7 |
+
*.bz2 filter=lfs diff=lfs merge=lfs -text
|
8 |
+
*.ftz filter=lfs diff=lfs merge=lfs -text
|
9 |
+
*.gz filter=lfs diff=lfs merge=lfs -text
|
10 |
+
*.h5 filter=lfs diff=lfs merge=lfs -text
|
11 |
+
*.joblib filter=lfs diff=lfs merge=lfs -text
|
12 |
+
*.lfs.* filter=lfs diff=lfs merge=lfs -text
|
13 |
+
*.model filter=lfs diff=lfs merge=lfs -text
|
14 |
+
*.msgpack filter=lfs diff=lfs merge=lfs -text
|
15 |
+
*.onnx filter=lfs diff=lfs merge=lfs -text
|
16 |
+
*.ot filter=lfs diff=lfs merge=lfs -text
|
17 |
+
*.parquet filter=lfs diff=lfs merge=lfs -text
|
18 |
+
*.pb filter=lfs diff=lfs merge=lfs -text
|
19 |
+
*.pt filter=lfs diff=lfs merge=lfs -text
|
20 |
+
*.pth filter=lfs diff=lfs merge=lfs -text
|
21 |
+
*.rar filter=lfs diff=lfs merge=lfs -text
|
22 |
+
saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
23 |
+
*.tar.* filter=lfs diff=lfs merge=lfs -text
|
24 |
+
*.tflite filter=lfs diff=lfs merge=lfs -text
|
25 |
+
*.tgz filter=lfs diff=lfs merge=lfs -text
|
26 |
+
*.xz filter=lfs diff=lfs merge=lfs -text
|
27 |
+
*.zip filter=lfs diff=lfs merge=lfs -text
|
28 |
+
*.zstandard filter=lfs diff=lfs merge=lfs -text
|
29 |
+
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
.gitignore
ADDED
@@ -0,0 +1,145 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Byte-compiled / optimized / DLL files
|
2 |
+
__pycache__/
|
3 |
+
*.py[cod]
|
4 |
+
*$py.class
|
5 |
+
|
6 |
+
# C extensions
|
7 |
+
*.so
|
8 |
+
|
9 |
+
# Distribution / packaging
|
10 |
+
.Python
|
11 |
+
build/
|
12 |
+
develop-eggs/
|
13 |
+
dist/
|
14 |
+
downloads/
|
15 |
+
eggs/
|
16 |
+
.eggs/
|
17 |
+
lib/
|
18 |
+
lib64/
|
19 |
+
parts/
|
20 |
+
sdist/
|
21 |
+
var/
|
22 |
+
wheels/
|
23 |
+
share/python-wheels/
|
24 |
+
*.egg-info/
|
25 |
+
.installed.cfg
|
26 |
+
*.egg
|
27 |
+
MANIFEST
|
28 |
+
|
29 |
+
# PyInstaller
|
30 |
+
# Usually these files are written by a python script from a template
|
31 |
+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
|
32 |
+
*.manifest
|
33 |
+
*.spec
|
34 |
+
|
35 |
+
# Installer logs
|
36 |
+
pip-log.txt
|
37 |
+
pip-delete-this-directory.txt
|
38 |
+
|
39 |
+
# Unit test / coverage reports
|
40 |
+
htmlcov/
|
41 |
+
.tox/
|
42 |
+
.nox/
|
43 |
+
.coverage
|
44 |
+
.coverage.*
|
45 |
+
.cache
|
46 |
+
nosetests.xml
|
47 |
+
coverage.xml
|
48 |
+
*.cover
|
49 |
+
*.py,cover
|
50 |
+
.hypothesis/
|
51 |
+
.pytest_cache/
|
52 |
+
cover/
|
53 |
+
|
54 |
+
# Translations
|
55 |
+
*.mo
|
56 |
+
*.pot
|
57 |
+
|
58 |
+
# Django stuff:
|
59 |
+
*.log
|
60 |
+
local_settings.py
|
61 |
+
db.sqlite3
|
62 |
+
db.sqlite3-journal
|
63 |
+
|
64 |
+
# Flask stuff:
|
65 |
+
instance/
|
66 |
+
.webassets-cache
|
67 |
+
|
68 |
+
# Scrapy stuff:
|
69 |
+
.scrapy
|
70 |
+
|
71 |
+
# Sphinx documentation
|
72 |
+
docs/_build/
|
73 |
+
|
74 |
+
# PyBuilder
|
75 |
+
.pybuilder/
|
76 |
+
target/
|
77 |
+
|
78 |
+
# Jupyter Notebook
|
79 |
+
.ipynb_checkpoints
|
80 |
+
|
81 |
+
# IPython
|
82 |
+
profile_default/
|
83 |
+
ipython_config.py
|
84 |
+
|
85 |
+
# pyenv
|
86 |
+
# For a library or package, you might want to ignore these files since the code is
|
87 |
+
# intended to run in multiple environments; otherwise, check them in:
|
88 |
+
# .python-version
|
89 |
+
|
90 |
+
# pipenv
|
91 |
+
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
|
92 |
+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
|
93 |
+
# having no cross-platform support, pipenv may install dependencies that don't work, or not
|
94 |
+
# install all needed dependencies.
|
95 |
+
#Pipfile.lock
|
96 |
+
|
97 |
+
# poetry
|
98 |
+
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
|
99 |
+
# This is especially recommended for binary packages to ensure reproducibility, and is more
|
100 |
+
# commonly ignored for libraries.
|
101 |
+
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
|
102 |
+
#poetry.lock
|
103 |
+
|
104 |
+
# PEP 582; used by e.g. github.com/David-OConnor/pyflow
|
105 |
+
__pypackages__/
|
106 |
+
|
107 |
+
# Celery stuff
|
108 |
+
celerybeat-schedule
|
109 |
+
celerybeat.pid
|
110 |
+
|
111 |
+
# SageMath parsed files
|
112 |
+
*.sage.py
|
113 |
+
|
114 |
+
# Environments
|
115 |
+
.env
|
116 |
+
.venv
|
117 |
+
env/
|
118 |
+
venv/
|
119 |
+
ENV/
|
120 |
+
env.bak/
|
121 |
+
venv.bak/
|
122 |
+
|
123 |
+
# Spyder project settings
|
124 |
+
.spyderproject
|
125 |
+
.spyproject
|
126 |
+
|
127 |
+
# Rope project settings
|
128 |
+
.ropeproject
|
129 |
+
|
130 |
+
# mkdocs documentation
|
131 |
+
/site
|
132 |
+
|
133 |
+
# mypy
|
134 |
+
.mypy_cache/
|
135 |
+
.dmypy.json
|
136 |
+
dmypy.json
|
137 |
+
|
138 |
+
# Pyre type checker
|
139 |
+
.pyre/
|
140 |
+
|
141 |
+
# pytype static type analyzer
|
142 |
+
.pytype/
|
143 |
+
|
144 |
+
# Cython debug symbols
|
145 |
+
cython_debug/
|
README.md
ADDED
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
---
|
2 |
+
title: HuggingFace Search Engine
|
3 |
+
emoji: 🔎🤗
|
4 |
+
colorFrom: blue
|
5 |
+
colorTo: gray
|
6 |
+
sdk: streamlit
|
7 |
+
app_file: app.py
|
8 |
+
pinned: true
|
9 |
+
duplicated_from: TNR-5/lib
|
10 |
+
---
|
11 |
+
# Configuration
|
12 |
+
|
13 |
+
`title`: _string_
|
14 |
+
Display title for the Space.
|
15 |
+
`emoji`: _string_
|
16 |
+
Space emoji (emoji-only character allowed)
|
17 |
+
`colorFrom`: _string_
|
18 |
+
Color for Thumbnail gradient (red, yellow, green, blue, indigo, purple, pink, gray)
|
19 |
+
`colorTo`: _string_
|
20 |
+
Color for Thumbnail gradient (red, yellow, green, blue, indigo, purple, pink, gray)
|
21 |
+
`sdk`: _string_
|
22 |
+
Can be either `gradio`, `streamlit`, or `static`
|
23 |
+
`sdk_version` : _string_
|
24 |
+
Only applicable for `streamlit` SDK.
|
25 |
+
See [doc](https://hf.co/docs/hub/spaces) for more info on supported versions.
|
26 |
+
|
27 |
+
`app_file`: _string_
|
28 |
+
Path to your main application file (which contains either `gradio` or `streamlit` Python code, or `static` html code).
|
29 |
+
Path is relative to the root of the repository.
|
30 |
+
|
31 |
+
`models`: _List[string]_
|
32 |
+
HF model IDs (like "gpt2" or "deepset/roberta-base-squad2") used in the Space.
|
33 |
+
Will be parsed automatically from your code if not specified here.
|
34 |
+
|
35 |
+
`datasets`: _List[string]_
|
36 |
+
HF dataset IDs (like "common_voice" or "oscar-corpus/OSCAR-2109") used in the Space.
|
37 |
+
Will be parsed automatically from your code if not specified here.
|
38 |
+
`pinned`: _boolean_
|
39 |
+
Whether the Space stays on top of your list.
|
app.py
ADDED
@@ -0,0 +1,118 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from st_utils import bm25_search, semantic_search, hf_api, paginator
|
3 |
+
from huggingface_hub import ModelSearchArguments
|
4 |
+
import webbrowser
|
5 |
+
from numerize.numerize import numerize
|
6 |
+
import math
|
7 |
+
|
8 |
+
st.set_page_config(
|
9 |
+
page_title="HF Search Engine",
|
10 |
+
page_icon="🔎",
|
11 |
+
layout="wide",
|
12 |
+
initial_sidebar_state="auto",
|
13 |
+
)
|
14 |
+
|
15 |
+
### SIDEBAR
|
16 |
+
search_backend = st.sidebar.selectbox(
|
17 |
+
"Search method",
|
18 |
+
["semantic", "bm25", "hfapi"],
|
19 |
+
format_func=lambda x: {"hfapi": "Keyword search", "bm25": "BM25 search", "semantic": "Semantic Search"}[x],
|
20 |
+
)
|
21 |
+
limit_results = int(st.sidebar.number_input("Limit results", min_value=0, value=10))
|
22 |
+
sort_by = st.sidebar.selectbox(
|
23 |
+
"Sort by",
|
24 |
+
[None, "downloads", "likes", "lastModified"],
|
25 |
+
format_func=lambda x: {None: "Relevance", "downloads": "Most downloads", "likes": "Most likes", "lastModified": "Recently updated"}[x],
|
26 |
+
)
|
27 |
+
|
28 |
+
st.sidebar.markdown("# Filters")
|
29 |
+
args = ModelSearchArguments()
|
30 |
+
library = st.sidebar.multiselect(
|
31 |
+
"Library", args.library.values(), format_func=lambda x: {v: k for k, v in args.library.items()}[x]
|
32 |
+
)
|
33 |
+
task = st.sidebar.multiselect(
|
34 |
+
"Task", args.pipeline_tag.values(), format_func=lambda x: {v: k for k, v in args.pipeline_tag.items()}[x]
|
35 |
+
)
|
36 |
+
|
37 |
+
### MAIN PAGE
|
38 |
+
st.markdown(
|
39 |
+
"<h1 style='text-align: center; '>🔎🤗 HF Search Engine</h1>",
|
40 |
+
unsafe_allow_html=True,
|
41 |
+
)
|
42 |
+
|
43 |
+
# Search bar
|
44 |
+
search_query = st.text_input("Search for a model in HuggingFace", value="", max_chars=None, key=None, type="default")
|
45 |
+
|
46 |
+
if search_query != "":
|
47 |
+
filters = {
|
48 |
+
"library": library,
|
49 |
+
"task": task,
|
50 |
+
}
|
51 |
+
if search_backend == "hfapi":
|
52 |
+
res = hf_api(search_query, limit_results, sort_by, filters)
|
53 |
+
elif search_backend == "semantic":
|
54 |
+
res = semantic_search(search_query, limit_results, sort_by, filters)
|
55 |
+
elif search_backend == "bm25":
|
56 |
+
res = bm25_search(search_query, limit_results, sort_by, filters)
|
57 |
+
hit_list, hits_count = res["hits"], res["count"]
|
58 |
+
hit_list = [
|
59 |
+
{
|
60 |
+
"modelId": hit["modelId"],
|
61 |
+
"tags": hit["tags"],
|
62 |
+
"downloads": hit["downloads"],
|
63 |
+
"likes": hit["likes"],
|
64 |
+
"readme": hit.get("readme", None),
|
65 |
+
}
|
66 |
+
for hit in hit_list
|
67 |
+
]
|
68 |
+
|
69 |
+
if hit_list:
|
70 |
+
st.write(f"Search results ({hits_count}):")
|
71 |
+
|
72 |
+
if hits_count > 100:
|
73 |
+
shown_results = 100
|
74 |
+
else:
|
75 |
+
shown_results = hits_count
|
76 |
+
|
77 |
+
for i, hit in paginator(
|
78 |
+
f"Select results (showing {shown_results} of {hits_count} results)",
|
79 |
+
hit_list,
|
80 |
+
):
|
81 |
+
col1, col2, col3 = st.columns([5, 1, 1])
|
82 |
+
col1.metric("Model", hit["modelId"])
|
83 |
+
col2.metric("N° downloads", numerize(hit["downloads"]) if hit["downloads"] and not math.isnan(hit["downloads"]) else "N/A")
|
84 |
+
col3.metric("N° likes", numerize(hit["likes"]) if hit["likes"] and not math.isnan(hit["likes"]) else "N/A")
|
85 |
+
st.button(
|
86 |
+
f"View model on 🤗",
|
87 |
+
on_click=lambda hit=hit: webbrowser.open(f"https://huggingface.co/{hit['modelId']}", new=2),
|
88 |
+
key=f"{i}-{hit['modelId']}",
|
89 |
+
)
|
90 |
+
st.write(f"**Tags:** {' • '.join(hit['tags'])}")
|
91 |
+
|
92 |
+
if hit["readme"]:
|
93 |
+
with st.expander("See README"):
|
94 |
+
st.write(hit["readme"])
|
95 |
+
|
96 |
+
# TODO: embed huggingface spaces
|
97 |
+
# import streamlit.components.v1 as components
|
98 |
+
# components.html(
|
99 |
+
# f"""
|
100 |
+
# <link rel="stylesheet" href="https://gradio.s3-us-west-2.amazonaws.com/2.6.2/static/bundle.css">
|
101 |
+
# <div id="target"></div>
|
102 |
+
# <script src="https://gradio.s3-us-west-2.amazonaws.com/2.6.2/static/bundle.js"></script>
|
103 |
+
# <script>
|
104 |
+
# launchGradioFromSpaces("abidlabs/question-answering", "#target")
|
105 |
+
# </script>
|
106 |
+
# """,
|
107 |
+
# height=400,
|
108 |
+
# )
|
109 |
+
|
110 |
+
st.markdown("---")
|
111 |
+
|
112 |
+
else:
|
113 |
+
st.write(f"No Search results, please try again with different keywords")
|
114 |
+
|
115 |
+
st.markdown(
|
116 |
+
"<h6 style='text-align: center; color: #808080;'>Made with ❤️ By <a href='https://github.com/NouamaneTazi'>Nouamane</a> - Checkout complete project <a href='https://github.com/NouamaneTazi/hf_search'>here</a></h6>",
|
117 |
+
unsafe_allow_html=True,
|
118 |
+
)
|
embeddings/multi-qa-MiniLM-L6-cos-v1-embeddings.pt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:c95fa4fe96c9e032d94d9c5af0d83667e088fe65c1bcfe5e40c18cc0096ae73c
|
3 |
+
size 486097131
|
hf_data/models.jsonl
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:23d9543aa28ea8b2205c690029c9b96f5bc76bd3e2b7c38cbc21c2113e952a15
|
3 |
+
size 72808045
|
hf_data/passages.jsonl
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:dbf33260e5185e04b42923e15039cd0cc081f407d6a4ac3ad35b08007b9a54e3
|
3 |
+
size 71939324
|
requirements.txt
ADDED
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
pandas
|
2 |
+
streamlit
|
3 |
+
huggingface_hub
|
4 |
+
numerize
|
5 |
+
pbr
|
6 |
+
git+https://github.com/NouamaneTazi/[email protected]
|
st_utils.py
ADDED
@@ -0,0 +1,126 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import json
|
2 |
+
from huggingface_hub import HfApi, ModelFilter, DatasetFilter, ModelSearchArguments
|
3 |
+
from pprint import pprint
|
4 |
+
from hf_search import HFSearch
|
5 |
+
import streamlit as st
|
6 |
+
import itertools
|
7 |
+
|
8 |
+
from pbr.version import VersionInfo
|
9 |
+
print("hf_search version:", VersionInfo('hf_search').version_string())
|
10 |
+
|
11 |
+
hf_search = HFSearch(top_k=200)
|
12 |
+
|
13 |
+
@st.cache
|
14 |
+
def hf_api(query, limit=5, sort=None, filters={}):
|
15 |
+
print("query", query)
|
16 |
+
print("filters", filters)
|
17 |
+
print("limit", limit)
|
18 |
+
print("sort", sort)
|
19 |
+
|
20 |
+
api = HfApi()
|
21 |
+
filt = ModelFilter(
|
22 |
+
task=filters["task"],
|
23 |
+
library=filters["library"],
|
24 |
+
)
|
25 |
+
models = api.list_models(search=query, filter=filt, limit=limit, sort=sort, full=True)
|
26 |
+
hits = []
|
27 |
+
for model in models:
|
28 |
+
model = model.__dict__
|
29 |
+
hits.append(
|
30 |
+
{
|
31 |
+
"modelId": model.get("modelId"),
|
32 |
+
"tags": model.get("tags"),
|
33 |
+
"downloads": model.get("downloads"),
|
34 |
+
"likes": model.get("likes"),
|
35 |
+
}
|
36 |
+
)
|
37 |
+
count = len(hits)
|
38 |
+
if len(hits) > limit:
|
39 |
+
hits = hits[:limit]
|
40 |
+
return {"hits": hits, "count": count}
|
41 |
+
|
42 |
+
|
43 |
+
@st.cache
|
44 |
+
def semantic_search(query, limit=5, sort=None, filters={}):
|
45 |
+
print("query", query)
|
46 |
+
print("filters", filters)
|
47 |
+
print("limit", limit)
|
48 |
+
print("sort", sort)
|
49 |
+
|
50 |
+
hits = hf_search.search(query=query, method="retrieve & rerank", limit=limit, sort=sort, filters=filters)
|
51 |
+
hits = [
|
52 |
+
{
|
53 |
+
"modelId": hit["modelId"],
|
54 |
+
"tags": hit["tags"],
|
55 |
+
"downloads": hit["downloads"],
|
56 |
+
"likes": hit["likes"],
|
57 |
+
"readme": hit.get("readme", None),
|
58 |
+
}
|
59 |
+
for hit in hits
|
60 |
+
]
|
61 |
+
return {"hits": hits, "count": len(hits)}
|
62 |
+
|
63 |
+
|
64 |
+
@st.cache
|
65 |
+
def bm25_search(query, limit=5, sort=None, filters={}):
|
66 |
+
print("query", query)
|
67 |
+
print("filters", filters)
|
68 |
+
print("limit", limit)
|
69 |
+
print("sort", sort)
|
70 |
+
|
71 |
+
# TODO: filters
|
72 |
+
hits = hf_search.search(query=query, method="bm25", limit=limit, sort=sort, filters=filters)
|
73 |
+
hits = [
|
74 |
+
{
|
75 |
+
"modelId": hit["modelId"],
|
76 |
+
"tags": hit["tags"],
|
77 |
+
"downloads": hit["downloads"],
|
78 |
+
"likes": hit["likes"],
|
79 |
+
"readme": hit.get("readme", None),
|
80 |
+
}
|
81 |
+
for hit in hits
|
82 |
+
]
|
83 |
+
hits = [
|
84 |
+
hits[i] for i in range(len(hits)) if hits[i]["modelId"] not in [h["modelId"] for h in hits[:i]]
|
85 |
+
] # unique hits
|
86 |
+
return {"hits": hits, "count": len(hits)}
|
87 |
+
|
88 |
+
|
89 |
+
def paginator(label, articles, articles_per_page=10, on_sidebar=True):
|
90 |
+
# https://gist.github.com/treuille/2ce0acb6697f205e44e3e0f576e810b7
|
91 |
+
"""Lets the user paginate a set of article.
|
92 |
+
Parameters
|
93 |
+
----------
|
94 |
+
label : str
|
95 |
+
The label to display over the pagination widget.
|
96 |
+
article : Iterator[Any]
|
97 |
+
The articles to display in the paginator.
|
98 |
+
articles_per_page: int
|
99 |
+
The number of articles to display per page.
|
100 |
+
on_sidebar: bool
|
101 |
+
Whether to display the paginator widget on the sidebar.
|
102 |
+
|
103 |
+
Returns
|
104 |
+
-------
|
105 |
+
Iterator[Tuple[int, Any]]
|
106 |
+
An iterator over *only the article on that page*, including
|
107 |
+
the item's index.
|
108 |
+
"""
|
109 |
+
|
110 |
+
# Figure out where to display the paginator
|
111 |
+
if on_sidebar:
|
112 |
+
location = st.sidebar.empty()
|
113 |
+
else:
|
114 |
+
location = st.empty()
|
115 |
+
|
116 |
+
# Display a pagination selectbox in the specified location.
|
117 |
+
articles = list(articles)
|
118 |
+
n_pages = (len(articles) - 1) // articles_per_page + 1
|
119 |
+
page_format_func = lambda i: f"Results {i*10} to {i*10 +10 -1}"
|
120 |
+
page_number = location.selectbox(label, range(n_pages), format_func=page_format_func)
|
121 |
+
|
122 |
+
# Iterate over the articles in the page to let the user display them.
|
123 |
+
min_index = page_number * articles_per_page
|
124 |
+
max_index = min_index + articles_per_page
|
125 |
+
|
126 |
+
return itertools.islice(enumerate(articles), min_index, max_index)
|