|
|
from __future__ import annotations |
|
|
import gradio as gr |
|
|
import numpy as np |
|
|
import pandas as pd |
|
|
import requests |
|
|
from huggingface_hub.hf_api import SpaceInfo |
|
|
|
|
|
SHEET_ID = '1J_AEBo7l0gtfKSbMLlblY69IXp70UmfBLGW2ZtR_z_c' |
|
|
SHEET_NAME = 'model' |
|
|
csv_url = f'https://docs.google.com/spreadsheets/d/{SHEET_ID}/gviz/tq?tqx=out:csv&sheet={SHEET_NAME}' |
|
|
|
|
|
class ModelList: |
|
|
def __init__(self): |
|
|
self.table = pd.read_csv(csv_url) |
|
|
self._preprocess_table() |
|
|
|
|
|
self.table_header = ''' |
|
|
<tr> |
|
|
<td width="20%">Title</td> |
|
|
<td width="10%">Link</td> |
|
|
<td width="10%">Tags</td> |
|
|
</tr>''' |
|
|
|
|
|
def _preprocess_table(self) -> None: |
|
|
self.table['name_lowercase'] = self.table['Title'].str.lower() |
|
|
|
|
|
rows = [] |
|
|
for row in self.table.itertuples(): |
|
|
source = f'<a href="{row.Link}" target="_blank">Link</a>' if isinstance( |
|
|
row.Link, str) else '' |
|
|
row = f''' |
|
|
<tr> |
|
|
<td>{row.Title}</td> |
|
|
<td>{source}</td> |
|
|
<td>{row.Tags}</td> |
|
|
</tr>''' |
|
|
rows.append(row) |
|
|
self.table['html_table_content'] = rows |
|
|
|
|
|
def render(self, search_query: str, |
|
|
case_sensitive: bool, |
|
|
filter_names: list[str], |
|
|
) -> tuple[int, str]: |
|
|
df = self.table |
|
|
if search_query: |
|
|
if case_sensitive: |
|
|
df = df[df.Title.str.contains(search_query)] |
|
|
else: |
|
|
df = df[df.name_lowercase.str.contains(search_query.lower())] |
|
|
df = self.filter_table(df, filter_names) |
|
|
result = self.to_html(df, self.table_header) |
|
|
return result |
|
|
|
|
|
@staticmethod |
|
|
def filter_table(df: pd.DataFrame, filter_names: list[str]) -> pd.DataFrame: |
|
|
df = df[df.Tags.str.contains(filter_names)] |
|
|
return df |
|
|
|
|
|
@staticmethod |
|
|
def to_html(df: pd.DataFrame, table_header: str) -> str: |
|
|
table_data = ''.join(df.html_table_content) |
|
|
html = f''' |
|
|
<table> |
|
|
{table_header} |
|
|
{table_data} |
|
|
</table>''' |
|
|
return html |
|
|
model_list = ModelList() |
|
|
with gr.Blocks() as demo: |
|
|
search_box = gr.Textbox(label='Search Title',placeholder='You can search for titles with regular expressions. e.g. (?<!sur)face',max_lines=1) |
|
|
case_sensitive = gr.Checkbox(label='Case Sensitive') |
|
|
filter_names1 = gr.CheckboxGroup(choices=['Filter A','Filter B ', 'Filer C'], value=['Filter A','Filter B ', 'Filer C'], label='Filter Tags') |
|
|
search_button = gr.Button('Search') |
|
|
table = gr.HTML(show_label=False) |
|
|
demo.load(fn=model_list.render, inputs=[search_box, case_sensitive, filter_names1,],outputs=[table,]) |
|
|
search_box.submit(fn=model_list.render, inputs=[search_box, case_sensitive, filter_names1,], outputs=[table,]) |
|
|
search_button.click(fn=model_list.render, inputs=[search_box, case_sensitive, filter_names1,], outputs=[table,]) |
|
|
demo.queue() |
|
|
demo.launch(share=False) |