hf-search / pages /search_engine.py
nouamanetazi's picture
nouamanetazi HF staff
initial commit
dc1b8a3
raw
history blame
No virus
5.72 kB
import os
import re
import json
import datetime
import itertools
import requests
from PIL import Image
import base64
import streamlit as st
from huggingface_hub import ModelSearchArguments
import webbrowser
from numerize.numerize import numerize
def paginator(label, articles, articles_per_page=10, on_sidebar=True):
# https://gist.github.com/treuille/2ce0acb6697f205e44e3e0f576e810b7
"""Lets the user paginate a set of article.
Parameters
----------
label : str
The label to display over the pagination widget.
article : Iterator[Any]
The articles to display in the paginator.
articles_per_page: int
The number of articles to display per page.
on_sidebar: bool
Whether to display the paginator widget on the sidebar.
Returns
-------
Iterator[Tuple[int, Any]]
An iterator over *only the article on that page*, including
the item's index.
"""
# Figure out where to display the paginator
if on_sidebar:
location = st.sidebar.empty()
else:
location = st.empty()
# Display a pagination selectbox in the specified location.
articles = list(articles)
n_pages = (len(articles) - 1) // articles_per_page + 1
page_format_func = lambda i: f"Results {i*10} to {i*10 +10 -1}"
page_number = location.selectbox(label, range(n_pages), format_func=page_format_func)
# Iterate over the articles in the page to let the user display them.
min_index = page_number * articles_per_page
max_index = min_index + articles_per_page
return itertools.islice(enumerate(articles), min_index, max_index)
def page():
st.set_page_config(
page_title="HF Search Engine",
page_icon="πŸ”Ž",
layout="wide",
initial_sidebar_state="auto",
# menu_items={
# "Get Help": "https://www.extremelycoolapp.com/help",
# "Report a bug": "https://www.extremelycoolapp.com/bug",
# "About": "# This is a header. This is an *extremely* cool app!",
# },
)
### SIDEBAR
search_backend = st.sidebar.selectbox(
"Search Engine",
["hfapi", "custom"],
format_func=lambda x: {"hfapi": "Huggingface API", "custom": "Sentence Bert"}[x],
)
limit_results = st.sidebar.number_input("Limit results", min_value=0, value=10)
st.sidebar.markdown("# Filters")
args = ModelSearchArguments()
library = st.sidebar.multiselect(
"Library", args.library.values(), format_func=lambda x: {v: k for k, v in args.library.items()}[x]
)
task = st.sidebar.multiselect(
"Task", args.pipeline_tag.values(), format_func=lambda x: {v: k for k, v in args.pipeline_tag.items()}[x]
)
### MAIN PAGE
st.markdown(
"<h1 style='text-align: center; '>πŸ”ŽπŸ€— HF Search Engine</h1>",
unsafe_allow_html=True,
)
# Search bar
search_query = st.text_input(
"Search for a model in HuggingFace", value="", max_chars=None, key=None, type="default"
)
# Search API
endpoint = "http://localhost:5000"
headers = {
"Content-Type": "application/json",
"api-key": "password",
}
search_url = f"{endpoint}/{search_backend}/search"
filters = {
"library": library,
"task": task,
}
search_body = {
"query": search_query,
"filters": json.dumps(filters, default=str),
"limit": limit_results,
}
if search_query != "":
response = requests.post(search_url, headers=headers, json=search_body).json()
record_list = []
_ = [
record_list.append(
{
"modelId": record["modelId"],
"tags": record["tags"],
"downloads": record["downloads"],
"likes": record["likes"],
}
)
for record in response.get("value")
]
# filter results
if record_list:
st.write(f'Search results ({response.get("count")}):')
if response.get("count") > 100:
shown_results = 100
else:
shown_results = response.get("count")
for i, record in paginator(
f"Select results (showing {shown_results} of {response.get('count')} results)",
record_list,
):
col1, col2, col3 = st.columns([5,1,1])
col1.metric("Model", record["modelId"])
col2.metric("NΒ° downloads", numerize(record["downloads"]))
col3.metric("NΒ° likes", numerize(record["likes"]))
st.button(f"View model", on_click=lambda record=record: webbrowser.open(f"https://huggingface.co/{record['modelId']})"), key=record["modelId"])
st.markdown(f"**Tags:** {' β€’ '.join(record['tags'])}")
# TODO: embed huggingface spaces
# import streamlit.components.v1 as components
# components.html(
# f"""
# <link rel="stylesheet" href="https://gradio.s3-us-west-2.amazonaws.com/2.6.2/static/bundle.css">
# <div id="target"></div>
# <script src="https://gradio.s3-us-west-2.amazonaws.com/2.6.2/static/bundle.js"></script>
# <script>
# launchGradioFromSpaces("abidlabs/question-answering", "#target")
# </script>
# """,
# height=400,
# )
st.markdown("---")
else:
st.write(f"No Search results, please try again with different keywords")