TenPoisk
commited on
Commit
·
2eb1ea5
1
Parent(s):
7d77553
Delete app.py
Browse files
app.py
DELETED
@@ -1,118 +0,0 @@
|
|
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 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|