File size: 2,734 Bytes
f3dcb90
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
from __future__ import annotations

import pandas as pd
import requests
from huggingface_hub.hf_api import SpaceInfo


class PaperList:
    def __init__(self):
        self.organization_name = 'ICLR2023'
        self.table = pd.read_csv('iclr_submissions.csv')
        self._preprocess_table()

        self.table_header = '''
            <tr>
                <td width="25%">Title</td>
                <td width="5%">PDF</td>
                <td width="20%">Tldr</td>
                <td width="50%">Abstract</td>
            </tr>'''

    @staticmethod
    def load_space_info(author: str) -> list[SpaceInfo]:
        path = 'https://huggingface.co/api/spaces'
        r = requests.get(path, params={'author': author})
        d = r.json()
        return [SpaceInfo(**x) for x in d]

    def add_spaces_to_table(self, organization_name: str,
                            df: pd.DataFrame) -> pd.DataFrame:
        spaces = self.load_space_info(organization_name)
        name2space = {
            s.id.split('/')[1].lower(): f'https://huggingface.co/spaces/{s.id}'
            for s in spaces
        }
        return df

    def _preprocess_table(self) -> None:
        self.table = self.add_spaces_to_table(self.organization_name,
                                              self.table)
        self.table['title_lowercase'] = self.table.title.str.lower()

        rows = []
        for row in self.table.itertuples():
            paper = f'<a href="{row.url}" target="_blank">{row.title}</a>' if isinstance(
                row.url, str) else row.title
            pdf = f'<a href="{row.pdf}" target="_blank">pdf</a>' if isinstance(
                row.pdf, str) else ''
            tldr = row.tldr if isinstance(row.tldr, str) else ''

            row = f'''
                <tr>
                    <td>{paper}</td>
                    <td>{pdf}</td>
                    <td>{tldr}</td>
                    <td>{row.abstract}</td>
                </tr>'''
            rows.append(row)
        self.table['html_table_content'] = rows

    def render(self, search_query: str, case_sensitive: bool) -> tuple[int, str]:
        df = self.add_spaces_to_table(self.organization_name, self.table)
        if search_query:
            if case_sensitive:
                df = df[df.title.str.contains(search_query)]
            else:
                df = df[df.title_lowercase.str.contains(search_query.lower())]

        return len(df), self.to_html(df, self.table_header)

    @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