nouamanetazi HF staff commited on
Commit
fabd282
1 Parent(s): cd6abe6

rename app.py

Browse files
Files changed (2) hide show
  1. app.py +145 -0
  2. streamlit_app.py +0 -60
app.py ADDED
@@ -0,0 +1,145 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from pages import search_engine_page, document_page
3
+ from st_utils import bm25_search, semantic_search, hf_api, paginator
4
+ from huggingface_hub import ModelSearchArguments
5
+ import webbrowser
6
+ from numerize.numerize import numerize
7
+
8
+ st.set_page_config(
9
+ page_title="HuggingFace Search Engine",
10
+ page_icon="🔎",
11
+ layout="wide",
12
+ initial_sidebar_state="auto",
13
+ # menu_items={
14
+ # "Get Help": "https://www.extremelycoolapp.com/help",
15
+ # "Report a bug": "https://www.extremelycoolapp.com/bug",
16
+ # "About": "# This is a header. This is an *extremely* cool app!",
17
+ # },
18
+ )
19
+
20
+ ### SIDEBAR
21
+ search_backend = st.sidebar.selectbox(
22
+ "Search method",
23
+ ["semantic", "bm25", "hfapi"],
24
+ format_func=lambda x: {"hfapi": "Keyword search", "bm25": "BM25 search", "semantic": "Semantic Search"}[x],
25
+ )
26
+ limit_results = st.sidebar.number_input("Limit results", min_value=0, value=10)
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, filters)
53
+ elif search_backend == "semantic":
54
+ res = semantic_search(search_query, limit_results, filters)
55
+ elif search_backend == "bm25":
56
+ res = bm25_search(search_query, limit_results, 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"]))
84
+ col3.metric("N° likes", numerize(hit["likes"]))
85
+ st.button(
86
+ f"View model on 🤗",
87
+ on_click=lambda hit=hit: webbrowser.open(f"https://huggingface.co/{hit['modelId']}"),
88
+ key=f"{i}-{hit['modelId']}",
89
+ )
90
+ st.write(f"**Tags:** {'&nbsp;&nbsp;•&nbsp;&nbsp;'.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
+
116
+ st.markdown(
117
+ """<style>
118
+ a:link , a:visited{
119
+ color: blue;
120
+ background-color: transparent;
121
+ text-decoration: underline;
122
+ }
123
+
124
+ a:hover, a:active {
125
+ color: red;
126
+ background-color: transparent;
127
+ text-decoration: underline;
128
+ }
129
+
130
+ .footer {
131
+ # position: fixed;
132
+ left: 0;
133
+ bottom: 0;
134
+ width: 100%;
135
+ background-color: white;
136
+ color: black;
137
+ text-align: center;
138
+ }
139
+ </style>
140
+ <div class="footer">
141
+ <p>Made with ❤️ by <b>Nouamane Tazi</b></p>
142
+ </div>
143
+ """,
144
+ unsafe_allow_html=True,
145
+ )
streamlit_app.py DELETED
@@ -1,60 +0,0 @@
1
- import streamlit as st
2
- from pages import search_engine_page, document_page
3
-
4
-
5
- if "selected_record" not in st.session_state:
6
- st.session_state["selected_record"] = None
7
-
8
-
9
- def set_record(record):
10
- st.session_state["selected_record"] = record
11
-
12
-
13
- if not st.session_state["selected_record"]: # search engine page
14
- st.set_page_config(
15
- page_title="HuggingFace Search Engine",
16
- page_icon="🔎",
17
- layout="wide",
18
- initial_sidebar_state="auto",
19
- # menu_items={
20
- # "Get Help": "https://www.extremelycoolapp.com/help",
21
- # "Report a bug": "https://www.extremelycoolapp.com/bug",
22
- # "About": "# This is a header. This is an *extremely* cool app!",
23
- # },
24
- )
25
- search_engine_page()
26
-
27
- else: # a record has been selected
28
- document_page()
29
-
30
-
31
- st.markdown(
32
- """<style>
33
- a:link , a:visited{
34
- color: blue;
35
- background-color: transparent;
36
- text-decoration: underline;
37
- }
38
-
39
- a:hover, a:active {
40
- color: red;
41
- background-color: transparent;
42
- text-decoration: underline;
43
- }
44
-
45
- .footer {
46
- # position: fixed;
47
- left: 0;
48
- bottom: 0;
49
- width: 100%;
50
- background-color: white;
51
- color: black;
52
- text-align: center;
53
- }
54
- </style>
55
- <div class="footer">
56
- <p>Made with ❤️ by <b>Nouamane Tazi</b></p>
57
- </div>
58
- """,
59
- unsafe_allow_html=True,
60
- )