Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -0,0 +1,107 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import pandas as pd
|
3 |
+
|
4 |
+
|
5 |
+
SHEET_ID = '1BWKw2ygYQUUPcNdSJhW9OkXILWO5i-dCa5Uahn9dHNo'
|
6 |
+
SHEET_NAME = 'Datasets'
|
7 |
+
csv_url = f'https://docs.google.com/spreadsheets/d/{SHEET_ID}/gviz/tq?tqx=out:csv&sheet={SHEET_NAME}'
|
8 |
+
|
9 |
+
class DataList:
|
10 |
+
def __init__(self):
|
11 |
+
self.table = pd.read_csv(csv_url)
|
12 |
+
self.table = self.table.astype({'Year':'string'})
|
13 |
+
self._preprocess_table()
|
14 |
+
|
15 |
+
self.table_header = '''
|
16 |
+
<tr>
|
17 |
+
<td width="15%">Name</td>
|
18 |
+
<td width="10%">URL</td>
|
19 |
+
<td width="30%">About</td>
|
20 |
+
<td width="15%">Publisher</td>
|
21 |
+
<td width="10%">Year Updated</td>
|
22 |
+
<td width="10%">Type</td>
|
23 |
+
<td width="10%">Tag</td>
|
24 |
+
</tr>'''
|
25 |
+
|
26 |
+
def _preprocess_table(self) -> None:
|
27 |
+
self.table['name_lowercase'] = self.table['Name'].str.lower()
|
28 |
+
|
29 |
+
rows = []
|
30 |
+
for row in self.table.itertuples():
|
31 |
+
source = f'<a href="{row.URL}" target="_blank">Link</a>' if isinstance(
|
32 |
+
row.URL, str) else ''
|
33 |
+
row = f'''
|
34 |
+
<tr>
|
35 |
+
<td>{row.Name}</td>
|
36 |
+
<td>{source}</td>
|
37 |
+
<td>{row.About}</td>
|
38 |
+
<td>{row.Publisher}</td>
|
39 |
+
<td>{row.Year}</td>
|
40 |
+
<td>{row.Type}</td>
|
41 |
+
<td>{row.Tags}</td>
|
42 |
+
</tr>'''
|
43 |
+
rows.append(row)
|
44 |
+
self.table['html_table_content'] = rows
|
45 |
+
|
46 |
+
def render(self, search_query: str,
|
47 |
+
case_sensitive: bool,
|
48 |
+
filter_names: list[str],
|
49 |
+
data_types: list[str]) -> tuple[int, str]:
|
50 |
+
df = self.table
|
51 |
+
if search_query:
|
52 |
+
if case_sensitive:
|
53 |
+
df = df[df.name.str.contains(search_query)]
|
54 |
+
else:
|
55 |
+
df = df[df.name_lowercase.str.contains(search_query.lower())]
|
56 |
+
df = self.filter_table(df, filter_names, data_types)
|
57 |
+
result = self.to_html(df, self.table_header)
|
58 |
+
return result
|
59 |
+
|
60 |
+
@staticmethod
|
61 |
+
def filter_table(df: pd.DataFrame, filter_names: list[str], data_types: list[str]) -> pd.DataFrame:
|
62 |
+
df = df.loc[df.Type.isin(set(filter_names))]
|
63 |
+
df = df.loc[df.Tags.isin(set(data_types))]
|
64 |
+
return df
|
65 |
+
|
66 |
+
@staticmethod
|
67 |
+
def to_html(df: pd.DataFrame, table_header: str) -> str:
|
68 |
+
table_data = ''.join(df.html_table_content)
|
69 |
+
html = f'''
|
70 |
+
<table>
|
71 |
+
{table_header}
|
72 |
+
{table_data}
|
73 |
+
</table>'''
|
74 |
+
return html
|
75 |
+
|
76 |
+
|
77 |
+
data_list = DataList()
|
78 |
+
|
79 |
+
css = """
|
80 |
+
button.svelte-kqij2n{font-weight: bold !important;
|
81 |
+
background-color: #ebecf0;
|
82 |
+
color: black;
|
83 |
+
margin-left: 5px;}
|
84 |
+
#tlsnlbs{}
|
85 |
+
#mtcs{}
|
86 |
+
|
87 |
+
#mdls{}
|
88 |
+
#dts{}
|
89 |
+
.svelte-kqij2n .selected {
|
90 |
+
background-color: black;
|
91 |
+
color: white;
|
92 |
+
}
|
93 |
+
span.svelte-s1r2yt{font-weight: bold !important;
|
94 |
+
}
|
95 |
+
"""
|
96 |
+
with gr.Blocks(css=css) as demo:
|
97 |
+
search_box = gr.Textbox( label='Search Name', placeholder='You can search for titles with regular expressions. e.g. (?<!sur)face',max_lines=1)
|
98 |
+
case_sensitive = gr.Checkbox(label='Case Sensitive')
|
99 |
+
filter_names = gr.CheckboxGroup(choices=['Real Data','Synthetic Data',], value=['Real Data','Synthetic Data',], label='Type')
|
100 |
+
search_button = gr.Button('Search')
|
101 |
+
table = gr.HTML(show_label=False)
|
102 |
+
demo.load(fn=data_list.render, inputs=[search_box, case_sensitive, filter_names,],outputs=[table,])
|
103 |
+
search_box.submit(fn=data_list.render, inputs=[search_box, case_sensitive, filter_names,], outputs=[table,])
|
104 |
+
search_button.click(fn=data_list.render, inputs=[search_box, case_sensitive, filter_names,], outputs=[table,])
|
105 |
+
|
106 |
+
demo.queue()
|
107 |
+
demo.launch(share=False)
|