Spaces:
Sleeping
Sleeping
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 = '1L7AHpWMVU_kZVLcsk8H2FTizgzeVxWPDoBxw7K8KHXw' | |
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.table = self.table.astype({'Year':'string'}) | |
self._preprocess_table() | |
self.table_header = ''' | |
<tr> | |
<td width="15%">Name</td> | |
<td width="10%">Year Published</td> | |
<td width="10%">Source</td> | |
<td width="30%">About</td> | |
<td width="10%">Task</td> | |
<td width="15%">Training Data Type</td> | |
<td width="10%">Publication</td> | |
</tr>''' | |
def _preprocess_table(self) -> None: | |
self.table['name_lowercase'] = self.table['Name'].str.lower() | |
rows = [] | |
for row in self.table.itertuples(): | |
source = f'<a href="{row.Source}" target="_blank">Link</a>' if isinstance( | |
row.Source, str) else '' | |
paper = f'<a href="{row.Paper}" target="_blank">Link</a>' if isinstance( | |
row.Source, str) else '' | |
row = f''' | |
<tr> | |
<td>{row.Name}</td> | |
<td>{row.Year}</td> | |
<td>{source}</td> | |
<td>{row.About}</td> | |
<td>{row.task}</td> | |
<td>{row.data}</td> | |
<td>{paper}</td> | |
</tr>''' | |
rows.append(row) | |
self.table['html_table_content'] = rows | |
def render(self, search_query: str, | |
case_sensitive: bool, | |
filter_names: list[str], | |
data_types: list[str]) -> tuple[int, str]: | |
df = self.table | |
if search_query: | |
if case_sensitive: | |
df = df[df.name.str.contains(search_query)] | |
else: | |
df = df[df.name_lowercase.str.contains(search_query.lower())] | |
df = self.filter_table(df, filter_names, data_types) | |
result = self.to_html(df, self.table_header) | |
return result | |
def filter_table(df: pd.DataFrame, filter_names: list[str], data_types: list[str]) -> pd.DataFrame: | |
df = df.loc[df.task.isin(set(filter_names))] | |
df = df.loc[df.data.isin(set(data_types))] | |
return df | |
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: | |
with gr.Row(): | |
gr.Image(value="RAII.svg",scale=1,show_download_button=False,show_share_button=False,show_label=False,height=100,container=False) | |
gr.Markdown("# Models for Healthcare Teams") | |
search_box = gr.Textbox(label='Search Name',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=['NLP','Computer Vision', 'Multi-Model'], value=['NLP','Computer Vision', 'Multi-Model'], label='Task') | |
data_type_names1 = ['Biomedical Corpus','Scientific Corpus','Clinical Corpus','Image','Mixed'] | |
data_types1 = gr.CheckboxGroup(choices=data_type_names1, value=data_type_names1, label='Training Data Type') | |
search_button = gr.Button('Search') | |
table = gr.HTML(show_label=False) | |
demo.load(fn=model_list.render, inputs=[search_box, case_sensitive, filter_names1, data_types1,],outputs=[table,]) | |
search_box.submit(fn=model_list.render, inputs=[search_box, case_sensitive, filter_names1, data_types1,], outputs=[table,]) | |
search_button.click(fn=model_list.render, inputs=[search_box, case_sensitive, filter_names1, data_types1,], outputs=[table,]) | |
demo.queue() | |
demo.launch(share=False) | |